Add mp_type_native_coro_wrap

This commit is contained in:
Jeff Epler 2023-08-20 09:45:43 -05:00
parent 562520e158
commit 7e38b79e21
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
3 changed files with 35 additions and 6 deletions

View File

@ -188,10 +188,11 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, const mp_module
case MP_CODE_NATIVE_VIPER: case MP_CODE_NATIVE_VIPER:
fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children); fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children);
// Check for a generator function, and if so change the type of the object // Check for a generator function, and if so change the type of the object
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { if ((rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0) {
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_coro_wrap;
} else if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap; ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
} }
// CIRCUITPY: no support for mp_type_native_coro_wrap, native coroutine objects (yet).
break; break;
#endif #endif
#if MICROPY_EMIT_INLINE_ASM #if MICROPY_EMIT_INLINE_ASM
@ -206,9 +207,12 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, const mp_module
// check for generator functions and if so change the type of the object // check for generator functions and if so change the type of the object
// A generator is MP_SCOPE_FLAG_ASYNC | MP_SCOPE_FLAG_GENERATOR, // A generator is MP_SCOPE_FLAG_ASYNC | MP_SCOPE_FLAG_GENERATOR,
// so check for ASYNC first. // so check for ASYNC first.
#if MICROPY_PY_ASYNC_AWAIT
if ((rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0) { if ((rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0) {
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_coro_wrap; ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_coro_wrap;
} else if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { } else
#endif
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap; ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
} }

View File

@ -739,9 +739,11 @@ extern const mp_obj_type_t mp_type_gen_wrap;
extern const mp_obj_type_t mp_type_native_gen_wrap; extern const mp_obj_type_t mp_type_native_gen_wrap;
extern const mp_obj_type_t mp_type_gen_instance; extern const mp_obj_type_t mp_type_gen_instance;
// CIRCUITPY // CIRCUITPY
extern const mp_obj_type_t mp_type_coro_wrap;
// CIRCUITPY
#if MICROPY_PY_ASYNC_AWAIT #if MICROPY_PY_ASYNC_AWAIT
extern const mp_obj_type_t mp_type_coro_wrap;
#if MICROPY_EMIT_NATIVE
extern const mp_obj_type_t mp_type_native_coro_wrap;
#endif
extern const mp_obj_type_t mp_type_coro_instance; extern const mp_obj_type_t mp_type_coro_instance;
#endif #endif
extern const mp_obj_type_t mp_type_fun_builtin_0; extern const mp_obj_type_t mp_type_fun_builtin_0;

View File

@ -98,6 +98,7 @@ const mp_obj_type_t mp_type_gen_wrap = {
), ),
}; };
#if MICROPY_PY_ASYNC_AWAIT
const mp_obj_type_t mp_type_coro_wrap = { const mp_obj_type_t mp_type_coro_wrap = {
{ &mp_type_type }, { &mp_type_type },
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_EXTENDED, .flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_EXTENDED,
@ -110,6 +111,7 @@ const mp_obj_type_t mp_type_coro_wrap = {
.unary_op = mp_generic_unary_op, .unary_op = mp_generic_unary_op,
), ),
}; };
#endif
/******************************************************************************/ /******************************************************************************/
// native generator wrapper // native generator wrapper
@ -141,7 +143,13 @@ STATIC mp_obj_t native_gen_wrap_call(mp_obj_t self_in, size_t n_args, size_t n_k
MP_BC_PRELUDE_SIG_DECODE(ip); MP_BC_PRELUDE_SIG_DECODE(ip);
// Allocate the generator object, with room for local stack (exception stack not needed). // Allocate the generator object, with room for local stack (exception stack not needed).
mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, byte, n_state * sizeof(mp_obj_t), &mp_type_gen_instance); mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, byte, n_state * sizeof(mp_obj_t),
#if MICROPY_PY_ASYNC_AWAIT
(self_fun->base.type == &mp_type_native_gen_wrap) ? &mp_type_gen_instance : &mp_type_coro_instance
#else
&mp_type_gen_instance
#endif
);
// Parse the input arguments and set up the code state // Parse the input arguments and set up the code state
o->pend_exc = mp_const_none; o->pend_exc = mp_const_none;
@ -177,6 +185,21 @@ const mp_obj_type_t mp_type_native_gen_wrap = {
), ),
}; };
#if MICROPY_PY_ASYNC_AWAIT
const mp_obj_type_t mp_type_native_coro_wrap = {
{ &mp_type_type },
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_EXTENDED,
.name = MP_QSTR_coroutine,
#if MICROPY_PY_FUNCTION_ATTRS
.attr = mp_obj_fun_bc_attr,
#endif
MP_TYPE_EXTENDED_FIELDS(
.call = native_gen_wrap_call,
.unary_op = mp_generic_unary_op,
),
};
#endif
#endif // MICROPY_EMIT_NATIVE #endif // MICROPY_EMIT_NATIVE
/******************************************************************************/ /******************************************************************************/