diff --git a/py/emitglue.c b/py/emitglue.c index 7dc3080870..415aa3a3d3 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -199,13 +199,15 @@ mp_obj_t mp_make_function_from_proto_fun(mp_proto_fun_t proto_fun, const mp_modu switch (rc->kind) { #if MICROPY_EMIT_NATIVE case MP_CODE_NATIVE_PY: - case MP_CODE_NATIVE_VIPER: 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 if (rc->is_generator) { ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap; } break; + case MP_CODE_NATIVE_VIPER: + fun = mp_obj_new_fun_viper(rc->fun_data, context, rc->children); + break; #endif #if MICROPY_EMIT_INLINE_ASM case MP_CODE_NATIVE_ASM: diff --git a/py/obj.h b/py/obj.h index f686fa44ca..9f2bb46e41 100644 --- a/py/obj.h +++ b/py/obj.h @@ -834,6 +834,7 @@ extern const mp_obj_type_t mp_type_fun_builtin_3; extern const mp_obj_type_t mp_type_fun_builtin_var; extern const mp_obj_type_t mp_type_fun_bc; extern const mp_obj_type_t mp_type_fun_native; +extern const mp_obj_type_t mp_type_fun_viper; extern const mp_obj_type_t mp_type_fun_asm; extern const mp_obj_type_t mp_type_module; extern const mp_obj_type_t mp_type_staticmethod; diff --git a/py/objfun.c b/py/objfun.c index ba0ba37068..1bdbb39166 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -427,6 +427,27 @@ MP_DEFINE_CONST_OBJ_TYPE( #endif // MICROPY_EMIT_NATIVE +/******************************************************************************/ +/* viper functions */ + +#if MICROPY_EMIT_NATIVE + +STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { + MP_STACK_CHECK(); + mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in); + mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode); + return fun(self_in, n_args, n_kw, args); +} + +MP_DEFINE_CONST_OBJ_TYPE( + mp_type_fun_viper, + MP_QSTR_function, + MP_TYPE_FLAG_BINDS_SELF, + call, fun_viper_call + ); + +#endif // MICROPY_EMIT_NATIVE + /******************************************************************************/ /* inline assembler functions */ diff --git a/py/objfun.h b/py/objfun.h index b6350d7b67..ddb148dd19 100644 --- a/py/objfun.h +++ b/py/objfun.h @@ -54,11 +54,21 @@ mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_ void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); #if MICROPY_EMIT_NATIVE + static inline mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) { mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(mp_obj_new_fun_bc(def_args, (const byte *)fun_data, mc, child_table)); o->base.type = &mp_type_fun_native; return MP_OBJ_FROM_PTR(o); } + +static inline mp_obj_t mp_obj_new_fun_viper(const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) { + mp_obj_fun_bc_t *o = mp_obj_malloc(mp_obj_fun_bc_t, &mp_type_fun_viper); + o->bytecode = fun_data; + o->context = mc; + o->child_table = child_table; + return MP_OBJ_FROM_PTR(o); +} + #endif #if MICROPY_EMIT_INLINE_ASM