@@ -117,6 +117,25 @@ where
117117
118118 let parsed_module = config. data_source . mapping . runtime . clone ( ) ;
119119
120+ // Hack: AS currently puts all user imports in one module, in addition
121+ // to the built-in "env" module. The name of that module is not fixed,
122+ // to able able to infer the name we allow only one module with imports,
123+ // with "env" being optional.
124+ let mut user_modules: Vec < _ > = parsed_module
125+ . import_section ( )
126+ . ok_or_else ( || err_msg ( "no import section" ) ) ?
127+ . entries ( )
128+ . into_iter ( )
129+ . map ( |import| import. module ( ) . to_owned ( ) )
130+ . filter ( |module| module != "env" )
131+ . collect ( ) ;
132+ user_modules. dedup ( ) ;
133+ let user_module = if user_modules. len ( ) != 1 {
134+ return Err ( err_msg ( "WASM module has wrong number of import sections" ) ) ;
135+ } else {
136+ user_modules. into_iter ( ) . next ( ) . unwrap ( )
137+ } ;
138+
120139 let module = Module :: from_parity_wasm_module ( parsed_module) . map_err ( |e| {
121140 format_err ! (
122141 "Wasmi could not interpret module of data source `{}`: {}" ,
@@ -128,7 +147,7 @@ where
128147 // Build import resolver
129148 let mut imports = ImportsBuilder :: new ( ) ;
130149 imports. push_resolver ( "env" , & EnvModuleResolver ) ;
131- imports. push_resolver ( "index" , & ModuleResolver ) ;
150+ imports. push_resolver ( user_module , & ModuleResolver ) ;
132151
133152 // Instantiate the runtime module using hosted functions and import resolver
134153 let module = ModuleInstance :: new ( & module, & imports)
@@ -578,18 +597,10 @@ where
578597pub struct EnvModuleResolver ;
579598
580599impl ModuleImportResolver for EnvModuleResolver {
581- fn resolve_func ( & self , field_name : & str , _signature : & Signature ) -> Result < FuncRef , Error > {
600+ fn resolve_func ( & self , field_name : & str , signature : & Signature ) -> Result < FuncRef , Error > {
582601 Ok ( match field_name {
583602 "abort" => FuncInstance :: alloc_host (
584- Signature :: new (
585- & [
586- ValueType :: I32 ,
587- ValueType :: I32 ,
588- ValueType :: I32 ,
589- ValueType :: I32 ,
590- ] [ ..] ,
591- None ,
592- ) ,
603+ signature. clone ( ) ,
593604 ABORT_FUNC_INDEX ,
594605 ) ,
595606 _ => {
@@ -642,7 +653,7 @@ impl ModuleImportResolver for ModuleResolver {
642653 // json
643654 "json.fromBytes" => FuncInstance :: alloc_host ( signature, JSON_FROM_BYTES_FUNC_INDEX ) ,
644655 "json.toI64" => FuncInstance :: alloc_host ( signature, JSON_TO_I64_FUNC_INDEX ) ,
645- "json, toU64" => FuncInstance :: alloc_host ( signature, JSON_TO_U64_FUNC_INDEX ) ,
656+ "json. toU64" => FuncInstance :: alloc_host ( signature, JSON_TO_U64_FUNC_INDEX ) ,
646657 "json.toF64" => FuncInstance :: alloc_host ( signature, JSON_TO_F64_FUNC_INDEX ) ,
647658 "json.toBigInt" => FuncInstance :: alloc_host ( signature, JSON_TO_BIG_INT_FUNC_INDEX ) ,
648659
0 commit comments