aboutsummaryrefslogtreecommitdiffhomepage
path: root/main_test.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-10-03 17:58:48 +0200
committerRon Evans <[email protected]>2024-10-18 10:07:21 +0100
commit4ef5109a07afb895c28a5703b839f0c681062410 (patch)
treeeededfbea0fae4e7dcdf7260134c211e3057672e /main_test.go
parent6016d0c73936026ad9f21a40e7deb04436d99ab9 (diff)
downloadtinygo-4ef5109a07afb895c28a5703b839f0c681062410.tar.gz
tinygo-4ef5109a07afb895c28a5703b839f0c681062410.zip
wasm: add //go:wasmexport support to js/wasm
This adds support for //go:wasmexport with `-target=wasm` (in the browser). This follows the //go:wasmexport proposal, meaning that blocking functions are not allowed. Both `-buildmode=default` and `-buildmode=c-shared` are supported. The latter allows calling exported functions after `go.run()` has returned.
Diffstat (limited to 'main_test.go')
-rw-r--r--main_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/main_test.go b/main_test.go
index c471f7620..95fad9442 100644
--- a/main_test.go
+++ b/main_test.go
@@ -690,6 +690,46 @@ func TestWasmExport(t *testing.T) {
}
}
+// Test //go:wasmexport in JavaScript (using NodeJS).
+func TestWasmExportJS(t *testing.T) {
+ type testCase struct {
+ name string
+ buildMode string
+ }
+
+ tests := []testCase{
+ {name: "default"},
+ {name: "c-shared", buildMode: "c-shared"},
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ // Build the wasm binary.
+ tmpdir := t.TempDir()
+ options := optionsFromTarget("wasm", sema)
+ options.BuildMode = tc.buildMode
+ buildConfig, err := builder.NewConfig(&options)
+ if err != nil {
+ t.Fatal(err)
+ }
+ result, err := builder.Build("testdata/wasmexport-noscheduler.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/wasmexport.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/wasmexport.txt", output.Bytes())
+ })
+ }
+}
+
// Check whether the output of a test equals the expected output.
func checkOutput(t *testing.T, filename string, actual []byte) {
expectedOutput, err := os.ReadFile(filename)