Revision: 8264
Author:   [email protected]
Date:     Fri Jun 10 05:33:55 2011
Log:      Add GetOwnPropertyNames method for Object in the API

Patch by Peter Varga.

BUG=none
TEST=cctest/test-api/PropertyEnumeration

Review URL: http://codereview.chromium.org/7129058
http://code.google.com/p/v8/source/detail?r=8264

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Fri Jun 10 02:54:04 2011
+++ /branches/bleeding_edge/include/v8.h        Fri Jun 10 05:33:55 2011
@@ -1464,6 +1464,13 @@
    */
   V8EXPORT Local<Array> GetPropertyNames();

+  /**
+   * This function has the same functionality as GetPropertyNames but
+   * the returned array doesn't contain the names of properties from
+   * prototype objects.
+   */
+  V8EXPORT Local<Array> GetOwnPropertyNames();
+
   /**
    * Get the prototype object.  This does not skip objects marked to
    * be skipped by __proto__ and it does not consult the security
=======================================
--- /branches/bleeding_edge/src/api.cc  Fri Jun 10 02:54:04 2011
+++ /branches/bleeding_edge/src/api.cc  Fri Jun 10 05:33:55 2011
@@ -2761,6 +2761,25 @@
       isolate->factory()->NewJSArrayWithElements(elms);
   return Utils::ToLocal(scope.CloseAndEscape(result));
 }
+
+
+Local<Array> v8::Object::GetOwnPropertyNames() {
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  ON_BAILOUT(isolate, "v8::Object::GetOwnPropertyNames()",
+             return Local<v8::Array>());
+  ENTER_V8(isolate);
+  i::HandleScope scope(isolate);
+  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+  i::Handle<i::FixedArray> value =
+      i::GetKeysInFixedArrayFor(self, i::LOCAL_ONLY);
+  // Because we use caching to speed up enumeration it is important
+  // to never change the result of the basic enumeration function so
+  // we clone the result.
+ i::Handle<i::FixedArray> elms = isolate->factory()->CopyFixedArray(value);
+  i::Handle<i::JSArray> result =
+      isolate->factory()->NewJSArrayWithElements(elms);
+  return Utils::ToLocal(scope.CloseAndEscape(result));
+}


 Local<String> v8::Object::ObjectProtoToString() {
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc     Fri Jun 10 02:54:04 2011
+++ /branches/bleeding_edge/test/cctest/test-api.cc     Fri Jun 10 05:33:55 2011
@@ -9911,6 +9911,19 @@
     CHECK_EQ(elmv[i], *elm);
   }
 }
+
+
+void CheckOwnProperties(v8::Handle<v8::Value> val,
+                        int elmc,
+                        const char* elmv[]) {
+  v8::Handle<v8::Object> obj = val.As<v8::Object>();
+  v8::Handle<v8::Array> props = obj->GetOwnPropertyNames();
+  CHECK_EQ(elmc, props->Length());
+  for (int i = 0; i < elmc; i++) {
+    v8::String::Utf8Value elm(props->Get(v8::Integer::New(i)));
+    CHECK_EQ(elmv[i], *elm);
+  }
+}


 THREADED_TEST(PropertyEnumeration) {
@@ -9930,15 +9943,21 @@
   int elmc0 = 0;
   const char** elmv0 = NULL;
   CheckProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0);
+  CheckOwnProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0);
   int elmc1 = 2;
   const char* elmv1[] = {"a", "b"};
   CheckProperties(elms->Get(v8::Integer::New(1)), elmc1, elmv1);
+  CheckOwnProperties(elms->Get(v8::Integer::New(1)), elmc1, elmv1);
   int elmc2 = 3;
   const char* elmv2[] = {"0", "1", "2"};
   CheckProperties(elms->Get(v8::Integer::New(2)), elmc2, elmv2);
+  CheckOwnProperties(elms->Get(v8::Integer::New(2)), elmc2, elmv2);
   int elmc3 = 4;
   const char* elmv3[] = {"w", "z", "x", "y"};
   CheckProperties(elms->Get(v8::Integer::New(3)), elmc3, elmv3);
+  int elmc4 = 2;
+  const char* elmv4[] = {"w", "z"};
+  CheckOwnProperties(elms->Get(v8::Integer::New(3)), elmc4, elmv4);
 }

 THREADED_TEST(PropertyEnumeration2) {

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to