From 61a011e711054c66d27529c9a63dba3c2d5c5f57 Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Tue, 4 Jun 2013 20:07:16 -0700 Subject: [PATCH] Add Object.getOwnPropertyNames (sdgoij) --- builtin_object.go | 14 +++++++++++++- global.go | 1 + object_test.go | 22 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/builtin_object.go b/builtin_object.go index b705b41..2bf01db 100644 --- a/builtin_object.go +++ b/builtin_object.go @@ -223,7 +223,7 @@ func builtinObject_freeze(call FunctionCall) Value { } func builtinObject_keys(call FunctionCall) Value { - if object, keys := call.Argument(0)._object(), []Value{}; nil != object { + if object, keys := call.Argument(0)._object(), []Value(nil); nil != object { object.enumerate(false, func(name string) { keys = append(keys, toValue(name)) }) @@ -231,3 +231,15 @@ func builtinObject_keys(call FunctionCall) Value { } panic(newTypeError()) } + +func builtinObject_getOwnPropertyNames(call FunctionCall) Value { + if object, propertyNames := call.Argument(0)._object(), []Value(nil); nil != object { + object.enumerate(true, func(name string) { + if object.hasOwnProperty(name) { + propertyNames = append(propertyNames, toValue(name)) + } + }) + return toValue(call.runtime.newArrayOf(propertyNames)) + } + panic(newTypeError()) +} diff --git a/global.go b/global.go index b5b4642..3569c3f 100644 --- a/global.go +++ b/global.go @@ -184,6 +184,7 @@ func newContext() *_runtime { "isFrozen", -1, builtinObject_isFrozen, "freeze", -1, builtinObject_freeze, "keys", -1, builtinObject_keys, + "getOwnPropertyNames", -1, builtinObject_getOwnPropertyNames, ) self.Global.Function = self.newGlobalFunction( diff --git a/object_test.go b/object_test.go index 490a8b8..a9d7e61 100644 --- a/object_test.go +++ b/object_test.go @@ -271,3 +271,25 @@ func TestObject_keys(t *testing.T) { })(undefined, undefined, undefined, undefined); `, "0,1,2,3") } + +func TestObject_getOwnPropertyNames(t *testing.T) { + Terst(t) + + test := runTest() + + test(`Object.getOwnPropertyNames({ abc:undefined, def:undefined })`, "abc,def") + + test(` + var ghi = Object.create( + { + abc: undefined, + def: undefined + }, + { + ghi: { value: undefined, enumerable: true }, + jkl: { value: undefined, enumerable: false } + } + ); + Object.getOwnPropertyNames(ghi) + `, "ghi,jkl") +}