py/objfun: Factor out macro for initializing codestate.
This is second part of fun_bc_call() vs mp_obj_fun_bc_prepare_codestate()
common code refactor. This factors out code to initialize codestate
object. After this patch, mp_obj_fun_bc_prepare_codestate() is effectively
DECODE_CODESTATE_SIZE() followed by allocation followed by
INIT_CODESTATE(), and fun_bc_call() starts with that too.
diff --git a/py/objfun.c b/py/objfun.c
index e27413e..8fb3ec6 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -210,6 +210,12 @@
state_size_out_var = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t); \
}
+#define INIT_CODESTATE(code_state, _fun_bc, n_args, n_kw, args) \
+ code_state->fun_bc = _fun_bc; \
+ code_state->ip = 0; \
+ mp_setup_code_state(code_state, n_args, n_kw, args); \
+ code_state->old_globals = mp_globals_get();
+
#if MICROPY_STACKLESS
mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
MP_STACK_CHECK();
@@ -229,12 +235,9 @@
return NULL;
}
- code_state->fun_bc = self;
- code_state->ip = 0;
- mp_setup_code_state(code_state, n_args, n_kw, args);
+ INIT_CODESTATE(code_state, self, n_args, n_kw, args);
// execute the byte code with the correct globals context
- code_state->old_globals = mp_globals_get();
mp_globals_set(self->globals);
return code_state;
@@ -265,12 +268,9 @@
state_size = 0; // indicate that we allocated using alloca
}
- code_state->fun_bc = self;
- code_state->ip = 0;
- mp_setup_code_state(code_state, n_args, n_kw, args);
+ INIT_CODESTATE(code_state, self, n_args, n_kw, args);
// execute the byte code with the correct globals context
- code_state->old_globals = mp_globals_get();
mp_globals_set(self->globals);
mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
mp_globals_set(code_state->old_globals);