From 05a9f4067e3830f90a8f8f2c2b1410c6f98f54a1 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 27 Nov 2017 19:09:48 +0300 Subject: [PATCH] Fixes --- src/interpreter/emscripten.rs | 6 +++--- src/interpreter/native.rs | 18 +++++++++--------- src/interpreter/tests/basics.rs | 30 +++++++++++++++++++----------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/interpreter/emscripten.rs b/src/interpreter/emscripten.rs index 62ce9dd..1d1994f 100644 --- a/src/interpreter/emscripten.rs +++ b/src/interpreter/emscripten.rs @@ -92,7 +92,7 @@ struct EmscriptenFunctionExecutor { params: EnvParams, } -impl UserFunctionExecutor for EmscriptenFunctionExecutor { +impl<'a> UserFunctionExecutor for EmscriptenFunctionExecutor { fn execute( &mut self, name: &str, @@ -241,7 +241,7 @@ pub fn env_module(params: EnvParams) -> Result, Err let stack_top = params.static_size.unwrap_or(DEFAULT_STACK_TOP); let elements = UserDefinedElements { - executor: Some(&mut function_executor), + executor: Some(function_executor), globals: vec![ ("STACK_BASE".into(), Arc::new(VariableInstance::new(false, VariableType::I32, RuntimeValue::I32(stack_top as i32)).unwrap())), ("STACKTOP".into(), Arc::new(VariableInstance::new(false, VariableType::I32, RuntimeValue::I32(stack_top as i32)).unwrap())), @@ -260,7 +260,7 @@ pub fn env_module(params: EnvParams) -> Result, Err let mut instance = ModuleInstance::new(Weak::default(), "env".into(), module)?; instance.instantiate(None)?; - Ok(Arc::new(native_module(Arc::new(instance), elements)?)) + Ok(native_module(Arc::new(instance), elements)?) } impl Default for EnvParams { diff --git a/src/interpreter/native.rs b/src/interpreter/native.rs index 3bc7127..29fa045 100644 --- a/src/interpreter/native.rs +++ b/src/interpreter/native.rs @@ -69,21 +69,21 @@ impl UserFunctionDescriptor { } /// Set of user-defined module elements. -pub struct UserDefinedElements<'a> { +pub struct UserDefinedElements { /// User globals list. pub globals: HashMap>, /// User functions list. pub functions: Cow<'static, [UserFunctionDescriptor]>, /// Functions executor. - pub executor: Option<&'a mut UserFunctionExecutor>, + pub executor: Option, } /// Native module instance. -pub struct NativeModuleInstance<'a> { +pub struct NativeModuleInstance { /// Underlying module reference. base: Arc, /// User function executor. - executor: RwLock>, + executor: RwLock>, /// By-name functions index. functions_by_name: HashMap, /// User functions list. @@ -94,9 +94,9 @@ pub struct NativeModuleInstance<'a> { globals: Vec>, } -impl<'a> NativeModuleInstance<'a> { +impl NativeModuleInstance { /// Create new native module - pub fn new(base: Arc, elements: UserDefinedElements<'a>) -> Result { + pub fn new(base: Arc, elements: UserDefinedElements) -> Result { if !elements.functions.is_empty() && elements.executor.is_none() { return Err(Error::Function("trying to construct native module with functions, but without executor".into())); } @@ -112,7 +112,7 @@ impl<'a> NativeModuleInstance<'a> { } } -impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> { +impl ModuleInstanceInterface for NativeModuleInstance { fn execute_index(&self, index: u32, params: ExecutionParams) -> Result, Error> { self.base.execute_index(index, params) } @@ -280,8 +280,8 @@ impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> { /// } /// } /// ``` -pub fn native_module<'a>(base: Arc, user_elements: UserDefinedElements<'a>) -> Result { - NativeModuleInstance::new(base, user_elements) +pub fn native_module<'a, E: UserFunctionExecutor + 'a>(base: Arc, user_elements: UserDefinedElements) -> Result, Error> { + Ok(Arc::new(NativeModuleInstance::new(base, user_elements)?)) } impl<'a> PartialEq for UserFunctionDescriptor { diff --git a/src/interpreter/tests/basics.rs b/src/interpreter/tests/basics.rs index e9e9d6b..b696dff 100644 --- a/src/interpreter/tests/basics.rs +++ b/src/interpreter/tests/basics.rs @@ -160,7 +160,7 @@ struct FunctionExecutor { pub values: Vec, } -impl UserFunctionExecutor for FunctionExecutor { +impl<'a> UserFunctionExecutor for &'a mut FunctionExecutor { fn execute(&mut self, name: &str, context: CallerContext) -> Result, Error> { match name { "add" => { @@ -213,7 +213,7 @@ fn native_env_function() { globals: HashMap::new(), functions: ::std::borrow::Cow::from(SIGNATURES), }; - let native_env_instance = Arc::new(native_module(env_instance, functions).unwrap()); + let native_env_instance = native_module(env_instance, functions).unwrap(); let params = ExecutionParams::with_external("env".into(), native_env_instance); let module = module() @@ -265,7 +265,7 @@ fn native_env_function_own_memory() { pub memory_ref: Arc, } - impl UserFunctionExecutor for OwnMemoryExecutor { + impl<'a> UserFunctionExecutor for &'a mut OwnMemoryExecutor { fn execute(&mut self, name: &str, context: CallerContext) -> Result, Error> { match name { "add" => { @@ -287,11 +287,11 @@ fn native_env_function_own_memory() { let env_instance = program.module("env").unwrap(); let memory_ref = Arc::new(OwnMemoryReference { memory: RefCell::new(None) }); let mut executor = OwnMemoryExecutor { memory_ref: memory_ref.clone() }; - let native_env_instance = Arc::new(native_module(env_instance, UserDefinedElements { + let native_env_instance = native_module(env_instance, UserDefinedElements { executor: Some(&mut executor), globals: HashMap::new(), functions: ::std::borrow::Cow::from(SIGNATURES), - }).unwrap()); + }).unwrap(); let params = ExecutionParams::with_external("env".into(), native_env_instance); // create module definition with its own memory @@ -324,11 +324,19 @@ fn native_env_function_own_memory() { #[test] fn native_env_global() { - // module constructor - let module_constructor = |elements| { + struct DummyExecutor; + impl UserFunctionExecutor for DummyExecutor { + fn execute(&mut self, name: &str, context: CallerContext) -> Result, Error> { + // this code should be unreachable, because we actually doesn't call any + // native functions in this test. + unreachable!(); + } + } + + let module_constructor = |elements: UserDefinedElements| { let program = ProgramInstance::with_emscripten_env(Default::default()).unwrap(); let env_instance = program.module("env").unwrap(); - let native_env_instance = Arc::new(native_module(env_instance.clone(), elements).unwrap()); + let native_env_instance = native_module(env_instance.clone(), elements).unwrap(); let params = ExecutionParams::with_external("env".into(), native_env_instance); let module = module() @@ -384,7 +392,7 @@ fn native_custom_error() { globals: HashMap::new(), functions: ::std::borrow::Cow::from(SIGNATURES), }; - let native_env_instance = Arc::new(native_module(env_instance, functions).unwrap()); + let native_env_instance = native_module(env_instance, functions).unwrap(); let params = ExecutionParams::with_external("env".into(), native_env_instance); let module = module() @@ -444,11 +452,11 @@ fn env_native_export_entry_type_check() { memory: program.module("env").unwrap().memory(ItemIndex::Internal(0)).unwrap(), values: Vec::new(), }; - let native_env_instance = Arc::new(native_module(program.module("env").unwrap(), UserDefinedElements { + let native_env_instance = native_module(program.module("env").unwrap(), UserDefinedElements { executor: Some(&mut function_executor), globals: vec![("ext_global".into(), Arc::new(VariableInstance::new(false, VariableType::I32, RuntimeValue::I32(1312)).unwrap()))].into_iter().collect(), functions: ::std::borrow::Cow::from(SIGNATURES), - }).unwrap()); + }).unwrap(); assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I32))))).is_ok()); match native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![], Some(ValueType::I32))))) {