Remove redundent check_function_args (#135)

* Remove redundent check_function_args

* Remove unused format convertion in check_function_args

* Remove unnecessary alloc
This commit is contained in:
Wei Tang 2018-10-30 01:29:46 +08:00 committed by Sergey Pepyakin
parent 20154c5e24
commit 1c04be64f8
3 changed files with 11 additions and 20 deletions

View File

@ -3,7 +3,7 @@ use alloc::prelude::*;
use alloc::rc::{Rc, Weak};
use core::fmt;
use parity_wasm::elements::Local;
use {Trap, TrapKind, Signature};
use {Trap, Signature};
use host::Externals;
use runner::{check_function_args, Interpreter, InterpreterState};
use value::RuntimeValue;
@ -144,7 +144,7 @@ impl FuncInstance {
args: &[RuntimeValue],
externals: &mut E,
) -> Result<Option<RuntimeValue>, Trap> {
check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?;
check_function_args(func.signature(), &args)?;
match *func.as_internal() {
FuncInstanceInternal::Internal { .. } => {
let mut interpreter = Interpreter::new(func, args)?;
@ -175,7 +175,7 @@ impl FuncInstance {
func: &FuncRef,
args: &'args [RuntimeValue],
) -> Result<FuncInvocation<'args>, Trap> {
check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?;
check_function_args(func.signature(), &args)?;
match *func.as_internal() {
FuncInstanceInternal::Internal { .. } => {
let interpreter = Interpreter::new(func, args)?;

View File

@ -1,7 +1,6 @@
#[allow(unused_imports)]
use alloc::prelude::*;
use alloc::rc::Rc;
use runner::check_function_args;
use Trap;
use core::cell::RefCell;
use core::fmt;
@ -630,7 +629,6 @@ impl ModuleInstance {
}
};
check_function_args(func_instance.signature(), &args)?;
FuncInstance::invoke(&func_instance, args, externals)
.map_err(|t| Error::Trap(t))
}

View File

@ -5,7 +5,7 @@ use core::{u32, usize};
use core::fmt;
use core::iter::repeat;
use parity_wasm::elements::Local;
use {Error, Trap, TrapKind, Signature};
use {Trap, TrapKind, Signature};
use module::ModuleRef;
use memory::MemoryRef;
use func::{FuncRef, FuncInstance, FuncInstanceInternal};
@ -1160,26 +1160,19 @@ fn prepare_function_args(
args
}
pub fn check_function_args(signature: &Signature, args: &[RuntimeValue]) -> Result<(), Error> {
pub fn check_function_args(signature: &Signature, args: &[RuntimeValue]) -> Result<(), Trap> {
if signature.params().len() != args.len() {
return Err(
Error::Function(
format!(
"not enough arguments, given {} but expected: {}",
args.len(),
signature.params().len(),
)
)
TrapKind::UnexpectedSignature.into()
);
}
signature.params().iter().cloned().zip(args).map(|(expected_type, param_value)| {
if signature.params().iter().zip(args).any(|(expected_type, param_value)| {
let actual_type = param_value.value_type();
if actual_type != expected_type {
return Err(Error::Function(format!("invalid parameter type {:?} when expected {:?}", actual_type, expected_type)));
&actual_type != expected_type
}) {
return Err(TrapKind::UnexpectedSignature.into());
}
Ok(())
}).collect::<Result<Vec<_>, _>>()?;
Ok(())
}