@@ -18,7 +18,7 @@ pub struct VM {
1818 pub pc : u64 ,
1919 pub stack : Vec < Value > ,
2020 pub call_stack : Vec < u64 > ,
21- pub environ : Option < Rc < RefCell < Environment > > > ,
21+ pub environs : Vec < Rc < RefCell < Environment > > > ,
2222 pub error : String ,
2323 pub ret : Value ,
2424 pub platform : Option < Box < dyn Platform > > ,
@@ -35,7 +35,7 @@ impl VM {
3535 pc : 0 ,
3636 stack : Vec :: new ( ) ,
3737 call_stack : Vec :: new ( ) ,
38- environ : None ,
38+ environs : Vec :: new ( ) ,
3939 error : String :: new ( ) ,
4040 ret : Value :: Null ( ) ,
4141 platform : None ,
@@ -78,7 +78,8 @@ impl VM {
7878 }
7979 }
8080
81- self . environ = Some ( Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ) ;
81+ self . environs
82+ . push ( Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ) ;
8283
8384 if !self . error . is_empty ( ) {
8485 self . halted = true ;
@@ -149,21 +150,27 @@ impl VM {
149150
150151 pub ( crate ) fn add_environment ( & mut self ) {
151152 let new = Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ;
152- if let Some ( environ) = & self . environ {
153+ if let Some ( environ) = self . environs . pop ( ) {
153154 ( & mut * new. borrow_mut ( ) ) . parent = Some ( environ. clone ( ) ) ;
154155 }
155- self . environ = Some ( new) ;
156+ self . environs . push ( new) ;
156157 }
157158
158- pub ( crate ) fn remove_scope ( & mut self ) {
159- if self . environ . is_none ( ) {
160- panic ! ( "environ is None " ) ;
159+ pub ( crate ) fn remove_environment ( & mut self ) {
160+ if self . environs . len ( ) == 0 {
161+ panic ! ( "environs is empty " ) ;
161162 }
162163
163- // Welcome to rust!
164- self . environ = core:: mem:: take (
165- & mut ( & mut * core:: mem:: take ( & mut self . environ ) . unwrap ( ) . borrow_mut ( ) ) . parent ,
166- ) ;
164+ let environ = self . environs . pop ( ) . unwrap ( ) ;
165+ let environ = & mut * environ. borrow_mut ( ) ;
166+
167+ if environ. parent . is_none ( ) {
168+ self . environs . clear ( ) ;
169+ return ;
170+ }
171+
172+ self . environs
173+ . push ( core:: mem:: take ( & mut environ. parent ) . unwrap ( ) ) ;
167174 }
168175
169176 pub fn get_function_name_from_pc ( & mut self , pc : u64 ) -> Option < String > {
0 commit comments