From 87a28b057e65fb15d3765f27c3419169ce84879c Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Mon, 8 Jun 2020 16:37:33 -0700 Subject: [PATCH] Move NativeFunc test from `api` to `test/compilers` --- lib/api/tests/module.rs | 45 ------------------------------------ tests/compilers/functions.rs | 40 ++++++++++++++++++++++++++++++++ tests/compilers/main.rs | 1 + 3 files changed, 41 insertions(+), 45 deletions(-) create mode 100644 tests/compilers/functions.rs diff --git a/lib/api/tests/module.rs b/lib/api/tests/module.rs index 08ce82ce9..3e87d89c0 100644 --- a/lib/api/tests/module.rs +++ b/lib/api/tests/module.rs @@ -156,48 +156,3 @@ fn exports() -> Result<()> { ); Ok(()) } - -#[test] -fn native_function_works_for_wasm() -> Result<()> { - let store = Store::default(); - let wat = r#"(module - (func $multiply (import "env" "multiply") (param i32 i32) (result i32)) - (func (export "add") (param i32 i32) (result i32) - (i32.add (local.get 0) - (local.get 1))) - (func (export "double_then_add") (param i32 i32) (result i32) - (i32.add (call $multiply (local.get 0) (i32.const 2)) - (call $multiply (local.get 1) (i32.const 2)))) -)"#; - let module = Module::new(&store, wat)?; - - let import_object = imports! { - "env" => { - "multiply" => Function::new(&store, |a: i32, b: i32| a * b), - }, - }; - - let instance = Instance::new(&module, &import_object).unwrap(); - - // TODO: - //let f: NativeFunc<(i32, i32), i32> = instance.exports.get("add").unwrap(); - let dyn_f: Function = instance.exports.get("add").unwrap(); - let dyn_result = dyn_f.call(&[Val::I32(4), Val::I32(6)]).unwrap(); - assert_eq!(dyn_result[0], Val::I32(10)); - - let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap(); - - let result = f.call(4, 6).unwrap(); - assert_eq!(result, 10); - - let dyn_f: Function = instance.exports.get("double_then_add").unwrap(); - let dyn_result = dyn_f.call(&[Val::I32(4), Val::I32(6)]).unwrap(); - assert_eq!(dyn_result[0], Val::I32(20)); - - let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap(); - - let result = f.call(4, 6).unwrap(); - assert_eq!(result, 20); - - Ok(()) -} diff --git a/tests/compilers/functions.rs b/tests/compilers/functions.rs new file mode 100644 index 000000000..8ecbf009c --- /dev/null +++ b/tests/compilers/functions.rs @@ -0,0 +1,40 @@ +use test_utils::wasmer_compilers; + +wasmer_compilers! { + use wasmer::*; + #[test] + fn native_function_works_for_wasm() { + let store = get_store(); + let wat = r#"(module + (func $multiply (import "env" "multiply") (param i32 i32) (result i32)) + (func (export "add") (param i32 i32) (result i32) + (i32.add (local.get 0) + (local.get 1))) + (func (export "double_then_add") (param i32 i32) (result i32) + (i32.add (call $multiply (local.get 0) (i32.const 2)) + (call $multiply (local.get 1) (i32.const 2)))) +)"#; + let module = Module::new(&store, wat).unwrap(); + + let import_object = imports! { + "env" => { + "multiply" => Function::new(&store, |a: i32, b: i32| a * b), + }, + }; + + let instance = Instance::new(&module, &import_object).unwrap(); + + let f: NativeFunc<(i32, i32), i32> = instance.exports.get("add").unwrap(); + let result = f.call(4, 6).unwrap(); + assert_eq!(result, 10); + + let dyn_f: Function = instance.exports.get("double_then_add").unwrap(); + let dyn_result = dyn_f.call(&[Val::I32(4), Val::I32(6)]).unwrap(); + assert_eq!(dyn_result[0], Val::I32(20)); + + let f: NativeFunc<(i32, i32), i32> = dyn_f.native().unwrap(); + + let result = f.call(4, 6).unwrap(); + assert_eq!(result, 20); + } +} diff --git a/tests/compilers/main.rs b/tests/compilers/main.rs index ad91cd39a..8dbfda95b 100644 --- a/tests/compilers/main.rs +++ b/tests/compilers/main.rs @@ -2,5 +2,6 @@ //! implementation, such as: singlepass, cranelift or llvm depending //! on what's available on the target. +mod functions; mod imports; mod wast;