Files
wazero/internal/gojs/js.go
Crypt Keeper 1ad900d179 gojs: refactors GOOS and GOARCH specific code into their own packages (#959)
This refactors GOOS and GOARCH specific code into their own packages.
This allows logging interceptors to be built without cyclic package
dependencies.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-12-27 08:45:43 +08:00

103 lines
2.8 KiB
Go

package gojs
import (
"context"
"fmt"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/gojs/goos"
)
// jsFn is a jsCall.call function, configured via jsVal.addFunction.
type jsFn interface {
invoke(ctx context.Context, mod api.Module, args ...interface{}) (interface{}, error)
}
type jsGet interface {
get(ctx context.Context, propertyKey string) interface{}
}
// jsCall allows calling a method/function by name.
type jsCall interface {
call(ctx context.Context, mod api.Module, this goos.Ref, method string, args ...interface{}) (interface{}, error)
}
func newJsVal(ref goos.Ref, name string) *jsVal {
return &jsVal{ref: ref, name: name, properties: map[string]interface{}{}, functions: map[string]jsFn{}}
}
// jsVal corresponds to a generic js.Value in go, when `GOOS=js`.
type jsVal struct {
// ref is the constant reference used for built-in values, such as
// objectConstructor.
ref goos.Ref
name string
properties map[string]interface{}
functions map[string]jsFn
}
func (v *jsVal) addProperties(properties map[string]interface{}) *jsVal {
for k, val := range properties {
v.properties[k] = val
}
return v
}
func (v *jsVal) addFunction(method string, fn jsFn) *jsVal {
v.functions[method] = fn
// If fn returns an error, js.Call does a type lookup to verify it is a
// function.
// See https://github.com/golang/go/blob/go1.19/src/syscall/js/js.go#L389
v.properties[method] = fn
return v
}
// get implements jsGet.get
func (v *jsVal) get(_ context.Context, propertyKey string) interface{} {
if v, ok := v.properties[propertyKey]; ok {
return v
}
panic(fmt.Sprintf("TODO: get %s.%s", v.name, propertyKey))
}
// call implements jsCall.call
func (v *jsVal) call(ctx context.Context, mod api.Module, this goos.Ref, method string, args ...interface{}) (interface{}, error) {
if v, ok := v.functions[method]; ok {
return v.invoke(ctx, mod, args...)
}
panic(fmt.Sprintf("TODO: call %s.%s", v.name, method))
}
// byteArray is a result of uint8ArrayConstructor which temporarily stores
// binary data outside linear memory.
//
// Note: This is a wrapper because a slice is not hashable.
type byteArray struct {
slice []byte
}
// get implements jsGet.get
func (a *byteArray) get(_ context.Context, propertyKey string) interface{} {
switch propertyKey {
case "byteLength":
return uint32(len(a.slice))
}
panic(fmt.Sprintf("TODO: get byteArray.%s", propertyKey))
}
// objectArray is a result of arrayConstructor typically used to pass
// indexed arguments.
//
// Note: This is a wrapper because a slice is not hashable.
type objectArray struct {
slice []interface{}
}
// object is a result of objectConstructor typically used to pass named
// arguments.
//
// Note: This is a wrapper because a map is not hashable.
type object struct {
properties map[string]interface{}
}