2015-02-16 15:28:33 +02:00
|
|
|
package otto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Object
|
|
|
|
|
|
|
|
func builtinObject(call FunctionCall) Value {
|
|
|
|
value := call.Argument(0)
|
2015-03-20 14:22:01 +02:00
|
|
|
switch value.kind {
|
2015-02-16 15:28:33 +02:00
|
|
|
case valueUndefined, valueNull:
|
|
|
|
return toValue_object(call.runtime.newObject())
|
|
|
|
}
|
|
|
|
|
|
|
|
return toValue_object(call.runtime.toObject(value))
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:22:01 +02:00
|
|
|
func builtinNewObject(self *_object, argumentList []Value) Value {
|
2015-02-16 15:28:33 +02:00
|
|
|
value := valueOfArrayIndex(argumentList, 0)
|
2015-03-20 14:22:01 +02:00
|
|
|
switch value.kind {
|
2015-02-16 15:28:33 +02:00
|
|
|
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 {
|
2015-03-20 14:22:01 +02:00
|
|
|
propertyName := call.Argument(0).string()
|
2015-02-16 15:28:33 +02:00
|
|
|
thisObject := call.thisObject()
|
|
|
|
return toValue_bool(thisObject.hasOwnProperty(propertyName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_isPrototypeOf(call FunctionCall) Value {
|
|
|
|
value := call.Argument(0)
|
|
|
|
if !value.IsObject() {
|
2015-03-20 14:22:01 +02:00
|
|
|
return falseValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
prototype := call.toObject(value).prototype
|
|
|
|
thisObject := call.thisObject()
|
|
|
|
for prototype != nil {
|
|
|
|
if thisObject == prototype {
|
2015-03-20 14:22:01 +02:00
|
|
|
return trueValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
prototype = prototype.prototype
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
return falseValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_propertyIsEnumerable(call FunctionCall) Value {
|
2015-03-20 14:22:01 +02:00
|
|
|
propertyName := call.Argument(0).string()
|
2015-02-16 15:28:33 +02:00
|
|
|
thisObject := call.thisObject()
|
|
|
|
property := thisObject.getOwnProperty(propertyName)
|
|
|
|
if property != nil && property.enumerable() {
|
2015-03-20 14:22:01 +02:00
|
|
|
return trueValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
return falseValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
return toString.call(call.runtime, call.This)
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_getPrototypeOf(call FunctionCall) Value {
|
|
|
|
objectValue := call.Argument(0)
|
|
|
|
object := objectValue._object()
|
|
|
|
if object == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if object.prototype == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
return nullValue
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return toValue_object(object.prototype)
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_getOwnPropertyDescriptor(call FunctionCall) Value {
|
|
|
|
objectValue := call.Argument(0)
|
|
|
|
object := objectValue._object()
|
|
|
|
if object == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-20 14:22:01 +02:00
|
|
|
name := call.Argument(1).string()
|
2015-02-16 15:28:33 +02:00
|
|
|
descriptor := object.getOwnProperty(name)
|
|
|
|
if descriptor == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
return Value{}
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
return toValue_object(call.runtime.fromPropertyDescriptor(*descriptor))
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_defineProperty(call FunctionCall) Value {
|
|
|
|
objectValue := call.Argument(0)
|
|
|
|
object := objectValue._object()
|
|
|
|
if object == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
name := call.Argument(1).string()
|
|
|
|
descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
|
2015-02-16 15:28:33 +02:00
|
|
|
object.defineOwnProperty(name, descriptor, true)
|
|
|
|
return objectValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_defineProperties(call FunctionCall) Value {
|
|
|
|
objectValue := call.Argument(0)
|
|
|
|
object := objectValue._object()
|
|
|
|
if object == nil {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
properties := call.runtime.toObject(call.Argument(1))
|
|
|
|
properties.enumerate(false, func(name string) bool {
|
2015-03-20 14:22:01 +02:00
|
|
|
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
|
2015-02-16 15:28:33 +02:00
|
|
|
object.defineOwnProperty(name, descriptor, true)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return objectValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_create(call FunctionCall) Value {
|
|
|
|
prototypeValue := call.Argument(0)
|
|
|
|
if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-03-20 14:22:01 +02:00
|
|
|
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
|
2015-02-16 15:28:33 +02:00
|
|
|
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)
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func builtinObject_preventExtensions(call FunctionCall) Value {
|
|
|
|
object := call.Argument(0)
|
|
|
|
if object := object._object(); object != nil {
|
|
|
|
object.extensible = false
|
|
|
|
} else {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2015-03-20 14:22:01 +02:00
|
|
|
panic(call.runtime.panicTypeError())
|
2015-02-16 15:28:33 +02:00
|
|
|
}
|