mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 20:13:40 +00:00
wasm: add glue JS code
The file wasm_glue.js was copied from the Go wasm port and was modified, most importantly to match the TinyGo calling convention.
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
func main() {
|
||||
}
|
||||
|
||||
@@ -7,3 +12,14 @@ func main() {
|
||||
func add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
//go:export update
|
||||
func update() {
|
||||
document := js.Global().Get("document")
|
||||
a_str := document.Call("getElementById", "a").Get("value").String()
|
||||
b_str := document.Call("getElementById", "b").Get("value").String()
|
||||
a, _ := strconv.Atoi(a_str)
|
||||
b, _ := strconv.Atoi(b_str)
|
||||
result := a + b
|
||||
document.Call("getElementById", "result").Set("value", result)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
<meta charset="utf-8"/>
|
||||
<title>Go WebAssembly</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<script src="wasm.js" async></script>
|
||||
<script src="../../../targets/wasm_exec.js" defer></script>
|
||||
<script src="wasm.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebAssembly</h1>
|
||||
|
||||
@@ -3,51 +3,19 @@
|
||||
const WASM_URL = '../../../wasm.wasm';
|
||||
|
||||
var wasm;
|
||||
var logLine = [];
|
||||
var memory8;
|
||||
|
||||
var importObject = {
|
||||
env: {
|
||||
io_get_stdout: function() {
|
||||
return 1;
|
||||
},
|
||||
resource_write: function(fd, ptr, len) {
|
||||
if (fd == 1) {
|
||||
for (let i=0; i<len; i++) {
|
||||
let c = memory8[ptr+i];
|
||||
if (c == 13) { // CR
|
||||
// ignore
|
||||
} else if (c == 10) { // LF
|
||||
// write line
|
||||
let line = new TextDecoder("utf-8").decode(new Uint8Array(logLine));
|
||||
logLine = [];
|
||||
console.log(line);
|
||||
} else {
|
||||
logLine.push(c);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error('invalid file descriptor:', fd);
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
function updateResult() {
|
||||
let a = parseInt(document.querySelector('#a').value);
|
||||
let b = parseInt(document.querySelector('#b').value);
|
||||
let result = wasm.exports.add(a, b);
|
||||
document.querySelector('#result').value = result;
|
||||
wasm.exports.update();
|
||||
}
|
||||
|
||||
function init() {
|
||||
document.querySelector('#a').oninput = updateResult;
|
||||
document.querySelector('#b').oninput = updateResult;
|
||||
|
||||
WebAssembly.instantiateStreaming(fetch(WASM_URL), importObject).then(function(obj) {
|
||||
const go = new Go();
|
||||
WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function(obj) {
|
||||
wasm = obj.instance;
|
||||
memory8 = new Uint8Array(wasm.exports.memory.buffer);
|
||||
wasm.exports.cwa_main();
|
||||
go.run(wasm);
|
||||
updateResult();
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user