diff --git a/src/func.rs b/src/func.rs index c3549ec..c686e36 100644 --- a/src/func.rs +++ b/src/func.rs @@ -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}; diff --git a/src/global.rs b/src/global.rs index 1e3947e..f277b0b 100644 --- a/src/global.rs +++ b/src/global.rs @@ -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); @@ -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() + } } diff --git a/src/imports.rs b/src/imports.rs index 685e278..43ed232 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 7103cf1..4e7ead5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/module.rs b/src/module.rs index 859cbae..07d5659 100644 --- a/src/module.rs +++ b/src/module.rs @@ -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) } diff --git a/src/runner.rs b/src/runner.rs index f15d675..e36553a 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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) }; diff --git a/src/tests/host.rs b/src/tests/host.rs index 58cbcd6..f76e9a2 100644 --- a/src/tests/host.rs +++ b/src/tests/host.rs @@ -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)] diff --git a/src/tests/wasm.rs b/src/tests/wasm.rs index 282bae9..22935a5 100644 --- a/src/tests/wasm.rs +++ b/src/tests/wasm.rs @@ -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}; diff --git a/src/types.rs b/src/types.rs index 5fefceb..c5d770b 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, + return_type: Option, } impl Signature { pub fn new(params: &[ValueType], return_type: Option) -> 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 { - 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 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, + } } } diff --git a/src/value.rs b/src/value.rs index 0b7c4ee..d426384 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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, } } }