|
15 | 15 | #include "map.h" |
16 | 16 | #include "builtin.h" |
17 | 17 |
|
18 | | -mp_obj_t mp_builtin___build_class__(mp_obj_t o_class_fun, mp_obj_t o_class_name) { |
| 18 | +// args[0] is function from class body |
| 19 | +// args[1] is class name |
| 20 | +// args[2:] are base objects |
| 21 | +mp_obj_t mp_builtin___build_class__(int n_args, const mp_obj_t *args) { |
| 22 | + assert(2 <= n_args); |
| 23 | + |
19 | 24 | // we differ from CPython: we set the new __locals__ object here |
20 | 25 | mp_map_t *old_locals = rt_locals_get(); |
21 | 26 | mp_map_t *class_locals = mp_map_new(MP_MAP_QSTR, 0); |
22 | 27 | rt_locals_set(class_locals); |
23 | 28 |
|
24 | 29 | // call the class code |
25 | | - rt_call_function_1(o_class_fun, (mp_obj_t)0xdeadbeef); |
| 30 | + mp_obj_t cell = rt_call_function_1(args[0], (mp_obj_t)0xdeadbeef); |
26 | 31 |
|
27 | 32 | // restore old __locals__ object |
28 | 33 | rt_locals_set(old_locals); |
29 | 34 |
|
30 | | - // create and return the new class |
31 | | - return mp_obj_new_class(class_locals); |
| 35 | + /* |
| 36 | + // get the class type (meta object) from the base objects |
| 37 | + mp_obj_t meta; |
| 38 | + if (n_args == 2) { |
| 39 | + // no explicit bases, so use 'type' |
| 40 | + meta = (mp_obj_t)&mp_const_type; |
| 41 | + } else { |
| 42 | + // use type of first base object |
| 43 | + meta = mp_obj_get_type(args[2]); |
| 44 | + } |
| 45 | + */ |
| 46 | + |
| 47 | + // TODO do proper metaclass resolution for multiple base objects |
| 48 | + |
| 49 | + /* |
| 50 | + // create the new class using a call to the meta object |
| 51 | + // (arguments must be backwards in the array) |
| 52 | + mp_obj_t meta_args[3]; |
| 53 | + meta_args[2] = args[1]; // class name |
| 54 | + meta_args[1] = mp_obj_new_tuple(n_args - 2, args + 2); // tuple of bases |
| 55 | + meta_args[0] = class_locals; // dict of members TODO, currently is a map |
| 56 | + mp_obj_t new_class = rt_call_function_n(meta, 3, meta_args); |
| 57 | + */ |
| 58 | + // create the new class |
| 59 | + mp_obj_t new_class = mp_obj_new_class(class_locals); |
| 60 | + |
| 61 | + // store into cell if neede |
| 62 | + if (cell != mp_const_none) { |
| 63 | + mp_obj_cell_set(cell, new_class); |
| 64 | + } |
| 65 | + |
| 66 | + return new_class; |
32 | 67 | } |
33 | 68 |
|
| 69 | +MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__); |
| 70 | + |
34 | 71 | mp_obj_t mp_builtin___repl_print__(mp_obj_t o) { |
35 | 72 | if (o != mp_const_none) { |
36 | 73 | mp_obj_print(o); |
@@ -281,12 +318,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) { |
281 | 318 |
|
282 | 319 | static mp_obj_t mp_builtin_type(mp_obj_t o_in) { |
283 | 320 | // TODO implement the 3 argument version of type() |
284 | | - if (MP_OBJ_IS_SMALL_INT(o_in)) { |
285 | | - return (mp_obj_t)&int_type; |
286 | | - } else { |
287 | | - mp_obj_base_t *o = o_in; |
288 | | - return (mp_obj_t)o->type; |
289 | | - } |
| 321 | + return mp_obj_get_type(o_in); |
290 | 322 | } |
291 | 323 |
|
292 | 324 | MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_type_obj, mp_builtin_type); |
0 commit comments