diff --git a/builtin_object.go b/builtin_object.go index a955770..d7c9272 100644 --- a/builtin_object.go +++ b/builtin_object.go @@ -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() diff --git a/global.go b/global.go index 563f613..28456d9 100644 --- a/global.go +++ b/global.go @@ -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, diff --git a/object_test.go b/object_test.go index 0c6043f..f4b4f5b 100644 --- a/object_test.go +++ b/object_test.go @@ -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") +} diff --git a/value.go b/value.go index 184e17c..89c092b 100644 --- a/value.go +++ b/value.go @@ -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: