Hide FuncInstance

This commit is contained in:
Sergey Pepyakin 2018-01-22 19:38:57 +03:00
parent f1d93302f4
commit 44f61251c1
6 changed files with 38 additions and 30 deletions

View File

@ -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<FuncRef, InterpreterError> {
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)

View File

@ -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);
}

View File

@ -14,15 +14,26 @@ use common::{DEFAULT_FRAME_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
#[derive(Clone, Debug)]
pub struct FuncRef(Rc<FuncInstance>);
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<Signature>,
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);

View File

@ -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 {

View File

@ -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<RuntimeValue>) -> 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<Self, Error> {
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"),
};

View File

@ -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(