Skip to content

Commit 763c058

Browse files
leoyvensJannis Pohlmann
authored andcommitted
runtime: Infer name of user module
1 parent a1236bb commit 763c058

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

graph/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub mod ext;
4242
/// use graph::prelude::*;
4343
/// ```
4444
pub mod prelude {
45-
pub use failure::{self, bail, format_err, Error, Fail, SyncFailure};
45+
pub use failure::{self, bail, err_msg, format_err, Error, Fail, SyncFailure};
4646
pub use serde_derive::{Deserialize, Serialize};
4747
pub use slog::{self, crit, debug, error, info, o, trace, warn, Logger};
4848
pub use std::fmt::Debug;

runtime/wasm/src/module/mod.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
578597
pub struct EnvModuleResolver;
579598

580599
impl 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

Comments
 (0)