diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-20 17:50:40 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-20 17:50:40 +0100 |
commit | 3558f62fb5c21a19a518c2ba75860f0b5963219e (patch) | |
tree | cf8626118be5d0169ec0f1cd0482563595704c4e /py/vm.c | |
parent | bc5f0c19775e23b4f0621d52de47fb9438a78ba2 (diff) | |
download | micropython-3558f62fb5c21a19a518c2ba75860f0b5963219e.tar.gz micropython-3558f62fb5c21a19a518c2ba75860f0b5963219e.zip |
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.
Diffstat (limited to 'py/vm.c')
-rw-r--r-- | py/vm.c | 20 |
1 files changed, 12 insertions, 8 deletions
@@ -759,19 +759,23 @@ unwind_jump: SET_TOP(mp_make_function_from_raw_code((mp_raw_code_t*)unum, TOP(), obj1)); DISPATCH(); - ENTRY(MP_BC_MAKE_CLOSURE): + ENTRY(MP_BC_MAKE_CLOSURE): { DECODE_PTR; - // Stack layout: closure_tuple <- TOS - SET_TOP(mp_make_closure_from_raw_code((mp_raw_code_t*)unum, TOP(), MP_OBJ_NULL, MP_OBJ_NULL)); + machine_uint_t n_closed_over = *ip++; + // Stack layout: closed_overs <- TOS + sp -= n_closed_over - 1; + SET_TOP(mp_make_closure_from_raw_code((mp_raw_code_t*)unum, n_closed_over, sp)); DISPATCH(); + } - ENTRY(MP_BC_MAKE_CLOSURE_DEFARGS): + ENTRY(MP_BC_MAKE_CLOSURE_DEFARGS): { DECODE_PTR; - // Stack layout: def_tuple def_dict closure_tuple <- TOS - obj1 = POP(); - obj2 = POP(); - SET_TOP(mp_make_closure_from_raw_code((mp_raw_code_t*)unum, obj1, TOP(), obj2)); + machine_uint_t n_closed_over = *ip++; + // Stack layout: def_tuple def_dict closed_overs <- TOS + sp -= 2 + n_closed_over - 1; + SET_TOP(mp_make_closure_from_raw_code((mp_raw_code_t*)unum, 0x100 | n_closed_over, sp)); DISPATCH(); + } ENTRY(MP_BC_CALL_FUNCTION): DECODE_UINT; |