From 7195b97095ad7efd5ebfe6efbc43c5011594ed5f Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Mon, 26 Jun 2017 17:32:18 +0300 Subject: [PATCH] fixed native functions argument order --- src/interpreter/runner.rs | 2 +- src/interpreter/tests/basics.rs | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/interpreter/runner.rs b/src/interpreter/runner.rs index 8eabcb9..429a214 100644 --- a/src/interpreter/runner.rs +++ b/src/interpreter/runner.rs @@ -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())?; } diff --git a/src/interpreter/tests/basics.rs b/src/interpreter/tests/basics.rs index 48cb4dd..ee54797 100644 --- a/src/interpreter/tests/basics.rs +++ b/src/interpreter/tests/basics.rs @@ -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::().unwrap() as u8; let fn_argument = context.value_stack.pop_as::().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::().unwrap() as u8; let fn_argument = context.value_stack.pop_as::().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]