forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_goarray_reflect.go
354 lines (306 loc) · 8.41 KB
/
object_goarray_reflect.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
package goja
import (
"reflect"
"strconv"
"github.com/dop251/goja/unistring"
)
type objectGoArrayReflect struct {
putIdx func(idx int, v Value, throw bool) bool
lengthProp valueProperty
valueCache valueArrayCache
objectGoReflect
}
type valueArrayCache []reflectValueWrapper
func (c *valueArrayCache) get(idx int) reflectValueWrapper {
if idx < len(*c) {
return (*c)[idx]
}
return nil
}
func (c *valueArrayCache) grow(newlen int) {
oldcap := cap(*c)
if oldcap < newlen {
a := make([]reflectValueWrapper, newlen, growCap(newlen, len(*c), oldcap))
copy(a, *c)
*c = a
} else {
*c = (*c)[:newlen]
}
}
func (c *valueArrayCache) put(idx int, w reflectValueWrapper) {
if len(*c) <= idx {
c.grow(idx + 1)
}
(*c)[idx] = w
}
func (c *valueArrayCache) shrink(newlen int) {
if len(*c) > newlen {
tail := (*c)[newlen:]
for i, item := range tail {
if item != nil {
copyReflectValueWrapper(item)
tail[i] = nil
}
}
*c = (*c)[:newlen]
}
}
func (o *objectGoArrayReflect) _init() {
o.objectGoReflect.init()
o.class = classArray
o.prototype = o.val.runtime.global.ArrayPrototype
o.updateLen()
o.baseObject._put("length", &o.lengthProp)
}
func (o *objectGoArrayReflect) init() {
o._init()
o.putIdx = o._putIdx
}
func (o *objectGoArrayReflect) updateLen() {
o.lengthProp.value = intToValue(int64(o.fieldsValue.Len()))
}
func (o *objectGoArrayReflect) _hasIdx(idx valueInt) bool {
if idx := int64(idx); idx >= 0 && idx < int64(o.fieldsValue.Len()) {
return true
}
return false
}
func (o *objectGoArrayReflect) _hasStr(name unistring.String) bool {
if idx := strToIdx64(name); idx >= 0 && idx < int64(o.fieldsValue.Len()) {
return true
}
return false
}
func (o *objectGoArrayReflect) _getIdx(idx int) Value {
if v := o.valueCache.get(idx); v != nil {
return v.esValue()
}
v := o.fieldsValue.Index(idx)
res, w := o.elemToValue(v)
if w != nil {
o.valueCache.put(idx, w)
}
return res
}
func (o *objectGoArrayReflect) getIdx(idx valueInt, receiver Value) Value {
if idx := toIntStrict(int64(idx)); idx >= 0 && idx < o.fieldsValue.Len() {
return o._getIdx(idx)
}
return o.objectGoReflect.getStr(idx.string(), receiver)
}
func (o *objectGoArrayReflect) getStr(name unistring.String, receiver Value) Value {
var ownProp Value
if idx := strToGoIdx(name); idx >= 0 && idx < o.fieldsValue.Len() {
ownProp = o._getIdx(idx)
} else if name == "length" {
ownProp = &o.lengthProp
} else {
ownProp = o.objectGoReflect.getOwnPropStr(name)
}
return o.getStrWithOwnProp(ownProp, name, receiver)
}
func (o *objectGoArrayReflect) getOwnPropStr(name unistring.String) Value {
if idx := strToGoIdx(name); idx >= 0 {
if idx < o.fieldsValue.Len() {
return &valueProperty{
value: o._getIdx(idx),
writable: true,
enumerable: true,
}
}
return nil
}
if name == "length" {
return &o.lengthProp
}
return o.objectGoReflect.getOwnPropStr(name)
}
func (o *objectGoArrayReflect) getOwnPropIdx(idx valueInt) Value {
if idx := toIntStrict(int64(idx)); idx >= 0 && idx < o.fieldsValue.Len() {
return &valueProperty{
value: o._getIdx(idx),
writable: true,
enumerable: true,
}
}
return nil
}
func (o *objectGoArrayReflect) _putIdx(idx int, v Value, throw bool) bool {
cached := o.valueCache.get(idx)
if cached != nil {
copyReflectValueWrapper(cached)
}
rv := o.fieldsValue.Index(idx)
err := o.val.runtime.toReflectValue(v, rv, &objectExportCtx{})
if err != nil {
if cached != nil {
cached.setReflectValue(rv)
}
o.val.runtime.typeErrorResult(throw, "Go type conversion error: %v", err)
return false
}
if cached != nil {
o.valueCache[idx] = nil
}
return true
}
func (o *objectGoArrayReflect) setOwnIdx(idx valueInt, val Value, throw bool) bool {
if i := toIntStrict(int64(idx)); i >= 0 {
if i >= o.fieldsValue.Len() {
if res, ok := o._setForeignIdx(idx, nil, val, o.val, throw); ok {
return res
}
}
return o.putIdx(i, val, throw)
} else {
name := idx.string()
if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
return false
} else {
return res
}
}
}
func (o *objectGoArrayReflect) setOwnStr(name unistring.String, val Value, throw bool) bool {
if idx := strToGoIdx(name); idx >= 0 {
if idx >= o.fieldsValue.Len() {
if res, ok := o._setForeignStr(name, nil, val, o.val, throw); ok {
return res
}
}
return o.putIdx(idx, val, throw)
} else {
if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
return false
} else {
return res
}
}
}
func (o *objectGoArrayReflect) setForeignIdx(idx valueInt, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignIdx(idx, trueValIfPresent(o._hasIdx(idx)), val, receiver, throw)
}
func (o *objectGoArrayReflect) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignStr(name, trueValIfPresent(o.hasOwnPropertyStr(name)), val, receiver, throw)
}
func (o *objectGoArrayReflect) hasOwnPropertyIdx(idx valueInt) bool {
return o._hasIdx(idx)
}
func (o *objectGoArrayReflect) hasOwnPropertyStr(name unistring.String) bool {
if o._hasStr(name) || name == "length" {
return true
}
return o.objectGoReflect._has(name.String())
}
func (o *objectGoArrayReflect) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
if i := toIntStrict(int64(idx)); i >= 0 {
if !o.val.runtime.checkHostObjectPropertyDescr(idx.string(), descr, throw) {
return false
}
val := descr.Value
if val == nil {
val = _undefined
}
return o.putIdx(i, val, throw)
}
o.val.runtime.typeErrorResult(throw, "Cannot define property '%d' on a Go slice", idx)
return false
}
func (o *objectGoArrayReflect) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
if idx := strToGoIdx(name); idx >= 0 {
if !o.val.runtime.checkHostObjectPropertyDescr(name, descr, throw) {
return false
}
val := descr.Value
if val == nil {
val = _undefined
}
return o.putIdx(idx, val, throw)
}
o.val.runtime.typeErrorResult(throw, "Cannot define property '%s' on a Go slice", name)
return false
}
func (o *objectGoArrayReflect) toPrimitive() Value {
return o.toPrimitiveString()
}
func (o *objectGoArrayReflect) _deleteIdx(idx int) {
if idx < o.fieldsValue.Len() {
if cv := o.valueCache.get(idx); cv != nil {
copyReflectValueWrapper(cv)
o.valueCache[idx] = nil
}
o.fieldsValue.Index(idx).Set(reflect.Zero(o.fieldsValue.Type().Elem()))
}
}
func (o *objectGoArrayReflect) deleteStr(name unistring.String, throw bool) bool {
if idx := strToGoIdx(name); idx >= 0 {
o._deleteIdx(idx)
return true
}
return o.objectGoReflect.deleteStr(name, throw)
}
func (o *objectGoArrayReflect) deleteIdx(i valueInt, throw bool) bool {
idx := toIntStrict(int64(i))
if idx >= 0 {
o._deleteIdx(idx)
}
return true
}
type goArrayReflectPropIter struct {
o *objectGoArrayReflect
idx, limit int
}
func (i *goArrayReflectPropIter) next() (propIterItem, iterNextFunc) {
if i.idx < i.limit && i.idx < i.o.fieldsValue.Len() {
name := strconv.Itoa(i.idx)
i.idx++
return propIterItem{name: asciiString(name), enumerable: _ENUM_TRUE}, i.next
}
return i.o.objectGoReflect.iterateStringKeys()()
}
func (o *objectGoArrayReflect) stringKeys(all bool, accum []Value) []Value {
for i := 0; i < o.fieldsValue.Len(); i++ {
accum = append(accum, asciiString(strconv.Itoa(i)))
}
return o.objectGoReflect.stringKeys(all, accum)
}
func (o *objectGoArrayReflect) iterateStringKeys() iterNextFunc {
return (&goArrayReflectPropIter{
o: o,
limit: o.fieldsValue.Len(),
}).next
}
func (o *objectGoArrayReflect) sortLen() int {
return o.fieldsValue.Len()
}
func (o *objectGoArrayReflect) sortGet(i int) Value {
return o.getIdx(valueInt(i), nil)
}
func (o *objectGoArrayReflect) swap(i int, j int) {
vi := o.fieldsValue.Index(i)
vj := o.fieldsValue.Index(j)
tmp := reflect.New(o.fieldsValue.Type().Elem()).Elem()
tmp.Set(vi)
vi.Set(vj)
vj.Set(tmp)
cachedI := o.valueCache.get(i)
cachedJ := o.valueCache.get(j)
if cachedI != nil {
cachedI.setReflectValue(vj)
o.valueCache.put(j, cachedI)
} else {
if j < len(o.valueCache) {
o.valueCache[j] = nil
}
}
if cachedJ != nil {
cachedJ.setReflectValue(vi)
o.valueCache.put(i, cachedJ)
} else {
if i < len(o.valueCache) {
o.valueCache[i] = nil
}
}
}