mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-11 22:43:40 +00:00
src/examples/wasm: Show both methods supported
Adds another example showing the simple case of executing main, adds a README explaining how everything fits together and how to execute the compiled code in the browser. Include a minimal webserver for local testing.
This commit is contained in:
committed by
Ron Evans
parent
a00a51e70e
commit
586023b45d
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const WASM_URL = 'wasm.wasm';
|
||||
|
||||
var wasm;
|
||||
|
||||
function updateResult() {
|
||||
wasm.exports.update();
|
||||
}
|
||||
|
||||
function init() {
|
||||
document.querySelector('#a').oninput = updateResult;
|
||||
document.querySelector('#b').oninput = updateResult;
|
||||
|
||||
const go = new Go();
|
||||
if ('instantiateStreaming' in WebAssembly) {
|
||||
WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function (obj) {
|
||||
wasm = obj.instance;
|
||||
go.run(wasm);
|
||||
updateResult();
|
||||
})
|
||||
} else {
|
||||
fetch(WASM_URL).then(resp =>
|
||||
resp.arrayBuffer()
|
||||
).then(bytes =>
|
||||
WebAssembly.instantiate(bytes, go.importObject).then(function (obj) {
|
||||
wasm = obj.instance;
|
||||
go.run(wasm);
|
||||
updateResult();
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
init();
|
||||
Reference in New Issue
Block a user