diff --git a/py/builtin.c b/py/builtin.c index a5193bf760..5dbf47ab84 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -315,10 +315,3 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { } return value; } - -static mp_obj_t mp_builtin_type(mp_obj_t o_in) { - // TODO implement the 3 argument version of type() - return mp_obj_get_type(o_in); -} - -MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_type_obj, mp_builtin_type); diff --git a/py/objfloat.c b/py/objfloat.c index d21472d5d6..283285b07f 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -24,6 +24,7 @@ static void float_print(void (*print)(void *env, const char *fmt, ...), void *en print(env, "%.8g", o->value); } +// args are reverse in the array static mp_obj_t float_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { switch (n_args) { case 0: diff --git a/py/objtype.c b/py/objtype.c index 8778c180ca..4a6025a70b 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -12,6 +12,32 @@ static void type_print(void (*print)(void *env, const char *fmt, ...), void *env print(env, "", self->name); } +// args are reverse in the array +static mp_obj_t type_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { + switch (n_args) { + case 1: + return mp_obj_get_type(args[0]); + + case 3: + { + // args[2] = name + // args[1] = bases tuple + // args[0] = locals dict + + mp_obj_type_t *new_type = m_new0(mp_obj_type_t, 1); + new_type->base.type = &mp_const_type; + new_type->name = qstr_str(mp_obj_get_qstr(args[2])); + return new_type; + + //mp_obj_t new_class = mp_obj_new_class(mp_obj_get_qstr(args[2]), args[0]); + //return new_class; + } + + default: + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "type takes at 1 or 3 arguments")); + } +} + static mp_obj_t type_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { mp_obj_type_t *self = self_in; if (self->make_new != NULL) { @@ -26,5 +52,6 @@ const mp_obj_type_t mp_const_type = { { &mp_const_type }, "type", .print = type_print, + .make_new = type_make_new, .call_n = type_call_n, }; diff --git a/py/runtime.c b/py/runtime.c index 387e10abbe..3c92ca68cb 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -111,7 +111,7 @@ void rt_init(void) { mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type); mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type); mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type); - mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_builtin_type_obj); // TODO + mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type); // built-in user functions; TODO covert all to &mp_builtin_xxx's mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs));