@@ -210,25 +210,23 @@ impl ModuleInstance {
210210 let instance = ModuleInstance ( Rc :: new ( instance) ) ;
211211 store. add_instance ( instance. clone ( ) ) ;
212212
213- match ( elem_trapped, data_trapped) {
214- ( Some ( trap) , _) | ( _, Some ( trap) ) => {
215- cold_path ( ) ;
216- Err ( trap. into ( ) )
217- }
218- _ => Ok ( instance) ,
213+ if let Some ( trap) = elem_trapped. or ( data_trapped) {
214+ cold_path ( ) ;
215+ return Err ( trap. into ( ) ) ;
219216 }
217+ Ok ( instance)
220218 }
221219
222220 /// Get a export by name
223221 pub fn export_addr ( & self , name : & str ) -> Option < ExternVal > {
224- let exports = self . 0 . exports . iter ( ) . find ( |e| * e. name == * name) ?;
225- let addr = match exports . kind {
226- ExternalKind :: Func => self . 0 . func_addrs . get ( exports . index as usize ) ?,
227- ExternalKind :: Table => self . 0 . table_addrs . get ( exports . index as usize ) ?,
228- ExternalKind :: Memory => self . 0 . mem_addrs . get ( exports . index as usize ) ?,
229- ExternalKind :: Global => self . 0 . global_addrs . get ( exports . index as usize ) ?,
222+ let export = self . 0 . exports . iter ( ) . find ( |e| * e. name == * name) ?;
223+ let addr = match export . kind {
224+ ExternalKind :: Func => self . 0 . func_addrs . get ( export . index as usize ) ?,
225+ ExternalKind :: Table => self . 0 . table_addrs . get ( export . index as usize ) ?,
226+ ExternalKind :: Memory => self . 0 . mem_addrs . get ( export . index as usize ) ?,
227+ ExternalKind :: Global => self . 0 . global_addrs . get ( export . index as usize ) ?,
230228 } ;
231- Some ( ExternVal :: new ( exports . kind , * addr) )
229+ Some ( ExternVal :: new ( export . kind , * addr) )
232230 }
233231
234232 /// Returns an iterator over all exported extern values for this instance.
@@ -288,25 +286,19 @@ impl ModuleInstance {
288286
289287 #[ inline]
290288 fn require_export ( & self , name : & str ) -> Result < ExternVal > {
291- match self . export_addr ( name) {
292- Some ( addr) => Ok ( addr) ,
293- None => {
294- cold_path ( ) ;
295- Err ( Error :: Other ( format ! ( "Export not found: {name}" ) ) )
296- }
297- }
289+ self . export_addr ( name) . ok_or_else ( || {
290+ cold_path ( ) ;
291+ Error :: Other ( format ! ( "Export not found: {name}" ) )
292+ } )
298293 }
299294
300295 #[ inline]
301296 #[ cfg( feature = "guest-debug" ) ]
302297 fn index_addr < T : Copy > ( slice : & [ T ] , idx : u32 , kind : & str ) -> Result < T > {
303- match slice. get ( idx as usize ) {
304- Some ( addr) => Ok ( * addr) ,
305- None => {
306- cold_path ( ) ;
307- Err ( Error :: Other ( format ! ( "{kind} index out of bounds: {idx}" ) ) )
308- }
309- }
298+ slice. get ( idx as usize ) . copied ( ) . ok_or_else ( || {
299+ cold_path ( ) ;
300+ Error :: Other ( format ! ( "{kind} index out of bounds: {idx}" ) )
301+ } )
310302 }
311303
312304 /// Get any exported extern value by name.
@@ -376,12 +368,9 @@ impl ModuleInstance {
376368 pub fn func_untyped ( & self , store : & Store , name : & str ) -> Result < Function > {
377369 self . validate_store ( store) ?;
378370
379- let func_addr = match self . require_export ( name) ? {
380- ExternVal :: Func ( func_addr) => func_addr,
381- _ => {
382- cold_path ( ) ;
383- return Err ( Error :: Other ( format ! ( "Export is not a function: {name}" ) ) ) ;
384- }
371+ let ExternVal :: Func ( func_addr) = self . require_export ( name) ? else {
372+ cold_path ( ) ;
373+ return Err ( Error :: Other ( format ! ( "Export is not a function: {name}" ) ) ) ;
385374 } ;
386375
387376 Ok ( Function {
@@ -444,7 +433,20 @@ impl ModuleInstance {
444433 store : & Store ,
445434 name : & str ,
446435 ) -> Result < FunctionTyped < P , R > > {
447- let func = self . func_untyped ( store, name) ?;
436+ self . validate_store ( store) ?;
437+
438+ let ExternVal :: Func ( func_addr) = self . require_export ( name) ? else {
439+ cold_path ( ) ;
440+ return Err ( Error :: Other ( format ! ( "Export is not a function: {name}" ) ) ) ;
441+ } ;
442+
443+ let func = Function {
444+ item : StoreItem :: new ( self . 0 . store_id , func_addr) ,
445+ addr : func_addr,
446+ module_addr : self . id ( ) ,
447+ ty : store. state . get_func ( func_addr) . ty ( ) . clone ( ) ,
448+ } ;
449+
448450 Self :: validate_typed_func :: < P , R > ( & func, name) ?;
449451 Ok ( FunctionTyped { func, marker : core:: marker:: PhantomData } )
450452 }
@@ -462,14 +464,17 @@ impl ModuleInstance {
462464 Ok ( FunctionTyped { func, marker : core:: marker:: PhantomData } )
463465 }
464466
467+ #[ inline]
465468 fn validate_typed_func < P : ToWasmTypes , R : ToWasmTypes > ( func : & Function , func_name : & str ) -> Result < ( ) > {
466- if * func. ty . params ( ) != * P :: wasm_types ( ) || * func. ty . results ( ) != * R :: wasm_types ( ) {
469+ let params = P :: wasm_types ( ) ;
470+ let results = R :: wasm_types ( ) ;
471+ if func. ty . params ( ) != params. as_ref ( ) || func. ty . results ( ) != results. as_ref ( ) {
467472 cold_path ( ) ;
468473
469474 #[ cfg( feature = "debug" ) ]
470475 return Err ( Error :: Other ( format ! (
471476 "function type mismatch for {func_name}: expected {:?}, actual {:?}" ,
472- FuncType :: new( & P :: wasm_types ( ) , & R :: wasm_types ( ) ) ,
477+ FuncType :: new( & params , & results ) ,
473478 func. ty
474479 ) ) ) ;
475480
0 commit comments