From 54b17b8df22478e12a09cc3a43e70b4044351b40 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 24 Jun 2019 11:20:51 +0200 Subject: [PATCH] test(import) Test that all Wasm types can be used in imported functions. --- wasmer/test/import_test.go | 4 ++ wasmer/test/imports.go | 63 ++++++++++++++++++++ wasmer/test/testdata/imported_function.rs | 26 ++++++++ wasmer/test/testdata/imported_function.wasm | Bin 0 -> 313 bytes 4 files changed, 93 insertions(+) create mode 100644 wasmer/test/testdata/imported_function.rs create mode 100644 wasmer/test/testdata/imported_function.wasm diff --git a/wasmer/test/import_test.go b/wasmer/test/import_test.go index 09a3e7f1..ac2bf033 100644 --- a/wasmer/test/import_test.go +++ b/wasmer/test/import_test.go @@ -11,6 +11,10 @@ func TestInstanceImport(t *testing.T) { testInstanceImport(t) } +func TestInstanceImportMultipleTypes(t *testing.T) { + testInstanceImportMultipleTypes(t) +} + func TestModuleImport(t *testing.T) { testModuleImport(t) } diff --git a/wasmer/test/imports.go b/wasmer/test/imports.go index e6e5ab5e..469f5275 100644 --- a/wasmer/test/imports.go +++ b/wasmer/test/imports.go @@ -3,6 +3,9 @@ package wasmertest // #include // // extern int32_t sum(void *context, int32_t x, int32_t y); +// extern int64_t sum_i64(void *context, int64_t x, int64_t y); +// extern float sum_f32(void *context, float x, float y); +// extern double sum_f64(void *context, double x, double y); // extern int32_t missingContext(); // extern int32_t badInstanceContext(int32_t x); // extern int32_t badInput(void *context, char x); @@ -51,6 +54,66 @@ func testInstanceImport(t *testing.T) { assert.NoError(t, err) } +//export sum_i64 +func sum_i64(context unsafe.Pointer, x int64, y int64) int64 { + return x + y +} + +//export sum_f32 +func sum_f32(context unsafe.Pointer, x float32, y float32) float32 { + return x + y +} + +//export sum_f64 +func sum_f64(context unsafe.Pointer, x float64, y float64) float64 { + return x + y +} + +func testInstanceImportMultipleTypes(t *testing.T) { + imports := wasm.NewImports().Namespace("env") + imports.Append("sum_i32", sum, C.sum) + imports.Append("sum_i64", sum_i64, C.sum_i64) + imports.Append("sum_f32", sum_f32, C.sum_f32) + imports.Append("sum_f64", sum_f64, C.sum_f64) + + instance, err := wasm.NewInstanceWithImports(getImportedFunctionBytes("imported_function.wasm"), imports) + defer instance.Close() + + assert.NoError(t, err) + + i32, exists := instance.Exports["sum_i32_and_add_one"] + assert.Equal(t, true, exists) + + result, err := i32(1, 2) + + assert.Equal(t, wasm.I32(4), result) + assert.NoError(t, err) + + i64, exists := instance.Exports["sum_i64_and_add_one"] + assert.Equal(t, true, exists) + + result, err = i64(1, 2) + + assert.Equal(t, wasm.I64(4), result) + assert.NoError(t, err) + + f32, exists := instance.Exports["sum_f32_and_add_one"] + assert.Equal(t, true, exists) + + result, err = f32(float32(1.), float32(2.)) + + assert.Equal(t, wasm.F32(4.), result) + assert.NoError(t, err) + + f64, exists := instance.Exports["sum_f64_and_add_one"] + assert.Equal(t, true, exists) + + result, err = f64(1., 2.) + + assert.Equal(t, wasm.F64(4.), result) + assert.NoError(t, err) +} + func testModuleImport(t *testing.T) { imports, err := wasm.NewImports().Namespace("env").Append("sum", sum, C.sum) assert.NoError(t, err) diff --git a/wasmer/test/testdata/imported_function.rs b/wasmer/test/testdata/imported_function.rs new file mode 100644 index 00000000..45050981 --- /dev/null +++ b/wasmer/test/testdata/imported_function.rs @@ -0,0 +1,26 @@ +extern "C" { + fn sum_i32(x: i32, y: i32) -> i32; + fn sum_i64(x: i64, y: i64) -> i64; + fn sum_f32(x: f32, y: f32) -> f32; + fn sum_f64(x: f64, y: f64) -> f64; +} + +#[no_mangle] +pub extern "C" fn sum_i32_and_add_one(x: i32, y: i32) -> i32 { + unsafe { sum_i32(x, y) + 1 } +} + +#[no_mangle] +pub extern "C" fn sum_i64_and_add_one(x: i64, y: i64) -> i64 { + unsafe { sum_i64(x, y) + 1 } +} + +#[no_mangle] +pub extern "C" fn sum_f32_and_add_one(x: f32, y: f32) -> f32 { + unsafe { sum_f32(x, y) + 1. } +} + +#[no_mangle] +pub extern "C" fn sum_f64_and_add_one(x: f64, y: f64) -> f64 { + unsafe { sum_f64(x, y) + 1. } +} diff --git a/wasmer/test/testdata/imported_function.wasm b/wasmer/test/testdata/imported_function.wasm new file mode 100644 index 0000000000000000000000000000000000000000..ad911986ccc48b55dc5da1d2ae8a637797dc74d8 GIT binary patch literal 313 zcmZ9IUuptD42OTo%z?EiG&>yWskFOMZ zN<|vPDpG`ge9sM*ZTk&#$w`&Ona}obp9~|i@%B|-Y~dApBF?^B@wrnpJZ~18JF(8m zKKIa_olS3Sbh6=+8yk1_-)z#GYlAVy)W?!`=3LVgmFBMVyaL+Aml<@Zp2Md?{49>< E7vLUEvj6}9 literal 0 HcmV?d00001