/
impl-js.go
240 lines (194 loc) · 6.06 KB
/
impl-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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// +build js
package js
// IMPLEMENTATION NOTE: (7 Apr 2019, bgp)
// Right now (as of Go 1.12), syscall/js essentially interacts with a big map of values
// on the JS side of things in the browser. "Value" is alias for "ref" which is basically
// a map key with some bit swizzling to stuff it into a float64, presumably so it's both
// easy to send back and forth between wasm and browser and also to encode info about the
// type in there so things like Truthy() and Type(), etc. can be answered without
// having to reach back out to the browser. As such, Values and refs and everything else
// don't appear to have any internal pointers or memory concerns at least on the Go side.
// So the approach here is we just alias syscall/js.Value in each appropriate place and then
// delegate all the calls using either casting or unsafe pointers to treat our js.Value exactly like
// a syscall/js.Value. (FuncOf and friends handled slightly differently.)
// This works with the current implementation. However if significant
// changes are made to syscall/js (probable), it could very well break this package.
// Since the future of Go-Wasm integration is unclear at this point, I'm just rolling
// forward with the approach above and if 1.13 or later breaks things, I'll just have to
// deal with it then. If Wasm gains standardized support for GC and DOM references,
// (see https://github.com/WebAssembly/proposals/issues/16) the odds are this package would
// end up just being completely rewritten to use that API+instruction set.
// It's also possible a WASI interface could address this functionality and/or make this entire package
// go away, can't really tell; my crystal ball is broken.
// See: https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/
import sjs "syscall/js"
// import "unsafe"
// Error alias to syscall/js
type Error struct {
// Value is the underlying JavaScript error value.
Value
}
// Error alias to syscall/js
func (e Error) Error() string {
return sjs.Error{Value: sjs.Value(e.Value)}.Error()
// return (*(*sjs.Error)(unsafe.Pointer(&e))).Error()
}
// Func alias to syscall/js
type Func struct {
Value
f sjs.Func // proxy for this func from syscall/js
}
// FuncOf alias to syscall/js
func FuncOf(fn func(Value, []Value) interface{}) Func {
fn2 := func(this sjs.Value, args []sjs.Value) interface{} {
args2 := make([]Value, len(args))
for i := range args {
args2[i] = Value(args[i])
}
return fn(Value(this), args2)
}
f := sjs.FuncOf(fn2)
ret := Func{
Value: Value(f.Value),
f: f,
}
return ret
}
// Release alias to syscall/js
func (c Func) Release() {
// return (*(*sjs.Func)(unsafe.Pointer(&c))).Release()
c.f.Release()
}
// Type alias to syscall/js
type Type int
const (
TypeUndefined = Type(sjs.TypeUndefined)
TypeNull = Type(sjs.TypeNull)
TypeBoolean = Type(sjs.TypeBoolean)
TypeNumber = Type(sjs.TypeNumber)
TypeString = Type(sjs.TypeString)
TypeSymbol = Type(sjs.TypeSymbol)
TypeObject = Type(sjs.TypeObject)
TypeFunction = Type(sjs.TypeFunction)
)
// String alias to syscall/js
func (t Type) String() string {
return sjs.Type(t).String()
}
// Undefined alias to syscall/js
func Undefined() Value {
return Value(sjs.Undefined())
}
// Null alias to syscall/js
func Null() Value {
return Value(sjs.Null())
}
// Global alias to syscall/js
func Global() Value {
return Value(sjs.Global())
}
// ValueOf alias to syscall/js
func ValueOf(x interface{}) Value {
return Value(sjs.ValueOf(x))
}
// CopyBytesToGo alias to syscall/js
func CopyBytesToGo(dst []byte, src Value) int {
return sjs.CopyBytesToGo(dst, sjs.Value(src))
}
// CopyBytesToJS alias to syscall/js
func CopyBytesToJS(dst Value, src []byte) int {
return sjs.CopyBytesToJS(sjs.Value(dst), src)
}
// // TypedArray alias to syscall/js
// type TypedArray struct {
// Value
// }
// // TypedArrayOf alias to syscall/js
// func TypedArrayOf(slice interface{}) TypedArray {
// return TypedArray{Value: Value(sjs.TypedArrayOf(slice).Value)}
// }
// // Release alias to syscall/js
// func (a TypedArray) Release() {
// sjs.TypedArray{Value: sjs.Value(a.Value)}.Release()
// }
// ValueError alias to syscall/js
type ValueError struct {
Method string
Type Type
}
// Error alias to syscall/js
func (e *ValueError) Error() string {
e2 := sjs.ValueError{Method: e.Method, Type: sjs.Type(e.Type)}
return e2.Error()
}
// Wrapper alias to syscall/js
type Wrapper interface {
JSValue() Value
}
// Value alias to syscall/js
type Value sjs.Value
// JSValue alias to syscall/js
func (v Value) JSValue() Value {
return v
}
// Type alias to syscall/js
func (v Value) Type() Type {
return Type(sjs.Value(v).Type())
}
func (v Value) Get(p string) Value {
return Value(sjs.Value(v).Get(p))
}
func (v Value) Set(p string, x interface{}) {
sjs.Value(v).Set(p, x)
}
func (v Value) Index(i int) Value {
return Value(sjs.Value(v).Index(i))
}
func (v Value) SetIndex(i int, x interface{}) {
sjs.Value(v).SetIndex(i, x)
}
func (v Value) Length() int {
return sjs.Value(v).Length()
}
func (v Value) Call(m string, args ...interface{}) Value {
return Value(sjs.Value(v).Call(m, fixArgsToSjs(args)...))
}
func (v Value) Invoke(args ...interface{}) Value {
return Value(sjs.Value(v).Invoke(fixArgsToSjs(args)...))
}
func (v Value) New(args ...interface{}) Value {
return Value(sjs.Value(v).New(fixArgsToSjs(args)...))
}
func (v Value) Float() float64 {
return sjs.Value(v).Float()
}
func (v Value) Int() int {
return sjs.Value(v).Int()
}
func (v Value) Bool() bool {
return sjs.Value(v).Bool()
}
func (v Value) Truthy() bool {
return sjs.Value(v).Truthy()
}
func (v Value) String() string {
return sjs.Value(v).String()
}
func (v Value) InstanceOf(t Value) bool {
return sjs.Value(v).InstanceOf(sjs.Value(t))
}
func fixArgsToSjs(args []interface{}) []interface{} {
for i := 0; i < len(args); i++ {
v := args[i]
if val, ok := v.(Value); ok {
args[i] = sjs.Value(val) // convert to sjs.Value
}
if f, ok := v.(Func); ok {
args[i] = f.f
}
// if ta, ok := v.(TypedArray); ok {
// args[i] = sjs.TypedArray{Value: sjs.Value(ta.Value)}
// }
}
return args
}