aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/robertkrimen/otto/builtin_object.go
blob: c2433f7be2b3d9b7cea9b8b60bd69e6b6e8f7bee (plain) (blame)
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
package otto

import (
    "fmt"
)

// Object

func builtinObject(call FunctionCall) Value {
    value := call.Argument(0)
    switch value.kind {
    case valueUndefined, valueNull:
        return toValue_object(call.runtime.newObject())
    }

    return toValue_object(call.runtime.toObject(value))
}

func builtinNewObject(self *_object, argumentList []Value) Value {
    value := valueOfArrayIndex(argumentList, 0)
    switch value.kind {
    case valueNull, valueUndefined:
    case valueNumber, valueString, valueBoolean:
        return toValue_object(self.runtime.toObject(value))
    case valueObject:
        return value
    default:
    }
    return toValue_object(self.runtime.newObject())
}

func builtinObject_valueOf(call FunctionCall) Value {
    return toValue_object(call.thisObject())
}

func builtinObject_hasOwnProperty(call FunctionCall) Value {
    propertyName := call.Argument(0).string()
    thisObject := call.thisObject()
    return toValue_bool(thisObject.hasOwnProperty(propertyName))
}

func builtinObject_isPrototypeOf(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 builtinObject_propertyIsEnumerable(call FunctionCall) Value {
    propertyName := call.Argument(0).string()
    thisObject := call.thisObject()
    property := thisObject.getOwnProperty(propertyName)
    if property != nil && property.enumerable() {
        return trueValue
    }
    return falseValue
}

func builtinObject_toString(call FunctionCall) Value {
    result := ""
    if call.This.IsUndefined() {
        result = "[object Undefined]"
    } else if call.This.IsNull() {
        result = "[object Null]"
    } else {
        result = fmt.Sprintf("[object %s]", call.thisObject().class)
    }
    return toValue_string(result)
}

func builtinObject_toLocaleString(call FunctionCall) Value {
    toString := call.thisObject().get("toString")
    if !toString.isCallable() {
        panic(call.runtime.panicTypeError())
    }
    return toString.call(call.runtime, call.This)
}

func builtinObject_getPrototypeOf(call FunctionCall) Value {
    objectValue := call.Argument(0)
    object := objectValue._object()
    if object == nil {
        panic(call.runtime.panicTypeError())
    }

    if object.prototype == nil {
        return nullValue
    }

    return toValue_object(object.prototype)
}

func builtinObject_getOwnPropertyDescriptor(call FunctionCall) Value {
    objectValue := call.Argument(0)
    object := objectValue._object()
    if object == nil {
        panic(call.runtime.panicTypeError())
    }

    name := call.Argument(1).string()
    descriptor := object.getOwnProperty(name)
    if descriptor == nil {
        return Value{}
    }
    return toValue_object(call.runtime.fromPropertyDescriptor(*descriptor))
}

func builtinObject_defineProperty(call FunctionCall) Value {
    objectValue := call.Argument(0)
    object := objectValue._object()
    if object == nil {
        panic(call.runtime.panicTypeError())
    }
    name := call.Argument(1).string()
    descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
    object.defineOwnProperty(name, descriptor, true)
    return objectValue
}

func builtinObject_defineProperties(call FunctionCall) Value {
    objectValue := call.Argument(0)
    object := objectValue._object()
    if object == nil {
        panic(call.runtime.panicTypeError())
    }

    properties := call.runtime.toObject(call.Argument(1))
    properties.enumerate(false, func(name string) bool {
        descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
        object.defineOwnProperty(name, descriptor, true)
        return true
    })

    return objectValue
}

func builtinObject_create(call FunctionCall) Value {
    prototypeValue := call.Argument(0)
    if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
        panic(call.runtime.panicTypeError())
    }

    object := call.runtime.newObject()
    object.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))
            object.defineOwnProperty(name, descriptor, true)
            return true
        })
    }

    return toValue_object(object)
}

func builtinObject_isExtensible(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        return toValue_bool(object.extensible)
    }
    panic(call.runtime.panicTypeError())
}

func builtinObject_preventExtensions(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        object.extensible = false
    } else {
        panic(call.runtime.panicTypeError())
    }
    return object
}

func builtinObject_isSealed(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        if object.extensible {
            return toValue_bool(false)
        }
        result := true
        object.enumerate(true, func(name string) bool {
            property := object.getProperty(name)
            if property.configurable() {
                result = false
            }
            return true
        })
        return toValue_bool(result)
    }
    panic(call.runtime.panicTypeError())
}

func builtinObject_seal(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        object.enumerate(true, func(name string) bool {
            if property := object.getOwnProperty(name); nil != property && property.configurable() {
                property.configureOff()
                object.defineOwnProperty(name, *property, true)
            }
            return true
        })
        object.extensible = false
    } else {
        panic(call.runtime.panicTypeError())
    }
    return object
}

func builtinObject_isFrozen(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        if object.extensible {
            return toValue_bool(false)
        }
        result := true
        object.enumerate(true, func(name string) bool {
            property := object.getProperty(name)
            if property.configurable() || property.writable() {
                result = false
            }
            return true
        })
        return toValue_bool(result)
    }
    panic(call.runtime.panicTypeError())
}

func builtinObject_freeze(call FunctionCall) Value {
    object := call.Argument(0)
    if object := object._object(); object != nil {
        object.enumerate(true, func(name string) bool {
            if property, update := object.getOwnProperty(name), false; nil != property {
                if property.isDataDescriptor() && property.writable() {
                    property.writeOff()
                    update = true
                }
                if property.configurable() {
                    property.configureOff()
                    update = true
                }
                if update {
                    object.defineOwnProperty(name, *property, true)
                }
            }
            return true
        })
        object.extensible = false
    } else {
        panic(call.runtime.panicTypeError())
    }
    return object
}

func builtinObject_keys(call FunctionCall) Value {
    if object, keys := call.Argument(0)._object(), []Value(nil); nil != object {
        object.enumerate(false, func(name string) bool {
            keys = append(keys, toValue_string(name))
            return true
        })
        return toValue_object(call.runtime.newArrayOf(keys))
    }
    panic(call.runtime.panicTypeError())
}

func builtinObject_getOwnPropertyNames(call FunctionCall) Value {
    if object, propertyNames := call.Argument(0)._object(), []Value(nil); nil != object {
        object.enumerate(true, func(name string) bool {
            if object.hasOwnProperty(name) {
                propertyNames = append(propertyNames, toValue_string(name))
            }
            return true
        })
        return toValue_object(call.runtime.newArrayOf(propertyNames))
    }
    panic(call.runtime.panicTypeError())
}