aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-10-18 12:29:28 +0200
committerRon Evans <[email protected]>2024-10-19 16:00:45 +0100
commitcd2bb8333d859a311d8ff9e94cb4e0fade8f65b3 (patch)
treec9942071f024513dded813226b0ede134c51d467
parent5e3c816373b831897a152e60ed858d811ead4bbf (diff)
downloadtinygo-cd2bb8333d859a311d8ff9e94cb4e0fade8f65b3.tar.gz
tinygo-cd2bb8333d859a311d8ff9e94cb4e0fade8f65b3.zip
wasm: add test for js.FuncOf
While there are some browser tests, Node.js is just a lot better for testing this kind of stuff because it's much faster and we don't need a browser for this.
-rw-r--r--main_test.go27
-rw-r--r--testdata/wasmfunc.go17
-rw-r--r--testdata/wasmfunc.js21
-rw-r--r--testdata/wasmfunc.txt7
4 files changed, 72 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go
index 95fad9442..93da10736 100644
--- a/main_test.go
+++ b/main_test.go
@@ -690,6 +690,33 @@ func TestWasmExport(t *testing.T) {
}
}
+// Test js.FuncOf (for syscall/js).
+// This test might be extended in the future to cover more cases in syscall/js.
+func TestWasmFuncOf(t *testing.T) {
+ // Build the wasm binary.
+ tmpdir := t.TempDir()
+ options := optionsFromTarget("wasm", sema)
+ buildConfig, err := builder.NewConfig(&options)
+ if err != nil {
+ t.Fatal(err)
+ }
+ result, err := builder.Build("testdata/wasmfunc.go", ".wasm", tmpdir, buildConfig)
+ if err != nil {
+ t.Fatal("failed to build binary:", err)
+ }
+
+ // Test the resulting binary using NodeJS.
+ output := &bytes.Buffer{}
+ cmd := exec.Command("node", "testdata/wasmfunc.js", result.Binary, buildConfig.BuildMode())
+ cmd.Stdout = output
+ cmd.Stderr = output
+ err = cmd.Run()
+ if err != nil {
+ t.Error("failed to run node:", err)
+ }
+ checkOutput(t, "testdata/wasmfunc.txt", output.Bytes())
+}
+
// Test //go:wasmexport in JavaScript (using NodeJS).
func TestWasmExportJS(t *testing.T) {
type testCase struct {
diff --git a/testdata/wasmfunc.go b/testdata/wasmfunc.go
new file mode 100644
index 000000000..9d0d690e4
--- /dev/null
+++ b/testdata/wasmfunc.go
@@ -0,0 +1,17 @@
+package main
+
+import "syscall/js"
+
+func main() {
+ js.Global().Call("setCallback", js.FuncOf(func(this js.Value, args []js.Value) any {
+ println("inside callback! parameters:")
+ sum := 0
+ for _, value := range args {
+ n := value.Int()
+ println(" parameter:", n)
+ sum += n
+ }
+ return sum
+ }))
+ js.Global().Call("callCallback")
+}
diff --git a/testdata/wasmfunc.js b/testdata/wasmfunc.js
new file mode 100644
index 000000000..3b1831ee4
--- /dev/null
+++ b/testdata/wasmfunc.js
@@ -0,0 +1,21 @@
+require('../targets/wasm_exec.js');
+
+var callback;
+
+global.setCallback = (cb) => {
+ callback = cb;
+};
+
+global.callCallback = () => {
+ console.log('calling callback!');
+ let result = callback(1, 2, 3, 4);
+ console.log('result from callback:', result);
+};
+
+let go = new Go();
+WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
+ go.run(result.instance);
+}).catch((err) => {
+ console.error(err);
+ process.exit(1);
+});
diff --git a/testdata/wasmfunc.txt b/testdata/wasmfunc.txt
new file mode 100644
index 000000000..be41eba3c
--- /dev/null
+++ b/testdata/wasmfunc.txt
@@ -0,0 +1,7 @@
+calling callback!
+inside callback! parameters:
+ parameter: 1
+ parameter: 2
+ parameter: 3
+ parameter: 4
+result from callback: 10