forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.go
383 lines (333 loc) · 8.75 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//go:build js
// +build js
package js
import (
"unsafe"
"github.com/gopherjs/gopherjs/js"
)
type Type int
const (
TypeUndefined Type = iota
TypeNull
TypeBoolean
TypeNumber
TypeString
TypeSymbol
TypeObject
TypeFunction
)
func (t Type) String() string {
switch t {
case TypeUndefined:
return "undefined"
case TypeNull:
return "null"
case TypeBoolean:
return "boolean"
case TypeNumber:
return "number"
case TypeString:
return "string"
case TypeSymbol:
return "symbol"
case TypeObject:
return "object"
case TypeFunction:
return "function"
default:
panic("bad type")
}
}
func (t Type) isObject() bool {
return t == TypeObject || t == TypeFunction
}
func Global() Value {
return objectToValue(js.Global)
}
func Null() Value {
return objectToValue(nil)
}
func Undefined() Value {
return objectToValue(js.Undefined)
}
type Func struct {
Value
}
func (f Func) Release() {
js.Global.Set("$exportedFunctions", js.Global.Get("$exportedFunctions").Int()-1)
f.Value = Null()
}
func FuncOf(fn func(this Value, args []Value) interface{}) Func {
// Existence of a wrapped function means that an external event may awaken the
// program and we need to suppress deadlock detection.
js.Global.Set("$exportedFunctions", js.Global.Get("$exportedFunctions").Int()+1)
return Func{
Value: objectToValue(js.MakeFunc(func(this *js.Object, args []*js.Object) interface{} {
vargs := make([]Value, len(args))
for i, a := range args {
vargs[i] = objectToValue(a)
}
return fn(objectToValue(this), vargs)
})),
}
}
type Error struct {
Value
}
func (e Error) Error() string {
return "JavaScript error: " + e.Get("message").String()
}
type Value struct {
v *js.Object
// inited represents whether Value is non-zero value. true represents the value is not 'undefined'.
inited bool
_ [0]func() // uncomparable; to make == not compile
}
func objectToValue(obj *js.Object) Value {
if obj == js.Undefined {
return Value{}
}
return Value{v: obj, inited: true}
}
var (
id *js.Object
instanceOf *js.Object
getValueType *js.Object
)
func init() {
if js.Global != nil {
id = js.Global.Call("eval", "(function(x) { return x; })")
instanceOf = js.Global.Call("eval", "(function(x, y) { return x instanceof y; })")
getValueType = js.Global.Call("eval", `(function(x) {
if (typeof(x) === "undefined") {
return 0; // TypeUndefined
}
if (x === null) {
return 1; // TypeNull
}
if (typeof(x) === "boolean") {
return 2; // TypeBoolean
}
if (typeof(x) === "number") {
return 3; // TypeNumber
}
if (typeof(x) === "string") {
return 4; // TypeString
}
if (typeof(x) === "symbol") {
return 5; // TypeSymbol
}
if (typeof(x) === "function") {
return 7; // TypeFunction
}
return 6; // TypeObject
})`)
}
}
func ValueOf(x interface{}) Value {
switch x := x.(type) {
case Value:
return x
case Func:
return x.Value
case nil:
return Null()
case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, unsafe.Pointer, string, map[string]interface{}, []interface{}:
return objectToValue(id.Invoke(x))
default:
panic("ValueOf: invalid value")
}
}
func (v Value) internal() *js.Object {
if !v.inited {
return js.Undefined
}
return v.v
}
func (v Value) Bool() bool {
if vType := v.Type(); vType != TypeBoolean {
panic(&ValueError{"Value.Bool", vType})
}
return v.internal().Bool()
}
// convertArgs converts arguments into values for GopherJS arguments.
func convertArgs(args ...interface{}) []interface{} {
newArgs := []interface{}{}
for _, arg := range args {
v := ValueOf(arg)
newArgs = append(newArgs, v.internal())
}
return newArgs
}
func convertJSError() {
err := recover()
if err == nil {
return
}
if jsErr, ok := err.(*js.Error); ok {
// We expect that all panics caught by Value.Call() are in fact JavaScript
// exceptions intercepted by GopherJS runtime, which we convert to
// syscall/js.Error, which the callers would expect.
panic(Error{Value: objectToValue(jsErr.Object)})
}
// Panics of other types are unexpected and should never happen. But if it
// does, we will just re-raise it as-is.
panic(err)
}
func (v Value) Call(m string, args ...interface{}) Value {
if vType := v.Type(); vType != TypeObject && vType != TypeFunction {
panic(&ValueError{"Value.Call", vType})
}
if propType := v.Get(m).Type(); propType != TypeFunction {
panic("js: Value.Call: property " + m + " is not a function, got " + propType.String())
}
defer convertJSError()
return objectToValue(v.internal().Call(m, convertArgs(args...)...))
}
func (v Value) Float() float64 {
if vType := v.Type(); vType != TypeNumber {
panic(&ValueError{"Value.Float", vType})
}
return v.internal().Float()
}
func (v Value) Get(p string) Value {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.Get", vType})
}
return objectToValue(v.internal().Get(p))
}
func (v Value) Index(i int) Value {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.Index", vType})
}
return objectToValue(v.internal().Index(i))
}
func (v Value) Int() int {
if vType := v.Type(); vType != TypeNumber {
panic(&ValueError{"Value.Int", vType})
}
return v.internal().Int()
}
func (v Value) InstanceOf(t Value) bool {
return instanceOf.Invoke(v.internal(), t.internal()).Bool()
}
func (v Value) Invoke(args ...interface{}) Value {
if vType := v.Type(); vType != TypeFunction {
panic(&ValueError{"Value.Invoke", vType})
}
return objectToValue(v.internal().Invoke(convertArgs(args...)...))
}
func (v Value) JSValue() Value {
return v
}
func (v Value) Length() int {
return v.internal().Length()
}
func (v Value) New(args ...interface{}) Value {
defer func() {
err := recover()
if err == nil {
return
}
if vType := v.Type(); vType != TypeFunction { // check here to avoid overhead in success case
panic(&ValueError{"Value.New", vType})
}
if jsErr, ok := err.(*js.Error); ok {
panic(Error{objectToValue(jsErr.Object)})
}
panic(err)
}()
return objectToValue(v.internal().New(convertArgs(args...)...))
}
func (v Value) Set(p string, x interface{}) {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.Set", vType})
}
v.internal().Set(p, convertArgs(x)[0])
}
func (v Value) SetIndex(i int, x interface{}) {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.SetIndex", vType})
}
v.internal().SetIndex(i, convertArgs(x)[0])
}
// String returns the value v as a string.
// String is a special case because of Go's String method convention. Unlike the other getters,
// it does not panic if v's Type is not TypeString. Instead, it returns a string of the form "<T>"
// or "<T: V>" where T is v's type and V is a string representation of v's value.
func (v Value) String() string {
switch v.Type() {
case TypeString:
return v.internal().String()
case TypeUndefined:
return "<undefined>"
case TypeNull:
return "<null>"
case TypeBoolean:
return "<boolean: " + v.internal().String() + ">"
case TypeNumber:
return "<number: " + v.internal().String() + ">"
case TypeSymbol:
return "<symbol>"
case TypeObject:
return "<object>"
case TypeFunction:
return "<function>"
default:
panic("bad type")
}
}
func (v Value) Truthy() bool {
return v.internal().Bool()
}
func (v Value) Type() Type {
return Type(getValueType.Invoke(v.internal()).Int())
}
func (v Value) IsNull() bool {
return v.Type() == TypeNull
}
func (v Value) IsUndefined() bool {
return !v.inited
}
func (v Value) IsNaN() bool {
return js.Global.Call("isNaN", v.internal()).Bool()
}
// Delete deletes the JavaScript property p of value v.
// It panics if v is not a JavaScript object.
func (v Value) Delete(p string) {
if vType := v.Type(); !vType.isObject() {
panic(&ValueError{"Value.Delete", vType})
}
v.internal().Delete(p)
}
func (v Value) Equal(w Value) bool {
return v.internal() == w.internal()
}
type ValueError struct {
Method string
Type Type
}
func (e *ValueError) Error() string {
return "syscall/js: call of " + e.Method + " on " + e.Type.String()
}
// CopyBytesToGo copies bytes from the Uint8Array src to dst.
// It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
// CopyBytesToGo panics if src is not an Uint8Array.
func CopyBytesToGo(dst []byte, src Value) int {
vlen := src.v.Length()
if dlen := len(dst); dlen < vlen {
vlen = dlen
}
copy(dst, src.v.Interface().([]byte))
return vlen
}
// CopyBytesToJS copies bytes from src to the Uint8Array dst.
// It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
// CopyBytesToJS panics if dst is not an Uint8Array.
func CopyBytesToJS(dst Value, src []byte) int {
dt, ok := dst.v.Interface().([]byte)
if !ok {
panic("syscall/js: CopyBytesToJS: expected dst to be an Uint8Array")
}
return copy(dt, src)
}