99#include "runtime.h"
1010#include "bc.h"
1111#include "objgenerator.h"
12+ #include "objfun.h"
1213
1314/******************************************************************************/
1415/* generator wrapper */
@@ -18,11 +19,12 @@ typedef struct _mp_obj_gen_wrap_t {
1819 mp_obj_t * fun ;
1920} mp_obj_gen_wrap_t ;
2021
21- mp_obj_t mp_obj_new_gen_instance (const byte * bytecode , uint n_args , const mp_obj_t * args , uint n_args2 , const mp_obj_t * args2 );
22+ mp_obj_t mp_obj_new_gen_instance (mp_obj_dict_t * globals , const byte * bytecode , uint n_args , const mp_obj_t * args ,
23+ uint n_args2 , const mp_obj_t * args2 );
2224
2325STATIC mp_obj_t gen_wrap_call (mp_obj_t self_in , uint n_args , uint n_kw , const mp_obj_t * args ) {
2426 mp_obj_gen_wrap_t * self = self_in ;
25- mp_obj_t self_fun = self -> fun ;
27+ mp_obj_fun_bc_t * self_fun = ( mp_obj_fun_bc_t * ) self -> fun ;
2628 assert (MP_OBJ_IS_TYPE (self_fun , & mp_type_fun_bc ));
2729 int bc_n_args ;
2830 const byte * bc_code ;
@@ -34,7 +36,7 @@ STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp
3436 assert (0 );
3537 }
3638
37- return mp_obj_new_gen_instance (bc_code , len1 , args1 , len2 , args2 );
39+ return mp_obj_new_gen_instance (self_fun -> globals , bc_code , len1 , args1 , len2 , args2 );
3840}
3941
4042const mp_obj_type_t mp_type_gen_wrap = {
@@ -55,6 +57,7 @@ mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
5557
5658typedef struct _mp_obj_gen_instance_t {
5759 mp_obj_base_t base ;
60+ mp_obj_dict_t * globals ;
5861 const byte * code_info ;
5962 const byte * ip ;
6063 mp_obj_t * sp ;
@@ -89,9 +92,12 @@ mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_
8992 } else {
9093 * self -> sp = send_value ;
9194 }
95+ mp_obj_dict_t * old_globals = mp_globals_get ();
96+ mp_globals_set (self -> globals );
9297 mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2 (self -> code_info , & self -> ip ,
9398 & self -> state [self -> n_state - 1 ], & self -> sp , (mp_exc_stack_t * )(self -> state + self -> n_state ),
9499 & self -> exc_sp , throw_value );
100+ mp_globals_set (old_globals );
95101
96102 switch (ret_kind ) {
97103 case MP_VM_RETURN_NORMAL :
@@ -225,7 +231,8 @@ const mp_obj_type_t mp_type_gen_instance = {
225231 .locals_dict = (mp_obj_t )& gen_instance_locals_dict ,
226232};
227233
228- mp_obj_t mp_obj_new_gen_instance (const byte * bytecode , uint n_args , const mp_obj_t * args , uint n_args2 , const mp_obj_t * args2 ) {
234+ mp_obj_t mp_obj_new_gen_instance (mp_obj_dict_t * globals , const byte * bytecode , uint n_args , const mp_obj_t * args ,
235+ uint n_args2 , const mp_obj_t * args2 ) {
229236 const byte * code_info = bytecode ;
230237 // get code info size, and skip the line number table
231238 machine_uint_t code_info_size = bytecode [0 ] | (bytecode [1 ] << 8 ) | (bytecode [2 ] << 16 ) | (bytecode [3 ] << 24 );
@@ -239,6 +246,7 @@ mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_args, const mp_obj
239246 // allocate the generator object, with room for local stack and exception stack
240247 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 ));
241248 o -> base .type = & mp_type_gen_instance ;
249+ o -> globals = globals ;
242250 o -> code_info = code_info ;
243251 o -> sp = & o -> state [0 ] - 1 ; // sp points to top of stack, which starts off 1 below the state
244252 o -> exc_sp = (mp_exc_stack_t * )(o -> state + n_state ) - 1 ;
0 commit comments