Reviewers: Mads Ager,

Description:
Add GetPropertyAttribute method for Object in the API

Patch by Peter Varga.

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


Please review this at http://codereview.chromium.org/7321006/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M include/v8.h
  M src/api.cc
  M test/cctest/test-api.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index fb10c71576da0a92963b4decfe4dde60067a34fb..ea7ab82628fff450e43af3b9e40b13045d749aef 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -1435,6 +1435,12 @@ class Object : public Value {

   V8EXPORT Local<Value> Get(uint32_t index);

+  /**
+   * Gets the property attributes of a property.
+   * Can be None or any combination of ReadOnly, DontEnum and DontDelete.
+   */
+  V8EXPORT PropertyAttribute GetPropertyAttribute(Handle<Value> key);
+
   // TODO(1245389): Replace the type-specific versions of these
   // functions with generic ones that accept a Handle<Value> key.
   V8EXPORT bool Has(Handle<String> key);
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 71a715c1bb28b20189b91421f7605518f9174f98..e2ca7f6ad66141327f7b94ece8a78417f18dc946 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -2712,6 +2712,22 @@ Local<Value> v8::Object::Get(uint32_t index) {
 }


+PropertyAttribute v8::Object::GetPropertyAttribute(v8::Handle<Value> key) {
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  ON_BAILOUT(isolate, "v8::Object::GetPropertyAttribute()",
+             return static_cast<PropertyAttribute>(NONE));
+  ENTER_V8(isolate);
+  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
+  EXCEPTION_PREAMBLE(isolate);
+  i::Handle<i::String> name = i::Handle<i::String>::cast(key_obj);
+  PropertyAttributes result = self->GetPropertyAttribute(*name);
+  has_pending_exception = (result == ABSENT);
+  EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
+  return static_cast<PropertyAttribute>(result);
+}
+
+
 Local<Value> v8::Object::GetPrototype() {
   i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
   ON_BAILOUT(isolate, "v8::Object::GetPrototype()",
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 1531f905d1b078f6286418e3c0096e98350a8a66..075f9ac7f73277a092a2105b09e8d5e02d23f089 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -2048,10 +2048,15 @@ THREADED_TEST(GetSetProperty) {
 THREADED_TEST(PropertyAttributes) {
   v8::HandleScope scope;
   LocalContext context;
+  // none
+  Local<String> prop = v8_str("none");
+  context->Global()->Set(prop, v8_num(7));
+  CHECK_EQ(v8::None, context->Global()->GetPropertyAttribute(prop));
   // read-only
-  Local<String> prop = v8_str("read_only");
+  prop = v8_str("read_only");
   context->Global()->Set(prop, v8_num(7), v8::ReadOnly);
   CHECK_EQ(7, context->Global()->Get(prop)->Int32Value());
+  CHECK_EQ(v8::ReadOnly, context->Global()->GetPropertyAttribute(prop));
   Script::Compile(v8_str("read_only = 9"))->Run();
   CHECK_EQ(7, context->Global()->Get(prop)->Int32Value());
   context->Global()->Set(prop, v8_num(10));
@@ -2062,6 +2067,11 @@ THREADED_TEST(PropertyAttributes) {
   CHECK_EQ(13, context->Global()->Get(prop)->Int32Value());
   Script::Compile(v8_str("delete dont_delete"))->Run();
   CHECK_EQ(13, context->Global()->Get(prop)->Int32Value());
+  CHECK_EQ(v8::DontDelete, context->Global()->GetPropertyAttribute(prop));
+  // dont-enum
+  prop = v8_str("dont_enum");
+  context->Global()->Set(prop, v8_num(28), v8::DontEnum);
+  CHECK_EQ(v8::DontEnum, context->Global()->GetPropertyAttribute(prop));
 }




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

Reply via email to