8000 py/objfun: Split viper fun type out to separate mp_type_fun_viper type. · micropython/micropython@9400229 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9400229

Browse files
committed
py/objfun: Split viper fun type out to separate mp_type_fun_viper type.
Viper functions are quite different to native functions and benefit from being a separate type. For example, viper functions don't have a bytecode- style prelude, and don't support generators or default arguments. Signed-off-by: Damien George <damien@micropython.org>
1 parent 648a757 commit 9400229

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

py/emitglue.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,15 @@ mp_obj_t mp_make_function_from_proto_fun(mp_proto_fun_t proto_fun, const mp_modu
199199
switch (rc->kind) {
200200
#if MICROPY_EMIT_NATIVE
201201
case MP_CODE_NATIVE_PY:
202-
case MP_CODE_NATIVE_VIPER:
203202
fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children);
204203
// Check for a generator function, and if so change the type of the object
205204
if (rc->is_generator) {
206205
((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
207206
}
208207
break;
208+
case MP_CODE_NATIVE_VIPER:
209+
fun = mp_obj_new_fun_viper(rc->fun_data, context, rc->children);
210+
break;
209211
#endif
210212
#if MICROPY_EMIT_INLINE_ASM
211213
case MP_CODE_NATIVE_ASM:

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ extern const mp_obj_type_t mp_type_fun_builtin_3;
834834
extern const mp_obj_type_t mp_type_fun_builtin_var;
835835
extern const mp_obj_type_t mp_type_fun_bc;
836836
extern const mp_obj_type_t mp_type_fun_native;
837+
extern const mp_obj_type_t mp_type_fun_viper;
837838
extern const mp_obj_type_t mp_type_fun_asm;
838839
extern const mp_obj_type_t mp_type_module;
839840
extern const mp_obj_type_t mp_type_staticmethod;

py/objfun.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,27 @@ MP_DEFINE_CONST_OBJ_TYPE(
427427

428428
#endif // MICROPY_EMIT_NATIVE
429429

430+
/******************************************************************************/
431+
/* viper functions */
432+
433+
#if MICROPY_EMIT_NATIVE
434+
435+
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) {
436+
MP_STACK_CHECK();
437+
mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
438+
mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
439+
return fun(self_in, n_args, n_kw, args);
440+
}
441+
442+
MP_DEFINE_CONST_OBJ_TYPE(
443+
mp_type_fun_viper,
444+
MP_QSTR_function,
445+
MP_TYPE_FLAG_BINDS_SELF,
446+
call, fun_viper_call
447+
);
448+
449+
#endif // MICROPY_EMIT_NATIVE
450+
430451
/******************************************************************************/
431452
/* inline assembler functions */
432453

py/objfun.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_
5454
void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
5555

5656
#if MICROPY_EMIT_NATIVE
57+
5758
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) {
5859
mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(mp_obj_new_fun_bc(def_args, (const byte *)fun_data, mc, child_table));
5960
o->base.type = &mp_type_fun_native;
6061
return MP_OBJ_FROM_PTR(o);
6162
}
63+
64+
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) {
65+
mp_obj_fun_bc_t *o = mp_obj_malloc(mp_obj_fun_bc_t, &mp_type_fun_viper);
66+
o->bytecode = fun_data;
67+
o->context = mc;
68+
o->child_table = child_table;
69+
return MP_OBJ_FROM_PTR(o);
70+
}
71+
6272
#endif
6373

6474
#if MICROPY_EMIT_INLINE_ASM

0 commit comments

Comments
 (0)
0