-
Notifications
You must be signed in to change notification settings - Fork 586
/
builtin_object.go
300 lines (266 loc) · 7.68 KB
/
builtin_object.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
package otto
import (
"fmt"
)
// Object
func builtinObject(call FunctionCall) Value {
value := call.Argument(0)
switch value.kind {
case valueUndefined, valueNull:
return objectValue(call.runtime.newObject())
}
return objectValue(call.runtime.toObject(value))
}
func builtinNewObject(obj *object, argumentList []Value) Value {
value := valueOfArrayIndex(argumentList, 0)
switch value.kind {
case valueNull, valueUndefined:
case valueNumber, valueString, valueBoolean:
return objectValue(obj.runtime.toObject(value))
case valueObject:
return value
default:
}
return objectValue(obj.runtime.newObject())
}
func builtinObjectValueOf(call FunctionCall) Value {
return objectValue(call.thisObject())
}
func builtinObjectHasOwnProperty(call FunctionCall) Value {
propertyName := call.Argument(0).string()
thisObject := call.thisObject()
return boolValue(thisObject.hasOwnProperty(propertyName))
}
func builtinObjectIsPrototypeOf(call FunctionCall) Value {
value := call.Argument(0)
if !value.IsObject() {
return falseValue
}
prototype := call.toObject(value).prototype
thisObject := call.thisObject()
for prototype != nil {
if thisObject == prototype {
return trueValue
}
prototype = prototype.prototype
}
return falseValue
}
func builtinObjectPropertyIsEnumerable(call FunctionCall) Value {
propertyName := call.Argument(0).string()
thisObject := call.thisObject()
prop := thisObject.getOwnProperty(propertyName)
if prop != nil && prop.enumerable() {
return trueValue
}
return falseValue
}
func builtinObjectToString(call FunctionCall) Value {
var result string
switch {
case call.This.IsUndefined():
result = "[object Undefined]"
case call.This.IsNull():
result = "[object Null]"
default:
result = fmt.Sprintf("[object %s]", call.thisObject().class)
}
return stringValue(result)
}
func builtinObjectToLocaleString(call FunctionCall) Value {
toString := call.thisObject().get("toString")
if !toString.isCallable() {
panic(call.runtime.panicTypeError("Object.toLocaleString %q is not callable", toString))
}
return toString.call(call.runtime, call.This)
}
func builtinObjectGetPrototypeOf(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError("Object.GetPrototypeOf is nil"))
}
if obj.prototype == nil {
return nullValue
}
return objectValue(obj.prototype)
}
func builtinObjectGetOwnPropertyDescriptor(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError("Object.GetOwnPropertyDescriptor is nil"))
}
name := call.Argument(1).string()
descriptor := obj.getOwnProperty(name)
if descriptor == nil {
return Value{}
}
return objectValue(call.runtime.fromPropertyDescriptor(*descriptor))
}
func builtinObjectDefineProperty(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError("Object.DefineProperty is nil"))
}
name := call.Argument(1).string()
descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
obj.defineOwnProperty(name, descriptor, true)
return val
}
func builtinObjectDefineProperties(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError("Object.DefineProperties is nil"))
}
properties := call.runtime.toObject(call.Argument(1))
properties.enumerate(false, func(name string) bool {
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
obj.defineOwnProperty(name, descriptor, true)
return true
})
return val
}
func builtinObjectCreate(call FunctionCall) Value {
prototypeValue := call.Argument(0)
if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
panic(call.runtime.panicTypeError("Object.Create is nil"))
}
obj := call.runtime.newObject()
obj.prototype = prototypeValue.object()
propertiesValue := call.Argument(1)
if propertiesValue.IsDefined() {
properties := call.runtime.toObject(propertiesValue)
properties.enumerate(false, func(name string) bool {
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
obj.defineOwnProperty(name, descriptor, true)
return true
})
}
return objectValue(obj)
}
func builtinObjectIsExtensible(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
return boolValue(obj.extensible)
}
panic(call.runtime.panicTypeError("Object.IsExtensible is nil"))
}
func builtinObjectPreventExtensions(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
obj.extensible = false
return val
}
panic(call.runtime.panicTypeError("Object.PreventExtensions is nil"))
}
func builtinObjectIsSealed(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
if obj.extensible {
return boolValue(false)
}
result := true
obj.enumerate(true, func(name string) bool {
prop := obj.getProperty(name)
if prop.configurable() {
result = false
}
return true
})
return boolValue(result)
}
panic(call.runtime.panicTypeError("Object.IsSealed is nil"))
}
func builtinObjectSeal(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
obj.enumerate(true, func(name string) bool {
if prop := obj.getOwnProperty(name); nil != prop && prop.configurable() {
prop.configureOff()
obj.defineOwnProperty(name, *prop, true)
}
return true
})
obj.extensible = false
return val
}
panic(call.runtime.panicTypeError("Object.Seal is nil"))
}
func builtinObjectIsFrozen(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
if obj.extensible {
return boolValue(false)
}
result := true
obj.enumerate(true, func(name string) bool {
prop := obj.getProperty(name)
if prop.configurable() || prop.writable() {
result = false
}
return true
})
return boolValue(result)
}
panic(call.runtime.panicTypeError("Object.IsFrozen is nil"))
}
func builtinObjectFreeze(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
obj.enumerate(true, func(name string) bool {
if prop, update := obj.getOwnProperty(name), false; nil != prop {
if prop.isDataDescriptor() && prop.writable() {
prop.writeOff()
update = true
}
if prop.configurable() {
prop.configureOff()
update = true
}
if update {
obj.defineOwnProperty(name, *prop, true)
}
}
return true
})
obj.extensible = false
return val
}
panic(call.runtime.panicTypeError("Object.Freeze is nil"))
}
func builtinObjectKeys(call FunctionCall) Value {
if obj, keys := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(false, func(name string) bool {
keys = append(keys, stringValue(name))
return true
})
return objectValue(call.runtime.newArrayOf(keys))
}
panic(call.runtime.panicTypeError("Object.Keys is nil"))
}
func builtinObjectValues(call FunctionCall) Value {
if obj, values := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(false, func(name string) bool {
values = append(values, obj.get(name))
return true
})
return objectValue(call.runtime.newArrayOf(values))
}
panic(call.runtime.panicTypeError("Object.Values is nil"))
}
func builtinObjectGetOwnPropertyNames(call FunctionCall) Value {
if obj, propertyNames := call.Argument(0).object(), []Value(nil); nil != obj {
obj.enumerate(true, func(name string) bool {
if obj.hasOwnProperty(name) {
propertyNames = append(propertyNames, stringValue(name))
}
return true
})
return objectValue(call.runtime.newArrayOf(propertyNames))
}
// Default to empty array for non object types.
return objectValue(call.runtime.newArray(0))
}