diff options
author | Damien George <damien.p.george@gmail.com> | 2013-12-30 22:32:17 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2013-12-30 22:32:17 +0000 |
commit | 6baf76e28b17055fc6e5a6c9560e756d32eaad5d (patch) | |
tree | f4245858b6755d6731a7ec6c9f0a6fc49901eb5b /py/vm.c | |
parent | 8cc96a35e532ef999e5a3739deeb44f51a80744b (diff) | |
download | micropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.tar.gz micropython-6baf76e28b17055fc6e5a6c9560e756d32eaad5d.zip |
py: make closures work.
Diffstat (limited to 'py/vm.c')
-rw-r--r-- | py/vm.c | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -39,10 +39,25 @@ mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_arg state[i] = args[n_args - 1 - i]; } const byte *ip = code; + + // execute prelude to make any cells (closed over variables) + { + for (uint n_local = *ip++; n_local > 0; n_local--) { + uint local_num = *ip++; + if (local_num < n_args) { + state[local_num] = mp_obj_new_cell(state[local_num]); + } else { + state[local_num] = mp_obj_new_cell(MP_OBJ_NULL); + } + } + } + + // execute the byte code if (mp_execute_byte_code_2(&ip, &state[0], &sp)) { // it shouldn't yield assert(0); } + // TODO check fails if, eg, return from within for loop //assert(sp == &state[17]); return *sp; @@ -127,11 +142,6 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** PUSH(rt_get_cell(fastn[unum])); break; - case MP_BC_LOAD_CLOSURE: - DECODE_UINT; - PUSH(fastn[unum]); - break; - case MP_BC_LOAD_NAME: DECODE_QSTR; PUSH(rt_load_name(qstr)); |