forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_goslice.go
303 lines (259 loc) · 6.17 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
package goja
import (
"reflect"
"strconv"
)
type objectGoSlice struct {
baseObject
data *[]interface{}
lengthProp valueProperty
sliceExtensible bool
}
func (o *objectGoSlice) init() {
o.baseObject.init()
o.class = classArray
o.prototype = o.val.runtime.global.ArrayPrototype
o.lengthProp.writable = o.sliceExtensible
o._setLen()
o.baseObject._put("length", &o.lengthProp)
}
func (o *objectGoSlice) _setLen() {
o.lengthProp.value = intToValue(int64(len(*o.data)))
}
func (o *objectGoSlice) getIdx(idx int64) Value {
if idx < int64(len(*o.data)) {
return o.val.runtime.ToValue((*o.data)[idx])
}
return nil
}
func (o *objectGoSlice) _get(n Value) Value {
if idx := toIdx(n); idx >= 0 {
return o.getIdx(idx)
}
return nil
}
func (o *objectGoSlice) _getStr(name string) Value {
if idx := strToIdx(name); idx >= 0 {
return o.getIdx(idx)
}
return nil
}
func (o *objectGoSlice) get(n Value) Value {
if v := o._get(n); v != nil {
return v
}
return o.baseObject._getStr(n.String())
}
func (o *objectGoSlice) getStr(name string) Value {
if v := o._getStr(name); v != nil {
return v
}
return o.baseObject._getStr(name)
}
func (o *objectGoSlice) getProp(n Value) Value {
if v := o._get(n); v != nil {
return v
}
return o.baseObject.getPropStr(n.String())
}
func (o *objectGoSlice) getPropStr(name string) Value {
if v := o._getStr(name); v != nil {
return v
}
return o.baseObject.getPropStr(name)
}
func (o *objectGoSlice) getOwnProp(name string) Value {
if v := o._getStr(name); v != nil {
return &valueProperty{
value: v,
writable: true,
enumerable: true,
}
}
return o.baseObject.getOwnProp(name)
}
func (o *objectGoSlice) grow(size int64) {
newcap := int64(cap(*o.data))
if newcap < size {
// Use the same algorithm as in runtime.growSlice
doublecap := newcap + newcap
if size > doublecap {
newcap = size
} else {
if len(*o.data) < 1024 {
newcap = doublecap
} else {
for newcap < size {
newcap += newcap / 4
}
}
}
n := make([]interface{}, size, newcap)
copy(n, *o.data)
*o.data = n
} else {
*o.data = (*o.data)[:size]
}
o._setLen()
}
func (o *objectGoSlice) putIdx(idx int64, v Value, throw bool) {
if idx >= int64(len(*o.data)) {
if !o.sliceExtensible {
o.val.runtime.typeErrorResult(throw, "Cannot extend Go slice")
return
}
o.grow(idx + 1)
}
(*o.data)[idx] = v.Export()
}
func (o *objectGoSlice) put(n Value, val Value, throw bool) {
if idx := toIdx(n); idx >= 0 {
o.putIdx(idx, val, throw)
return
}
// TODO: length
o.baseObject.put(n, val, throw)
}
func (o *objectGoSlice) putStr(name string, val Value, throw bool) {
if idx := strToIdx(name); idx >= 0 {
o.putIdx(idx, val, throw)
return
}
// TODO: length
o.baseObject.putStr(name, val, throw)
}
func (o *objectGoSlice) _has(n Value) bool {
if idx := toIdx(n); idx >= 0 {
return idx < int64(len(*o.data))
}
return false
}
func (o *objectGoSlice) _hasStr(name string) bool {
if idx := strToIdx(name); idx >= 0 {
return idx < int64(len(*o.data))
}
return false
}
func (o *objectGoSlice) hasProperty(n Value) bool {
if o._has(n) {
return true
}
return o.baseObject.hasProperty(n)
}
func (o *objectGoSlice) hasPropertyStr(name string) bool {
if o._hasStr(name) {
return true
}
return o.baseObject.hasPropertyStr(name)
}
func (o *objectGoSlice) hasOwnProperty(n Value) bool {
if o._has(n) {
return true
}
return o.baseObject.hasOwnProperty(n)
}
func (o *objectGoSlice) hasOwnPropertyStr(name string) bool {
if o._hasStr(name) {
return true
}
return o.baseObject.hasOwnPropertyStr(name)
}
func (o *objectGoSlice) _putProp(name string, value Value, writable, enumerable, configurable bool) Value {
o.putStr(name, value, false)
return value
}
func (o *objectGoSlice) defineOwnProperty(n Value, descr propertyDescr, throw bool) bool {
if idx := toIdx(n); idx >= 0 {
if !o.val.runtime.checkHostObjectPropertyDescr(n.String(), descr, throw) {
return false
}
val := descr.Value
if val == nil {
val = _undefined
}
o.putIdx(idx, val, throw)
return true
}
return o.baseObject.defineOwnProperty(n, descr, throw)
}
func (o *objectGoSlice) toPrimitiveNumber() Value {
return o.toPrimitiveString()
}
func (o *objectGoSlice) toPrimitiveString() Value {
return o.val.runtime.arrayproto_join(FunctionCall{
This: o.val,
})
}
func (o *objectGoSlice) toPrimitive() Value {
return o.toPrimitiveString()
}
func (o *objectGoSlice) deleteStr(name string, throw bool) bool {
if idx := strToIdx(name); idx >= 0 && idx < int64(len(*o.data)) {
(*o.data)[idx] = nil
return true
}
return o.baseObject.deleteStr(name, throw)
}
func (o *objectGoSlice) delete(name Value, throw bool) bool {
if idx := toIdx(name); idx >= 0 && idx < int64(len(*o.data)) {
(*o.data)[idx] = nil
return true
}
return o.baseObject.delete(name, throw)
}
type goslicePropIter struct {
o *objectGoSlice
recursive bool
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: name, enumerable: _ENUM_TRUE}, i.next
}
if i.recursive {
return i.o.prototype.self._enumerate(i.recursive)()
}
return propIterItem{}, nil
}
func (o *objectGoSlice) enumerate(all, recursive bool) iterNextFunc {
return (&propFilterIter{
wrapped: o._enumerate(recursive),
all: all,
seen: make(map[string]bool),
}).next
}
func (o *objectGoSlice) _enumerate(recursive bool) iterNextFunc {
return (&goslicePropIter{
o: o,
recursive: recursive,
limit: len(*o.data),
}).next
}
func (o *objectGoSlice) export() interface{} {
return *o.data
}
func (o *objectGoSlice) exportType() reflect.Type {
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) sortLen() int64 {
return int64(len(*o.data))
}
func (o *objectGoSlice) sortGet(i int64) Value {
return o.get(intToValue(i))
}
func (o *objectGoSlice) swap(i, j int64) {
ii := intToValue(i)
jj := intToValue(j)
x := o.get(ii)
y := o.get(jj)
o.put(ii, y, false)
o.put(jj, x, false)
}