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:
parent
20154c5e24
commit
1c04be64f8
|
@ -3,7 +3,7 @@ use alloc::prelude::*;
|
||||||
use alloc::rc::{Rc, Weak};
|
use alloc::rc::{Rc, Weak};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use parity_wasm::elements::Local;
|
use parity_wasm::elements::Local;
|
||||||
use {Trap, TrapKind, Signature};
|
use {Trap, Signature};
|
||||||
use host::Externals;
|
use host::Externals;
|
||||||
use runner::{check_function_args, Interpreter, InterpreterState};
|
use runner::{check_function_args, Interpreter, InterpreterState};
|
||||||
use value::RuntimeValue;
|
use value::RuntimeValue;
|
||||||
|
@ -144,7 +144,7 @@ impl FuncInstance {
|
||||||
args: &[RuntimeValue],
|
args: &[RuntimeValue],
|
||||||
externals: &mut E,
|
externals: &mut E,
|
||||||
) -> Result<Option<RuntimeValue>, Trap> {
|
) -> Result<Option<RuntimeValue>, Trap> {
|
||||||
check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?;
|
check_function_args(func.signature(), &args)?;
|
||||||
match *func.as_internal() {
|
match *func.as_internal() {
|
||||||
FuncInstanceInternal::Internal { .. } => {
|
FuncInstanceInternal::Internal { .. } => {
|
||||||
let mut interpreter = Interpreter::new(func, args)?;
|
let mut interpreter = Interpreter::new(func, args)?;
|
||||||
|
@ -175,7 +175,7 @@ impl FuncInstance {
|
||||||
func: &FuncRef,
|
func: &FuncRef,
|
||||||
args: &'args [RuntimeValue],
|
args: &'args [RuntimeValue],
|
||||||
) -> Result<FuncInvocation<'args>, Trap> {
|
) -> Result<FuncInvocation<'args>, Trap> {
|
||||||
check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?;
|
check_function_args(func.signature(), &args)?;
|
||||||
match *func.as_internal() {
|
match *func.as_internal() {
|
||||||
FuncInstanceInternal::Internal { .. } => {
|
FuncInstanceInternal::Internal { .. } => {
|
||||||
let interpreter = Interpreter::new(func, args)?;
|
let interpreter = Interpreter::new(func, args)?;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use alloc::prelude::*;
|
use alloc::prelude::*;
|
||||||
use alloc::rc::Rc;
|
use alloc::rc::Rc;
|
||||||
use runner::check_function_args;
|
|
||||||
use Trap;
|
use Trap;
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
@ -630,7 +629,6 @@ impl ModuleInstance {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
check_function_args(func_instance.signature(), &args)?;
|
|
||||||
FuncInstance::invoke(&func_instance, args, externals)
|
FuncInstance::invoke(&func_instance, args, externals)
|
||||||
.map_err(|t| Error::Trap(t))
|
.map_err(|t| Error::Trap(t))
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use core::{u32, usize};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use core::iter::repeat;
|
use core::iter::repeat;
|
||||||
use parity_wasm::elements::Local;
|
use parity_wasm::elements::Local;
|
||||||
use {Error, Trap, TrapKind, Signature};
|
use {Trap, TrapKind, Signature};
|
||||||
use module::ModuleRef;
|
use module::ModuleRef;
|
||||||
use memory::MemoryRef;
|
use memory::MemoryRef;
|
||||||
use func::{FuncRef, FuncInstance, FuncInstanceInternal};
|
use func::{FuncRef, FuncInstance, FuncInstanceInternal};
|
||||||
|
@ -1160,26 +1160,19 @@ fn prepare_function_args(
|
||||||
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() {
|
if signature.params().len() != args.len() {
|
||||||
return Err(
|
return Err(
|
||||||
Error::Function(
|
TrapKind::UnexpectedSignature.into()
|
||||||
format!(
|
|
||||||
"not enough arguments, given {} but expected: {}",
|
|
||||||
args.len(),
|
|
||||||
signature.params().len(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
let actual_type = param_value.value_type();
|
||||||
if actual_type != expected_type {
|
&actual_type != expected_type
|
||||||
return Err(Error::Function(format!("invalid parameter type {:?} when expected {:?}", actual_type, expected_type)));
|
}) {
|
||||||
}
|
return Err(TrapKind::UnexpectedSignature.into());
|
||||||
Ok(())
|
}
|
||||||
}).collect::<Result<Vec<_>, _>>()?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue