Hide ValueType.

This commit is contained in:
Sergey Pepyakin 2018-01-18 16:39:14 +03:00
parent 1bf3702440
commit ca6299ba53
10 changed files with 63 additions and 27 deletions

View File

@ -2,7 +2,7 @@ use std::rc::Rc;
use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
use parity_wasm::elements::{FunctionType, Local, Opcodes};
use parity_wasm::elements::{Local, Opcodes};
use {Error, Signature};
use host::Externals;
use runner::{prepare_function_args, FunctionContext, Interpreter};

View File

@ -1,8 +1,9 @@
use std::rc::Rc;
use std::cell::Cell;
use parity_wasm::elements::ValueType;
use value::RuntimeValue;
use Error;
use types::ValueType;
use parity_wasm::elements::{ValueType as EValueType};
#[derive(Clone, Debug)]
pub struct GlobalRef(Rc<GlobalInstance>);
@ -56,4 +57,8 @@ impl GlobalInstance {
pub fn value_type(&self) -> ValueType {
self.val.get().value_type()
}
pub(crate) fn elements_value_type(&self) -> EValueType {
self.value_type().into_elements()
}
}

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use parity_wasm::elements::{FunctionType, GlobalType, MemoryType, TableType};
use parity_wasm::elements::{GlobalType, MemoryType, TableType};
use global::GlobalRef;
use memory::MemoryRef;
use func::FuncRef;

View File

@ -97,8 +97,6 @@ impl From<::common::stack::Error> for Error {
}
}
use types::Signature;
mod validation;
mod common;
mod memory;
@ -123,3 +121,4 @@ 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::types::{Signature, ValueType};

View File

@ -3,7 +3,7 @@ use std::cell::RefCell;
use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
use parity_wasm::elements::{External, FunctionType, InitExpr, Internal, Opcode, ResizableLimits, Type};
use parity_wasm::elements::{External, InitExpr, Internal, Opcode, ResizableLimits, Type};
use {Error, Signature, MemoryInstance, RuntimeValue, TableInstance};
use imports::ImportResolver;
use global::{GlobalInstance, GlobalRef};
@ -165,7 +165,7 @@ impl ModuleInstance {
let instance = ModuleRef(Rc::new(ModuleInstance::default()));
for &Type::Function(ref ty) in module.type_section().map(|ts| ts.types()).unwrap_or(&[]) {
let signature = Rc::new(ty.clone().into());
let signature = Rc::new(Signature::from_elements(ty.clone()));
instance.push_signature(signature);
}
@ -207,7 +207,7 @@ impl ModuleInstance {
instance.push_memory(memory.clone());
}
(&External::Global(ref gl), &ExternVal::Global(ref global)) => {
if gl.content_type() != global.value_type() {
if gl.content_type() != global.elements_value_type() {
return Err(Error::Instantiation(format!(
"Expect global with {:?} type, but provided global with {:?} type",
gl.content_type(),
@ -386,7 +386,7 @@ impl ModuleInstance {
let &Type::Function(ref func_type) = types
.get(fn_ty_idx as usize)
.expect("Due to validation functions should have valid types");
let signature = func_type.clone().into();
let signature = Signature::from_elements(func_type.clone());
let func = imports.resolve_func(module_name, field_name, &signature)?;
ExternVal::Func(func)
}

View File

@ -4,7 +4,7 @@ use std::{u32, usize};
use std::fmt::{self, Display};
use std::iter::repeat;
use std::collections::{HashMap, VecDeque};
use parity_wasm::elements::{Opcode, BlockType, Local, FunctionType};
use parity_wasm::elements::{Opcode, BlockType, Local};
use {Error, Signature};
use module::ModuleRef;
use func::FuncRef;
@ -1010,7 +1010,7 @@ impl FunctionContext {
is_initialized: false,
function: function,
module: module,
return_type: signature.return_type().map(|vt| BlockType::Value(vt)).unwrap_or(BlockType::NoResult),
return_type: signature.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult),
value_stack: StackWithLimit::with_limit(value_stack_limit),
frame_stack: StackWithLimit::with_limit(frame_stack_limit),
locals: args,
@ -1025,7 +1025,7 @@ impl FunctionContext {
FuncInstance::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
};
let function_type = function.signature();
let function_return_type = function_type.return_type().map(|vt| BlockType::Value(vt)).unwrap_or(BlockType::NoResult);
let function_return_type = function_type.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult);
let function_locals = prepare_function_args(&function_type, &mut self.value_stack)?;
(function_locals, module, function_return_type)
};

View File

@ -1,11 +1,12 @@
use parity_wasm::elements::{deserialize_buffer, FunctionType, MemoryType, TableType, ValueType};
use parity_wasm::elements::{deserialize_buffer, MemoryType, TableType};
use validation::{validate_module, ValidatedModule};
use {
Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder,
MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef,
RuntimeValue, TryInto,
};
use types::ValueType;
use wabt::wat2wasm;
#[derive(Debug, Clone, PartialEq)]

View File

@ -1,5 +1,5 @@
use parity_wasm::elements::deserialize_file;
use parity_wasm::elements::{FunctionType, GlobalType, MemoryType, Module, TableType};
use parity_wasm::elements::{GlobalType, MemoryType, Module, TableType};
use {Error, Signature, FuncRef, GlobalInstance, GlobalRef, ImportsBuilder, MemoryInstance,
MemoryRef, ModuleImportResolver, ModuleInstance, NopExternals, RuntimeValue,
TableInstance, TableRef};

View File

@ -1,28 +1,59 @@
use parity_wasm::elements::{FunctionType, ValueType};
use parity_wasm::elements::{FunctionType, ValueType as EValueType};
#[derive(Debug, Clone, PartialEq)]
pub struct Signature {
func_type: FunctionType,
params: Vec<ValueType>,
return_type: Option<ValueType>,
}
impl Signature {
pub fn new(params: &[ValueType], return_type: Option<ValueType>) -> Signature {
Signature {
func_type: FunctionType::new(params.to_vec(), return_type),
params: params.to_vec(),
return_type: return_type,
}
}
pub fn params(&self) -> &[ValueType] {
self.func_type.params()
&self.params
}
pub fn return_type(&self) -> Option<ValueType> {
self.func_type.return_type()
self.return_type
}
pub(crate) fn from_elements(func_type: FunctionType) -> Signature {
Signature {
params: func_type.params().iter().cloned().map(ValueType::from_elements).collect(),
return_type: func_type.return_type().map(ValueType::from_elements),
}
}
}
impl From<FunctionType> for Signature {
fn from(func_type: FunctionType) -> Signature {
Signature { func_type }
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ValueType {
I32,
I64,
F32,
F64,
}
impl ValueType {
pub(crate) fn from_elements(value_type: EValueType) -> ValueType {
match value_type {
EValueType::I32 => ValueType::I32,
EValueType::I64 => ValueType::I64,
EValueType::F32 => ValueType::F32,
EValueType::F64 => ValueType::F64,
}
}
pub(crate) fn into_elements(self) -> EValueType {
match self {
ValueType::I32 => EValueType::I32,
ValueType::I64 => EValueType::I64,
ValueType::F32 => EValueType::F32,
ValueType::F64 => EValueType::F64,
}
}
}

View File

@ -130,12 +130,12 @@ impl RuntimeValue {
}
/// Get variable type for this value.
pub fn value_type(&self) -> ValueType {
pub fn value_type(&self) -> ::types::ValueType {
match *self {
RuntimeValue::I32(_) => ValueType::I32,
RuntimeValue::I64(_) => ValueType::I64,
RuntimeValue::F32(_) => ValueType::F32,
RuntimeValue::F64(_) => ValueType::F64,
RuntimeValue::I32(_) => ::types::ValueType::I32,
RuntimeValue::I64(_) => ::types::ValueType::I64,
RuntimeValue::F32(_) => ::types::ValueType::F32,
RuntimeValue::F64(_) => ::types::ValueType::F64,
}
}
}