@@ -8,8 +8,8 @@ use crate::{
88} ;
99use alloc:: {
1010 collections:: BTreeMap ,
11+ rc:: Rc ,
1112 string:: { String , ToString } ,
12- sync:: Arc ,
1313 vec:: Vec ,
1414} ;
1515use tinywasm_types:: * ;
@@ -52,8 +52,7 @@ impl HostFunction {
5252 }
5353}
5454
55- pub ( crate ) type HostFuncInner =
56- Arc < dyn Fn ( FuncContext < ' _ > , & [ WasmValue ] ) -> Result < Vec < WasmValue > > + ' static + Send + Sync > ;
55+ pub ( crate ) type HostFuncInner = Rc < dyn Fn ( FuncContext < ' _ > , & [ WasmValue ] ) -> Result < Vec < WasmValue > > > ;
5756
5857/// The context of a host-function call
5958#[ derive( Debug ) ]
@@ -139,31 +138,28 @@ impl Extern {
139138 /// Create a new function import
140139 pub fn func (
141140 ty : & tinywasm_types:: FuncType ,
142- func : impl Fn ( FuncContext < ' _ > , & [ WasmValue ] ) -> Result < Vec < WasmValue > > + ' static + Send + Sync ,
141+ func : impl Fn ( FuncContext < ' _ > , & [ WasmValue ] ) -> Result < Vec < WasmValue > > + ' static ,
143142 ) -> Self {
144- let inner_func = move |ctx : FuncContext < ' _ > , args : & [ WasmValue ] | {
145- let args = args. to_vec ( ) ;
146- func ( ctx, & args)
147- } ;
148-
149- Self :: Function ( Function :: Host ( HostFunction { func : Arc :: new ( inner_func) , ty : ty. clone ( ) } ) )
143+ Self :: Function ( Function :: Host ( HostFunction { func : Rc :: new ( func) , ty : ty. clone ( ) } ) )
150144 }
151145
152146 /// Create a new typed function import
153- pub fn typed_func < P , R > ( func : impl Fn ( FuncContext < ' _ > , P ) -> Result < R > + ' static + Send + Sync ) -> Self
147+ // TODO: currently, this is slower than `Extern::func` because of the type conversions.
148+ // we should be able to optimize this and make it even faster than `Extern::func`.
149+ pub fn typed_func < P , R > ( func : impl Fn ( FuncContext < ' _ > , P ) -> Result < R > + ' static ) -> Self
154150 where
155151 P : FromWasmValueTuple + ValTypesFromTuple ,
156152 R : IntoWasmValueTuple + ValTypesFromTuple + Debug ,
157153 {
158154 let inner_func = move |ctx : FuncContext < ' _ > , args : & [ WasmValue ] | -> Result < Vec < WasmValue > > {
159- let args = P :: from_wasm_value_tuple ( args. to_vec ( ) ) ?;
155+ let args = P :: from_wasm_value_tuple ( args) ?;
160156 let result = func ( ctx, args) ?;
161157 Ok ( result. into_wasm_value_tuple ( ) )
162158 } ;
163159
164160 let ty = tinywasm_types:: FuncType { params : P :: val_types ( ) , results : R :: val_types ( ) } ;
165161
166- Self :: Function ( Function :: Host ( HostFunction { func : Arc :: new ( inner_func) , ty } ) )
162+ Self :: Function ( Function :: Host ( HostFunction { func : Rc :: new ( inner_func) , ty } ) )
167163 }
168164
169165 pub ( crate ) fn kind ( & self ) -> ExternalKind {
0 commit comments