From 44f61251c1c9265d8be44d12199f18611a8df904 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 22 Jan 2018 19:38:57 +0300 Subject: [PATCH] Hide FuncInstance --- examples/tictactoe.rs | 6 +++--- spec/src/run.rs | 4 ++-- src/func.rs | 29 ++++++++++++++++------------- src/lib.rs | 2 +- src/runner.rs | 13 +++++++++---- src/tests/host.rs | 14 +++++++------- 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index 76760fc..b7decbb 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -7,7 +7,7 @@ use std::fs::File; use wasmi::{ Error as InterpreterError, ModuleInstance, ModuleRef, Externals, RuntimeValue, FuncRef, TryInto, ModuleImportResolver, - FuncInstance, HostError, ImportsBuilder, Signature, ValueType, + HostError, ImportsBuilder, Signature, ValueType, }; #[derive(Debug)] @@ -175,9 +175,9 @@ impl<'a> ModuleImportResolver for RuntimeModuleImportResolver { ) -> Result { let func_ref = match field_name { "set" => { - FuncInstance::alloc_host(Signature::new(&[ValueType::I32], None), SET_FUNC_INDEX) + FuncRef::alloc_host(Signature::new(&[ValueType::I32], None), SET_FUNC_INDEX) }, - "get" => FuncInstance::alloc_host(Signature::new(&[ValueType::I32], Some(ValueType::I32)), GET_FUNC_INDEX), + "get" => FuncRef::alloc_host(Signature::new(&[ValueType::I32], Some(ValueType::I32)), GET_FUNC_INDEX), _ => return Err( InterpreterError::Function( format!("host module doesn't export function with name {}", field_name) diff --git a/spec/src/run.rs b/spec/src/run.rs index 3a3e1a2..4cd5ac2 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use serde_json; use super::test; use wasmi::{ - Error as InterpreterError, Externals, FuncInstance, FuncRef, + Error as InterpreterError, Externals, FuncRef, GlobalInstance, GlobalRef, ImportResolver, ImportsBuilder, MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance, ModuleRef, RuntimeValue, TableInstance, TableRef, ValueType, @@ -81,7 +81,7 @@ impl ModuleImportResolver for SpecModule { )); } - let func = FuncInstance::alloc_host(func_type.clone(), PRINT_FUNC_INDEX); + let func = FuncRef::alloc_host(func_type.clone(), PRINT_FUNC_INDEX); return Ok(func); } diff --git a/src/func.rs b/src/func.rs index c686e36..3b382e8 100644 --- a/src/func.rs +++ b/src/func.rs @@ -14,15 +14,26 @@ use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT}; #[derive(Clone, Debug)] pub struct FuncRef(Rc); -impl ::std::ops::Deref for FuncRef { - type Target = FuncInstance; - fn deref(&self) -> &FuncInstance { +impl FuncRef { + pub fn alloc_host(signature: Signature, host_func_index: usize) -> FuncRef { + let func = FuncInstance::Host { + signature, + host_func_index, + }; + FuncRef(Rc::new(func)) + } + + pub fn signature(&self) -> &Signature { + self.0.signature() + } + + pub(crate) fn as_instance(&self) -> &FuncInstance { &self.0 } } #[derive(Clone)] -pub enum FuncInstance { +pub(crate) enum FuncInstance { Internal { signature: Rc, module: ModuleRef, @@ -70,14 +81,6 @@ impl FuncInstance { FuncRef(Rc::new(func)) } - pub fn alloc_host(signature: Signature, host_func_index: usize) -> FuncRef { - let func = FuncInstance::Host { - signature, - host_func_index, - }; - FuncRef(Rc::new(func)) - } - pub fn signature(&self) -> &Signature { match *self { FuncInstance::Internal { ref signature, .. } => signature, @@ -102,7 +105,7 @@ impl FuncInstance { Host(usize, &'a [RuntimeValue]), } - let result = match *func { + let result = match *func.as_instance() { FuncInstance::Internal { ref signature, .. } => { let mut stack = StackWithLimit::with_data(args.into_iter().cloned(), DEFAULT_VALUE_STACK_LIMIT); diff --git a/src/lib.rs b/src/lib.rs index da44a23..497ac58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,7 +134,7 @@ pub use self::host::{Externals, NopExternals, HostError}; pub use self::imports::{ModuleImportResolver, ImportResolver, ImportsBuilder}; pub use self::module::{ModuleInstance, ModuleRef, ExternVal, NotStartedModuleRef}; pub use self::global::{GlobalInstance, GlobalRef}; -pub use self::func::{FuncInstance, FuncRef}; +pub use self::func::{FuncRef}; pub use self::types::{Signature, ValueType, GlobalDescriptor, TableDescriptor, MemoryDescriptor}; pub struct LoadedModule { diff --git a/src/runner.rs b/src/runner.rs index e36553a..63f740b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -77,7 +77,12 @@ impl<'a, E: Externals> Interpreter<'a, E> { loop { let mut function_context = function_stack.pop_back().expect("on loop entry - not empty; on loop continue - checking for emptiness; qed"); let function_ref = function_context.function.clone(); - let function_body = function_ref.body().expect("Host functions checked in function_return below; Internal functions always have a body; qed"); + let function_body = function_ref + .as_instance() + .body() + .expect( + "Host functions checked in function_return below; Internal functions always have a body; qed" + ); if !function_context.is_initialized() { let return_type = function_context.return_type; function_context.initialize(&function_body.locals); @@ -96,7 +101,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } }, RunResult::NestedCall(nested_func) => { - match *nested_func { + match *nested_func.as_instance() { FuncInstance::Internal { .. } => { let nested_context = function_context.nested(nested_func.clone())?; function_stack.push_back(function_context); @@ -1002,7 +1007,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { impl FunctionContext { pub fn new<'store>(function: FuncRef, value_stack_limit: usize, frame_stack_limit: usize, signature: &Signature, args: Vec) -> Self { - let module = match *function { + let module = match *function.as_instance() { FuncInstance::Internal { ref module, .. } => module.clone(), FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"), }; @@ -1020,7 +1025,7 @@ impl FunctionContext { pub fn nested(&mut self, function: FuncRef) -> Result { let (function_locals, module, function_return_type) = { - let module = match *function { + let module = match *function.as_instance() { FuncInstance::Internal { ref module, .. } => module.clone(), FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"), }; diff --git a/src/tests/host.rs b/src/tests/host.rs index c96a2d9..a952957 100644 --- a/src/tests/host.rs +++ b/src/tests/host.rs @@ -1,5 +1,5 @@ use { - Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, + Error, Signature, Externals, FuncRef, HostError, ImportsBuilder, MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef, RuntimeValue, TryInto, LoadedModule, load_from_buffer, TableDescriptor, MemoryDescriptor, }; @@ -187,7 +187,7 @@ impl ModuleImportResolver for TestHost { ))); } - Ok(FuncInstance::alloc_host(signature.clone(), index)) + Ok(FuncRef::alloc_host(signature.clone(), index)) } fn resolve_memory( @@ -435,7 +435,7 @@ fn defer_providing_externals() { ))); } - Ok(FuncInstance::alloc_host(signature.clone(), INC_FUNC_INDEX)) + Ok(FuncRef::alloc_host(signature.clone(), INC_FUNC_INDEX)) } fn resolve_memory( @@ -560,7 +560,7 @@ fn two_envs_one_externals() { } }; - Ok(FuncInstance::alloc_host(signature.clone(), index)) + Ok(FuncRef::alloc_host(signature.clone(), index)) } } @@ -584,7 +584,7 @@ fn two_envs_one_externals() { } }; - Ok(FuncInstance::alloc_host(signature.clone(), index)) + Ok(FuncRef::alloc_host(signature.clone(), index)) } } @@ -658,7 +658,7 @@ fn dynamically_add_host_func() { let host_func_index = table_index + 1; self.added_funcs += 1; - let added_func = FuncInstance::alloc_host( + let added_func = FuncRef::alloc_host( Signature::new(&[], Some(ValueType::I32)), host_func_index as usize, ); @@ -688,7 +688,7 @@ fn dynamically_add_host_func() { )) } }; - Ok(FuncInstance::alloc_host(signature.clone(), index)) + Ok(FuncRef::alloc_host(signature.clone(), index)) } fn resolve_table(