Revision: 8679
Author: [email protected]
Date: Mon Jul 18 10:44:57 2011
Log: Add GetPropertyAttribute method for Object in the API
Patch by Peter Varga.
BUG=none
TEST=cctest/test-api/PropertyAttributes
Review URL: http://codereview.chromium.org/7321006
http://code.google.com/p/v8/source/detail?r=8679
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 Sun Jul 17 02:16:28 2011
+++ /branches/bleeding_edge/include/v8.h Mon Jul 18 10:44:57 2011
@@ -1458,6 +1458,13 @@
V8EXPORT Local<Value> Get(uint32_t index);
+ /**
+ * Gets the property attributes of a property which can be None or
+ * any combination of ReadOnly, DontEnum and DontDelete. Returns
+ * None when the property doesn't exist.
+ */
+ V8EXPORT PropertyAttribute GetPropertyAttributes(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);
=======================================
--- /branches/bleeding_edge/src/api.cc Sun Jul 17 02:16:28 2011
+++ /branches/bleeding_edge/src/api.cc Mon Jul 18 10:44:57 2011
@@ -2792,6 +2792,26 @@
EXCEPTION_BAILOUT_CHECK(isolate, Local<Value>());
return Utils::ToLocal(result);
}
+
+
+PropertyAttribute v8::Object::GetPropertyAttributes(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::HandleScope scope(isolate);
+ i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+ i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
+ if (!key_obj->IsString()) {
+ EXCEPTION_PREAMBLE(isolate);
+ key_obj = i::Execution::ToString(key_obj, &has_pending_exception);
+ EXCEPTION_BAILOUT_CHECK(isolate, static_cast<PropertyAttribute>(NONE));
+ }
+ i::Handle<i::String> key_string = i::Handle<i::String>::cast(key_obj);
+ PropertyAttributes result = self->GetPropertyAttribute(*key_string);
+ if (result == ABSENT) return static_cast<PropertyAttribute>(NONE);
+ return static_cast<PropertyAttribute>(result);
+}
Local<Value> v8::Object::GetPrototype() {
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc Thu Jul 14 04:00:04 2011
+++ /branches/bleeding_edge/test/cctest/test-api.cc Mon Jul 18 10:44:57 2011
@@ -2132,10 +2132,15 @@
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()->GetPropertyAttributes(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()->GetPropertyAttributes(prop));
Script::Compile(v8_str("read_only = 9"))->Run();
CHECK_EQ(7, context->Global()->Get(prop)->Int32Value());
context->Global()->Set(prop, v8_num(10));
@@ -2146,6 +2151,25 @@
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()->GetPropertyAttributes(prop));
+ // dont-enum
+ prop = v8_str("dont_enum");
+ context->Global()->Set(prop, v8_num(28), v8::DontEnum);
+ CHECK_EQ(v8::DontEnum, context->Global()->GetPropertyAttributes(prop));
+ // absent
+ prop = v8_str("absent");
+ CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(prop));
+ Local<Value> fake_prop = v8_num(1);
+ CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(fake_prop));
+ // exception
+ TryCatch try_catch;
+ Local<Value> exception =
+ CompileRun("({ toString: function() { throw 'exception';} })");
+ CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(exception));
+ CHECK(try_catch.HasCaught());
+ String::AsciiValue exception_value(try_catch.Exception());
+ CHECK_EQ("exception", *exception_value);
+ try_catch.Reset();
}
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev