This repository was archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjs_notwasm.go
263 lines (223 loc) · 5.04 KB
/
js_notwasm.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
// Copyright 2018 The GopherWasm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !wasm
package js
import (
"reflect"
"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 Global() Value {
return Value{v: js.Global}
}
func Null() Value {
return Value{v: nil}
}
func Undefined() Value {
return Value{v: js.Undefined}
}
type Func struct {
Value
}
func (f Func) Release() {
f.Value = Null()
}
func FuncOf(fn func(this Value, args []Value) interface{}) Func {
return Func{
Value: Value{
v: js.MakeFunc(func(this *js.Object, args []*js.Object) interface{} {
vargs := make([]Value, len(args))
for i, a := range args {
vargs[i] = Value{a}
}
return fn(Value{this}, vargs)
}),
},
}
}
type Error struct {
Value
}
func (e Error) Error() string {
return "JavaScript error: " + e.Get("message").String()
}
type Value struct {
v *js.Object
}
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 TypedArray:
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 Value{v: id.Invoke(x)}
default:
panic(`invalid arg: ` + reflect.TypeOf(x).String())
}
}
func (v Value) Bool() bool {
if vType := v.Type(); vType != TypeBoolean {
panic(&ValueError{"Value.Bool", vType})
}
return v.v.Bool()
}
func convertArgs(args []interface{}) []interface{} {
newArgs := []interface{}{}
for _, arg := range args {
v := ValueOf(arg)
newArgs = append(newArgs, v.v)
}
return newArgs
}
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())
}
return Value{v: v.v.Call(m, convertArgs(args)...)}
}
func (v Value) Float() float64 {
if vType := v.Type(); vType != TypeNumber {
panic(&ValueError{"Value.Float", vType})
}
return v.v.Float()
}
func (v Value) Get(p string) Value {
return Value{v: v.v.Get(p)}
}
func (v Value) Index(i int) Value {
return Value{v: v.v.Index(i)}
}
func (v Value) Int() int {
if vType := v.Type(); vType != TypeNumber {
panic(&ValueError{"Value.Int", vType})
}
return v.v.Int()
}
func (v Value) Invoke(args ...interface{}) Value {
if vType := v.Type(); vType != TypeFunction {
panic(&ValueError{"Value.Invoke", vType})
}
return Value{v: v.v.Invoke(convertArgs(args)...)}
}
func (v Value) Length() int {
return v.v.Length()
}
func (v Value) New(args ...interface{}) Value {
return Value{v: v.v.New(convertArgs(args)...)}
}
func (v Value) Set(p string, x interface{}) {
v.v.Set(p, x)
}
func (v Value) SetIndex(i int, x interface{}) {
v.v.SetIndex(i, x)
}
func (v Value) String() string {
return v.v.String()
}
func (v Value) InstanceOf(t Value) bool {
return instanceOf.Invoke(v, t).Bool()
}
func (v Value) Type() Type {
return Type(getValueType.Invoke(v).Int())
}
type TypedArray struct {
Value
}
func TypedArrayOf(slice interface{}) TypedArray {
switch slice := slice.(type) {
case []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32, []float64:
return TypedArray{Value{v: id.Invoke(slice)}}
default:
panic("TypedArrayOf: not a supported slice")
}
}
func (t *TypedArray) Release() {
t.Value = Null()
}
func GetInternalObject(v Value) interface{} {
return v.v
}
type ValueError struct {
Method string
Type Type
}
func (e *ValueError) Error() string {
return "syscall/js: call of " + e.Method + " on " + e.Type.String()
}