mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-05-24 05:01:34 +00:00
Merge pull request #59 from NikVolf/fix_argument_order
Fixed native functions argument order
This commit is contained in:
commit
a6700633d1
@ -90,7 +90,7 @@ impl Interpreter {
|
||||
},
|
||||
None => {
|
||||
// move locals back to the stack
|
||||
let locals_to_move: Vec<_> = function_context.locals.drain(..).rev().collect();
|
||||
let locals_to_move: Vec<_> = function_context.locals.drain(..).collect();
|
||||
for local in locals_to_move {
|
||||
function_context.value_stack_mut().push(local.get())?;
|
||||
}
|
||||
|
@ -99,17 +99,17 @@ fn global_get_set() {
|
||||
assert_eq!(module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(50));
|
||||
}
|
||||
|
||||
const SIGNATURE_I32: &'static [ValueType] = &[ValueType::I32];
|
||||
const SIGNATURE_I32_I32: &'static [ValueType] = &[ValueType::I32, ValueType::I32];
|
||||
|
||||
const SIGNATURES: &'static [UserFunctionDescriptor] = &[
|
||||
UserFunctionDescriptor::Static(
|
||||
"add",
|
||||
SIGNATURE_I32,
|
||||
SIGNATURE_I32_I32,
|
||||
Some(ValueType::I32),
|
||||
),
|
||||
UserFunctionDescriptor::Static(
|
||||
"sub",
|
||||
SIGNATURE_I32,
|
||||
SIGNATURE_I32_I32,
|
||||
Some(ValueType::I32),
|
||||
),
|
||||
];
|
||||
@ -125,7 +125,10 @@ impl UserFunctionExecutor for FunctionExecutor {
|
||||
match name {
|
||||
"add" => {
|
||||
let memory_value = self.memory.get(0, 1).unwrap()[0];
|
||||
let fn_argument_unused = context.value_stack.pop_as::<u32>().unwrap() as u8;
|
||||
let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
|
||||
assert_eq!(fn_argument_unused, 0);
|
||||
|
||||
let sum = memory_value + fn_argument;
|
||||
self.memory.set(0, &vec![sum]).unwrap();
|
||||
self.values.push(sum as i32);
|
||||
@ -133,7 +136,10 @@ impl UserFunctionExecutor for FunctionExecutor {
|
||||
},
|
||||
"sub" => {
|
||||
let memory_value = self.memory.get(0, 1).unwrap()[0];
|
||||
let fn_argument_unused = context.value_stack.pop_as::<u32>().unwrap() as u8;
|
||||
let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
|
||||
assert_eq!(fn_argument_unused, 0);
|
||||
|
||||
let diff = memory_value - fn_argument;
|
||||
self.memory.set(0, &vec![diff]).unwrap();
|
||||
self.values.push(diff as i32);
|
||||
@ -170,17 +176,19 @@ fn single_program_different_modules() {
|
||||
.with_import(ImportEntry::new("env".into(), "add".into(), External::Function(0)))
|
||||
.with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0)))
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.signature().param().i32().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::GetLocal(1),
|
||||
Opcode::Call(0),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().return_type().i32().build()
|
||||
.signature().param().i32().param().i32().return_type().i32().build()
|
||||
.body().with_opcodes(Opcodes::new(vec![
|
||||
Opcode::GetLocal(0),
|
||||
Opcode::GetLocal(1),
|
||||
Opcode::Call(1),
|
||||
Opcode::End,
|
||||
])).build()
|
||||
@ -191,9 +199,9 @@ fn single_program_different_modules() {
|
||||
let module_instance = program.add_module("main", module, Some(¶ms.externals)).unwrap();
|
||||
|
||||
{
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(7))).unwrap().unwrap(), RuntimeValue::I32(7));
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(50))).unwrap().unwrap(), RuntimeValue::I32(57));
|
||||
assert_eq!(module_instance.execute_index(3, params.clone().add_argument(RuntimeValue::I32(15))).unwrap().unwrap(), RuntimeValue::I32(42));
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(7)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(7));
|
||||
assert_eq!(module_instance.execute_index(2, params.clone().add_argument(RuntimeValue::I32(50)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(57));
|
||||
assert_eq!(module_instance.execute_index(3, params.clone().add_argument(RuntimeValue::I32(15)).add_argument(RuntimeValue::I32(0))).unwrap().unwrap(), RuntimeValue::I32(42));
|
||||
}
|
||||
}
|
||||
|
||||
@ -213,10 +221,10 @@ fn env_native_export_entry_type_check() {
|
||||
functions: ::std::borrow::Cow::from(SIGNATURES),
|
||||
}).unwrap());
|
||||
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32], Some(ValueType::I32))))).is_ok());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I32))))).is_ok());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![], Some(ValueType::I32))))).is_err());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32], None)))).is_err());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32], Some(ValueType::I64))))).is_err());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], None)))).is_err());
|
||||
assert!(native_env_instance.export_entry("add", &ExportEntryType::Function(FunctionSignature::Module(&FunctionType::new(vec![ValueType::I32, ValueType::I32], Some(ValueType::I64))))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
x
Reference in New Issue
Block a user