@@ -3,7 +3,7 @@ use crate::reference::StoreItem;
33use crate :: { Error , FunctionInstance , InterpreterRuntime , Result , Store , unlikely} ;
44use alloc:: rc:: Rc ;
55use alloc:: { boxed:: Box , format, string:: ToString , vec, vec:: Vec } ;
6- use tinywasm_types:: { ExternRef , FuncRef , FuncType , ModuleInstanceAddr , ValType , WasmValue } ;
6+ use tinywasm_types:: { ExternRef , FuncRef , FuncType , ModuleInstanceAddr , WasmType , WasmValue } ;
77
88impl Function {
99 /// Call a function (Invocation)
@@ -132,12 +132,12 @@ impl HostFunction {
132132 let ty = ty_inner. clone ( ) ;
133133 let result = func ( ctx, args) ?;
134134
135- if result. len ( ) != ty. results . len ( ) {
135+ if result. len ( ) != ty. results ( ) . len ( ) {
136136 return Err ( crate :: Error :: InvalidHostFnReturn { expected : ty. clone ( ) , actual : result } ) ;
137137 } ;
138138
139- result. iter ( ) . zip ( ty. results . iter ( ) ) . try_for_each ( |( val, res_ty) | {
140- if val . val_type ( ) != * res_ty {
139+ result. iter ( ) . zip ( ty. results ( ) . iter ( ) ) . try_for_each ( |( val, res_ty) | {
140+ if WasmType :: from ( val ) != * res_ty {
141141 return Err ( crate :: Error :: InvalidHostFnReturn { expected : ty. clone ( ) , actual : result. clone ( ) } ) ;
142142 }
143143 Ok ( ( ) )
@@ -153,17 +153,16 @@ impl HostFunction {
153153 /// Create a new typed host function import.
154154 pub fn from < P , R > ( store : & mut Store , func : impl Fn ( FuncContext < ' _ > , P ) -> Result < R > + ' static ) -> Function
155155 where
156- P : FromWasmValueTuple + ValTypesFromTuple ,
157- R : IntoWasmValueTuple + ValTypesFromTuple ,
156+ P : FromWasmValueTuple + WasmTypesFromTuple ,
157+ R : IntoWasmValueTuple + WasmTypesFromTuple ,
158158 {
159159 let inner_func = move |ctx : FuncContext < ' _ > , args : & [ WasmValue ] | -> Result < Vec < WasmValue > > {
160160 let args = P :: from_wasm_value_tuple ( args) ?;
161161 let result = func ( ctx, args) ?;
162162 Ok ( result. into_wasm_value_tuple ( ) )
163163 } ;
164164
165- let results = R :: val_types ( ) ;
166- let ty = tinywasm_types:: FuncType { params : P :: val_types ( ) , results } ;
165+ let ty = tinywasm_types:: FuncType :: new ( & P :: wasm_types ( ) , & R :: wasm_types ( ) ) ;
167166 let addr = store. add_func ( FunctionInstance :: Host ( Rc :: new ( Self { func : Box :: new ( inner_func) , ty : ty. clone ( ) } ) ) ) ;
168167 Function { item : crate :: StoreItem :: new ( store. id ( ) , addr) , module_addr : 0 , addr, ty }
169168 }
@@ -363,24 +362,24 @@ impl<'store> FuncExecution<'store> {
363362}
364363
365364fn validate_call_params ( func_ty : & FuncType , params : & [ WasmValue ] ) -> Result < ( ) > {
366- if unlikely ( func_ty. params . len ( ) != params. len ( ) ) {
365+ if unlikely ( func_ty. params ( ) . len ( ) != params. len ( ) ) {
367366 return Err ( Error :: Other ( format ! (
368367 "param count mismatch: expected {}, got {}" ,
369- func_ty. params. len( ) ,
368+ func_ty. params( ) . len( ) ,
370369 params. len( )
371370 ) ) ) ;
372371 }
373372
374- if !( func_ty. params . iter ( ) . zip ( params) . all ( |( ty, param) | ty == & param. val_type ( ) ) ) {
373+ if !( func_ty. params ( ) . iter ( ) . zip ( params) . all ( |( ty, param) | ty == & param. into ( ) ) ) {
375374 return Err ( Error :: Other ( "Type mismatch" . into ( ) ) ) ;
376375 }
377376
378377 Ok ( ( ) )
379378}
380379
381380fn collect_call_results ( store : & mut Store , func_ty : & FuncType ) -> Result < Vec < WasmValue > > {
382- debug_assert ! ( store. stack. values. len( ) >= func_ty. results. len( ) ) ; // m values are on the top of the stack (Ensured by validation)
383- let mut res: Vec < _ > = store. stack . values . pop_types ( func_ty. results . iter ( ) . rev ( ) ) . collect ( ) ; // pop in reverse order since the stack is LIFO
381+ debug_assert ! ( store. stack. values. len( ) >= func_ty. results( ) . len( ) ) ; // m values are on the top of the stack (Ensured by validation)
382+ let mut res: Vec < _ > = store. stack . values . pop_types ( func_ty. results ( ) . iter ( ) . rev ( ) ) . collect ( ) ; // pop in reverse order since the stack is LIFO
384383 res. reverse ( ) ; // reverse to get the original order
385384 Ok ( res)
386385}
@@ -443,28 +442,28 @@ impl<'store, R: FromWasmValueTuple> FuncExecutionTyped<'store, R> {
443442 }
444443}
445444
446- pub trait ValTypesFromTuple {
447- fn val_types ( ) -> Box < [ ValType ] > ;
445+ pub trait WasmTypesFromTuple {
446+ fn wasm_types ( ) -> Box < [ WasmType ] > ;
448447}
449448
450- pub trait ToValType {
451- fn to_val_type ( ) -> ValType ;
449+ pub trait ToWasmType {
450+ fn to_wasm_type ( ) -> WasmType ;
452451}
453452
454453macro_rules! impl_scalar_wasm_traits {
455454 ( $( $T: ty => $val_ty: ident) ,+ $( , ) ?) => {
456455 $(
457- impl ToValType for $T {
456+ impl ToWasmType for $T {
458457 #[ inline]
459- fn to_val_type ( ) -> ValType {
460- ValType :: $val_ty
458+ fn to_wasm_type ( ) -> WasmType {
459+ WasmType :: $val_ty
461460 }
462461 }
463462
464- impl ValTypesFromTuple for $T {
463+ impl WasmTypesFromTuple for $T {
465464 #[ inline]
466- fn val_types ( ) -> Box <[ ValType ] > {
467- Box :: new( [ ValType :: $val_ty] )
465+ fn wasm_types ( ) -> Box <[ WasmType ] > {
466+ Box :: new( [ WasmType :: $val_ty] )
468467 }
469468 }
470469
@@ -495,13 +494,13 @@ macro_rules! impl_scalar_wasm_traits {
495494
496495macro_rules! impl_tuple_traits {
497496 ( $( $T: ident) ,+) => {
498- impl <$( $T) ,+> ValTypesFromTuple for ( $( $T, ) +)
497+ impl <$( $T) ,+> WasmTypesFromTuple for ( $( $T, ) +)
499498 where
500- $( $T: ToValType , ) +
499+ $( $T: ToWasmType , ) +
501500 {
502501 #[ inline]
503- fn val_types ( ) -> Box <[ ValType ] > {
504- Box :: new( [ $( $T:: to_val_type ( ) , ) +] )
502+ fn wasm_types ( ) -> Box <[ WasmType ] > {
503+ Box :: new( [ $( $T:: to_wasm_type ( ) , ) +] )
505504 }
506505 }
507506
@@ -569,9 +568,9 @@ impl_scalar_wasm_traits!(
569568) ;
570569impl_tuple ! ( impl_tuple_traits) ;
571570
572- impl ValTypesFromTuple for ( ) {
571+ impl WasmTypesFromTuple for ( ) {
573572 #[ inline]
574- fn val_types ( ) -> Box < [ ValType ] > {
573+ fn wasm_types ( ) -> Box < [ WasmType ] > {
575574 Box :: new ( [ ] )
576575 }
577576}
0 commit comments