@@ -70,17 +70,14 @@ impl VM {
7070 if func. ctor {
7171 self . pc . set_low ( func. addr as u32 ) ;
7272 self . pc . set_high ( ( self . modules . len ( ) - 1 ) as u32 ) ;
73- self . add_environment ( ) ;
73+ self . push_environment ( ) ;
7474
7575 while !self . halted {
7676 if !self . step ( ) {
7777 break ;
7878 }
7979 }
8080
81- self . environs
82- . push ( Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ) ;
83-
8481 if !self . error . is_empty ( ) {
8582 self . halted = true ;
8683 return Err ( format ! (
@@ -131,7 +128,7 @@ impl VM {
131128 if function. name == entry_fn_name {
132129 self . pc . set_low ( function. addr as u32 ) ;
133130 self . pc . set_high ( i as u32 ) ;
134- self . add_environment ( ) ;
131+ self . push_environment ( ) ;
135132 return Ok ( ( ) ) ;
136133 }
137134 }
@@ -149,30 +146,44 @@ impl VM {
149146 }
150147
151148 pub ( crate ) fn add_environment ( & mut self ) {
152- let new = Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ;
153- if let Some ( environ) = self . environs . pop ( ) {
154- ( & mut * new. borrow_mut ( ) ) . parent = Some ( environ. clone ( ) ) ;
149+ if self . environs . len ( ) == 0 {
150+ panic ! ( "add_environment: environs is empty" ) ;
155151 }
152+
153+ let new = Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ;
154+ ( & mut * new. borrow_mut ( ) ) . parent = Some ( self . environs . pop ( ) . unwrap ( ) . clone ( ) ) ;
156155 self . environs . push ( new) ;
157156 }
158157
159158 pub ( crate ) fn remove_environment ( & mut self ) {
160159 if self . environs . len ( ) == 0 {
161- panic ! ( "environs is empty" ) ;
160+ panic ! ( "remove_environment: environs is empty" ) ;
162161 }
163162
164163 let environ = self . environs . pop ( ) . unwrap ( ) ;
165164 let environ = & mut * environ. borrow_mut ( ) ;
166165
167166 if environ. parent . is_none ( ) {
168- self . environs . clear ( ) ;
167+ self . pop_environment ( ) ;
169168 return ;
170169 }
171170
172171 self . environs
173172 . push ( core:: mem:: take ( & mut environ. parent ) . unwrap ( ) ) ;
174173 }
175174
175+ pub fn push_environment ( & mut self ) {
176+ self . environs
177+ . push ( Rc :: new ( RefCell :: new ( Environment :: new ( ) ) ) ) ;
178+ }
179+
180+ pub fn pop_environment ( & mut self ) {
181+ if self . environs . len ( ) == 0 {
182+ panic ! ( "pop_environment: environs is empty" ) ;
183+ }
184+ self . environs . pop ( ) ;
185+ }
186+
176187 pub fn get_function_name_from_pc ( & mut self , pc : u64 ) -> Option < String > {
177188 let module_index = pc. get_high ( ) as usize ;
178189 let opcode_index = pc. get_low ( ) ;
0 commit comments