Skip to content

Commit 7e38b79

Browse files
committed
Add mp_type_native_coro_wrap
1 parent 562520e commit 7e38b79

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

py/emitglue.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, const mp_module
188188
case MP_CODE_NATIVE_VIPER:
189189
fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children);
190190
// Check for a generator function, and if so change the type of the object
191-
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
191+
if ((rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0) {
192+
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_coro_wrap;
193+
} else if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
192194
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
193195
}
194-
// CIRCUITPY: no support for mp_type_native_coro_wrap, native coroutine objects (yet).
195196
break;
196197
#endif
197198
#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
206207
// check for generator functions and if so change the type of the object
207208
// A generator is MP_SCOPE_FLAG_ASYNC | MP_SCOPE_FLAG_GENERATOR,
208209
// so check for ASYNC first.
210+
#if MICROPY_PY_ASYNC_AWAIT
209211
if ((rc->scope_flags & MP_SCOPE_FLAG_ASYNC) != 0) {
210212
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_coro_wrap;
211-
} else if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
213+
} else
214+
#endif
215+
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
212216
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
213217
}
214218

py/obj.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,11 @@ extern const mp_obj_type_t mp_type_gen_wrap;
739739
extern const mp_obj_type_t mp_type_native_gen_wrap;
740740
extern const mp_obj_type_t mp_type_gen_instance;
741741
// CIRCUITPY
742-
extern const mp_obj_type_t mp_type_coro_wrap;
743-
// CIRCUITPY
744742
#if MICROPY_PY_ASYNC_AWAIT
743+
extern const mp_obj_type_t mp_type_coro_wrap;
744+
#if MICROPY_EMIT_NATIVE
745+
extern const mp_obj_type_t mp_type_native_coro_wrap;
746+
#endif
745747
extern const mp_obj_type_t mp_type_coro_instance;
746748
#endif
747749
extern const mp_obj_type_t mp_type_fun_builtin_0;

py/objgenerator.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const mp_obj_type_t mp_type_gen_wrap = {
9898
),
9999
};
100100

101+
#if MICROPY_PY_ASYNC_AWAIT
101102
const mp_obj_type_t mp_type_coro_wrap = {
102103
{ &mp_type_type },
103104
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_EXTENDED,
@@ -110,6 +111,7 @@ const mp_obj_type_t mp_type_coro_wrap = {
110111
.unary_op = mp_generic_unary_op,
111112
),
112113
};
114+
#endif
113115

114116
/******************************************************************************/
115117
// 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
141143
MP_BC_PRELUDE_SIG_DECODE(ip);
142144

143145
// Allocate the generator object, with room for local stack (exception stack not needed).
144-
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);
146+
mp_obj_gen_instance_native_t *o = mp_obj_malloc_var(mp_obj_gen_instance_native_t, byte, n_state * sizeof(mp_obj_t),
147+
#if MICROPY_PY_ASYNC_AWAIT
148+
(self_fun->base.type == &mp_type_native_gen_wrap) ? &mp_type_gen_instance : &mp_type_coro_instance
149+
#else
150+
&mp_type_gen_instance
151+
#endif
152+
);
145153

146154
// Parse the input arguments and set up the code state
147155
o->pend_exc = mp_const_none;
@@ -177,6 +185,21 @@ const mp_obj_type_t mp_type_native_gen_wrap = {
177185
),
178186
};
179187

188+
#if MICROPY_PY_ASYNC_AWAIT
189+
const mp_obj_type_t mp_type_native_coro_wrap = {
190+
{ &mp_type_type },
191+
.flags = MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_EXTENDED,
192+
.name = MP_QSTR_coroutine,
193+
#if MICROPY_PY_FUNCTION_ATTRS
194+
.attr = mp_obj_fun_bc_attr,
195+
#endif
196+
MP_TYPE_EXTENDED_FIELDS(
197+
.call = native_gen_wrap_call,
198+
.unary_op = mp_generic_unary_op,
199+
),
200+
};
201+
#endif
202+
180203
#endif // MICROPY_EMIT_NATIVE
181204

182205
/******************************************************************************/

0 commit comments

Comments
 (0)