diff options
author | Ayke van Laethem <[email protected]> | 2024-10-03 17:58:48 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-10-18 10:07:21 +0100 |
commit | 4ef5109a07afb895c28a5703b839f0c681062410 (patch) | |
tree | eededfbea0fae4e7dcdf7260134c211e3057672e /testdata | |
parent | 6016d0c73936026ad9f21a40e7deb04436d99ab9 (diff) | |
download | tinygo-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 'testdata')
-rw-r--r-- | testdata/wasmexport.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/testdata/wasmexport.js b/testdata/wasmexport.js new file mode 100644 index 000000000..c4a065125 --- /dev/null +++ b/testdata/wasmexport.js @@ -0,0 +1,40 @@ +require('../targets/wasm_exec.js'); + +function runTests() { + let testCall = (name, params, expected) => { + let result = go._inst.exports[name].apply(null, params); + if (result !== expected) { + console.error(`${name}(...${params}): expected result ${expected}, got ${result}`); + } + } + + // These are the same tests as in TestWasmExport. + testCall('hello', [], undefined); + testCall('add', [3, 5], 8); + testCall('add', [7, 9], 16); + testCall('add', [6, 1], 7); + testCall('reentrantCall', [2, 3], 5); + testCall('reentrantCall', [1, 8], 9); +} + +let go = new Go(); +go.importObject.tester = { + callOutside: (a, b) => { + return go._inst.exports.add(a, b); + }, + callTestMain: () => { + runTests(); + }, +}; +WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => { + let buildMode = process.argv[3]; + if (buildMode === 'default') { + go.run(result.instance); + } else if (buildMode === 'c-shared') { + go.run(result.instance); + runTests(); + } +}).catch((err) => { + console.error(err); + process.exit(1); +}); |