Commit dae7eb72 authored by Damien's avatar Damien
Browse files

py: add dict length function, and fix rt_store_set.

parent 732407f1
...@@ -209,11 +209,8 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) { ...@@ -209,11 +209,8 @@ mp_obj_t mp_builtin_len(mp_obj_t o_in) {
mp_obj_t *seq_items; mp_obj_t *seq_items;
mp_obj_list_get(o_in, &seq_len, &seq_items); mp_obj_list_get(o_in, &seq_len, &seq_items);
len = seq_len; len = seq_len;
/* TODO
} else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) { } else if (MP_OBJ_IS_TYPE(o_in, &dict_type)) {
mp_obj_base_t *o = o_in; len = mp_obj_dict_len(o_in);
len = o->u_map.used;
*/
} else { } else {
nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in))); nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "object of type '%s' has no len()", mp_obj_get_type_str(o_in)));
} }
......
...@@ -206,6 +206,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); ...@@ -206,6 +206,7 @@ void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
// dict // dict
extern const mp_obj_type_t dict_type; extern const mp_obj_type_t dict_type;
uint mp_obj_dict_len(mp_obj_t self_in);
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value); mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
// set // set
......
...@@ -53,13 +53,6 @@ mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { ...@@ -53,13 +53,6 @@ mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
} }
} }
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
mp_obj_dict_t *self = self_in;
mp_map_lookup_helper(&self->map, key, true)->value = value;
return self_in;
}
const mp_obj_type_t dict_type = { const mp_obj_type_t dict_type = {
{ &mp_const_type }, { &mp_const_type },
"dict", "dict",
...@@ -78,3 +71,21 @@ mp_obj_t mp_obj_new_dict(int n_args) { ...@@ -78,3 +71,21 @@ mp_obj_t mp_obj_new_dict(int n_args) {
mp_map_init(&o->map, MP_MAP_OBJ, n_args); mp_map_init(&o->map, MP_MAP_OBJ, n_args);
return o; return o;
} }
uint mp_obj_dict_len(mp_obj_t self_in) {
mp_obj_dict_t *self = self_in;
uint len = 0;
for (int i = 0; i < self->map.alloc; i++) {
if (self->map.table[i].key != NULL) {
len += 1;
}
}
return len;
}
mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
assert(MP_OBJ_IS_TYPE(self_in, &dict_type));
mp_obj_dict_t *self = self_in;
mp_map_lookup_helper(&self->map, key, true)->value = value;
return self_in;
}
...@@ -737,7 +737,7 @@ mp_obj_t rt_build_set(int n_args, mp_obj_t *items) { ...@@ -737,7 +737,7 @@ mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
} }
mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) { mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
mp_set_lookup(set, item, true); mp_obj_set_store(set, item);
return set; return set;
} }
......
...@@ -279,21 +279,17 @@ void mp_show_byte_code(const byte *ip, int len) { ...@@ -279,21 +279,17 @@ void mp_show_byte_code(const byte *ip, int len) {
rt_store_map(sp[unum + 1], sp[0], sp[1]); rt_store_map(sp[unum + 1], sp[0], sp[1]);
sp += 2; sp += 2;
break; break;
*/
case MP_BC_BUILD_SET: case MP_BC_BUILD_SET:
DECODE_UINT; DECODE_UINT;
obj1 = rt_build_set(unum, sp); printf("BUILD_SET %lu", unum);
sp += unum - 1;
*sp = obj1;
break; break;
case MP_BC_SET_ADD: case MP_BC_SET_ADD:
DECODE_UINT; DECODE_UINT;
// I think it's guaranteed by the compiler that sp[unum] is a set printf("SET_ADD %lu", unum);
rt_store_set(sp[unum], sp[0]);
sp++;
break; break;
*/
case MP_BC_UNPACK_SEQUENCE: case MP_BC_UNPACK_SEQUENCE:
DECODE_UINT; DECODE_UINT;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment