diff --git a/examples/invoke.rs b/examples/invoke.rs index 417db14..2e7bc38 100644 --- a/examples/invoke.rs +++ b/examples/invoke.rs @@ -18,6 +18,7 @@ fn main() { // Intrepreter initialization. // parity_wasm::ProgramInstance can be parameterized with a custom User error which could be returned from imported functions // parity_wasm::DefaultProgramInstance parametrize ProgramInstance with a pre-defined "DummyUserError" + // Initializes a default "env" module also. let program = parity_wasm::DefaultProgramInstance::with_env_params( interpreter::EnvParams { total_stack: 128*1024, @@ -37,7 +38,7 @@ fn main() { // Type section stores function types which are referenced by function_section entries let type_section = module.type_section().expect("No type section found"); - // A given function name used to find export section entry which contains + // Given function name used to find export section entry which contains // an `internal` field which points to the index in the function index space let found_entry = export_section.entries().iter() .find(|entry| func_name == entry.field()).expect(&format!("No export with name {} found", func_name)); @@ -81,7 +82,13 @@ fn main() { interpreter::ExecutionParams::from(args) }; - // Intialize deserialized module. + // Intialize deserialized module. It adds module into It expects 3 parameters: + // - a name for the module + // - a module declaration + // - an optional HashMap of additional external modules (which takes priority over already initialized modules) + // to be: + // - formally validated + // - validated imports against these external modules let module = program.add_module("main", module, None).expect("Failed to initialize module"); println!("Result: {:?}", module.execute_export(func_name, execution_params).expect("")); diff --git a/src/interpreter/module.rs b/src/interpreter/module.rs index 9f0f044..a81a2ba 100644 --- a/src/interpreter/module.rs +++ b/src/interpreter/module.rs @@ -297,7 +297,7 @@ impl ModuleInstance where E: UserError { export_function_type.params(), export_function_type.return_type()))); } } - }, + }, &External::Global(ref global_type) => if global_type.is_mutable() { return Err(Error::Validation(format!("trying to import mutable global {}", import.field()))); } else { @@ -347,15 +347,15 @@ impl ModuleInstance where E: UserError { let mut context = FunctionValidationContext::new( self, externals, - &locals, - DEFAULT_VALUE_STACK_LIMIT, - DEFAULT_FRAME_STACK_LIMIT, + &locals, + DEFAULT_VALUE_STACK_LIMIT, + DEFAULT_FRAME_STACK_LIMIT, function_type.clone()); let block_type = function_type.return_type().map(BlockType::Value).unwrap_or(BlockType::NoResult); Validator::validate_function(&mut context, block_type, function_body.code().elements()) - .map_err(|e| { - if let Error::Validation(msg) = e { + .map_err(|e| { + if let Error::Validation(msg) = e { Error::Validation(format!("Function #{} validation error: {}", index, msg)) } else { e @@ -474,7 +474,7 @@ impl ModuleInstanceInterface for ModuleInstance where E: UserError { }, }) .map(|i| *i) - .ok_or(Error::Program(format!("unresolved import {}", name)))) + .ok_or(Error::Program(format!("unresolved export {}", name)))) } fn table(&self, index: ItemIndex) -> Result>, Error> { diff --git a/src/interpreter/program.rs b/src/interpreter/program.rs index 1040cb9..f34236e 100644 --- a/src/interpreter/program.rs +++ b/src/interpreter/program.rs @@ -67,7 +67,7 @@ impl ProgramInstanceEssence where E: UserError { modules.insert("env".into(), env_module); Ok(ProgramInstanceEssence { modules: RwLock::new(modules), - }) + }) } /// Get module reference.