fix: Use function wrapper provided by JS

master
Chan Wen Xu 2021-03-21 21:38:39 +07:00
parent 37088bf382
commit 9faa609930
Signed by untrusted user: chanbakjsd
GPG Key ID: 7E9A74B1B07A7558
2 changed files with 16 additions and 5 deletions

@ -27,7 +27,7 @@ func toJSFunc(x reflect.Value) js.Value {
hasError = funcType.Out(funcType.NumOut()-1) == errorType hasError = funcType.Out(funcType.NumOut()-1) == errorType
} }
return js.FuncOf(func(this js.Value, args []js.Value) interface{} { return funcWrapper.Invoke(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
in, err := conformJSValueToType(funcType, this, args) in, err := conformJSValueToType(funcType, this, args)
if err != nil { if err != nil {
return ToJSValue(goThrowable{ return ToJSValue(goThrowable{
@ -52,7 +52,7 @@ func toJSFunc(x reflect.Value) js.Value {
return ToJSValue(goThrowable{ return ToJSValue(goThrowable{
Result: returnValue(out[:len(out)-1]), Result: returnValue(out[:len(out)-1]),
}) })
}).JSValue() }))
} }
var jsValueType = reflect.TypeOf(js.Value{}) var jsValueType = reflect.TypeOf(js.Value{})

@ -1,12 +1,18 @@
package wasm package wasm
import "syscall/js"
// Magic values to communicate with the JS library. // Magic values to communicate with the JS library.
const ( const (
globalIdent = "__go_wasm__" globalIdent = "__go_wasm__"
readyHint = "__ready__" readyHint = "__ready__"
funcWrapperName = "__wrapper__"
) )
var bridge Object var (
bridge Object
funcWrapper js.Value
)
func init() { func init() {
bridgeJS, err := Global().Get(globalIdent) bridgeJS, err := Global().Get(globalIdent)
@ -18,6 +24,11 @@ func init() {
if err != nil { if err != nil {
panic("JS wrapper " + globalIdent + " is not an object") 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. // Ready notifies the JS bridge that the WASM is ready.