Skip to content

Commit cd2bb83

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 5e3c816 commit cd2bb83

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,33 @@ func TestWasmExport(t *testing.T) {
690690
}
691691
}
692692

693+
// Test js.FuncOf (for syscall/js).
694+
// This test might be extended in the future to cover more cases in syscall/js.
695+
func TestWasmFuncOf(t *testing.T) {
696+
// Build the wasm binary.
697+
tmpdir := t.TempDir()
698+
options := optionsFromTarget("wasm", sema)
699+
buildConfig, err := builder.NewConfig(&options)
700+
if err != nil {
701+
t.Fatal(err)
702+
}
703+
result, err := builder.Build("testdata/wasmfunc.go", ".wasm", tmpdir, buildConfig)
704+
if err != nil {
705+
t.Fatal("failed to build binary:", err)
706+
}
707+
708+
// Test the resulting binary using NodeJS.
709+
output := &bytes.Buffer{}
710+
cmd := exec.Command("node", "testdata/wasmfunc.js", result.Binary, buildConfig.BuildMode())
711+
cmd.Stdout = output
712+
cmd.Stderr = output
713+
err = cmd.Run()
714+
if err != nil {
715+
t.Error("failed to run node:", err)
716+
}
717+
checkOutput(t, "testdata/wasmfunc.txt", output.Bytes())
718+
}
719+
693720
// Test //go:wasmexport in JavaScript (using NodeJS).
694721
func TestWasmExportJS(t *testing.T) {
695722
type testCase struct {

testdata/wasmfunc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "syscall/js"
4+
5+
func main() {
6+
js.Global().Call("setCallback", js.FuncOf(func(this js.Value, args []js.Value) any {
7+
println("inside callback! parameters:")
8+
sum := 0
9+
for _, value := range args {
10+
n := value.Int()
11+
println(" parameter:", n)
12+
sum += n
13+
}
14+
return sum
15+
}))
16+
js.Global().Call("callCallback")
17+
}

testdata/wasmfunc.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require('../targets/wasm_exec.js');
2+
3+
var callback;
4+
5+
global.setCallback = (cb) => {
6+
callback = cb;
7+
};
8+
9+
global.callCallback = () => {
10+
console.log('calling callback!');
11+
let result = callback(1, 2, 3, 4);
12+
console.log('result from callback:', result);
13+
};
14+
15+
let go = new Go();
16+
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
17+
go.run(result.instance);
18+
}).catch((err) => {
19+
console.error(err);
20+
process.exit(1);
21+
});

testdata/wasmfunc.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
calling callback!
2+
inside callback! parameters:
3+
parameter: 1
4+
parameter: 2
5+
parameter: 3
6+
parameter: 4
7+
result from callback: 10

0 commit comments

Comments
 (0)