@@ -15,32 +15,32 @@ use macros::*;
1515use traits:: * ;
1616
1717impl DefaultRuntime {
18- pub ( crate ) fn exec ( & self , store : & mut Store , stack : & mut Stack , module : ModuleInstance ) -> Result < ( ) > {
19- log:: debug!( "func_addrs: {:?}" , module. func_addrs( ) ) ;
20- log:: debug!( "func_ty_addrs: {:?}" , module. func_ty_addrs( ) . len( ) ) ;
21- log:: debug!( "store funcs: {:?}" , store. data. funcs. len( ) ) ;
22-
18+ pub ( crate ) fn exec ( & self , store : & mut Store , stack : & mut Stack ) -> Result < ( ) > {
2319 // The current call frame, gets updated inside of exec_one
2420 let mut cf = stack. call_stack . pop ( ) ?;
2521
26- // The function to execute, gets updated from ExecResult::Call
27- let mut func_inst = store. get_func ( cf. func_ptr ) ?. clone ( ) ;
22+ let mut func_inst = cf. func_instance . clone ( ) ;
2823 let mut wasm_func = func_inst. assert_wasm ( ) . expect ( "exec expected wasm function" ) ;
24+
25+ // The function to execute, gets updated from ExecResult::Call
2926 let mut instrs = & wasm_func. instructions ;
3027
31- let mut current_module = module;
28+ let mut current_module = store
29+ . get_module_instance ( func_inst. owner )
30+ . expect ( "exec expected module instance to exist for function" )
31+ . clone ( ) ;
3232
3333 while let Some ( instr) = instrs. get ( cf. instr_ptr ) {
3434 match exec_one ( & mut cf, instr, instrs, stack, store, & current_module) ? {
3535 // Continue execution at the new top of the call stack
3636 ExecResult :: Call => {
3737 cf = stack. call_stack . pop ( ) ?;
38- func_inst = store . get_func ( cf. func_ptr ) ? . clone ( ) ;
39- wasm_func = func_inst. assert_wasm ( ) . expect ( "call expected wasm function" ) ;
38+ func_inst = cf. func_instance . clone ( ) ;
39+ wasm_func = func_inst. assert_wasm ( ) . expect ( "exec expected wasm function" ) ;
4040 instrs = & wasm_func. instructions ;
4141
42- if cf. module != current_module. id ( ) {
43- current_module. swap ( store. get_module_instance ( cf. module ) . unwrap ( ) . clone ( ) ) ;
42+ if cf. func_instance . owner != current_module. id ( ) {
43+ current_module. swap ( store. get_module_instance ( cf. func_instance . owner ) . unwrap ( ) . clone ( ) ) ;
4444 }
4545
4646 continue ;
@@ -135,10 +135,10 @@ fn exec_one(
135135 log:: info!( "start call" ) ;
136136 // prepare the call frame
137137 let func_idx = module. resolve_func_addr ( * v) ;
138- let func_inst = store. get_func ( func_idx as usize ) ?;
138+ let func_inst = store. get_func ( func_idx as usize ) ?. clone ( ) ;
139139
140- let func = match & func_inst. func {
141- crate :: Function :: Wasm ( ref f) => f ,
140+ let ( locals , ty ) = match & func_inst. func {
141+ crate :: Function :: Wasm ( ref f) => ( f . locals . to_vec ( ) , f . ty . clone ( ) ) ,
142142 crate :: Function :: Host ( host_func) => {
143143 let func = host_func. func . clone ( ) ;
144144 log:: info!( "Getting params: {:?}" , host_func. ty. params) ;
@@ -150,8 +150,11 @@ fn exec_one(
150150 }
151151 } ;
152152
153- let params = stack. values . pop_n_rev ( func. ty . params . len ( ) ) ?;
154- let call_frame = CallFrame :: new_raw ( func_idx as usize , & params, func. locals . to_vec ( ) , func_inst. _owner ) ;
153+ let params = stack. values . pop_n_rev ( ty. params . len ( ) ) ?;
154+ log:: info!( "call: current fn owner: {:?}" , module. id( ) ) ;
155+ log:: info!( "call: func owner: {:?}" , func_inst. owner) ;
156+
157+ let call_frame = CallFrame :: new_raw ( func_inst, & params, locals) ;
155158
156159 // push the call frame
157160 cf. instr_ptr += 1 ; // skip the call instruction
@@ -163,29 +166,39 @@ fn exec_one(
163166 }
164167
165168 CallIndirect ( type_addr, table_addr) => {
166- let table_idx = module. resolve_table_addr ( * table_addr) ;
167- let table = store. get_table ( table_idx as usize ) ?;
168-
169- // TODO: currently, the type resolution is subtlely broken for imported functions
170-
171- let call_ty = module. func_ty ( * type_addr) ;
172-
173- let func_idx = stack. values . pop_t :: < u32 > ( ) ?;
169+ let table = store. get_table ( module. resolve_table_addr ( * table_addr) as usize ) ?;
170+ let table_idx = stack. values . pop_t :: < u32 > ( ) ?;
174171
175172 // verify that the table is of the right type, this should be validated by the parser already
176173 assert ! ( table. borrow( ) . kind. element_type == ValType :: RefFunc , "table is not of type funcref" ) ;
177174
178- let func_ref = table
179- . borrow ( )
180- . get ( func_idx as usize ) ?
181- . addr ( )
182- . ok_or_else ( || Trap :: UninitializedElement { index : func_idx as usize } ) ?;
175+ let func_ref = {
176+ table
177+ . borrow ( )
178+ . get ( table_idx as usize ) ?
179+ . addr ( )
180+ . ok_or ( Trap :: UninitializedElement { index : table_idx as usize } ) ?
181+ } ;
183182
184- let func_inst = store. get_func ( func_ref as usize ) ?;
183+ let func_inst = store. get_func ( func_ref as usize ) ?. clone ( ) ;
185184 let func_ty = func_inst. func . ty ( ) ;
186185
187- let func = match & func_inst. func {
188- crate :: Function :: Wasm ( ref f) => f,
186+ log:: info!( "type_addr: {}" , type_addr) ;
187+ log:: info!( "types: {:?}" , module. func_tys( ) ) ;
188+ let call_ty = module. func_ty ( * type_addr) ;
189+
190+ log:: info!( "call_indirect: current fn owner: {:?}" , module. id( ) ) ;
191+ log:: info!( "call_indirect: func owner: {:?}" , func_inst. owner) ;
192+
193+ if func_ty != call_ty {
194+ log:: error!( "indirect call type mismatch: {:?} != {:?}" , func_ty, call_ty) ;
195+ return Err (
196+ Trap :: IndirectCallTypeMismatch { actual : func_ty. clone ( ) , expected : call_ty. clone ( ) } . into ( )
197+ ) ;
198+ }
199+
200+ let locals = match & func_inst. func {
201+ crate :: Function :: Wasm ( ref f) => f. locals . to_vec ( ) ,
189202 crate :: Function :: Host ( host_func) => {
190203 let func = host_func. func . clone ( ) ;
191204 let params = stack. values . pop_params ( & func_ty. params ) ?;
@@ -195,14 +208,8 @@ fn exec_one(
195208 }
196209 } ;
197210
198- if func_ty != call_ty {
199- return Err (
200- Trap :: IndirectCallTypeMismatch { actual : func_ty. clone ( ) , expected : call_ty. clone ( ) } . into ( )
201- ) ;
202- }
203-
204211 let params = stack. values . pop_n_rev ( func_ty. params . len ( ) ) ?;
205- let call_frame = CallFrame :: new_raw ( func_ref as usize , & params, func . locals . to_vec ( ) , func_inst . _owner ) ;
212+ let call_frame = CallFrame :: new_raw ( func_inst , & params, locals) ;
206213
207214 // push the call frame
208215 cf. instr_ptr += 1 ; // skip the call instruction
0 commit comments