11use crate :: interpreter:: stack:: CallFrame ;
22use crate :: reference:: StoreItem ;
3- use crate :: { Error , FunctionDef , InterpreterRuntime , Result , Store , unlikely} ;
3+ use crate :: { Error , FunctionInstance , InterpreterRuntime , Result , Store , unlikely} ;
44use alloc:: rc:: Rc ;
55use alloc:: { boxed:: Box , format, string:: ToString , vec, vec:: Vec } ;
66use tinywasm_types:: { ExternRef , FuncRef , FuncType , ModuleInstanceAddr , ValType , WasmValue } ;
@@ -14,20 +14,19 @@ impl Function {
1414 self . item . validate_store ( store) ?;
1515 validate_call_params ( & self . ty , params) ?;
1616
17- let func_inst = store. state . get_func ( self . addr ) ;
18- let wasm_func = match & func_inst. func {
19- FunctionDef :: Host ( host_func) => {
17+ let wasm_func = match store. state . get_func ( self . addr ) {
18+ FunctionInstance :: Host ( host_func) => {
2019 return host_func. clone ( ) . call ( FuncContext { store, module_addr : self . module_addr } , params) ;
2120 }
22- FunctionDef :: Wasm ( wasm_func) => wasm_func. clone ( ) ,
21+ FunctionInstance :: Wasm ( wasm_func) => wasm_func,
2322 } ;
2423
2524 // Reset stack, push args, allocate locals, create entry frame.
2625 store. stack . clear ( ) ;
2726 store. stack . values . extend_from_wasmvalues ( params) ?;
28- let locals_base = store. stack . values . enter_locals ( & wasm_func. params , & wasm_func. locals ) ?;
29- let stack_offset = wasm_func. locals ;
30- let callframe = CallFrame :: new ( self . addr , func_inst . owner , locals_base, stack_offset) ;
27+ let locals_base = store. stack . values . enter_locals ( & wasm_func. func . params , & wasm_func. func . locals ) ?;
28+ let stack_offset = wasm_func. func . locals ;
29+ let callframe = CallFrame :: new ( self . addr , wasm_func . owner , locals_base, stack_offset) ;
3130
3231 // Execute until completion and then collect result values from the stack.
3332 InterpreterRuntime :: exec ( store, callframe) ?;
@@ -48,21 +47,17 @@ impl Function {
4847 self . item . validate_store ( store) ?;
4948 validate_call_params ( & self . ty , params) ?;
5049
51- let func_inst = store. state . get_func ( self . addr ) ;
52- let func_inst_owner = func_inst. owner ;
53- let func = func_inst. func . clone ( ) ;
54-
55- match func {
56- FunctionDef :: Host ( host_func) => {
57- let result = host_func. call ( FuncContext { store, module_addr : self . module_addr } , params) ?;
50+ match store. state . get_func ( self . addr ) {
51+ FunctionInstance :: Host ( host_func) => {
52+ let result = host_func. clone ( ) . call ( FuncContext { store, module_addr : self . module_addr } , params) ?;
5853 Ok ( FuncExecution { store, state : FuncExecutionState :: Completed { result : Some ( result) } } )
5954 }
60- FunctionDef :: Wasm ( wasm_func) => {
55+ FunctionInstance :: Wasm ( wasm_func) => {
6156 store. stack . clear ( ) ;
6257 store. stack . values . extend_from_wasmvalues ( params) ?;
63- let locals_base = store. stack . values . enter_locals ( & wasm_func. params , & wasm_func. locals ) ?;
64- let stack_offset = wasm_func. locals ;
65- let callframe = CallFrame :: new ( self . addr , func_inst_owner , locals_base, stack_offset) ;
58+ let locals_base = store. stack . values . enter_locals ( & wasm_func. func . params , & wasm_func. func . locals ) ?;
59+ let stack_offset = wasm_func. func . locals ;
60+ let callframe = CallFrame :: new ( self . addr , wasm_func . owner , locals_base, stack_offset) ;
6661
6762 Ok ( FuncExecution {
6863 store,
@@ -151,7 +146,7 @@ impl HostFunction {
151146 Ok ( result)
152147 } ;
153148
154- let addr = store. add_func ( FunctionDef :: Host ( Rc :: new ( Self { func : Box :: new ( inner_func) , ty : ty. clone ( ) } ) ) , 0 ) ;
149+ let addr = store. add_func ( FunctionInstance :: Host ( Rc :: new ( Self { func : Box :: new ( inner_func) , ty : ty. clone ( ) } ) ) ) ;
155150 Function { item : crate :: StoreItem :: new ( store. id ( ) , addr) , module_addr : 0 , addr, ty : ty. clone ( ) }
156151 }
157152
@@ -169,7 +164,7 @@ impl HostFunction {
169164
170165 let results = R :: val_types ( ) ;
171166 let ty = tinywasm_types:: FuncType { params : P :: val_types ( ) , results } ;
172- let addr = store. add_func ( FunctionDef :: Host ( Rc :: new ( Self { func : Box :: new ( inner_func) , ty : ty. clone ( ) } ) ) , 0 ) ;
167+ let addr = store. add_func ( FunctionInstance :: Host ( Rc :: new ( Self { func : Box :: new ( inner_func) , ty : ty. clone ( ) } ) ) ) ;
173168 Function { item : crate :: StoreItem :: new ( store. id ( ) , addr) , module_addr : 0 , addr, ty }
174169 }
175170}
@@ -317,7 +312,7 @@ impl<'store> FuncExecution<'store> {
317312
318313 match InterpreterRuntime :: exec_with_fuel ( self . store , exec_state. callframe , fuel) ? {
319314 crate :: interpreter:: ExecState :: Completed => {
320- let result_ty = self . store . state . get_func ( * root_func_addr) . func . ty ( ) . clone ( ) ;
315+ let result_ty = self . store . state . get_func ( * root_func_addr) . ty ( ) . clone ( ) ;
321316 let result = collect_call_results ( self . store , & result_ty) ?;
322317 self . state = FuncExecutionState :: Completed { result : None } ;
323318 Ok ( ExecProgress :: Completed ( result) )
@@ -354,7 +349,7 @@ impl<'store> FuncExecution<'store> {
354349
355350 match InterpreterRuntime :: exec_with_time_budget ( self . store , exec_state. callframe , time_budget) ? {
356351 crate :: interpreter:: ExecState :: Completed => {
357- let result_ty = self . store . state . get_func ( * root_func_addr) . func . ty ( ) . clone ( ) ;
352+ let result_ty = self . store . state . get_func ( * root_func_addr) . ty ( ) . clone ( ) ;
358353 let result = collect_call_results ( self . store , & result_ty) ?;
359354 self . state = FuncExecutionState :: Completed { result : None } ;
360355 Ok ( ExecProgress :: Completed ( result) )
0 commit comments