1- use crate :: { log, runtime:: RawWasmValue } ;
1+ use crate :: { log, runtime:: RawWasmValue , unlikely , Function } ;
22use alloc:: { boxed:: Box , format, string:: String , string:: ToString , vec, vec:: Vec } ;
3- use tinywasm_types:: { FuncAddr , FuncType , ValType , WasmValue } ;
3+ use tinywasm_types:: { FuncType , ModuleInstanceAddr , ValType , WasmValue } ;
44
55use crate :: {
66 runtime:: { CallFrame , Stack } ,
7- Error , FuncContext , ModuleInstance , Result , Store ,
7+ Error , FuncContext , Result , Store ,
88} ;
99
1010#[ derive( Debug ) ]
1111/// A function handle
1212pub struct FuncHandle {
13- pub ( crate ) module : ModuleInstance ,
14- pub ( crate ) addr : FuncAddr ,
13+ pub ( crate ) module_addr : ModuleInstanceAddr ,
14+ pub ( crate ) addr : u32 ,
1515 pub ( crate ) ty : FuncType ,
1616
1717 /// The name of the function, if it has one
@@ -22,18 +22,13 @@ impl FuncHandle {
2222 /// Call a function (Invocation)
2323 ///
2424 /// See <https://webassembly.github.io/spec/core/exec/modules.html#invocation>
25+ #[ inline]
2526 pub fn call ( & self , store : & mut Store , params : & [ WasmValue ] ) -> Result < Vec < WasmValue > > {
26- let mut stack = Stack :: default ( ) ;
27-
28- // 1. Assert: funcs[func_addr] exists
29- // 2. let func_inst be the functiuon instance funcs[func_addr]
30- let func_inst = store. get_func ( self . addr as usize ) ?. clone ( ) ;
31-
3227 // 3. Let func_ty be the function type
3328 let func_ty = & self . ty ;
3429
3530 // 4. If the length of the provided argument values is different from the number of expected arguments, then fail
36- if func_ty. params . len ( ) != params. len ( ) {
31+ if unlikely ( func_ty. params . len ( ) != params. len ( ) ) {
3732 log:: info!( "func_ty.params: {:?}" , func_ty. params) ;
3833 return Err ( Error :: Other ( format ! (
3934 "param count mismatch: expected {}, got {}" ,
@@ -43,31 +38,34 @@ impl FuncHandle {
4338 }
4439
4540 // 5. For each value type and the corresponding value, check if types match
46- for ( i , ( ty , param ) ) in func_ty. params . iter ( ) . zip ( params) . enumerate ( ) {
41+ if ! unlikely ( func_ty. params . iter ( ) . zip ( params) . enumerate ( ) . all ( | ( i , ( ty , param ) ) | {
4742 if ty != & param. val_type ( ) {
48- return Err ( Error :: Other ( format ! (
49- "param type mismatch at index {}: expected {:?}, got {:?}" ,
50- i , ty , param
51- ) ) ) ;
43+ log :: error! ( "param type mismatch at index {}: expected {:?}, got {:?}" , i , ty , param ) ;
44+ false
45+ } else {
46+ true
5247 }
48+ } ) ) {
49+ return Err ( Error :: Other ( "Type mismatch" . into ( ) ) ) ;
5350 }
5451
55- let locals = match & func_inst. func {
56- crate :: Function :: Host ( h) => {
57- let func = h. func . clone ( ) ;
58- let ctx = FuncContext { store, module : & self . module } ;
52+ let func_inst = store. get_func ( self . addr as usize ) ?;
53+ let wasm_func = match & func_inst. func {
54+ Function :: Host ( host_func) => {
55+ let func = & host_func. clone ( ) . func ;
56+ let ctx = FuncContext { store, module_addr : self . module_addr } ;
5957 return ( func) ( ctx, params) ;
6058 }
61- crate :: Function :: Wasm ( ref f ) => f . locals . to_vec ( ) ,
59+ Function :: Wasm ( wasm_func ) => wasm_func ,
6260 } ;
6361
6462 // 6. Let f be the dummy frame
65- log :: debug! ( "locals: {:?}" , locals ) ;
66- let call_frame = CallFrame :: new ( func_inst, params. iter ( ) . map ( |v| RawWasmValue :: from ( * v) ) , locals ) ;
63+ let call_frame =
64+ CallFrame :: new ( wasm_func . clone ( ) , func_inst. owner , params. iter ( ) . map ( |v| RawWasmValue :: from ( * v) ) ) ;
6765
6866 // 7. Push the frame f to the call stack
6967 // & 8. Push the values to the stack (Not needed since the call frame owns the values)
70- stack. call_stack . push ( call_frame) ? ;
68+ let mut stack = Stack :: new ( call_frame) ;
7169
7270 // 9. Invoke the function instance
7371 let runtime = store. runtime ( ) ;
@@ -125,6 +123,7 @@ macro_rules! impl_into_wasm_value_tuple {
125123 $( $T: Into <WasmValue >) ,*
126124 {
127125 #[ allow( non_snake_case) ]
126+ #[ inline]
128127 fn into_wasm_value_tuple( self ) -> Vec <WasmValue > {
129128 let ( $( $T, ) * ) = self ;
130129 vec![ $( $T. into( ) , ) * ]
@@ -136,6 +135,7 @@ macro_rules! impl_into_wasm_value_tuple {
136135macro_rules! impl_into_wasm_value_tuple_single {
137136 ( $T: ident) => {
138137 impl IntoWasmValueTuple for $T {
138+ #[ inline]
139139 fn into_wasm_value_tuple( self ) -> Vec <WasmValue > {
140140 vec![ self . into( ) ]
141141 }
@@ -164,6 +164,7 @@ macro_rules! impl_from_wasm_value_tuple {
164164 where
165165 $( $T: TryFrom <WasmValue , Error = ( ) >) ,*
166166 {
167+ #[ inline]
167168 fn from_wasm_value_tuple( values: & [ WasmValue ] ) -> Result <Self > {
168169 #[ allow( unused_variables, unused_mut) ]
169170 let mut iter = values. iter( ) ;
@@ -186,6 +187,7 @@ macro_rules! impl_from_wasm_value_tuple {
186187macro_rules! impl_from_wasm_value_tuple_single {
187188 ( $T: ident) => {
188189 impl FromWasmValueTuple for $T {
190+ #[ inline]
189191 fn from_wasm_value_tuple( values: & [ WasmValue ] ) -> Result <Self > {
190192 #[ allow( unused_variables, unused_mut) ]
191193 let mut iter = values. iter( ) ;
@@ -254,6 +256,7 @@ macro_rules! impl_val_types_from_tuple {
254256 where
255257 $( $t: ToValType , ) +
256258 {
259+ #[ inline]
257260 fn val_types( ) -> Box <[ ValType ] > {
258261 Box :: new( [ $( $t:: to_val_type( ) , ) +] )
259262 }
@@ -262,6 +265,7 @@ macro_rules! impl_val_types_from_tuple {
262265}
263266
264267impl ValTypesFromTuple for ( ) {
268+ #[ inline]
265269 fn val_types ( ) -> Box < [ ValType ] > {
266270 Box :: new ( [ ] )
267271 }
@@ -271,6 +275,7 @@ impl<T1> ValTypesFromTuple for T1
271275where
272276 T1 : ToValType ,
273277{
278+ #[ inline]
274279 fn val_types ( ) -> Box < [ ValType ] > {
275280 Box :: new ( [ T1 :: to_val_type ( ) ] )
276281 }
0 commit comments