mirror of https://github.com/tinygo-org/tinygo.git
Ayke van Laethem
6 years ago
committed by
Ayke
9 changed files with 129 additions and 27 deletions
@ -0,0 +1,19 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
|
||||
|
<html> |
||||
|
|
||||
|
<head> |
||||
|
<meta charset="utf-8" /> |
||||
|
<title>Go WebAssembly</title> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
||||
|
<script src="wasm_exec.js" defer></script> |
||||
|
<script src="wasm.js" defer></script> |
||||
|
</head> |
||||
|
|
||||
|
<body> |
||||
|
<h1>WebAssembly</h1> |
||||
|
<p>Add two numbers, using WebAssembly:</p> |
||||
|
<input type="number" id="a" value="0" /> + <input type="number" id="b" value="0" /> = <input type="number" id="result" readonly /> |
||||
|
</body> |
||||
|
|
||||
|
</html> |
@ -0,0 +1,27 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"strconv" |
||||
|
"syscall/js" |
||||
|
) |
||||
|
|
||||
|
var a, b int |
||||
|
|
||||
|
func main() { |
||||
|
document := js.Global().Get("document") |
||||
|
document.Call("getElementById", "a").Set("oninput", updater(&a)) |
||||
|
document.Call("getElementById", "b").Set("oninput", updater(&b)) |
||||
|
update() |
||||
|
} |
||||
|
|
||||
|
func updater(n *int) js.Func { |
||||
|
return js.FuncOf(func(this js.Value, args []js.Value) interface{} { |
||||
|
*n, _ = strconv.Atoi(this.Get("value").String()) |
||||
|
update() |
||||
|
return nil |
||||
|
}) |
||||
|
} |
||||
|
|
||||
|
func update() { |
||||
|
js.Global().Get("document").Call("getElementById", "result").Set("value", a+b) |
||||
|
} |
@ -0,0 +1,26 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
const WASM_URL = 'wasm.wasm'; |
||||
|
|
||||
|
var wasm; |
||||
|
|
||||
|
function init() { |
||||
|
const go = new Go(); |
||||
|
if ('instantiateStreaming' in WebAssembly) { |
||||
|
WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject).then(function (obj) { |
||||
|
wasm = obj.instance; |
||||
|
go.run(wasm); |
||||
|
}) |
||||
|
} else { |
||||
|
fetch(WASM_URL).then(resp => |
||||
|
resp.arrayBuffer() |
||||
|
).then(bytes => |
||||
|
WebAssembly.instantiate(bytes, go.importObject).then(function (obj) { |
||||
|
wasm = obj.instance; |
||||
|
go.run(wasm); |
||||
|
}) |
||||
|
) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
init(); |
Loading…
Reference in new issue