diff --git a/py/builtin.h b/py/builtin.h index 743f748da0..caca71b523 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -37,6 +37,8 @@ MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj); MP_DECLARE_CONST_FUN_OBJ(mp_op_contains_obj); MP_DECLARE_CONST_FUN_OBJ(mp_op_getitem_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_op_setitem_obj); +MP_DECLARE_CONST_FUN_OBJ(mp_op_delitem_obj); extern const mp_obj_module_t mp_module___main__; extern const mp_obj_module_t mp_module_array; diff --git a/py/mkrules.mk b/py/mkrules.mk index ab4514d1c4..08ba8fc416 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -59,7 +59,7 @@ $(BUILD)/%.pp: %.c # object directories (but only for existance), and the object directories # will be created if they don't exist. OBJ_DIRS = $(sort $(dir $(OBJ))) -$(OBJ): | $(OBJ_DIRS) +$(OBJ): $(PY_BUILD)/qstrdefs.generated.h | $(OBJ_DIRS) $(OBJ_DIRS): $(MKDIR) -p $@ diff --git a/py/modsys.c b/py/modsys.c index 10efd6004d..41ade1f275 100644 --- a/py/modsys.c +++ b/py/modsys.c @@ -6,6 +6,7 @@ #include "runtime.h" #include "objlist.h" #include "objtuple.h" +#include "objstr.h" #if MICROPY_ENABLE_MOD_SYS @@ -20,13 +21,15 @@ mp_obj_list_t mp_sys_path_obj; mp_obj_list_t mp_sys_argv_obj; #define I(n) MP_OBJ_NEW_SMALL_INT(n) // TODO: CPython is now at 5-element array, but save 2 els so far... -STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(3), I(5)}}; +STATIC const mp_obj_tuple_t mp_sys_version_info_obj = {{&mp_type_tuple}, 3, {I(3), I(4), I(0)}}; #undef I +STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0"); STATIC const mp_map_elem_t mp_module_sys_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_sys) }, { MP_OBJ_NEW_QSTR(MP_QSTR_path), (mp_obj_t)&mp_sys_path_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_argv), (mp_obj_t)&mp_sys_argv_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_version), (mp_obj_t)&version_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_version_info), (mp_obj_t)&mp_sys_version_info_obj }, #if MP_ENDIANNESS_LITTLE { MP_OBJ_NEW_QSTR(MP_QSTR_byteorder), MP_OBJ_NEW_QSTR(MP_QSTR_little) }, diff --git a/py/objdict.c b/py/objdict.c index 2c56540d15..19d9ae9e9f 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -497,6 +497,8 @@ STATIC const mp_map_elem_t dict_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj }, }; STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table); diff --git a/py/objstr.c b/py/objstr.c index d933fa5e34..08db2af669 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -10,13 +10,7 @@ #include "runtime0.h" #include "runtime.h" #include "pfenv.h" - -typedef struct _mp_obj_str_t { - mp_obj_base_t base; - machine_uint_t hash : 16; // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c) - machine_uint_t len : 16; // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte - const byte *data; -} mp_obj_str_t; +#include "objstr.h" STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t *args); const mp_obj_t mp_const_empty_bytes; @@ -1463,7 +1457,8 @@ bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2) { } else { GET_STR_HASH(s1, h1); GET_STR_HASH(s2, h2); - if (h1 != h2) { + // If any of hashes is 0, it means it's not valid + if (h1 != 0 && h2 != 0 && h1 != h2) { return false; } GET_STR_DATA_LEN(s1, d1, l1); diff --git a/py/objstr.h b/py/objstr.h new file mode 100644 index 0000000000..a32ba53b1d --- /dev/null +++ b/py/objstr.h @@ -0,0 +1,10 @@ +typedef struct _mp_obj_str_t { + mp_obj_base_t base; + // XXX here we assume the hash size is 16 bits (it is at the moment; see qstr.c) + machine_uint_t hash : 16; + // len == number of bytes used in data, alloc = len + 1 because (at the moment) we also append a null byte + machine_uint_t len : 16; + const byte *data; +} mp_obj_str_t; + +#define MP_DEFINE_STR_OBJ(obj_name, str) mp_obj_str_t obj_name = {{&mp_type_str}, 0, sizeof(str) - 1, (const byte*)str}; diff --git a/py/opmethods.c b/py/opmethods.c index af2524cf20..27415fef9b 100644 --- a/py/opmethods.c +++ b/py/opmethods.c @@ -13,6 +13,18 @@ STATIC mp_obj_t op_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) { } MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem); +STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { + mp_store_subscr(self_in, key_in, value_in); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem); + +STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) { + mp_store_subscr(self_in, key_in, MP_OBJ_NULL); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem); + STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) { mp_obj_type_t *type = mp_obj_get_type(lhs_in); return type->binary_op(MP_BINARY_OP_IN, lhs_in, rhs_in); diff --git a/py/qstr.c b/py/qstr.c index e4b5c111b5..990ff37727 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -35,7 +35,12 @@ machine_uint_t qstr_compute_hash(const byte *data, uint len) { for (const byte *top = data + len; data < top; data++) { hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data } - return hash & 0xffff; + hash &= 0xffff; + // Make sure that valid hash is never zero, zero means "hash not computed" + if (hash == 0) { + hash++; + } + return hash; } typedef struct _qstr_pool_t { diff --git a/py/qstrdefs.h b/py/qstrdefs.h index df383b776c..80cf681ae7 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -24,6 +24,7 @@ Q(__len__) Q(__iter__) Q(__getitem__) Q(__setitem__) +Q(__delitem__) Q(__add__) Q(__sub__) Q(__repr__) @@ -279,6 +280,7 @@ Q(little) Q(stdin) Q(stdout) Q(stderr) +Q(version) Q(version_info) #endif