Reviewers: Mads Ager,

Description:
Expose hasOwnProperty() through API.

BUG=http://code.google.com/p/v8/issues/detail?id=1342
TEST=test-api/HasOwnProperty


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

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 71c61f9218a421e284b6023c5f058716e7c8017d..4ab1ec2b4f8c54c83f36f4ee2ecd53ddec1be599 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -1,4 +1,4 @@
-// Copyright 2007-2009 the V8 project authors. All rights reserved.
+// Copyright 2011 the V8 project authors. All rights reserved.
 // Redistribution and use in source and binary forms, with or without
 // modification, are permitted provided that the following conditions are
 // met:
@@ -1588,6 +1588,7 @@ class Object : public Value {
   V8EXPORT void SetPointerInInternalField(int index, void* value);

   // Testers for local properties.
+  V8EXPORT bool HasOwnProperty(Handle<String> key);
   V8EXPORT bool HasRealNamedProperty(Handle<String> key);
   V8EXPORT bool HasRealIndexedProperty(uint32_t index);
   V8EXPORT bool HasRealNamedCallbackProperty(Handle<String> key);
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index aa36641f7e570640dfa5966c13d22122151e3d12..e6833530041b9794dbd0c63f34e5c6e26d33d0e9 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -1,4 +1,4 @@
-// Copyright 2010 the V8 project authors. All rights reserved.
+// Copyright 2011 the V8 project authors. All rights reserved.
 // Redistribution and use in source and binary forms, with or without
 // modification, are permitted provided that the following conditions are
 // met:
@@ -2751,6 +2751,15 @@ bool Object::SetAccessor(Handle<String> name,
 }


+bool v8::Object::HasOwnProperty(Handle<String> key) {
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  ON_BAILOUT(isolate, "v8::Object::HasOwnProperty()",
+             return false);
+  return Utils::OpenHandle(this)->HasLocalProperty(
+      *Utils::OpenHandle(*key));
+}
+
+
 bool v8::Object::HasRealNamedProperty(Handle<String> key) {
   i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
   ON_BAILOUT(isolate, "v8::Object::HasRealNamedProperty()",
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 19492a53278f5fdb714633a941775f7a94945ab5..dfb8edb2f91ae99ac569fb2fce4a8921302879e1 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -202,6 +202,20 @@ THREADED_TEST(ReceiverSignature) {
 }


+TEST(HasOwnProperty) {
+  v8::HandleScope scope;
+  LocalContext env;
+  Handle<Value> value = CompileRun(
+      "function Foo() { this.foo = 11; };"
+      "function Bar() { this.bar = 13; };"
+      "Bar.prototype = new Foo();"
+      "new Bar();");
+  CHECK(value->IsObject());
+  Handle<Object> object = value->ToObject();
+  CHECK_EQ(true, object->Has(v8_str("foo")));
+  CHECK_EQ(false, object->HasOwnProperty(v8_str("foo")));
+  CHECK_EQ(true, object->HasOwnProperty(v8_str("bar")));
+}


 THREADED_TEST(ArgumentSignature) {


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

Reply via email to