mirror of
https://github.com/taigrr/wasm-experiments
synced 2025-01-18 04:03:21 -08:00
Add export example
This commit is contained in:
parent
12f8883116
commit
3f16d27172
6
Makefile
6
Makefile
@ -70,6 +70,12 @@ vecty: clean
|
||||
cp ./vecty/index.html ./html/index.html
|
||||
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js
|
||||
|
||||
.PHONY: export
|
||||
export: clean
|
||||
GOOS=js GOARCH=wasm go build -o ./html/test.wasm ./export/main.go
|
||||
cp ./export/index.html ./html/index.html
|
||||
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
GOOS=js GOARCH=wasm go test -v ./test/
|
||||
|
@ -110,3 +110,7 @@ bitcoin price.
|
||||
|
||||
An example of a [Vecty](https://github.com/gopherjs/vecty) application automatically
|
||||
translating the input textbox to rendered markdown.
|
||||
|
||||
### Export
|
||||
|
||||
An example of exporting a Go function to JavaScript.
|
||||
|
47
export/index.html
Normal file
47
export/index.html
Normal file
@ -0,0 +1,47 @@
|
||||
<!doctype html>
|
||||
<!--
|
||||
Copyright 2018 The Go Authors. All rights reserved.
|
||||
Use of this source code is governed by a BSD-style
|
||||
license that can be found in the LICENSE file.
|
||||
-->
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Go wasm</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<!--
|
||||
Add the following polyfill for Microsoft Edge 17/18 support:
|
||||
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
|
||||
(see https://caniuse.com/#feat=textencoder)
|
||||
-->
|
||||
<script src="wasm_exec.js"></script>
|
||||
<script>
|
||||
if (!WebAssembly.instantiateStreaming) { // polyfill
|
||||
WebAssembly.instantiateStreaming = async (resp, importObject) => {
|
||||
const source = await (await resp).arrayBuffer();
|
||||
return await WebAssembly.instantiate(source, importObject);
|
||||
};
|
||||
}
|
||||
|
||||
const go = new Go();
|
||||
let mod, inst;
|
||||
WebAssembly.instantiateStreaming(fetch("test.wasm"), go.importObject).then((result) => {
|
||||
mod = result.module;
|
||||
inst = result.instance;
|
||||
console.clear();
|
||||
document.getElementById("button").disabled = false;
|
||||
go.run(inst);
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
});
|
||||
</script>
|
||||
|
||||
<input type="text" id="input" />
|
||||
<button onClick="appendText();" id="button" disabled>Append</button>
|
||||
<div id="target"></div>
|
||||
</body>
|
||||
|
||||
</html>
|
27
export/main.go
Normal file
27
export/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
var document js.Value
|
||||
|
||||
func init() {
|
||||
document = js.Global().Get("document")
|
||||
}
|
||||
|
||||
func main() {
|
||||
document.Set("appendText", js.FuncOf(appendText))
|
||||
|
||||
// Prevent main from exiting
|
||||
select {}
|
||||
}
|
||||
|
||||
func appendText(_ js.Value, _ []js.Value) interface{} {
|
||||
msg := document.Call("getElementById", "input").Get("value").String()
|
||||
|
||||
p := document.Call("createElement", "p")
|
||||
p.Set("innerHTML", msg)
|
||||
document.Call("getElementById", "target").Call("appendChild", p)
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user