1919use std:: { str, cell:: RefCell , sync:: Arc } ;
2020use wasmi:: {
2121 Module , ModuleInstance , MemoryInstance , MemoryRef , TableRef , ImportsBuilder , ModuleRef ,
22- memory_units:: Pages ,
22+ FuncInstance , memory_units:: Pages ,
2323 RuntimeValue :: { I32 , I64 , self } ,
2424} ;
2525use codec:: { Encode , Decode } ;
@@ -29,7 +29,7 @@ use sp_wasm_interface::{
2929 FunctionContext , Pointer , WordSize , Sandbox , MemoryId , Result as WResult , Function ,
3030} ;
3131use sp_runtime_interface:: unpack_ptr_and_len;
32- use sc_executor_common:: wasm_runtime:: { WasmModule , WasmInstance } ;
32+ use sc_executor_common:: wasm_runtime:: { WasmModule , WasmInstance , InvokeLocation } ;
3333use sc_executor_common:: {
3434 error:: { Error , WasmError } ,
3535 sandbox,
@@ -434,7 +434,7 @@ fn get_heap_base(module: &ModuleRef) -> Result<u32, Error> {
434434fn call_in_wasm_module (
435435 module_instance : & ModuleRef ,
436436 memory : & MemoryRef ,
437- method : & str ,
437+ method : InvokeLocation ,
438438 data : & [ u8 ] ,
439439 host_functions : & [ & ' static dyn Function ] ,
440440 allow_missing_func_imports : bool ,
@@ -449,7 +449,7 @@ fn call_in_wasm_module(
449449 let mut fec = FunctionExecutor :: new (
450450 memory. clone ( ) ,
451451 heap_base,
452- table,
452+ table. clone ( ) ,
453453 host_functions,
454454 allow_missing_func_imports,
455455 missing_functions,
@@ -459,11 +459,36 @@ fn call_in_wasm_module(
459459 let offset = fec. allocate_memory ( data. len ( ) as u32 ) ?;
460460 fec. write_memory ( offset, data) ?;
461461
462- let result = module_instance. invoke_export (
463- method,
464- & [ I32 ( u32:: from ( offset) as i32 ) , I32 ( data. len ( ) as i32 ) ] ,
465- & mut fec,
466- ) ;
462+ let result = match method {
463+ InvokeLocation :: Export ( method) => {
464+ module_instance. invoke_export (
465+ method,
466+ & [ I32 ( u32:: from ( offset) as i32 ) , I32 ( data. len ( ) as i32 ) ] ,
467+ & mut fec,
468+ )
469+ } ,
470+ InvokeLocation :: Table ( func_ref) => {
471+ let func = table. ok_or ( Error :: NoTable ) ?
472+ . get ( func_ref) ?
473+ . ok_or ( Error :: NoTableEntryWithIndex ( func_ref) ) ?;
474+ FuncInstance :: invoke (
475+ & func,
476+ & [ I32 ( u32:: from ( offset) as i32 ) , I32 ( data. len ( ) as i32 ) ] ,
477+ & mut fec,
478+ ) . map_err ( Into :: into)
479+ } ,
480+ InvokeLocation :: TableWithWrapper { dispatcher_ref, func } => {
481+ let dispatcher = table. ok_or ( Error :: NoTable ) ?
482+ . get ( dispatcher_ref) ?
483+ . ok_or ( Error :: NoTableEntryWithIndex ( dispatcher_ref) ) ?;
484+
485+ FuncInstance :: invoke (
486+ & dispatcher,
487+ & [ I32 ( func as _ ) , I32 ( u32:: from ( offset) as i32 ) , I32 ( data. len ( ) as i32 ) ] ,
488+ & mut fec,
489+ ) . map_err ( Into :: into)
490+ } ,
491+ } ;
467492
468493 match result {
469494 Ok ( Some ( I64 ( r) ) ) => {
@@ -677,7 +702,7 @@ pub struct WasmiInstance {
677702unsafe impl Send for WasmiInstance { }
678703
679704impl WasmInstance for WasmiInstance {
680- fn call ( & self , method : & str , data : & [ u8 ] ) -> Result < Vec < u8 > , Error > {
705+ fn call ( & self , method : InvokeLocation , data : & [ u8 ] ) -> Result < Vec < u8 > , Error > {
681706 // We reuse a single wasm instance for multiple calls and a previous call (if any)
682707 // altered the state. Therefore, we need to restore the instance to original state.
683708
0 commit comments