Skip to content

Commit b97669a

Browse files
committed
py: Improve __build_class__.
1 parent 6d6bc9e commit b97669a

5 files changed

Lines changed: 54 additions & 12 deletions

File tree

py/builtin.c

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,59 @@
1515
#include "map.h"
1616
#include "builtin.h"
1717

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+
1924
// we differ from CPython: we set the new __locals__ object here
2025
mp_map_t *old_locals = rt_locals_get();
2126
mp_map_t *class_locals = mp_map_new(MP_MAP_QSTR, 0);
2227
rt_locals_set(class_locals);
2328

2429
// 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);
2631

2732
// restore old __locals__ object
2833
rt_locals_set(old_locals);
2934

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;
3267
}
3368

69+
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__);
70+
3471
mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
3572
if (o != mp_const_none) {
3673
mp_obj_print(o);
@@ -281,12 +318,7 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
281318

282319
static mp_obj_t mp_builtin_type(mp_obj_t o_in) {
283320
// 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);
290322
}
291323

292324
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_type_obj, mp_builtin_type);

py/builtin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TODO convert all these to objects using MP_DECLARE and MP_DEFINE
22

3-
mp_obj_t mp_builtin___build_class__(mp_obj_t o_class_fun, mp_obj_t o_class_name);
3+
MP_DECLARE_CONST_FUN_OBJ(mp_builtin___build_class___obj);
44
mp_obj_t mp_builtin___import__(int n, mp_obj_t *args);
55
mp_obj_t mp_builtin___repl_print__(mp_obj_t o);
66
mp_obj_t mp_builtin_abs(mp_obj_t o_in);

py/obj.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
#include "runtime.h"
1414
#include "map.h"
1515

16+
mp_obj_t mp_obj_get_type(mp_obj_t o_in) {
17+
if (MP_OBJ_IS_SMALL_INT(o_in)) {
18+
return (mp_obj_t)&int_type;
19+
} else {
20+
mp_obj_base_t *o = o_in;
21+
return (mp_obj_t)o->type;
22+
}
23+
}
24+
1625
const char *mp_obj_get_type_str(mp_obj_t o_in) {
1726
if (MP_OBJ_IS_SMALL_INT(o_in)) {
1827
return "int";

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ mp_obj_t mp_obj_new_class(struct _mp_map_t *class_locals);
161161
mp_obj_t mp_obj_new_instance(mp_obj_t clas);
162162
mp_obj_t mp_obj_new_module(qstr module_name);
163163

164+
mp_obj_t mp_obj_get_type(mp_obj_t o_in);
164165
const char *mp_obj_get_type_str(mp_obj_t o_in);
165166

166167
void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in);

py/runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void rt_init(void) {
9090
mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis;
9191

9292
// built-in core functions
93-
mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
93+
mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = (mp_obj_t)&mp_builtin___build_class___obj;
9494
mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);
9595

9696
// built-in types

0 commit comments

Comments
 (0)