| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "macro-assembler.h" | 34 #include "macro-assembler.h" |
| 35 #include "objects.h" | 35 #include "objects.h" |
| 36 #include "global-handles.h" | 36 #include "global-handles.h" |
| 37 #include "cctest.h" | 37 #include "cctest.h" |
| 38 | 38 |
| 39 using namespace v8::internal; | 39 using namespace v8::internal; |
| 40 | 40 |
| 41 | 41 |
| 42 TEST(ObjectHashTable) { | 42 TEST(ObjectHashTable) { |
| 43 LocalContext context; | 43 LocalContext context; |
| 44 Isolate* isolate = Isolate::Current(); |
| 45 Factory* factory = isolate->factory(); |
| 44 v8::HandleScope scope(context->GetIsolate()); | 46 v8::HandleScope scope(context->GetIsolate()); |
| 45 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(23); | 47 Handle<ObjectHashTable> table = factory->NewObjectHashTable(23); |
| 46 Handle<JSObject> a = FACTORY->NewJSArray(7); | 48 Handle<JSObject> a = factory->NewJSArray(7); |
| 47 Handle<JSObject> b = FACTORY->NewJSArray(11); | 49 Handle<JSObject> b = factory->NewJSArray(11); |
| 48 table = PutIntoObjectHashTable(table, a, b); | 50 table = PutIntoObjectHashTable(table, a, b); |
| 49 CHECK_EQ(table->NumberOfElements(), 1); | 51 CHECK_EQ(table->NumberOfElements(), 1); |
| 50 CHECK_EQ(table->Lookup(*a), *b); | 52 CHECK_EQ(table->Lookup(*a), *b); |
| 51 CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value()); | 53 CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value()); |
| 52 | 54 |
| 53 // Keys still have to be valid after objects were moved. | 55 // Keys still have to be valid after objects were moved. |
| 54 HEAP->CollectGarbage(NEW_SPACE); | 56 HEAP->CollectGarbage(NEW_SPACE); |
| 55 CHECK_EQ(table->NumberOfElements(), 1); | 57 CHECK_EQ(table->NumberOfElements(), 1); |
| 56 CHECK_EQ(table->Lookup(*a), *b); | 58 CHECK_EQ(table->Lookup(*a), *b); |
| 57 CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value()); | 59 CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value()); |
| 58 | 60 |
| 59 // Keys that are overwritten should not change number of elements. | 61 // Keys that are overwritten should not change number of elements. |
| 60 table = PutIntoObjectHashTable(table, a, FACTORY->NewJSArray(13)); | 62 table = PutIntoObjectHashTable(table, a, factory->NewJSArray(13)); |
| 61 CHECK_EQ(table->NumberOfElements(), 1); | 63 CHECK_EQ(table->NumberOfElements(), 1); |
| 62 CHECK_NE(table->Lookup(*a), *b); | 64 CHECK_NE(table->Lookup(*a), *b); |
| 63 | 65 |
| 64 // Keys mapped to the hole should be removed permanently. | 66 // Keys mapped to the hole should be removed permanently. |
| 65 table = PutIntoObjectHashTable(table, a, FACTORY->the_hole_value()); | 67 table = PutIntoObjectHashTable(table, a, factory->the_hole_value()); |
| 66 CHECK_EQ(table->NumberOfElements(), 0); | 68 CHECK_EQ(table->NumberOfElements(), 0); |
| 67 CHECK_EQ(table->NumberOfDeletedElements(), 1); | 69 CHECK_EQ(table->NumberOfDeletedElements(), 1); |
| 68 CHECK_EQ(table->Lookup(*a), HEAP->the_hole_value()); | 70 CHECK_EQ(table->Lookup(*a), HEAP->the_hole_value()); |
| 69 | 71 |
| 70 // Keys should map back to their respective values and also should get | 72 // Keys should map back to their respective values and also should get |
| 71 // an identity hash code generated. | 73 // an identity hash code generated. |
| 72 for (int i = 0; i < 100; i++) { | 74 for (int i = 0; i < 100; i++) { |
| 73 Handle<JSObject> key = FACTORY->NewJSArray(7); | 75 Handle<JSObject> key = factory->NewJSArray(7); |
| 74 Handle<JSObject> value = FACTORY->NewJSArray(11); | 76 Handle<JSObject> value = factory->NewJSArray(11); |
| 75 table = PutIntoObjectHashTable(table, key, value); | 77 table = PutIntoObjectHashTable(table, key, value); |
| 76 CHECK_EQ(table->NumberOfElements(), i + 1); | 78 CHECK_EQ(table->NumberOfElements(), i + 1); |
| 77 CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound); | 79 CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound); |
| 78 CHECK_EQ(table->Lookup(*key), *value); | 80 CHECK_EQ(table->Lookup(*key), *value); |
| 79 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); | 81 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); |
| 80 } | 82 } |
| 81 | 83 |
| 82 // Keys never added to the map which already have an identity hash | 84 // Keys never added to the map which already have an identity hash |
| 83 // code should not be found. | 85 // code should not be found. |
| 84 for (int i = 0; i < 100; i++) { | 86 for (int i = 0; i < 100; i++) { |
| 85 Handle<JSObject> key = FACTORY->NewJSArray(7); | 87 Handle<JSObject> key = factory->NewJSArray(7); |
| 86 CHECK(key->GetIdentityHash(ALLOW_CREATION)->ToObjectChecked()->IsSmi()); | 88 CHECK(key->GetIdentityHash(ALLOW_CREATION)->ToObjectChecked()->IsSmi()); |
| 87 CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound); | 89 CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound); |
| 88 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); | 90 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); |
| 89 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); | 91 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // Keys that don't have an identity hash should not be found and also | 94 // Keys that don't have an identity hash should not be found and also |
| 93 // should not get an identity hash code generated. | 95 // should not get an identity hash code generated. |
| 94 for (int i = 0; i < 100; i++) { | 96 for (int i = 0; i < 100; i++) { |
| 95 Handle<JSObject> key = FACTORY->NewJSArray(7); | 97 Handle<JSObject> key = factory->NewJSArray(7); |
| 96 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); | 98 CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value()); |
| 97 CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value()); | 99 CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value()); |
| 98 } | 100 } |
| 99 } | 101 } |
| 100 | 102 |
| 101 | 103 |
| 102 #ifdef DEBUG | 104 #ifdef DEBUG |
| 103 TEST(ObjectHashSetCausesGC) { | 105 TEST(ObjectHashSetCausesGC) { |
| 104 LocalContext context; | 106 LocalContext context; |
| 107 Isolate* isolate = Isolate::Current(); |
| 108 Factory* factory = isolate->factory(); |
| 105 v8::HandleScope scope(context->GetIsolate()); | 109 v8::HandleScope scope(context->GetIsolate()); |
| 106 Handle<ObjectHashSet> table = FACTORY->NewObjectHashSet(1); | 110 Handle<ObjectHashSet> table = factory->NewObjectHashSet(1); |
| 107 Handle<JSObject> key = FACTORY->NewJSArray(0); | 111 Handle<JSObject> key = factory->NewJSArray(0); |
| 108 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); | 112 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); |
| 109 | 113 |
| 110 // Force allocation of hash table backing store for hidden properties. | 114 // Force allocation of hash table backing store for hidden properties. |
| 111 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); | 115 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); |
| 112 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); | 116 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); |
| 113 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); | 117 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); |
| 114 | 118 |
| 115 // Simulate a full heap so that generating an identity hash code | 119 // Simulate a full heap so that generating an identity hash code |
| 116 // in subsequent calls will request GC. | 120 // in subsequent calls will request GC. |
| 117 SimulateFullSpace(HEAP->new_space()); | 121 SimulateFullSpace(HEAP->new_space()); |
| 118 SimulateFullSpace(HEAP->old_pointer_space()); | 122 SimulateFullSpace(HEAP->old_pointer_space()); |
| 119 | 123 |
| 120 // Calling Contains() should not cause GC ever. | 124 // Calling Contains() should not cause GC ever. |
| 121 CHECK(!table->Contains(*key)); | 125 CHECK(!table->Contains(*key)); |
| 122 | 126 |
| 123 // Calling Remove() should not cause GC ever. | 127 // Calling Remove() should not cause GC ever. |
| 124 CHECK(!table->Remove(*key)->IsFailure()); | 128 CHECK(!table->Remove(*key)->IsFailure()); |
| 125 | 129 |
| 126 // Calling Add() should request GC by returning a failure. | 130 // Calling Add() should request GC by returning a failure. |
| 127 CHECK(table->Add(*key)->IsRetryAfterGC()); | 131 CHECK(table->Add(*key)->IsRetryAfterGC()); |
| 128 } | 132 } |
| 129 #endif | 133 #endif |
| 130 | 134 |
| 131 | 135 |
| 132 #ifdef DEBUG | 136 #ifdef DEBUG |
| 133 TEST(ObjectHashTableCausesGC) { | 137 TEST(ObjectHashTableCausesGC) { |
| 134 LocalContext context; | 138 LocalContext context; |
| 139 Isolate* isolate = Isolate::Current(); |
| 140 Factory* factory = isolate->factory(); |
| 135 v8::HandleScope scope(context->GetIsolate()); | 141 v8::HandleScope scope(context->GetIsolate()); |
| 136 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(1); | 142 Handle<ObjectHashTable> table = factory->NewObjectHashTable(1); |
| 137 Handle<JSObject> key = FACTORY->NewJSArray(0); | 143 Handle<JSObject> key = factory->NewJSArray(0); |
| 138 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); | 144 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key); |
| 139 | 145 |
| 140 // Force allocation of hash table backing store for hidden properties. | 146 // Force allocation of hash table backing store for hidden properties. |
| 141 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); | 147 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); |
| 142 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); | 148 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); |
| 143 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); | 149 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); |
| 144 | 150 |
| 145 // Simulate a full heap so that generating an identity hash code | 151 // Simulate a full heap so that generating an identity hash code |
| 146 // in subsequent calls will request GC. | 152 // in subsequent calls will request GC. |
| 147 SimulateFullSpace(HEAP->new_space()); | 153 SimulateFullSpace(HEAP->new_space()); |
| 148 SimulateFullSpace(HEAP->old_pointer_space()); | 154 SimulateFullSpace(HEAP->old_pointer_space()); |
| 149 | 155 |
| 150 // Calling Lookup() should not cause GC ever. | 156 // Calling Lookup() should not cause GC ever. |
| 151 CHECK(table->Lookup(*key)->IsTheHole()); | 157 CHECK(table->Lookup(*key)->IsTheHole()); |
| 152 | 158 |
| 153 // Calling Put() should request GC by returning a failure. | 159 // Calling Put() should request GC by returning a failure. |
| 154 CHECK(table->Put(*key, *key)->IsRetryAfterGC()); | 160 CHECK(table->Put(*key, *key)->IsRetryAfterGC()); |
| 155 } | 161 } |
| 156 #endif | 162 #endif |
| OLD | NEW |