Couple of fixes

This commit is contained in:
Sergey Pepyakin 2018-01-23 18:12:41 +03:00
parent 55342dceaf
commit a98852e62a
7 changed files with 29 additions and 31 deletions

View File

@ -1,7 +1,6 @@
use std::rc::{Rc, Weak};
use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
use parity_wasm::elements::{Local, Opcodes};
use {Error, Signature};
use host::Externals;
@ -99,8 +98,8 @@ impl FuncInstance {
}
pub(crate) fn invoke<E: Externals>(
func: FuncRef,
args: Cow<[RuntimeValue]>,
func: &FuncRef,
args: &[RuntimeValue],
externals: &mut E,
) -> Result<Option<RuntimeValue>, Error> {
enum InvokeKind<'a> {

View File

@ -84,7 +84,7 @@ impl MemoryInstance {
None => u32::MAX,
};
let initial_size = calculate_memory_size(0, limits.initial(), maximum_size)
.ok_or(Error::Memory(format!("initial memory size must be at most {} pages", LINEAR_MEMORY_MAX_PAGES)))?;
.ok_or_else(|| Error::Memory(format!("initial memory size must be at most {} pages", LINEAR_MEMORY_MAX_PAGES)))?;
let memory = MemoryInstance {
limits: limits,
@ -165,7 +165,7 @@ impl MemoryInstance {
where B: ::std::ops::Deref<Target=Vec<u8>>
{
let end = offset.checked_add(size)
.ok_or(Error::Memory(format!("trying to access memory block of size {} from offset {}", size, offset)))?;
.ok_or_else(|| Error::Memory(format!("trying to access memory block of size {} from offset {}", size, offset)))?;
if end > buffer.len() {
return Err(Error::Memory(format!("trying to access region [{}..{}] in memory [0..{}]", offset, end, buffer.len())));

View File

@ -2,7 +2,6 @@ use std::rc::Rc;
use std::cell::RefCell;
use std::fmt;
use std::collections::HashMap;
use std::borrow::Cow;
use parity_wasm::elements::{External, InitExpr, Internal, Opcode, ResizableLimits, Type};
use {LoadedModule, Error, Signature, MemoryInstance, RuntimeValue, TableInstance};
use imports::ImportResolver;
@ -165,7 +164,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(Signature::from_elements(ty.clone()));
let signature = Rc::new(Signature::from_elements(ty));
instance.push_signature(signature);
}
@ -174,9 +173,9 @@ impl ModuleInstance {
&[],
);
if imports.len() != extern_vals.len() {
return Err(Error::Instantiation(format!(
"extern_vals length is not equal to import section entries"
)));
return Err(Error::Instantiation(
"extern_vals length is not equal to import section entries".to_owned()
));
}
for (import, extern_val) in
@ -386,7 +385,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 = Signature::from_elements(func_type.clone());
let signature = Signature::from_elements(func_type);
let func = imports.resolve_func(module_name, field_name, &signature)?;
ExternVal::Func(func)
}
@ -437,7 +436,7 @@ impl ModuleInstance {
}
};
FuncInstance::invoke(func_instance.clone(), Cow::Borrowed(args), externals)
FuncInstance::invoke(&func_instance, args, externals)
}
}
@ -451,12 +450,12 @@ impl<'a> NotStartedModuleRef<'a> {
&self.instance
}
pub fn run_start<'b, E: Externals>(self, state: &'b mut E) -> Result<ModuleRef, Error> {
pub fn run_start<E: Externals>(self, state: &mut E) -> Result<ModuleRef, Error> {
if let Some(start_fn_idx) = self.loaded_module.module().start_section() {
let start_func = self.instance.func_by_index(start_fn_idx).expect(
"Due to validation start function should exists",
);
FuncInstance::invoke(start_func, Cow::Borrowed(&[]), state)?;
FuncInstance::invoke(&start_func, &[], state)?;
}
Ok(self.instance)
}

View File

@ -107,7 +107,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
},
FuncInstanceInternal::Host { ref signature, .. } => {
let args = prepare_function_args(signature, &mut function_context.value_stack)?;
let return_val = FuncInstance::invoke(nested_func.clone(), args.into(), self.externals)?;
let return_val = FuncInstance::invoke(&nested_func, &args, self.externals)?;
if let Some(return_val) = return_val {
function_context.value_stack_mut().push(return_val)?;
}
@ -407,7 +407,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
}
}
fn run_br_table(&mut self, context: &mut FunctionContext, table: &Vec<u32>, default: u32) -> Result<InstructionOutcome, Error> {
fn run_br_table(&mut self, context: &mut FunctionContext, table: &[u32], default: u32) -> Result<InstructionOutcome, Error> {
let index: u32 = context.value_stack_mut().pop_as()?;
Ok(InstructionOutcome::Branch(table.get(index as usize).cloned().unwrap_or(default) as usize))
}
@ -1004,7 +1004,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 {
pub fn new(function: FuncRef, value_stack_limit: usize, frame_stack_limit: usize, signature: &Signature, args: Vec<RuntimeValue>) -> Self {
let module = match *function.as_internal() {
FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"),
FuncInstanceInternal::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"),
@ -1029,7 +1029,7 @@ impl FunctionContext {
};
let function_type = function.signature();
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)?;
let function_locals = prepare_function_args(function_type, &mut self.value_stack)?;
(function_locals, module, function_return_type)
};
@ -1055,7 +1055,7 @@ impl FunctionContext {
let locals = locals.iter()
.flat_map(|l| repeat(l.value_type()).take(l.count() as usize))
.map(|vt| RuntimeValue::default(vt))
.map(RuntimeValue::default)
.collect::<Vec<_>>();
self.locals.extend(locals);
}

View File

@ -99,7 +99,7 @@ impl TableInstance {
buffer_len
)),
)?;
Ok(table_elem.ok_or(Error::Table(format!(
Ok(table_elem.ok_or_else(|| Error::Table(format!(
"trying to read uninitialized element on index {}",
offset
)))?)

View File

@ -23,7 +23,7 @@ impl Signature {
self.return_type
}
pub(crate) fn from_elements(func_type: FunctionType) -> Signature {
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),
@ -122,5 +122,5 @@ impl MemoryDescriptor {
pub fn maximum(&self) -> Option<u32> {
self.maximum
}
}
}

View File

@ -180,7 +180,7 @@ impl TryInto<bool, Error> for RuntimeValue {
fn try_into(self) -> Result<bool, Error> {
match self {
RuntimeValue::I32(val) => Ok(val != 0),
_ => Err(Error::Value(format!("32-bit int value expected"))),
_ => Err(Error::Value("32-bit int value expected".to_owned())),
}
}
}
@ -189,7 +189,7 @@ impl TryInto<i32, Error> for RuntimeValue {
fn try_into(self) -> Result<i32, Error> {
match self {
RuntimeValue::I32(val) => Ok(val),
_ => Err(Error::Value(format!("32-bit int value expected"))),
_ => Err(Error::Value("32-bit int value expected".to_owned())),
}
}
}
@ -198,7 +198,7 @@ impl TryInto<i64, Error> for RuntimeValue {
fn try_into(self) -> Result<i64, Error> {
match self {
RuntimeValue::I64(val) => Ok(val),
_ => Err(Error::Value(format!("64-bit int value expected"))),
_ => Err(Error::Value("64-bit int value expected".to_owned())),
}
}
}
@ -207,7 +207,7 @@ impl TryInto<f32, Error> for RuntimeValue {
fn try_into(self) -> Result<f32, Error> {
match self {
RuntimeValue::F32(val) => Ok(val),
_ => Err(Error::Value(format!("32-bit float value expected"))),
_ => Err(Error::Value("32-bit float value expected".to_owned())),
}
}
}
@ -216,7 +216,7 @@ impl TryInto<f64, Error> for RuntimeValue {
fn try_into(self) -> Result<f64, Error> {
match self {
RuntimeValue::F64(val) => Ok(val),
_ => Err(Error::Value(format!("64-bit float value expected"))),
_ => Err(Error::Value("64-bit float value expected".to_owned())),
}
}
}
@ -225,7 +225,7 @@ impl TryInto<u32, Error> for RuntimeValue {
fn try_into(self) -> Result<u32, Error> {
match self {
RuntimeValue::I32(val) => Ok(val as u32),
_ => Err(Error::Value(format!("32-bit int value expected"))),
_ => Err(Error::Value("32-bit int value expected".to_owned())),
}
}
}
@ -234,7 +234,7 @@ impl TryInto<u64, Error> for RuntimeValue {
fn try_into(self) -> Result<u64, Error> {
match self {
RuntimeValue::I64(val) => Ok(val as u64),
_ => Err(Error::Value(format!("64-bit int value expected"))),
_ => Err(Error::Value("64-bit int value expected".to_owned())),
}
}
}
@ -379,7 +379,7 @@ impl LittleEndianConvert for i8 {
fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
buffer.get(0)
.map(|v| *v as i8)
.ok_or(Error::Value("invalid little endian buffer".into()))
.ok_or_else(|| Error::Value("invalid little endian buffer".into()))
}
}
@ -391,7 +391,7 @@ impl LittleEndianConvert for u8 {
fn from_little_endian(buffer: Vec<u8>) -> Result<Self, Error> {
buffer.get(0)
.cloned()
.ok_or(Error::Value("invalid little endian buffer".into()))
.ok_or_else(|| Error::Value("invalid little endian buffer".into()))
}
}