forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 22
/
object_goslice.go
343 lines (300 loc) · 7.76 KB
/
object_goslice.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
package goja
import (
"math"
"math/bits"
"reflect"
"strconv"
"github.com/dop251/goja/unistring"
)
type objectGoSlice struct {
baseObject
data *[]interface{}
lengthProp valueProperty
origIsPtr bool
}
func (r *Runtime) newObjectGoSlice(data *[]interface{}, isPtr bool) *objectGoSlice {
obj := &Object{runtime: r}
a := &objectGoSlice{
baseObject: baseObject{
val: obj,
},
data: data,
origIsPtr: isPtr,
}
obj.self = a
a.init()
return a
}
func (o *objectGoSlice) init() {
o.baseObject.init()
o.class = classArray
o.prototype = o.val.runtime.getArrayPrototype()
o.lengthProp.writable = true
o.extensible = true
o.baseObject._put("length", &o.lengthProp)
}
func (o *objectGoSlice) updateLen() {
o.lengthProp.value = intToValue(int64(len(*o.data)))
}
func (o *objectGoSlice) _getIdx(idx int) Value {
return o.val.runtime.ToValue((*o.data)[idx])
}
func (o *objectGoSlice) getStr(name unistring.String, receiver Value) Value {
var ownProp Value
if idx := strToGoIdx(name); idx >= 0 && idx < len(*o.data) {
ownProp = o._getIdx(idx)
} else if name == "length" {
o.updateLen()
ownProp = &o.lengthProp
}
return o.getStrWithOwnProp(ownProp, name, receiver)
}
func (o *objectGoSlice) getIdx(idx valueInt, receiver Value) Value {
if idx := int64(idx); idx >= 0 && idx < int64(len(*o.data)) {
return o._getIdx(int(idx))
}
if o.prototype != nil {
if receiver == nil {
return o.prototype.self.getIdx(idx, o.val)
}
return o.prototype.self.getIdx(idx, receiver)
}
return nil
}
func (o *objectGoSlice) getOwnPropStr(name unistring.String) Value {
if idx := strToGoIdx(name); idx >= 0 {
if idx < len(*o.data) {
return &valueProperty{
value: o._getIdx(idx),
writable: true,
enumerable: true,
}
}
return nil
}
if name == "length" {
o.updateLen()
return &o.lengthProp
}
return nil
}
func (o *objectGoSlice) getOwnPropIdx(idx valueInt) Value {
if idx := int64(idx); idx >= 0 && idx < int64(len(*o.data)) {
return &valueProperty{
value: o._getIdx(int(idx)),
writable: true,
enumerable: true,
}
}
return nil
}
func (o *objectGoSlice) grow(size int) {
oldcap := cap(*o.data)
if oldcap < size {
n := make([]interface{}, size, growCap(size, len(*o.data), oldcap))
copy(n, *o.data)
*o.data = n
} else {
tail := (*o.data)[len(*o.data):size]
for k := range tail {
tail[k] = nil
}
*o.data = (*o.data)[:size]
}
}
func (o *objectGoSlice) shrink(size int) {
tail := (*o.data)[size:]
for k := range tail {
tail[k] = nil
}
*o.data = (*o.data)[:size]
}
func (o *objectGoSlice) putIdx(idx int, v Value, throw bool) {
if idx >= len(*o.data) {
o.grow(idx + 1)
}
(*o.data)[idx] = v.Export()
}
func (o *objectGoSlice) putLength(v uint32, throw bool) bool {
if bits.UintSize == 32 && v > math.MaxInt32 {
panic(rangeError("Integer value overflows 32-bit int"))
}
newLen := int(v)
curLen := len(*o.data)
if newLen > curLen {
o.grow(newLen)
} else if newLen < curLen {
o.shrink(newLen)
}
return true
}
func (o *objectGoSlice) setOwnIdx(idx valueInt, val Value, throw bool) bool {
if i := toIntStrict(int64(idx)); i >= 0 {
if i >= len(*o.data) {
if res, ok := o._setForeignIdx(idx, nil, val, o.val, throw); ok {
return res
}
}
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
}
}
return true
}
func (o *objectGoSlice) setOwnStr(name unistring.String, val Value, throw bool) bool {
if idx := strToGoIdx(name); idx >= 0 {
if idx >= len(*o.data) {
if res, ok := o._setForeignStr(name, nil, val, o.val, throw); ok {
return res
}
}
o.putIdx(idx, val, throw)
} else {
if name == "length" {
return o.putLength(o.val.runtime.toLengthUint32(val), throw)
}
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
}
}
return true
}
func (o *objectGoSlice) setForeignIdx(idx valueInt, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignIdx(idx, trueValIfPresent(o.hasOwnPropertyIdx(idx)), val, receiver, throw)
}
func (o *objectGoSlice) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
return o._setForeignStr(name, trueValIfPresent(o.hasOwnPropertyStr(name)), val, receiver, throw)
}
func (o *objectGoSlice) hasOwnPropertyIdx(idx valueInt) bool {
if idx := int64(idx); idx >= 0 {
return idx < int64(len(*o.data))
}
return false
}
func (o *objectGoSlice) hasOwnPropertyStr(name unistring.String) bool {
if idx := strToIdx64(name); idx >= 0 {
return idx < int64(len(*o.data))
}
return name == "length"
}
func (o *objectGoSlice) 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
}
o.putIdx(i, val, throw)
return true
}
o.val.runtime.typeErrorResult(throw, "Cannot define property '%d' on a Go slice", idx)
return false
}
func (o *objectGoSlice) 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
}
o.putIdx(idx, val, throw)
return true
}
if name == "length" {
return o.val.runtime.defineArrayLength(&o.lengthProp, descr, o.putLength, throw)
}
o.val.runtime.typeErrorResult(throw, "Cannot define property '%s' on a Go slice", name)
return false
}
func (o *objectGoSlice) _deleteIdx(idx int64) {
if idx < int64(len(*o.data)) {
(*o.data)[idx] = nil
}
}
func (o *objectGoSlice) deleteStr(name unistring.String, throw bool) bool {
if idx := strToIdx64(name); idx >= 0 {
o._deleteIdx(idx)
return true
}
return o.baseObject.deleteStr(name, throw)
}
func (o *objectGoSlice) deleteIdx(i valueInt, throw bool) bool {
idx := int64(i)
if idx >= 0 {
o._deleteIdx(idx)
}
return true
}
type goslicePropIter struct {
o *objectGoSlice
idx, limit int
}
func (i *goslicePropIter) next() (propIterItem, iterNextFunc) {
if i.idx < i.limit && i.idx < len(*i.o.data) {
name := strconv.Itoa(i.idx)
i.idx++
return propIterItem{name: newStringValue(name), enumerable: _ENUM_TRUE}, i.next
}
return propIterItem{}, nil
}
func (o *objectGoSlice) iterateStringKeys() iterNextFunc {
return (&goslicePropIter{
o: o,
limit: len(*o.data),
}).next
}
func (o *objectGoSlice) stringKeys(_ bool, accum []Value) []Value {
for i := range *o.data {
accum = append(accum, asciiString(strconv.Itoa(i)))
}
return accum
}
func (o *objectGoSlice) export(*objectExportCtx) interface{} {
if o.origIsPtr {
return o.data
}
return *o.data
}
func (o *objectGoSlice) exportType() reflect.Type {
if o.origIsPtr {
return reflectTypeArrayPtr
}
return reflectTypeArray
}
func (o *objectGoSlice) equal(other objectImpl) bool {
if other, ok := other.(*objectGoSlice); ok {
return o.data == other.data
}
return false
}
func (o *objectGoSlice) esValue() Value {
return o.val
}
func (o *objectGoSlice) reflectValue() reflect.Value {
return reflect.ValueOf(o.data).Elem()
}
func (o *objectGoSlice) setReflectValue(value reflect.Value) {
o.data = value.Addr().Interface().(*[]interface{})
}
func (o *objectGoSlice) sortLen() int {
return len(*o.data)
}
func (o *objectGoSlice) sortGet(i int) Value {
return o.getIdx(valueInt(i), nil)
}
func (o *objectGoSlice) swap(i int, j int) {
(*o.data)[i], (*o.data)[j] = (*o.data)[j], (*o.data)[i]
}