mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
7.4 KiB
290 lines
7.4 KiB
10 years ago
|
package otto
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
)
|
||
|
|
||
|
// Object
|
||
|
|
||
|
func builtinObject(call FunctionCall) Value {
|
||
|
value := call.Argument(0)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
case valueUndefined, valueNull:
|
||
|
return toValue_object(call.runtime.newObject())
|
||
|
}
|
||
|
|
||
|
return toValue_object(call.runtime.toObject(value))
|
||
|
}
|
||
|
|
||
10 years ago
|
func builtinNewObject(self *_object, argumentList []Value) Value {
|
||
10 years ago
|
value := valueOfArrayIndex(argumentList, 0)
|
||
10 years ago
|
switch value.kind {
|
||
10 years ago
|
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 {
|
||
10 years ago
|
propertyName := call.Argument(0).string()
|
||
10 years ago
|
thisObject := call.thisObject()
|
||
|
return toValue_bool(thisObject.hasOwnProperty(propertyName))
|
||
|
}
|
||
|
|
||
|
func builtinObject_isPrototypeOf(call FunctionCall) Value {
|
||
|
value := call.Argument(0)
|
||
|
if !value.IsObject() {
|
||
10 years ago
|
return falseValue
|
||
10 years ago
|
}
|
||
|
prototype := call.toObject(value).prototype
|
||
|
thisObject := call.thisObject()
|
||
|
for prototype != nil {
|
||
|
if thisObject == prototype {
|
||
10 years ago
|
return trueValue
|
||
10 years ago
|
}
|
||
|
prototype = prototype.prototype
|
||
|
}
|
||
10 years ago
|
return falseValue
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinObject_propertyIsEnumerable(call FunctionCall) Value {
|
||
10 years ago
|
propertyName := call.Argument(0).string()
|
||
10 years ago
|
thisObject := call.thisObject()
|
||
|
property := thisObject.getOwnProperty(propertyName)
|
||
|
if property != nil && property.enumerable() {
|
||
10 years ago
|
return trueValue
|
||
10 years ago
|
}
|
||
10 years ago
|
return falseValue
|
||
10 years ago
|
}
|
||
|
|
||
|
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() {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
10 years ago
|
return toString.call(call.runtime, call.This)
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinObject_getPrototypeOf(call FunctionCall) Value {
|
||
|
objectValue := call.Argument(0)
|
||
|
object := objectValue._object()
|
||
|
if object == nil {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
if object.prototype == nil {
|
||
10 years ago
|
return nullValue
|
||
10 years ago
|
}
|
||
|
|
||
|
return toValue_object(object.prototype)
|
||
|
}
|
||
|
|
||
|
func builtinObject_getOwnPropertyDescriptor(call FunctionCall) Value {
|
||
|
objectValue := call.Argument(0)
|
||
|
object := objectValue._object()
|
||
|
if object == nil {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
name := call.Argument(1).string()
|
||
10 years ago
|
descriptor := object.getOwnProperty(name)
|
||
|
if descriptor == nil {
|
||
10 years ago
|
return Value{}
|
||
10 years ago
|
}
|
||
|
return toValue_object(call.runtime.fromPropertyDescriptor(*descriptor))
|
||
|
}
|
||
|
|
||
|
func builtinObject_defineProperty(call FunctionCall) Value {
|
||
|
objectValue := call.Argument(0)
|
||
|
object := objectValue._object()
|
||
|
if object == nil {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
10 years ago
|
name := call.Argument(1).string()
|
||
|
descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
|
||
10 years ago
|
object.defineOwnProperty(name, descriptor, true)
|
||
|
return objectValue
|
||
|
}
|
||
|
|
||
|
func builtinObject_defineProperties(call FunctionCall) Value {
|
||
|
objectValue := call.Argument(0)
|
||
|
object := objectValue._object()
|
||
|
if object == nil {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
properties := call.runtime.toObject(call.Argument(1))
|
||
|
properties.enumerate(false, func(name string) bool {
|
||
10 years ago
|
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
|
||
10 years ago
|
object.defineOwnProperty(name, descriptor, true)
|
||
|
return true
|
||
|
})
|
||
|
|
||
|
return objectValue
|
||
|
}
|
||
|
|
||
|
func builtinObject_create(call FunctionCall) Value {
|
||
|
prototypeValue := call.Argument(0)
|
||
|
if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
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 {
|
||
10 years ago
|
descriptor := toPropertyDescriptor(call.runtime, properties.get(name))
|
||
10 years ago
|
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)
|
||
|
}
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
func builtinObject_preventExtensions(call FunctionCall) Value {
|
||
|
object := call.Argument(0)
|
||
|
if object := object._object(); object != nil {
|
||
|
object.extensible = false
|
||
|
} else {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
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)
|
||
|
}
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
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 {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
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)
|
||
|
}
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
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 {
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
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))
|
||
|
}
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|
||
|
|
||
|
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))
|
||
|
}
|
||
10 years ago
|
panic(call.runtime.panicTypeError())
|
||
10 years ago
|
}
|