@@ -225,6 +225,9 @@ mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args
225225 DECODE_CODESTATE_SIZE (self -> bytecode , n_state , state_size );
226226
227227 mp_code_state_t * code_state ;
228+ #if MICROPY_ENABLE_PYSTACK
229+ code_state = mp_pystack_alloc (sizeof (mp_code_state_t ) + state_size );
230+ #else
228231 // If we use m_new_obj_var(), then on no memory, MemoryError will be
229232 // raised. But this is not correct exception for a function call,
230233 // RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
@@ -234,6 +237,7 @@ mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args
234237 if (!code_state ) {
235238 return NULL ;
236239 }
240+ #endif
237241
238242 INIT_CODESTATE (code_state , self , n_args , n_kw , args );
239243
@@ -260,13 +264,17 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
260264
261265 // allocate state for locals and stack
262266 mp_code_state_t * code_state = NULL ;
267+ #if MICROPY_ENABLE_PYSTACK
268+ code_state = mp_pystack_alloc (sizeof (mp_code_state_t ) + state_size );
269+ #else
263270 if (state_size > VM_MAX_STATE_ON_STACK ) {
264271 code_state = m_new_obj_var_maybe (mp_code_state_t , byte , state_size );
265272 }
266273 if (code_state == NULL ) {
267274 code_state = alloca (sizeof (mp_code_state_t ) + state_size );
268275 state_size = 0 ; // indicate that we allocated using alloca
269276 }
277+ #endif
270278
271279 INIT_CODESTATE (code_state , self , n_args , n_kw , args );
272280
@@ -312,10 +320,14 @@ STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const
312320 result = code_state -> state [n_state - 1 ];
313321 }
314322
323+ #if MICROPY_ENABLE_PYSTACK
324+ mp_pystack_free (code_state );
325+ #else
315326 // free the state if it was allocated on the heap
316327 if (state_size != 0 ) {
317328 m_del_var (mp_code_state_t , byte , state_size , code_state );
318329 }
330+ #endif
319331
320332 if (vm_return_kind == MP_VM_RETURN_NORMAL ) {
321333 return result ;
0 commit comments