1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Add Object.getPrototypeOf

This commit is contained in:
Robert Krimen 2013-03-05 12:03:33 -08:00
parent 299495ba0a
commit f3da7237f2
4 changed files with 28 additions and 0 deletions

View File

@ -32,6 +32,20 @@ func builtinObject_toString(call FunctionCall) Value {
return toValue(result)
}
func builtinObject_getPrototypeOf(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()
if object == nil {
panic(newTypeError())
}
if object.prototype == nil {
return NullValue()
}
return toValue(object.prototype)
}
func builtinObject_getOwnPropertyDescriptor(call FunctionCall) Value {
objectValue := call.Argument(0)
object := objectValue._object()

View File

@ -168,6 +168,7 @@ func newContext() *_runtime {
},
)
self.Global.Object.write(
"getPrototypeOf", 1, builtinObject_getPrototypeOf,
"getOwnPropertyDescriptor", 2, builtinObject_getOwnPropertyDescriptor,
"defineProperty", 3, builtinObject_defineProperty,
"defineProperties", 2, builtinObject_defineProperties,

View File

@ -23,3 +23,15 @@ func TestStringObject(t *testing.T) {
Is(object.get("10"), "undefined")
Is(object.get("2"), "z")
}
func TestObject_getPrototypeOf(t *testing.T) {
Terst(t)
test := runTest()
test(`
abc = {};
def = Object.getPrototypeOf(abc);
ghi = Object.getPrototypeOf(def);
[abc,def,ghi,ghi+""];
`, "[object Object],[object Object],,null")
}

View File

@ -250,6 +250,7 @@ func toValue_reflectValuePanic(value interface{}, kind reflect.Kind) {
}
}
// TODO toValue(nil)?
func toValue(value interface{}) Value {
switch value := value.(type) {
case Value: