forked from robertkrimen/otto
/
object_class.go
358 lines (326 loc) · 7.55 KB
/
object_class.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
355
356
357
358
package otto
type _objectClass struct {
getOwnProperty func(*_object, string) *_property
getProperty func(*_object, string) *_property
get func(*_object, string) Value
canPut func(*_object, string) bool
put func(*_object, string, Value, bool)
hasProperty func(*_object, string) bool
hasOwnProperty func(*_object, string) bool
defineOwnProperty func(*_object, string, _property, bool) bool
delete func(*_object, string, bool) bool
enumerate func(*_object, bool, func(string) bool)
clone func(*_object, *_object, *_clone) *_object
}
func objectEnumerate(self *_object, all bool, each func(string) bool) {
for _, name := range self.propertyOrder {
if all || self.property[name].enumerable() {
if !each(name) {
return
}
}
}
}
var (
_classObject,
_classArray,
_classString,
_classArguments,
_classGoStruct,
_classGoMap,
_classGoArray,
_classGoSlice,
_ *_objectClass
)
func init() {
_classObject = &_objectClass{
objectGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
objectDefineOwnProperty,
objectDelete,
objectEnumerate,
objectClone,
}
_classArray = &_objectClass{
objectGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
arrayDefineOwnProperty,
objectDelete,
objectEnumerate,
objectClone,
}
_classString = &_objectClass{
stringGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
objectDefineOwnProperty,
objectDelete,
stringEnumerate,
objectClone,
//stringClone,
}
_classArguments = &_objectClass{
argumentsGetOwnProperty,
objectGetProperty,
argumentsGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
argumentsDefineOwnProperty,
argumentsDelete,
argumentsEnumerate,
//argumentsClone
objectClone,
}
_classGoStruct = &_objectClass{
goStructGetOwnProperty,
objectGetProperty,
objectGet,
goStructCanPut,
goStructPut,
objectHasProperty,
objectHasOwnProperty,
objectDefineOwnProperty,
objectDelete,
goStructEnumerate,
objectClone,
}
_classGoMap = &_objectClass{
goMapGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
goMapDefineOwnProperty,
goMapDelete,
goMapEnumerate,
objectClone,
}
_classGoArray = &_objectClass{
goArrayGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
goArrayDefineOwnProperty,
goArrayDelete,
goArrayEnumerate,
objectClone,
}
_classGoSlice = &_objectClass{
goSliceGetOwnProperty,
objectGetProperty,
objectGet,
objectCanPut,
objectPut,
objectHasProperty,
objectHasOwnProperty,
goSliceDefineOwnProperty,
goSliceDelete,
goSliceEnumerate,
objectClone,
}
}
// Allons-y
// 8.12.1
func objectGetOwnProperty(self *_object, name string) *_property {
// Return a _copy_ of the property
property, exists := self._read(name)
if !exists {
return nil
}
return &property
}
// 8.12.2
func objectGetProperty(self *_object, name string) *_property {
property := self.getOwnProperty(name)
if property != nil {
return property
}
if self.prototype != nil {
return self.prototype.getProperty(name)
}
return nil
}
// 8.12.3
func objectGet(self *_object, name string) Value {
property := self.getProperty(name)
if property != nil {
return property.value.(Value)
}
return UndefinedValue()
}
// 8.12.4
func objectCanPut(self *_object, name string) bool {
property := self.getOwnProperty(name)
if property != nil {
switch value := property.value.(type) {
case Value:
return property.writable()
case _propertyGetSet:
return value[1] != nil
default:
panic(newTypeError())
}
}
if self.prototype == nil {
return self.extensible
}
property = self.prototype.getOwnProperty(name)
if property == nil {
return self.extensible
}
switch value := property.value.(type) {
case Value:
if !self.extensible {
return false
}
return property.writable()
case _propertyGetSet:
return value[1] != nil
default:
panic(newTypeError())
}
return false
}
// 8.12.5
func objectPut(self *_object, name string, value Value, throw bool) {
if !self.canPut(name) {
typeErrorResult(throw)
return
}
// TODO This is not exacting to the specification
// TODO Shortcut?
property := self.getOwnProperty(name)
if property == nil {
self.defineProperty(name, value, 0111, throw)
} else {
property.value = value
self.defineOwnProperty(name, *property, throw)
}
}
// 8.12.6
func objectHasProperty(self *_object, name string) bool {
return self.getProperty(name) != nil
}
func objectHasOwnProperty(self *_object, name string) bool {
return self.getOwnProperty(name) != nil
}
// 8.12.9
func objectDefineOwnProperty(self *_object, name string, descriptor _property, throw bool) bool {
property, exists := self._read(name)
{
if !exists {
if !self.extensible {
goto Reject
}
self._write(name, descriptor.value, descriptor.mode)
return true
}
if descriptor.isEmpty() {
return true
}
// TODO Per 8.12.9.6 - We should shortcut here (returning true) if
// the current and new (define) properties are the same
configurable := property.configurable()
if !configurable {
if descriptor.configurable() {
goto Reject
}
// Test that, if enumerable is set on the property descriptor, then it should
// be the same as the existing property
if descriptor.enumerateSet() && descriptor.enumerable() != property.enumerable() {
return false
}
}
value, isDataDescriptor := property.value.(Value)
getSet, _ := property.value.(_propertyGetSet)
if descriptor.isGenericDescriptor() {
// GenericDescriptor
} else if isDataDescriptor != descriptor.isDataDescriptor() {
var interface_ interface{}
if isDataDescriptor {
property.writeOff()
property.value = interface_
} else {
property.writeOn()
property.value = interface_
}
} else if isDataDescriptor && descriptor.isDataDescriptor() {
if !configurable {
if !property.writable() && descriptor.writable() {
goto Reject
}
if !property.writable() {
if !sameValue(value, descriptor.value.(Value)) {
goto Reject
}
}
}
} else {
if !configurable {
defineGetSet, _ := descriptor.value.(_propertyGetSet)
if getSet[0] != defineGetSet[0] || getSet[1] != defineGetSet[1] {
goto Reject
}
}
}
self._write(name, descriptor.value, descriptor.mode)
return true
}
Reject:
if throw {
panic(newTypeError())
}
return false
}
func objectDelete(self *_object, name string, throw bool) bool {
property_ := self.getOwnProperty(name)
if property_ == nil {
return true
}
if property_.configurable() {
self._delete(name)
return true
}
return typeErrorResult(throw)
}
func objectClone(self0 *_object, self1 *_object, clone *_clone) *_object {
*self1 = *self0
self1.runtime = clone.runtime
if self1.prototype != nil {
self1.prototype = clone.object(self0.prototype)
}
self1.property = make(map[string]_property, len(self0.property))
self1.propertyOrder = make([]string, len(self0.propertyOrder))
copy(self1.propertyOrder, self0.propertyOrder)
for index, property := range self0.property {
self1.property[index] = clone.property(property)
}
switch value := self0.value.(type) {
case _functionObject:
self1.value = value.clone(clone)
case _argumentsObject:
self1.value = value.clone(clone)
}
return self1
}