Merge pull request #59 from NikVolf/fix_argument_order

Fixed native functions argument order
This commit is contained in:
Nikolay Volf 2017-06-26 17:40:29 +03:00 committed by GitHub
commit a6700633d1
2 changed files with 20 additions and 12 deletions

View File

@ -90,7 +90,7 @@ impl Interpreter {
}, },
None => { None => {
// move locals back to the stack // 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 { for local in locals_to_move {
function_context.value_stack_mut().push(local.get())?; function_context.value_stack_mut().push(local.get())?;
} }

View File

@ -99,17 +99,17 @@ fn global_get_set() {
assert_eq!(module.execute_index(0, vec![].into()).unwrap().unwrap(), RuntimeValue::I32(50)); 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] = &[ const SIGNATURES: &'static [UserFunctionDescriptor] = &[
UserFunctionDescriptor::Static( UserFunctionDescriptor::Static(
"add", "add",
SIGNATURE_I32, SIGNATURE_I32_I32,
Some(ValueType::I32), Some(ValueType::I32),
), ),
UserFunctionDescriptor::Static( UserFunctionDescriptor::Static(
"sub", "sub",
SIGNATURE_I32, SIGNATURE_I32_I32,
Some(ValueType::I32), Some(ValueType::I32),
), ),
]; ];
@ -125,7 +125,10 @@ impl UserFunctionExecutor for FunctionExecutor {
match name { match name {
"add" => { "add" => {
let memory_value = self.memory.get(0, 1).unwrap()[0]; 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; let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
assert_eq!(fn_argument_unused, 0);
let sum = memory_value + fn_argument; let sum = memory_value + fn_argument;
self.memory.set(0, &vec![sum]).unwrap(); self.memory.set(0, &vec![sum]).unwrap();
self.values.push(sum as i32); self.values.push(sum as i32);
@ -133,7 +136,10 @@ impl UserFunctionExecutor for FunctionExecutor {
}, },
"sub" => { "sub" => {
let memory_value = self.memory.get(0, 1).unwrap()[0]; 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; let fn_argument = context.value_stack.pop_as::<u32>().unwrap() as u8;
assert_eq!(fn_argument_unused, 0);
let diff = memory_value - fn_argument; let diff = memory_value - fn_argument;
self.memory.set(0, &vec![diff]).unwrap(); self.memory.set(0, &vec![diff]).unwrap();
self.values.push(diff as i32); 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(), "add".into(), External::Function(0)))
.with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0))) .with_import(ImportEntry::new("env".into(), "sub".into(), External::Function(0)))
.function() .function()
.signature().param().i32().return_type().i32().build() .signature().param().i32().param().i32().return_type().i32().build()
.body().with_opcodes(Opcodes::new(vec![ .body().with_opcodes(Opcodes::new(vec![
Opcode::GetLocal(0), Opcode::GetLocal(0),
Opcode::GetLocal(1),
Opcode::Call(0), Opcode::Call(0),
Opcode::End, Opcode::End,
])).build() ])).build()
.build() .build()
.function() .function()
.signature().param().i32().return_type().i32().build() .signature().param().i32().param().i32().return_type().i32().build()
.body().with_opcodes(Opcodes::new(vec![ .body().with_opcodes(Opcodes::new(vec![
Opcode::GetLocal(0), Opcode::GetLocal(0),
Opcode::GetLocal(1),
Opcode::Call(1), Opcode::Call(1),
Opcode::End, Opcode::End,
])).build() ])).build()
@ -191,9 +199,9 @@ fn single_program_different_modules() {
let module_instance = program.add_module("main", module, Some(&params.externals)).unwrap(); let module_instance = program.add_module("main", module, Some(&params.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(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))).unwrap().unwrap(), RuntimeValue::I32(57)); 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))).unwrap().unwrap(), RuntimeValue::I32(42)); 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), functions: ::std::borrow::Cow::from(SIGNATURES),
}).unwrap()); }).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![], 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, 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], Some(ValueType::I64))))).is_err());
} }
#[test] #[test]