Make Object::GetIdentityHash() never return 0.
authorasargent@chromium.org <asargent@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 29 Apr 2009 21:44:13 +0000 (21:44 +0000)
committerasargent@chromium.org <asargent@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 29 Apr 2009 21:44:13 +0000 (21:44 +0000)
This is convenient when using identity hashes in data structures that
want to reserve 0 as a sentinel value, such as WebKit's WTF::HashMap.

Review URL: http://codereview.chromium.org/100147

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1821 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/api.cc

index 4beb75f..238342a 100644 (file)
@@ -2072,7 +2072,12 @@ int v8::Object::GetIdentityHash() {
   if (hash->IsSmi()) {
     hash_value = i::Smi::cast(*hash)->value();
   } else {
-    hash_value = random() & i::Smi::kMaxValue;  // Limit range to fit a smi.
+    int attempts = 0;
+    do {
+      hash_value = random() & i::Smi::kMaxValue;  // Limit range to fit a smi.
+      attempts++;
+    } while (hash_value == 0 && attempts < 30);
+    hash_value = hash_value != 0 ? hash_value : 1;  // never return 0
     i::SetProperty(hidden_props,
                    hash_symbol,
                    i::Handle<i::Object>(i::Smi::FromInt(hash_value)),