Skip to content

Commit

Permalink
test(import) Test that all Wasm types can be used in imported functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Jun 24, 2019
1 parent eb98ef2 commit 54b17b8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wasmer/test/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
63 changes: 63 additions & 0 deletions wasmer/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package wasmertest
// #include <stdlib.h>
//
// 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);
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions wasmer/test/testdata/imported_function.rs
Original file line number Diff line number Diff line change
@@ -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. }
}
Binary file added wasmer/test/testdata/imported_function.wasm
Binary file not shown.

0 comments on commit 54b17b8

Please sign in to comment.