py: Making closures now passes pointer to stack, not a tuple for vars.
Closed over variables are now passed on the stack, instead of creating a
tuple and passing that. This way memory for the closed over variables
can be allocated within the closure object itself. See issue #510 for
background.
diff --git a/py/emitglue.c b/py/emitglue.c
index ce296f4..8b1fd97 100644
--- a/py/emitglue.c
+++ b/py/emitglue.c
@@ -158,10 +158,17 @@
return fun;
}
-mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_obj_t closure_tuple, mp_obj_t def_args, mp_obj_t def_kw_args) {
- DEBUG_OP_printf("make_closure_from_raw_code %p\n", rc);
+mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, uint n_closed_over, const mp_obj_t *args) {
+ DEBUG_OP_printf("make_closure_from_raw_code %p %u %p\n", rc, n_closed_over, argrs);
// make function object
- mp_obj_t ffun = mp_make_function_from_raw_code(rc, def_args, def_kw_args);
+ mp_obj_t ffun;
+ if (n_closed_over & 0x100) {
+ // default positional and keyword args given
+ ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
+ } else {
+ // default positional and keyword args not given
+ ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
+ }
// wrap function in closure object
- return mp_obj_new_closure(ffun, closure_tuple);
+ return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
}