diff --git a/spec/src/run.rs b/spec/src/run.rs index 943ba8a..007f8c9 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -21,25 +21,12 @@ use parity_wasm::interpreter::{ MemoryInstance, TableInstance, ModuleInstance, - AnyFunc, + HostFunc, }; -struct DefaultHostCallback; - -impl AnyFunc for DefaultHostCallback { - fn call_as_any( - &self, - _: &mut Any, - args: &[RuntimeValue], - ) -> Result, InterpreterError> { - println!("called host: {:?}", args); - Ok(None) - } -} - struct SpecModule { - default_host_callback: Rc, - table: Rc, + default_host_callback: Rc>, + table: Rc>, memory: Rc, global_i32: Rc, global_i64: Rc, @@ -48,9 +35,14 @@ struct SpecModule { } impl SpecModule { - fn new() -> SpecModule { + fn new() -> Self { + let default_host_callback = Rc::new(|_: &mut (), args: &[RuntimeValue]| -> Result, InterpreterError> { + println!("called host: {:?}", args); + Ok(None) + }); + SpecModule { - default_host_callback: Rc::new(DefaultHostCallback) as Rc, + default_host_callback: default_host_callback, table: Rc::new(TableInstance::new(&TableType::new(10, Some(20))).unwrap()), memory: Rc::new(MemoryInstance::new(&MemoryType::new(1, Some(2))).unwrap()), global_i32: Rc::new(GlobalInstance::new(RuntimeValue::I32(666), false)), @@ -61,18 +53,18 @@ impl SpecModule { } } -impl ImportResolver for SpecModule { +impl ImportResolver<()> for SpecModule { fn resolve_func( &self, field_name: &str, func_type: &FunctionType, - ) -> Result, InterpreterError> { + ) -> Result>, InterpreterError> { if field_name == "print" { - let func = FuncInstance::Host { - func_type: Rc::new(func_type.clone()), - host_func: Rc::clone(&self.default_host_callback), - }; - return Ok(Rc::new(func)); + let func = FuncInstance::alloc_host( + Rc::new(func_type.clone()), + Rc::clone(&self.default_host_callback) + ); + return Ok(func); } Err(InterpreterError::Global(format!("Unknown host func import {}", field_name))) @@ -119,7 +111,7 @@ impl ImportResolver for SpecModule { &self, field_name: &str, _table_type: &TableType, - ) -> Result, InterpreterError> { + ) -> Result>, InterpreterError> { if field_name == "table" { return Ok(Rc::clone(&self.table)); } @@ -128,10 +120,10 @@ impl ImportResolver for SpecModule { } } -fn load_module(base_dir: &str, path: &str, name: &Option, program: &mut ProgramInstance) -> Rc { +fn load_module(base_dir: &str, path: &str, name: &Option, program: &mut ProgramInstance) -> Rc> { let module = try_deserialize(base_dir, path).expect(&format!("Wasm file {} failed to load", path)); - program.add_import_resolver("spectest", Box::new(SpecModule::new()) as Box); + program.add_import_resolver("spectest", Box::new(SpecModule::new()) as Box>); let module_name = name.as_ref().map(|s| s.as_ref()).unwrap_or("wasm_test").trim_left_matches('$'); let module_instance = program.add_module(module_name, module, &mut ()).expect(&format!("Failed adding {} module", module_name)); diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index 8980add..73912e7 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -149,7 +149,7 @@ pub use self::memory::MemoryInstance; pub use self::table::TableInstance; pub use self::program::ProgramInstance; pub use self::value::RuntimeValue; -pub use self::host::{HostModule, HostModuleBuilder, AnyFunc, AsReturnVal, FromArg}; +pub use self::host::{HostModule, HostModuleBuilder, HostFunc, AsReturnVal, FromArg}; pub use self::imports::{ImportResolver, Imports}; pub use self::module::ModuleInstance; pub use self::global::GlobalInstance; diff --git a/src/interpreter/program.rs b/src/interpreter/program.rs index 9829a37..b840874 100644 --- a/src/interpreter/program.rs +++ b/src/interpreter/program.rs @@ -102,10 +102,14 @@ impl ProgramInstance { FuncInstance::invoke(Rc::clone(&func_instance), args, state) } - pub fn module(&self, name: &str) -> Option<&ImportResolver> { + pub fn resolver(&self, name: &str) -> Option<&ImportResolver> { self.modules .get(name) .map(|x| &**x as &ImportResolver) .or_else(|| self.resolvers.get(name).map(|x| &**x)) } + + pub fn module(&self, name: &str) -> Option>> { + self.modules.get(name).cloned() + } } diff --git a/src/interpreter/tests/wasm.rs b/src/interpreter/tests/wasm.rs index 2f4875a..975cd4d 100644 --- a/src/interpreter/tests/wasm.rs +++ b/src/interpreter/tests/wasm.rs @@ -47,7 +47,7 @@ fn interpreter_accumulate_u8() { .expect("Failed to initialize module"); let env_memory = { - let env_module = program.module("env").unwrap(); + let env_module = program.resolver("env").unwrap(); env_module.resolve_memory("memory", &MemoryType::new(1, None)).unwrap() };