2021-03-20 15:45:44 +07:00
|
|
|
package wasm
|
|
|
|
|
2021-03-21 13:38:39 +07:00
|
|
|
import "syscall/js"
|
|
|
|
|
2021-03-20 15:45:44 +07:00
|
|
|
// Magic values to communicate with the JS library.
|
|
|
|
const (
|
2021-03-21 13:38:39 +07:00
|
|
|
globalIdent = "__go_wasm__"
|
|
|
|
readyHint = "__ready__"
|
|
|
|
funcWrapperName = "__wrapper__"
|
2021-03-20 15:45:44 +07:00
|
|
|
)
|
|
|
|
|
2021-03-21 13:38:39 +07:00
|
|
|
var (
|
|
|
|
bridge Object
|
|
|
|
funcWrapper js.Value
|
|
|
|
)
|
2021-03-20 15:45:44 +07:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2021-03-21 13:38:39 +07:00
|
|
|
|
|
|
|
funcWrapper, err = bridge.Get(funcWrapperName)
|
|
|
|
if err != nil {
|
|
|
|
panic("JS wrapper " + globalIdent + "." + funcWrapperName + " not found")
|
|
|
|
}
|
2021-03-20 15:45:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|