-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
94 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build tinygo.wasm && !js | ||
//go:build tinygo.wasm | ||
|
||
package runtime | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |