forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
destruct.go
304 lines (243 loc) · 7.72 KB
/
destruct.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
package goja
import (
"github.com/dop251/goja/unistring"
"reflect"
)
type destructKeyedSource struct {
r *Runtime
wrapped Value
usedKeys map[Value]struct{}
}
func newDestructKeyedSource(r *Runtime, wrapped Value) *destructKeyedSource {
return &destructKeyedSource{
r: r,
wrapped: wrapped,
}
}
func (r *Runtime) newDestructKeyedSource(wrapped Value) *Object {
return &Object{
runtime: r,
self: newDestructKeyedSource(r, wrapped),
}
}
func (d *destructKeyedSource) w() objectImpl {
return d.wrapped.ToObject(d.r).self
}
func (d *destructKeyedSource) recordKey(key Value) {
if d.usedKeys == nil {
d.usedKeys = make(map[Value]struct{})
}
d.usedKeys[key] = struct{}{}
}
func (d *destructKeyedSource) sortLen() int {
return d.w().sortLen()
}
func (d *destructKeyedSource) sortGet(i int) Value {
return d.w().sortGet(i)
}
func (d *destructKeyedSource) swap(i int, i2 int) {
d.w().swap(i, i2)
}
func (d *destructKeyedSource) className() string {
return d.w().className()
}
func (d *destructKeyedSource) getStr(p unistring.String, receiver Value) Value {
d.recordKey(stringValueFromRaw(p))
return d.w().getStr(p, receiver)
}
func (d *destructKeyedSource) getIdx(p valueInt, receiver Value) Value {
d.recordKey(p.toString())
return d.w().getIdx(p, receiver)
}
func (d *destructKeyedSource) getSym(p *Symbol, receiver Value) Value {
d.recordKey(p)
return d.w().getSym(p, receiver)
}
func (d *destructKeyedSource) getOwnPropStr(u unistring.String) Value {
d.recordKey(stringValueFromRaw(u))
return d.w().getOwnPropStr(u)
}
func (d *destructKeyedSource) getOwnPropIdx(v valueInt) Value {
d.recordKey(v.toString())
return d.w().getOwnPropIdx(v)
}
func (d *destructKeyedSource) getOwnPropSym(symbol *Symbol) Value {
d.recordKey(symbol)
return d.w().getOwnPropSym(symbol)
}
func (d *destructKeyedSource) setOwnStr(p unistring.String, v Value, throw bool) bool {
return d.w().setOwnStr(p, v, throw)
}
func (d *destructKeyedSource) setOwnIdx(p valueInt, v Value, throw bool) bool {
return d.w().setOwnIdx(p, v, throw)
}
func (d *destructKeyedSource) setOwnSym(p *Symbol, v Value, throw bool) bool {
return d.w().setOwnSym(p, v, throw)
}
func (d *destructKeyedSource) setForeignStr(p unistring.String, v, receiver Value, throw bool) (res bool, handled bool) {
return d.w().setForeignStr(p, v, receiver, throw)
}
func (d *destructKeyedSource) setForeignIdx(p valueInt, v, receiver Value, throw bool) (res bool, handled bool) {
return d.w().setForeignIdx(p, v, receiver, throw)
}
func (d *destructKeyedSource) setForeignSym(p *Symbol, v, receiver Value, throw bool) (res bool, handled bool) {
return d.w().setForeignSym(p, v, receiver, throw)
}
func (d *destructKeyedSource) hasPropertyStr(u unistring.String) bool {
return d.w().hasPropertyStr(u)
}
func (d *destructKeyedSource) hasPropertyIdx(idx valueInt) bool {
return d.w().hasPropertyIdx(idx)
}
func (d *destructKeyedSource) hasPropertySym(s *Symbol) bool {
return d.w().hasPropertySym(s)
}
func (d *destructKeyedSource) hasOwnPropertyStr(u unistring.String) bool {
return d.w().hasOwnPropertyStr(u)
}
func (d *destructKeyedSource) hasOwnPropertyIdx(v valueInt) bool {
return d.w().hasOwnPropertyIdx(v)
}
func (d *destructKeyedSource) hasOwnPropertySym(s *Symbol) bool {
return d.w().hasOwnPropertySym(s)
}
func (d *destructKeyedSource) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
return d.w().defineOwnPropertyStr(name, desc, throw)
}
func (d *destructKeyedSource) defineOwnPropertyIdx(name valueInt, desc PropertyDescriptor, throw bool) bool {
return d.w().defineOwnPropertyIdx(name, desc, throw)
}
func (d *destructKeyedSource) defineOwnPropertySym(name *Symbol, desc PropertyDescriptor, throw bool) bool {
return d.w().defineOwnPropertySym(name, desc, throw)
}
func (d *destructKeyedSource) deleteStr(name unistring.String, throw bool) bool {
return d.w().deleteStr(name, throw)
}
func (d *destructKeyedSource) deleteIdx(idx valueInt, throw bool) bool {
return d.w().deleteIdx(idx, throw)
}
func (d *destructKeyedSource) deleteSym(s *Symbol, throw bool) bool {
return d.w().deleteSym(s, throw)
}
func (d *destructKeyedSource) toPrimitiveNumber() Value {
return d.w().toPrimitiveNumber()
}
func (d *destructKeyedSource) toPrimitiveString() Value {
return d.w().toPrimitiveString()
}
func (d *destructKeyedSource) toPrimitive() Value {
return d.w().toPrimitive()
}
func (d *destructKeyedSource) assertCallable() (call func(FunctionCall) Value, ok bool) {
return d.w().assertCallable()
}
func (d *destructKeyedSource) assertConstructor() func(args []Value, newTarget *Object) *Object {
return d.w().assertConstructor()
}
func (d *destructKeyedSource) proto() *Object {
return d.w().proto()
}
func (d *destructKeyedSource) setProto(proto *Object, throw bool) bool {
return d.w().setProto(proto, throw)
}
func (d *destructKeyedSource) hasInstance(v Value) bool {
return d.w().hasInstance(v)
}
func (d *destructKeyedSource) isExtensible() bool {
return d.w().isExtensible()
}
func (d *destructKeyedSource) preventExtensions(throw bool) bool {
return d.w().preventExtensions(throw)
}
type destructKeyedSourceIter struct {
d *destructKeyedSource
wrapped iterNextFunc
}
func (i *destructKeyedSourceIter) next() (propIterItem, iterNextFunc) {
for {
item, next := i.wrapped()
if next == nil {
return item, nil
}
i.wrapped = next
if _, exists := i.d.usedKeys[item.name]; !exists {
return item, i.next
}
}
}
func (d *destructKeyedSource) iterateStringKeys() iterNextFunc {
return (&destructKeyedSourceIter{
d: d,
wrapped: d.w().iterateStringKeys(),
}).next
}
func (d *destructKeyedSource) iterateSymbols() iterNextFunc {
return (&destructKeyedSourceIter{
d: d,
wrapped: d.w().iterateSymbols(),
}).next
}
func (d *destructKeyedSource) iterateKeys() iterNextFunc {
return (&destructKeyedSourceIter{
d: d,
wrapped: d.w().iterateKeys(),
}).next
}
func (d *destructKeyedSource) export(ctx *objectExportCtx) interface{} {
return d.w().export(ctx)
}
func (d *destructKeyedSource) exportType() reflect.Type {
return d.w().exportType()
}
func (d *destructKeyedSource) exportToMap(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
return d.w().exportToMap(dst, typ, ctx)
}
func (d *destructKeyedSource) exportToArrayOrSlice(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
return d.w().exportToArrayOrSlice(dst, typ, ctx)
}
func (d *destructKeyedSource) equal(impl objectImpl) bool {
return d.w().equal(impl)
}
func (d *destructKeyedSource) stringKeys(all bool, accum []Value) []Value {
var next iterNextFunc
if all {
next = d.iterateStringKeys()
} else {
next = (&enumerableIter{
o: d.wrapped.ToObject(d.r),
wrapped: d.iterateStringKeys(),
}).next
}
for item, next := next(); next != nil; item, next = next() {
accum = append(accum, item.name)
}
return accum
}
func (d *destructKeyedSource) filterUsedKeys(keys []Value) []Value {
k := 0
for i, key := range keys {
if _, exists := d.usedKeys[key]; exists {
continue
}
if k != i {
keys[k] = key
}
k++
}
return keys[:k]
}
func (d *destructKeyedSource) symbols(all bool, accum []Value) []Value {
return d.filterUsedKeys(d.w().symbols(all, accum))
}
func (d *destructKeyedSource) keys(all bool, accum []Value) []Value {
return d.filterUsedKeys(d.w().keys(all, accum))
}
func (d *destructKeyedSource) _putProp(name unistring.String, value Value, writable, enumerable, configurable bool) Value {
return d.w()._putProp(name, value, writable, enumerable, configurable)
}
func (d *destructKeyedSource) _putSym(s *Symbol, prop Value) {
d.w()._putSym(s, prop)
}
func (d *destructKeyedSource) getPrivateEnv(typ *privateEnvType, create bool) *privateElements {
return d.w().getPrivateEnv(typ, create)
}