-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm.go
46 lines (37 loc) · 973 Bytes
/
wasm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//go:build js && wasm
// +build js,wasm
package gowasm
import "syscall/js"
// Magic values to communicate with the JS library.
const (
globalIdent = "__go_wasm__"
readyHint = "__ready__"
funcWrapperName = "__wrapper__"
)
var (
bridge Object
funcWrapper js.Value
)
func init() {
bridgeJS, err := Global().Get(globalIdent)
if err != nil {
panic("JS wrapper " + globalIdent + " not found")
}
bridge, err = NewObject(bridgeJS)
if err != nil {
panic("JS wrapper " + globalIdent + " is not an object")
}
funcWrapper, err = bridge.Get(funcWrapperName)
if err != nil {
panic("JS wrapper " + globalIdent + "." + funcWrapperName + " not found")
}
}
// Ready notifies the JS bridge that the WASM is ready.
// It should be called when every value and function is exposed.
func Ready() {
Expose(readyHint, true)
}
// Expose exposes a copy of the provided value in JS.
func Expose(property string, x interface{}) {
bridge.Set(property, x)
}