Reviewers: aandrey, Yang, adamk,

Description:
Add GetIdentityHash to v8::Name object API

v8::Object already has GetIdentityHash on it. This change adds its counterpart
to v8::Name.

BUG=437416
LOG=Y

Please review this at https://codereview.chromium.org/753373003/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+54, -0 lines):
  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 ee6b2e8d5b188c64bc174122c9b4e921d3b11793..99210a589a3a86c7b7a0bd345451f9903b6569bf 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -1822,6 +1822,15 @@ class V8_EXPORT Boolean : public Primitive {
  */
 class V8_EXPORT Name : public Primitive {
  public:
+  /**
+   * Returns the identity hash for this object. The current implementation
+   * uses a inline property on the object to store the identity hash.
+   *
+   * The return value will never be 0. Also, it is not guaranteed to be
+   * unique.
+   */
+  int GetIdentityHash();
+
   V8_INLINE static Name* Cast(v8::Value* obj);
  private:
   static void CheckCast(v8::Value* obj);
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 58c08f59c69ac39bd0f50729f3559fae748b9c9c..d349d59f13f82d9a7adf28303beb5b9035a40b8c 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -4231,6 +4231,17 @@ Local<v8::Value> Function::GetBoundFunction() const {
 }


+int Name::GetIdentityHash() {
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  ON_BAILOUT(isolate, "v8::Name::GetIdentityHash()", return 0);
+  ENTER_V8(isolate);
+  i::HandleScope scope(isolate);
+  i::Handle<i::Name> self = Utils::OpenHandle(this);
+  DCHECK(!self->IsSymbol() || self->HasHashCode());
+  return static_cast<int>(self->Hash());
+}
+
+
 int String::Length() const {
   i::Handle<i::String> str = Utils::OpenHandle(this);
   return str->length();
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 0920178785e1664112f32c7c375c8c306ba7df64..716f5cbaf6cc2c1fb4c926b935d9b44ff2f8a25b 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -3033,6 +3033,40 @@ THREADED_TEST(GlobalProxyIdentityHash) {
 }


+TEST(SymbolIdentityHash) {
+  LocalContext env;
+  v8::Isolate* isolate = env->GetIsolate();
+  v8::HandleScope scope(isolate);
+
+  Local<v8::Symbol> symbol = v8::Symbol::New(isolate);
+  int hash = symbol->GetIdentityHash();
+  int hash1 = symbol->GetIdentityHash();
+  CHECK_EQ(hash, hash1);
+  CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
+  int hash3 = symbol->GetIdentityHash();
+  CHECK_EQ(hash, hash3);
+}
+
+
+TEST(StringIdentityHash) {
+  LocalContext env;
+  v8::Isolate* isolate = env->GetIsolate();
+  v8::HandleScope scope(isolate);
+
+  Local<v8::String> str = v8::String::NewFromUtf8(isolate, "str1");
+  int hash = str->GetIdentityHash();
+  int hash1 = str->GetIdentityHash();
+  CHECK_EQ(hash, hash1);
+  CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags);
+  int hash3 = str->GetIdentityHash();
+  CHECK_EQ(hash, hash3);
+
+  Local<v8::String> str2 = v8::String::NewFromUtf8(isolate, "str1");
+  int hash4 = str2->GetIdentityHash();
+  CHECK_EQ(hash, hash4);
+}
+
+
 THREADED_TEST(SymbolProperties) {
   LocalContext env;
   v8::Isolate* isolate = env->GetIsolate();


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to