-
Notifications
You must be signed in to change notification settings - Fork 244
/
js.go
83 lines (71 loc) · 2.36 KB
/
js.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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.
//
// Note: This is not a `func` because we need it to be a hashable type.
type jsFn interface {
invoke(ctx context.Context, mod api.Module, args ...interface{}) (interface{}, error)
}
// 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.20/src/syscall/js/js.go#L389
v.properties[method] = fn
return v
}
// Get implements the same method as documented on goos.GetFunction
func (v *jsVal) Get(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))
}
// 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{}
}