Add store_item() virtual method to type to implement container[index] = val.

pull/191/head
Paul Sokolovsky 2014-01-18 13:10:51 +02:00
rodzic 166bb40fb2
commit 6d8edf6acf
2 zmienionych plików z 13 dodań i 1 usunięć

Wyświetl plik

@ -97,6 +97,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 void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[1] = value; for method, dest[0] = self, dest[1] = method
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 {
const char *name;
@ -162,6 +163,9 @@ struct _mp_obj_type_t {
mp_load_attr_fun_t load_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)
mp_obj_t bases_tuple;

Wyświetl plik

@ -905,7 +905,15 @@ void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
// dict store
mp_obj_dict_store(base, index, value);
} 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)));
}
}