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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 template<typename HashMap> | 44 template<typename HashMap> |
45 static void TestHashMap(Handle<HashMap> table) { | 45 static void TestHashMap(Handle<HashMap> table) { |
46 Isolate* isolate = CcTest::i_isolate(); | 46 Isolate* isolate = CcTest::i_isolate(); |
47 Factory* factory = isolate->factory(); | 47 Factory* factory = isolate->factory(); |
48 | 48 |
49 Handle<JSObject> a = factory->NewJSArray(7); | 49 Handle<JSObject> a = factory->NewJSArray(7); |
50 Handle<JSObject> b = factory->NewJSArray(11); | 50 Handle<JSObject> b = factory->NewJSArray(11); |
51 table = HashMap::Put(table, a, b); | 51 table = HashMap::Put(table, a, b); |
52 CHECK_EQ(table->NumberOfElements(), 1); | 52 CHECK_EQ(table->NumberOfElements(), 1); |
53 CHECK_EQ(table->Lookup(*a), *b); | 53 CHECK_EQ(table->Lookup(a), *b); |
54 CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value()); | 54 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value()); |
55 | 55 |
56 // Keys still have to be valid after objects were moved. | 56 // Keys still have to be valid after objects were moved. |
57 CcTest::heap()->CollectGarbage(NEW_SPACE); | 57 CcTest::heap()->CollectGarbage(NEW_SPACE); |
58 CHECK_EQ(table->NumberOfElements(), 1); | 58 CHECK_EQ(table->NumberOfElements(), 1); |
59 CHECK_EQ(table->Lookup(*a), *b); | 59 CHECK_EQ(table->Lookup(a), *b); |
60 CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value()); | 60 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value()); |
61 | 61 |
62 // Keys that are overwritten should not change number of elements. | 62 // Keys that are overwritten should not change number of elements. |
63 table = HashMap::Put(table, a, factory->NewJSArray(13)); | 63 table = HashMap::Put(table, a, factory->NewJSArray(13)); |
64 CHECK_EQ(table->NumberOfElements(), 1); | 64 CHECK_EQ(table->NumberOfElements(), 1); |
65 CHECK_NE(table->Lookup(*a), *b); | 65 CHECK_NE(table->Lookup(a), *b); |
66 | 66 |
67 // Keys mapped to the hole should be removed permanently. | 67 // Keys mapped to the hole should be removed permanently. |
68 table = HashMap::Put(table, a, factory->the_hole_value()); | 68 table = HashMap::Put(table, a, factory->the_hole_value()); |
69 CHECK_EQ(table->NumberOfElements(), 0); | 69 CHECK_EQ(table->NumberOfElements(), 0); |
70 CHECK_EQ(table->Lookup(*a), CcTest::heap()->the_hole_value()); | 70 CHECK_EQ(table->Lookup(a), CcTest::heap()->the_hole_value()); |
71 | 71 |
72 // 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 |
73 // an identity hash code generated. | 73 // an identity hash code generated. |
74 for (int i = 0; i < 100; i++) { | 74 for (int i = 0; i < 100; i++) { |
75 Handle<JSReceiver> key = factory->NewJSArray(7); | 75 Handle<JSReceiver> key = factory->NewJSArray(7); |
76 Handle<JSObject> value = factory->NewJSArray(11); | 76 Handle<JSObject> value = factory->NewJSArray(11); |
77 table = HashMap::Put(table, key, value); | 77 table = HashMap::Put(table, key, value); |
78 CHECK_EQ(table->NumberOfElements(), i + 1); | 78 CHECK_EQ(table->NumberOfElements(), i + 1); |
79 CHECK_NE(table->FindEntry(*key), HashMap::kNotFound); | 79 CHECK_NE(table->FindEntry(*key), HashMap::kNotFound); |
80 CHECK_EQ(table->Lookup(*key), *value); | 80 CHECK_EQ(table->Lookup(key), *value); |
81 CHECK(key->GetIdentityHash()->IsSmi()); | 81 CHECK(key->GetIdentityHash()->IsSmi()); |
82 } | 82 } |
83 | 83 |
84 // 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 |
85 // code should not be found. | 85 // code should not be found. |
86 for (int i = 0; i < 100; i++) { | 86 for (int i = 0; i < 100; i++) { |
87 Handle<JSReceiver> key = factory->NewJSArray(7); | 87 Handle<JSReceiver> key = factory->NewJSArray(7); |
88 CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi()); | 88 CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi()); |
89 CHECK_EQ(table->FindEntry(*key), HashMap::kNotFound); | 89 CHECK_EQ(table->FindEntry(*key), HashMap::kNotFound); |
90 CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value()); | 90 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value()); |
91 CHECK(key->GetIdentityHash()->IsSmi()); | 91 CHECK(key->GetIdentityHash()->IsSmi()); |
92 } | 92 } |
93 | 93 |
94 // 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 |
95 // should not get an identity hash code generated. | 95 // should not get an identity hash code generated. |
96 for (int i = 0; i < 100; i++) { | 96 for (int i = 0; i < 100; i++) { |
97 Handle<JSReceiver> key = factory->NewJSArray(7); | 97 Handle<JSReceiver> key = factory->NewJSArray(7); |
98 CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value()); | 98 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value()); |
99 CHECK_EQ(key->GetIdentityHash(), | 99 CHECK_EQ(key->GetIdentityHash(), |
100 CcTest::heap()->undefined_value()); | 100 CcTest::heap()->undefined_value()); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 TEST(HashMap) { | 105 TEST(HashMap) { |
106 LocalContext context; | 106 LocalContext context; |
107 v8::HandleScope scope(context->GetIsolate()); | 107 v8::HandleScope scope(context->GetIsolate()); |
108 Isolate* isolate = CcTest::i_isolate(); | 108 Isolate* isolate = CcTest::i_isolate(); |
109 TestHashMap(ObjectHashTable::New(isolate, 23)); | 109 TestHashMap(ObjectHashTable::New(isolate, 23)); |
110 TestHashMap(isolate->factory()->NewOrderedHashMap()); | 110 TestHashMap(isolate->factory()->NewOrderedHashMap()); |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 class ObjectHashTableTest: public ObjectHashTable { | 114 class ObjectHashTableTest: public ObjectHashTable { |
115 public: | 115 public: |
116 void insert(int entry, int key, int value) { | 116 void insert(int entry, int key, int value) { |
117 set(EntryToIndex(entry), Smi::FromInt(key)); | 117 set(EntryToIndex(entry), Smi::FromInt(key)); |
118 set(EntryToIndex(entry) + 1, Smi::FromInt(value)); | 118 set(EntryToIndex(entry) + 1, Smi::FromInt(value)); |
119 } | 119 } |
120 | 120 |
121 int lookup(int key) { | 121 int lookup(int key) { |
122 return Smi::cast(Lookup(Smi::FromInt(key)))->value(); | 122 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate()); |
| 123 return Smi::cast(Lookup(key_obj))->value(); |
123 } | 124 } |
124 | 125 |
125 int capacity() { | 126 int capacity() { |
126 return Capacity(); | 127 return Capacity(); |
127 } | 128 } |
128 }; | 129 }; |
129 | 130 |
130 | 131 |
131 TEST(HashTableRehash) { | 132 TEST(HashTableRehash) { |
132 LocalContext context; | 133 LocalContext context; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); | 219 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1")); |
219 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); | 220 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2")); |
220 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); | 221 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3")); |
221 | 222 |
222 // Simulate a full heap so that generating an identity hash code | 223 // Simulate a full heap so that generating an identity hash code |
223 // in subsequent calls will request GC. | 224 // in subsequent calls will request GC. |
224 SimulateFullSpace(CcTest::heap()->new_space()); | 225 SimulateFullSpace(CcTest::heap()->new_space()); |
225 SimulateFullSpace(CcTest::heap()->old_pointer_space()); | 226 SimulateFullSpace(CcTest::heap()->old_pointer_space()); |
226 | 227 |
227 // Calling Lookup() should not cause GC ever. | 228 // Calling Lookup() should not cause GC ever. |
228 CHECK(table->Lookup(*key)->IsTheHole()); | 229 CHECK(table->Lookup(key)->IsTheHole()); |
229 | 230 |
230 // Calling Put() should request GC by returning a failure. | 231 // Calling Put() should request GC by returning a failure. |
231 int gc_count = isolate->heap()->gc_count(); | 232 int gc_count = isolate->heap()->gc_count(); |
232 HashMap::Put(table, key, key); | 233 HashMap::Put(table, key, key); |
233 CHECK(gc_count < isolate->heap()->gc_count()); | 234 CHECK(gc_count < isolate->heap()->gc_count()); |
234 } | 235 } |
235 | 236 |
236 | 237 |
237 TEST(ObjectHashTableCausesGC) { | 238 TEST(ObjectHashTableCausesGC) { |
238 i::FLAG_stress_compaction = false; | 239 i::FLAG_stress_compaction = false; |
239 LocalContext context; | 240 LocalContext context; |
240 v8::HandleScope scope(context->GetIsolate()); | 241 v8::HandleScope scope(context->GetIsolate()); |
241 Isolate* isolate = CcTest::i_isolate(); | 242 Isolate* isolate = CcTest::i_isolate(); |
242 TestHashMapCausesGC(ObjectHashTable::New(isolate, 1)); | 243 TestHashMapCausesGC(ObjectHashTable::New(isolate, 1)); |
243 TestHashMapCausesGC(isolate->factory()->NewOrderedHashMap()); | 244 TestHashMapCausesGC(isolate->factory()->NewOrderedHashMap()); |
244 } | 245 } |
245 #endif | 246 #endif |
246 | 247 |
247 | 248 |
248 } | 249 } |
OLD | NEW |