diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/builtin.h | 1 | ||||
-rw-r--r-- | py/builtintables.c | 1 | ||||
-rw-r--r-- | py/obj.c | 2 | ||||
-rw-r--r-- | py/objfun.c | 13 | ||||
-rw-r--r-- | py/objtype.c | 11 | ||||
-rw-r--r-- | py/runtime.c | 23 |
6 files changed, 44 insertions, 7 deletions
diff --git a/py/builtin.h b/py/builtin.h index 3120e5baf6..60b7c85b81 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -33,6 +33,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj); MP_DECLARE_CONST_FUN_OBJ(mp_namedtuple_obj); +extern const mp_obj_module_t mp_module___main__; extern const mp_obj_module_t mp_module_array; extern const mp_obj_module_t mp_module_collections; extern const mp_obj_module_t mp_module_io; diff --git a/py/builtintables.c b/py/builtintables.c index 03fb92e569..1edeefa441 100644 --- a/py/builtintables.c +++ b/py/builtintables.c @@ -118,6 +118,7 @@ STATIC const mp_builtin_elem_t builtin_object_table[] = { }; STATIC const mp_builtin_elem_t builtin_module_table[] = { + { MP_QSTR___main__, (mp_obj_t)&mp_module___main__ }, { MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython }, { MP_QSTR_array, (mp_obj_t)&mp_module_array }, @@ -118,6 +118,8 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) { return (machine_int_t)o_in; } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) { return mp_obj_tuple_hash(o_in); + } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) { + return (machine_int_t)o_in; // TODO hash class and instances // TODO delegate to __hash__ method if it exists diff --git a/py/objfun.c b/py/objfun.c index 3b48b570fd..db18aa8af6 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -53,6 +53,16 @@ void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args } } +STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + switch (op) { + case MP_BINARY_OP_EQUAL: + // These objects can be equal only if it's the same underlying structure, + // we don't even need to check for 2nd arg type. + return MP_BOOL(lhs_in == rhs_in); + } + return NULL; +} + STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_native)); mp_obj_fun_native_t *self = self_in; @@ -102,6 +112,7 @@ const mp_obj_type_t mp_type_fun_native = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_native_call, + .binary_op = fun_binary_op, }; // fun must have the correct signature for n_args fixed arguments @@ -338,6 +349,7 @@ const mp_obj_type_t mp_type_fun_bc = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_bc_call, + .binary_op = fun_binary_op, }; mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args_in, const byte *code) { @@ -464,6 +476,7 @@ STATIC const mp_obj_type_t mp_type_fun_asm = { { &mp_type_type }, .name = MP_QSTR_function, .call = fun_asm_call, + .binary_op = fun_binary_op, }; mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) { diff --git a/py/objtype.c b/py/objtype.c index f4ce6a4f8c..6426ec2d04 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -367,6 +367,16 @@ STATIC bool type_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { return false; } +STATIC mp_obj_t type_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + switch (op) { + case MP_BINARY_OP_EQUAL: + // Types can be equal only if it's the same type structure, + // we don't even need to check for 2nd arg type. + return MP_BOOL(lhs_in == rhs_in); + } + return NULL; +} + const mp_obj_type_t mp_type_type = { { &mp_type_type }, .name = MP_QSTR_type, @@ -375,6 +385,7 @@ const mp_obj_type_t mp_type_type = { .call = type_call, .load_attr = type_load_attr, .store_attr = type_store_attr, + .binary_op = type_binary_op, }; mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict) { diff --git a/py/runtime.c b/py/runtime.c index 238c3a56e2..773f998d34 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -33,6 +33,14 @@ STATIC mp_map_t *map_locals; STATIC mp_map_t *map_globals; STATIC mp_map_t map_builtins; +STATIC mp_map_t map_main; + +const mp_obj_module_t mp_module___main__ = { + .base = { &mp_type_module }, + .name = MP_QSTR___main__, + .globals = (mp_map_t*)&map_main, +}; + // a good optimising compiler will inline this if necessary STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) { mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value; @@ -41,17 +49,18 @@ STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) { void mp_init(void) { mp_emit_glue_init(); - // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New()) - map_locals = map_globals = mp_map_new(1); - - // init built-in hash table - mp_map_init(&map_builtins, 3); - // init global module stuff mp_module_init(); + mp_map_init(&map_main, 1); // add some builtins that can't be done in ROM - mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__)); + mp_map_add_qstr(&map_main, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__)); + + // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New()) + map_locals = map_globals = &map_main; + + // init built-in hash table + mp_map_init(&map_builtins, 3); #if MICROPY_CPYTHON_COMPAT // Precreate sys module, so "import sys" didn't throw exceptions. |