Merge pull request #191 from pfalcon/store-item
Add store_item() virtual method to type to implement container[index] = val
This commit is contained in:
commit
0c4e909e76
4
py/obj.h
4
py/obj.h
@ -96,6 +96,7 @@ typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
|
|||||||
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
|
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
|
||||||
typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
|
typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
|
||||||
typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded
|
typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded
|
||||||
|
typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded
|
||||||
|
|
||||||
typedef struct _mp_method_t {
|
typedef struct _mp_method_t {
|
||||||
const char *name;
|
const char *name;
|
||||||
@ -160,6 +161,9 @@ struct _mp_obj_type_t {
|
|||||||
|
|
||||||
mp_load_attr_fun_t load_attr;
|
mp_load_attr_fun_t load_attr;
|
||||||
mp_store_attr_fun_t store_attr;
|
mp_store_attr_fun_t store_attr;
|
||||||
|
// Implements container[index] = val; note that load_item is implemented
|
||||||
|
// by binary_op(RT_BINARY_OP_SUBSCR)
|
||||||
|
mp_store_item_fun_t store_item;
|
||||||
|
|
||||||
// these are for dynamically created types (classes)
|
// these are for dynamically created types (classes)
|
||||||
mp_obj_t bases_tuple;
|
mp_obj_t bases_tuple;
|
||||||
|
10
py/runtime.c
10
py/runtime.c
@ -879,7 +879,15 @@ void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
|
|||||||
// dict store
|
// dict store
|
||||||
mp_obj_dict_store(base, index, value);
|
mp_obj_dict_store(base, index, value);
|
||||||
} else {
|
} else {
|
||||||
assert(0);
|
mp_obj_type_t *type = mp_obj_get_type(base);
|
||||||
|
if (type->store_item != NULL) {
|
||||||
|
bool r = type->store_item(base, index, value);
|
||||||
|
if (r) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// TODO: call base classes here?
|
||||||
|
}
|
||||||
|
nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user