@@ -46,21 +46,14 @@ typedef struct _mp_obj_gen_wrap_t {
4646 mp_obj_t * fun ;
4747} mp_obj_gen_wrap_t ;
4848
49- mp_obj_t mp_obj_new_gen_instance (mp_obj_dict_t * globals , const byte * bytecode , uint n_args , const mp_obj_t * args ,
50- uint n_args2 , const mp_obj_t * args2 );
49+ mp_obj_t mp_obj_new_gen_instance (mp_obj_fun_bc_t * self_fun , uint n_args , uint n_kw , const mp_obj_t * args );
5150
5251STATIC mp_obj_t gen_wrap_call (mp_obj_t self_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
5352 mp_obj_gen_wrap_t * self = self_in ;
5453 mp_obj_fun_bc_t * self_fun = (mp_obj_fun_bc_t * )self -> fun ;
5554 assert (MP_OBJ_IS_TYPE (self_fun , & mp_type_fun_bc ));
5655
57- const mp_obj_t * args1 , * args2 ;
58- uint len1 , len2 ;
59- if (!mp_obj_fun_prepare_simple_args (self_fun , n_args , n_kw , args , & len1 , & args1 , & len2 , & args2 )) {
60- assert (0 );
61- }
62-
63- return mp_obj_new_gen_instance (self_fun -> globals , self_fun -> bytecode , len1 , args1 , len2 , args2 );
56+ return mp_obj_new_gen_instance (self_fun , n_args , n_kw , args );
6457}
6558
6659const mp_obj_type_t mp_type_gen_wrap = {
@@ -241,10 +234,8 @@ const mp_obj_type_t mp_type_gen_instance = {
241234 .locals_dict = (mp_obj_t )& gen_instance_locals_dict ,
242235};
243236
244- mp_obj_t mp_obj_new_gen_instance (mp_obj_dict_t * globals , const byte * bytecode ,
245- uint n_args , const mp_obj_t * args ,
246- uint n_args2 , const mp_obj_t * args2 ) {
247- const byte * code_info = bytecode ;
237+ mp_obj_t mp_obj_new_gen_instance (mp_obj_fun_bc_t * self_fun , uint n_args , uint n_kw , const mp_obj_t * args ) {
238+ const byte * bytecode = self_fun -> bytecode ;
248239 // get code info size, and skip the line number table
249240 machine_uint_t code_info_size = bytecode [0 ] | (bytecode [1 ] << 8 ) | (bytecode [2 ] << 16 ) | (bytecode [3 ] << 24 );
250241 bytecode += code_info_size ;
@@ -257,33 +248,11 @@ mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode,
257248 // allocate the generator object, with room for local stack and exception stack
258249 mp_obj_gen_instance_t * o = m_new_obj_var (mp_obj_gen_instance_t , byte , n_state * sizeof (mp_obj_t ) + n_exc_stack * sizeof (mp_exc_stack_t ));
259250 o -> base .type = & mp_type_gen_instance ;
260- o -> globals = globals ;
261- o -> code_state .code_info = code_info ;
262- o -> code_state .sp = & o -> code_state .state [0 ] - 1 ; // sp points to top of stack, which starts off 1 below the state
263- o -> code_state .exc_sp = (mp_exc_stack_t * )(o -> code_state .state + n_state ) - 1 ;
264- o -> code_state .n_state = n_state ;
265-
266- // copy args to end of state array, in reverse (that's how mp_execute_bytecode needs it)
267- for (uint i = 0 ; i < n_args ; i ++ ) {
268- o -> code_state .state [n_state - 1 - i ] = args [i ];
269- }
270- for (uint i = 0 ; i < n_args2 ; i ++ ) {
271- o -> code_state .state [n_state - 1 - n_args - i ] = args2 [i ];
272- }
251+ o -> globals = self_fun -> globals ;
273252
274- // set rest of state to MP_OBJ_NULL
275- for (uint i = 0 ; i < n_state - n_args - n_args2 ; i ++ ) {
276- o -> code_state .state [i ] = MP_OBJ_NULL ;
277- }
278-
279- // bytecode prelude: initialise closed over variables
280- for (uint n_local = * bytecode ++ ; n_local > 0 ; n_local -- ) {
281- uint local_num = * bytecode ++ ;
282- o -> code_state .state [n_state - 1 - local_num ] = mp_obj_new_cell (o -> code_state .state [n_state - 1 - local_num ]);
283- }
284-
285- // set ip to start of actual byte code
253+ o -> code_state .n_state = n_state ;
286254 o -> code_state .ip = bytecode ;
255+ mp_setup_code_state (& o -> code_state , self_fun , n_args , n_kw , args );
287256
288257 return o ;
289258}
0 commit comments