This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.go
348 lines (285 loc) · 7.13 KB
/
value.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
// +build js,wasm
package wasm
import (
"reflect"
"syscall/js"
"unsafe"
)
type (
jsValue = js.Value
jsWrapper = js.Wrapper
jsTypedArray = js.TypedArray
)
var (
jsObject = js.Global().Get("Object")
jsArray = js.Global().Get("Array")
jsTypeFunc = jsObject.Get("prototype").Get("toString")
jsGlobal = Value{js.Global()}
jsNull = js.Null()
jsUndefined = js.Undefined()
)
const (
TypeUndefined = js.TypeUndefined
TypeNull = js.TypeNull
TypeBoolean = js.TypeBoolean
TypeNumber = js.TypeNumber
TypeString = js.TypeString
TypeSymbol = js.TypeSymbol
TypeObject = js.TypeObject
TypeFunction = js.TypeFunction
)
func jsTypedArrayOf(slice interface{}) jsTypedArray {
return js.TypedArrayOf(slice)
}
type Value struct {
jsValue jsValue
}
func (p Value) valid() bool {
if p.jsValue == jsNull || p.jsValue == jsUndefined {
return false
}
return true
}
func (p Value) toBool() bool {
if p.valid() {
return p.jsValue.Bool()
}
return false
}
func (p Value) toString() string {
if p.valid() {
return p.jsValue.String()
}
return ""
}
func (p Value) toFloat64() float64 {
if p.valid() {
return p.jsValue.Float()
}
return 0.0
}
func (p Value) toInt64() int64 {
return int64(p.toFloat64())
}
func (p Value) toUint64() uint64 {
return uint64(p.toFloat64())
}
func (p Value) toInt() int {
return int(p.toFloat64())
}
func (p Value) toUint16() uint16 {
return uint16(p.toFloat64())
}
func (p Value) toInt16() int16 {
return int16(p.toFloat64())
}
func (p Value) toUint8() uint8 {
return uint8(p.toFloat64())
}
func (p Value) toUint() uint {
return uint(p.toFloat64())
}
func (p Value) toFloat32() float32 {
return float32(p.toFloat64())
}
func (v Value) instanceOf(t Value) bool {
return v.jsValue.InstanceOf(t.jsValue)
}
func (p Value) JSValue() js.Value {
return p.jsValue
}
func (p Value) jsType() string {
if p.jsValue.Type() == js.TypeObject {
str := jsTypeFunc.Call("call", p.jsValue).String()
return str[8 : len(str)-1]
}
return p.jsValue.Type().String()
}
func (p Value) get(property string) Value {
return Value{
p.jsValue.Get(property),
}
}
func (p Value) set(property string, x interface{}) {
p.jsValue.Set(property, x)
}
func (p Value) setIndex(i int, x interface{}) {
p.jsValue.SetIndex(i, x)
}
func (p Value) length() int {
return p.jsValue.Length()
}
func (p Value) call(m string, args ...interface{}) Value {
return Value{
p.jsValue.Call(m, args...),
}
}
func (p Value) invoke(args ...interface{}) Value {
return Value{
p.jsValue.Invoke(args...),
}
}
func (p Value) jsNew(args ...interface{}) Value {
return Value{
p.jsValue.New(args...),
}
}
func (p Value) index(i int) Value {
return Value{
p.jsValue.Index(i),
}
}
func (p Value) toSlice() []Value {
if p.valid() {
slc := make([]Value, p.length())
for i := range slc {
slc[i] = p.index(i)
}
return slc
}
return nil
}
/*
func nodeListToSlice(v Value) []Node {
if v.valid() && v.length() > 0 {
ret := make([]Node, v.length())
for i := range ret {
ret[i] = wrapAsNode(v.index(i))
}
return ret
}
return nil
}
valuePtr := reflect.ValueOf(arrPtr)
value := valuePtr.Elem()
value.Set(reflect.Append(value, reflect.ValueOf(55)))
value.Set(reflect.Append(value, reflect.ValueOf(56)))
value.Set(reflect.Append(value, reflect.ValueOf(57)))
fmt.Println(value.Len())
*/
// Append's Values to given slice according to wrapfn function
// expects slice pointer
func (p Value) AppendToSlice(t interface{}, wrapfn func(v Value) interface{}) {
if p.valid() && p.length() > 0 {
if rv := reflect.ValueOf(t); rv.Kind() == reflect.Ptr {
if slice := rv.Elem(); slice.Kind() == reflect.Slice {
for i := 0; i < p.length(); i++ {
if item := wrapfn(p.index(i)); item != nil {
slice.Set(reflect.Append(slice, reflect.ValueOf(item)))
}
}
}
}
}
}
// -------------8<---------------------------------------
// taken from https://go-review.googlesource.com/c/go/+/150917/
// modified as standalone func
func await(v Value) (result Value, ok bool) {
if v.jsValue.Type() != js.TypeObject || v.get("then").jsValue.Type() != js.TypeFunction {
return v, true
}
done := make(chan struct{})
onResolve := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
result = Value{args[0]}
ok = true
close(done)
return nil
})
defer onResolve.Release()
onReject := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
result = Value{args[0]}
ok = false
close(done)
return nil
})
defer onReject.Release()
v.call("then", onResolve, onReject)
<-done
return
}
// -------------8<---------------------------------------
func JSValueOf(x interface{}) jsValue {
switch x := x.(type) {
case jsValue: // should precede Wrapper to avoid a loop
return x
case jsWrapper:
return x.JSValue()
case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, unsafe.Pointer, float32, float64, string, []interface{}, map[string]interface{}:
return js.ValueOf(x)
default: // if type has embedded Value field
if v, ok := reflect.ValueOf(x).Elem().FieldByName("Value").Interface().(Value); ok {
return v.JSValue()
}
return jsNull
}
}
// -------------8<---------------------------------------
func Equal(x interface{}, y interface{}) bool {
return JSValueOf(x) == JSValueOf(y)
}
// -------------8<---------------------------------------
type Func struct {
js.Func
}
func FuncOf(fn func(this Value, args []Value) interface{}) Func {
fx := func(xthis js.Value, xargs []js.Value) interface{} {
var (
fxThis = Value{xthis}
fxArgs []Value
)
if xargs != nil && len(xargs) > 0 {
fxArgs = make([]Value, len(xargs))
for i, v := range xargs {
fxArgs[i] = Value{v}
}
}
return fn(fxThis, fxArgs)
}
return Func{js.FuncOf(fx)}
}
// -------------8<---------------------------------------
func uint8ArrayToByteSlice(v Value) []byte {
jsa := jsUint8Array.jsNew(v)
ret := make([]byte, jsa.get("byteLength").toInt())
ta := js.TypedArrayOf(ret)
ta.Call("set", jsa)
ta.Release()
return ret
}
// -------------8<---------------------------------------
// expects a go slice and returns JavaScript Array with the slice values
func ToJSArray(t interface{}) jsValue {
if reflect.TypeOf(t).Kind() == reflect.Slice {
slc := reflect.ValueOf(t)
l := slc.Len()
jsArr := jsArray.New(l)
if l > 0 {
for i := 0; i < l; i++ {
jsArr.SetIndex(i, JSValueOf(slc.Index(i).Interface()))
}
}
return jsArr
}
return js.Null()
}
// -------------8<---------------------------------------
var (
ifaceSliceValue = reflect.ValueOf([]interface{}{})
ifaceSliceType = ifaceSliceValue.Type()
ifaceSliceZero = reflect.Zero(ifaceSliceType).Interface().([]interface{})
)
// expects a go slice with any type, returns new []interface{} slice with given slice's values
func ToIfaceSlice(t interface{}) []interface{} {
if v := reflect.ValueOf(t); v.Kind() == reflect.Slice {
if l := v.Len(); l > 0 {
ifaceSlice := reflect.MakeSlice(ifaceSliceType, l, l)
for i := 0; i < l; i++ {
ifaceSlice.Index(i).Set(v.Index(i))
}
return ifaceSlice.Interface().([]interface{})
}
}
return ifaceSliceZero
}
// -------------8<---------------------------------------