From 8896e45dec231cdadc161a86e9bd53f410d39054 Mon Sep 17 00:00:00 2001 From: "yurys@chromium.org" Date: Mon, 28 Jun 2010 08:31:31 +0000 Subject: [PATCH] Object.getOwnPropertyNames should be free of duplicates BUG=41243 Review URL: http://codereview.chromium.org/2825026 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4958 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/v8natives.js | 14 +++++++++++--- test/cctest/test-api.cc | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/v8natives.js b/src/v8natives.js index 1d47eb7..690a6ca 100644 --- a/src/v8natives.js +++ b/src/v8natives.js @@ -677,9 +677,17 @@ function ObjectGetOwnPropertyNames(obj) { } } - // Property names are expected to be strings. - for (var i = 0; i < propertyNames.length; ++i) - propertyNames[i] = ToString(propertyNames[i]); + // Property names are expected to be unique strings. + var propertySet = {}; + var j = 0; + for (var i = 0; i < propertyNames.length; ++i) { + var name = ToString(propertyNames[i]); + if (name in propertySet) + continue; + propertySet[name] = true; + propertyNames[j++] = name; + } + propertyNames.length = j; return propertyNames; } diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index c426db4..f830820 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -5035,6 +5035,31 @@ THREADED_TEST(AccessControlGetOwnPropertyNames) { } +static v8::Handle NamedPropertyEnumerator(const AccessorInfo& info) { + v8::Handle result = v8::Array::New(1); + result->Set(0, v8_str("x")); + return result; +} + + +THREADED_TEST(GetOwnPropertyNamesWithInterceptor) { + v8::HandleScope handle_scope; + v8::Handle obj_template = v8::ObjectTemplate::New(); + + obj_template->Set(v8_str("x"), v8::Integer::New(42)); + obj_template->SetNamedPropertyHandler(NULL, NULL, NULL, NULL, + NamedPropertyEnumerator); + + LocalContext context; + v8::Handle global = context->Global(); + global->Set(v8_str("object"), obj_template->NewInstance()); + + v8::Handle value = + CompileRun("Object.getOwnPropertyNames(object).join(',')"); + CHECK_EQ(v8_str("x"), value); +} + + static v8::Handle ConstTenGetter(Local name, const AccessorInfo& info) { return v8_num(10); -- 2.7.4