2021-03-20 15:45:44 +07:00
|
|
|
package wasm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
|
|
|
"syscall/js"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrInvalidArgumentType is returned when a generated Go function wrapper receives invalid argument types from JS.
|
|
|
|
var ErrInvalidArgumentType = errors.New("invalid argument passed into Go function")
|
|
|
|
|
|
|
|
var errorType = reflect.TypeOf((*error)(nil)).Elem()
|
|
|
|
|
|
|
|
type goThrowable struct {
|
|
|
|
Result js.Value `wasm:"result"`
|
|
|
|
Error js.Value `wasm:"error"`
|
|
|
|
}
|
|
|
|
|
2021-03-21 12:29:31 +07:00
|
|
|
// toJSFunc takes a reflect.Value of a Go function and converts it to a JS function that:
|
|
|
|
// Errors if the parameter types do not conform to the Go function signature,
|
|
|
|
// Throws an error if the last returned value is an error and is non-nil,
|
|
|
|
// Return an array if there's multiple non-error return values.
|
2021-03-20 15:45:44 +07:00
|
|
|
func toJSFunc(x reflect.Value) js.Value {
|
|
|
|
funcType := x.Type()
|
|
|
|
var hasError bool
|
|
|
|
if funcType.NumOut() != 0 {
|
|
|
|
hasError = funcType.Out(funcType.NumOut()-1) == errorType
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:38:39 +07:00
|
|
|
return funcWrapper.Invoke(js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
2021-03-20 15:45:44 +07:00
|
|
|
in, err := conformJSValueToType(funcType, this, args)
|
|
|
|
if err != nil {
|
|
|
|
return ToJSValue(goThrowable{
|
|
|
|
Error: NewError(err),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
out := x.Call(in)
|
|
|
|
|
|
|
|
if !hasError {
|
|
|
|
return ToJSValue(goThrowable{
|
|
|
|
Result: returnValue(out),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
lastParam := out[len(out)-1]
|
|
|
|
if !lastParam.IsNil() {
|
|
|
|
return ToJSValue(goThrowable{
|
|
|
|
Error: NewError(lastParam.Interface().(error)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return ToJSValue(goThrowable{
|
|
|
|
Result: returnValue(out[:len(out)-1]),
|
|
|
|
})
|
2021-03-21 13:38:39 +07:00
|
|
|
}))
|
2021-03-20 15:45:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
var jsValueType = reflect.TypeOf(js.Value{})
|
|
|
|
|
2021-03-21 12:29:31 +07:00
|
|
|
// conformJSValueToType attempts to convert the provided JS values to reflect.Values that match the
|
|
|
|
// types expected for the parameters of funcType.
|
2021-03-20 15:45:44 +07:00
|
|
|
func conformJSValueToType(funcType reflect.Type, this js.Value, values []js.Value) ([]reflect.Value, error) {
|
|
|
|
if funcType.NumIn() == 0 {
|
|
|
|
if len(values) != 0 {
|
|
|
|
return nil, ErrInvalidArgumentType
|
|
|
|
}
|
|
|
|
return []reflect.Value{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if funcType.In(0) == jsValueType {
|
2021-03-21 12:29:31 +07:00
|
|
|
// If the first parameter is a js.Value, it is assumed to be the value of `this`.
|
2021-03-20 15:45:44 +07:00
|
|
|
values = append([]js.Value{this}, values...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if funcType.IsVariadic() && funcType.NumIn()-1 > len(values) {
|
|
|
|
return nil, ErrInvalidArgumentType
|
|
|
|
}
|
|
|
|
|
|
|
|
if !funcType.IsVariadic() && funcType.NumIn() != len(values) {
|
|
|
|
return nil, ErrInvalidArgumentType
|
|
|
|
}
|
|
|
|
|
|
|
|
in := make([]reflect.Value, 0, len(values))
|
|
|
|
for i, v := range values {
|
|
|
|
paramType := funcType.In(i)
|
2021-03-22 14:22:07 +07:00
|
|
|
ptrX := reflect.New(paramType).Interface()
|
|
|
|
err := FromJSValue(v, ptrX)
|
2021-03-20 15:45:44 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-22 14:22:07 +07:00
|
|
|
in = append(in, reflect.ValueOf(ptrX).Elem())
|
2021-03-20 15:45:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 12:29:31 +07:00
|
|
|
// returnValue wraps returned values by Go in a JS-friendly way.
|
|
|
|
// If there are no returned values, it returns undefined.
|
|
|
|
// If there is exactly one, it returns the JS equivalent.
|
|
|
|
// If there is more than one, it returns an array containing the JS equivalent of every returned value.
|
2021-03-20 15:45:44 +07:00
|
|
|
func returnValue(x []reflect.Value) js.Value {
|
|
|
|
switch len(x) {
|
|
|
|
case 0:
|
|
|
|
return js.Undefined()
|
|
|
|
case 1:
|
|
|
|
return ToJSValue(x[0].Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
xInterface := make([]interface{}, 0, len(x))
|
|
|
|
for _, v := range x {
|
|
|
|
xInterface = append(xInterface, v.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToJSValue(xInterface)
|
|
|
|
}
|