diff options
author | Damien George <damien.p.george@gmail.com> | 2014-03-29 12:18:14 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-03-29 12:18:14 +0000 |
commit | da51a399cf628699546f77f320ef0388a268b49f (patch) | |
tree | 44e842786517ed39a8738e24da605160c46edcf3 /py/vm.c | |
parent | 75f71584a67b3aa8ad03762547ac6441551c63ca (diff) | |
parent | 3c2b2acd8c21294d046f4d2ef9195a976a5ef999 (diff) | |
download | micropython-da51a399cf628699546f77f320ef0388a268b49f.tar.gz micropython-da51a399cf628699546f77f320ef0388a268b49f.zip |
Merge pull request #383 from pfalcon/yield-from
Implement "yield from"
Diffstat (limited to 'py/vm.c')
-rw-r--r-- | py/vm.c | 58 |
1 files changed, 57 insertions, 1 deletions
@@ -9,6 +9,7 @@ #include "runtime.h" #include "bc0.h" #include "bc.h" +#include "objgenerator.h" // Value stack grows up (this makes it incompatible with native C stack, but // makes sure that arguments to functions are in natural order arg1..argN @@ -146,7 +147,9 @@ outer_dispatch_loop: // If we have exception to inject, now that we finish setting up // execution context, raise it. This works as if RAISE_VARARGS // bytecode was executed. - if (inject_exc != MP_OBJ_NULL) { + // Injecting exc into yield from generator is a special case, + // handled by MP_BC_YIELD_FROM itself + if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) { mp_obj_t t = inject_exc; inject_exc = MP_OBJ_NULL; nlr_jump(rt_make_raise_obj(t)); @@ -634,12 +637,65 @@ unwind_return: nlr_jump(rt_make_raise_obj(obj1)); case MP_BC_YIELD_VALUE: +yield: nlr_pop(); *ip_in_out = ip; *sp_in_out = sp; *exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block); return MP_VM_RETURN_YIELD; + case MP_BC_YIELD_FROM: + { +//#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type) +#define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type) +#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, &mp_type_GeneratorExit)) { nlr_jump(t); } + mp_vm_return_kind_t ret_kind; + obj1 = POP(); + mp_obj_t t_exc = MP_OBJ_NULL; + if (inject_exc != MP_OBJ_NULL) { + t_exc = inject_exc; + inject_exc = MP_OBJ_NULL; + ret_kind = mp_obj_gen_resume(TOP(), mp_const_none, t_exc, &obj2); + } else { + ret_kind = mp_obj_gen_resume(TOP(), obj1, MP_OBJ_NULL, &obj2); + } + + if (ret_kind == MP_VM_RETURN_YIELD) { + ip--; + PUSH(obj2); + goto yield; + } + if (ret_kind == MP_VM_RETURN_NORMAL) { + // Pop exhausted gen + sp--; + if (obj2 == MP_OBJ_NULL) { + // Optimize StopIteration + // TODO: get StopIteration's value + PUSH(mp_const_none); + } else { + PUSH(obj2); + } + + // If we injected GeneratorExit downstream, then even + // if it was swallowed, we re-raise GeneratorExit + GENERATOR_EXIT_IF_NEEDED(t_exc); + break; + } + if (ret_kind == MP_VM_RETURN_EXCEPTION) { + // Pop exhausted gen + sp--; + if (EXC_MATCH(obj2, &mp_type_StopIteration)) { + PUSH(mp_obj_exception_get_value(obj2)); + // If we injected GeneratorExit downstream, then even + // if it was swallowed, we re-raise GeneratorExit + GENERATOR_EXIT_IF_NEEDED(t_exc); + break; + } else { + nlr_jump(obj2); + } + } + } + case MP_BC_IMPORT_NAME: DECODE_QSTR; obj1 = POP(); |