blob: 14e5d69d43714bba76b9e66a6c96026590f60b88 [file] [log] [blame]
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#include "src/v8.h"
29#include "test/cctest/cctest.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/api.h"
32#include "src/debug.h"
33#include "src/execution.h"
34#include "src/factory.h"
35#include "src/global-handles.h"
36#include "src/macro-assembler.h"
37#include "src/objects.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000038
39using namespace v8::internal;
40
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041namespace {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010042
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043
44template<typename HashMap>
45static void TestHashMap(Handle<HashMap> table) {
46 Isolate* isolate = CcTest::i_isolate();
47 Factory* factory = isolate->factory();
48
49 Handle<JSObject> a = factory->NewJSArray(7);
50 Handle<JSObject> b = factory->NewJSArray(11);
51 table = HashMap::Put(table, a, b);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000052 CHECK_EQ(table->NumberOfElements(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 CHECK_EQ(table->Lookup(a), *b);
54 // When the key does not exist in the map, Lookup returns the hole.
55 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000056
57 // Keys still have to be valid after objects were moved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 CcTest::heap()->CollectGarbage(NEW_SPACE);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000059 CHECK_EQ(table->NumberOfElements(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 CHECK_EQ(table->Lookup(a), *b);
61 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000062
63 // Keys that are overwritten should not change number of elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064 table = HashMap::Put(table, a, factory->NewJSArray(13));
Ben Murdoch69a99ed2011-11-30 16:03:39 +000065 CHECK_EQ(table->NumberOfElements(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 CHECK_NE(table->Lookup(a), *b);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000067
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 // Keys that have been removed are mapped to the hole.
69 bool was_present = false;
70 table = HashMap::Remove(table, a, &was_present);
71 CHECK(was_present);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000072 CHECK_EQ(table->NumberOfElements(), 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 CHECK_EQ(table->Lookup(a), CcTest::heap()->the_hole_value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000074
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075 // Keys should map back to their respective values and also should get
76 // an identity hash code generated.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000077 for (int i = 0; i < 100; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 Handle<JSReceiver> key = factory->NewJSArray(7);
79 Handle<JSObject> value = factory->NewJSArray(11);
80 table = HashMap::Put(table, key, value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +000081 CHECK_EQ(table->NumberOfElements(), i + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 CHECK_NE(table->FindEntry(key), HashMap::kNotFound);
83 CHECK_EQ(table->Lookup(key), *value);
84 CHECK(key->GetIdentityHash()->IsSmi());
Ben Murdoch69a99ed2011-11-30 16:03:39 +000085 }
86
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087 // Keys never added to the map which already have an identity hash
88 // code should not be found.
89 for (int i = 0; i < 100; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 Handle<JSReceiver> key = factory->NewJSArray(7);
91 CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi());
92 CHECK_EQ(table->FindEntry(key), HashMap::kNotFound);
93 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
94 CHECK(key->GetIdentityHash()->IsSmi());
Ben Murdoch3ef787d2012-04-12 10:51:47 +010095 }
96
97 // Keys that don't have an identity hash should not be found and also
98 // should not get an identity hash code generated.
99 for (int i = 0; i < 100; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100 Handle<JSReceiver> key = factory->NewJSArray(7);
101 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
102 Object* identity_hash = key->GetIdentityHash();
103 CHECK_EQ(identity_hash, CcTest::heap()->undefined_value());
104 }
105}
106
107
108TEST(HashMap) {
109 LocalContext context;
110 v8::HandleScope scope(context->GetIsolate());
111 Isolate* isolate = CcTest::i_isolate();
112 TestHashMap(ObjectHashTable::New(isolate, 23));
113 TestHashMap(isolate->factory()->NewOrderedHashMap());
114}
115
116
117class ObjectHashTableTest: public ObjectHashTable {
118 public:
119 void insert(int entry, int key, int value) {
120 set(EntryToIndex(entry), Smi::FromInt(key));
121 set(EntryToIndex(entry) + 1, Smi::FromInt(value));
122 }
123
124 int lookup(int key) {
125 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate());
126 return Smi::cast(Lookup(key_obj))->value();
127 }
128
129 int capacity() {
130 return Capacity();
131 }
132};
133
134
135TEST(HashTableRehash) {
136 LocalContext context;
137 Isolate* isolate = CcTest::i_isolate();
138 v8::HandleScope scope(context->GetIsolate());
139 // Test almost filled table.
140 {
141 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
142 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
143 int capacity = t->capacity();
144 for (int i = 0; i < capacity - 1; i++) {
145 t->insert(i, i * i, i);
146 }
147 t->Rehash(handle(Smi::FromInt(0), isolate));
148 for (int i = 0; i < capacity - 1; i++) {
149 CHECK_EQ(i, t->lookup(i * i));
150 }
151 }
152 // Test half-filled table.
153 {
154 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
155 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
156 int capacity = t->capacity();
157 for (int i = 0; i < capacity / 2; i++) {
158 t->insert(i, i * i, i);
159 }
160 t->Rehash(handle(Smi::FromInt(0), isolate));
161 for (int i = 0; i < capacity / 2; i++) {
162 CHECK_EQ(i, t->lookup(i * i));
163 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000164 }
165}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100166
167
168#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169template<class HashSet>
170static void TestHashSetCausesGC(Handle<HashSet> table) {
171 Isolate* isolate = CcTest::i_isolate();
172 Factory* factory = isolate->factory();
173
174 Handle<JSObject> key = factory->NewJSArray(0);
175 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
176
177 // Force allocation of hash table backing store for hidden properties.
178 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
179 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
180 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100181
182 // Simulate a full heap so that generating an identity hash code
183 // in subsequent calls will request GC.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 SimulateFullSpace(CcTest::heap()->new_space());
185 SimulateFullSpace(CcTest::heap()->old_pointer_space());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100186
187 // Calling Contains() should not cause GC ever.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 int gc_count = isolate->heap()->gc_count();
189 CHECK(!table->Contains(key));
190 CHECK(gc_count == isolate->heap()->gc_count());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 // Calling Remove() will not cause GC in this case.
193 bool was_present = false;
194 table = HashSet::Remove(table, key, &was_present);
195 CHECK(!was_present);
196 CHECK(gc_count == isolate->heap()->gc_count());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100197
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 // Calling Add() should cause GC.
199 table = HashSet::Add(table, key);
200 CHECK(gc_count < isolate->heap()->gc_count());
201}
202
203
204TEST(ObjectHashSetCausesGC) {
205 i::FLAG_stress_compaction = false;
206 LocalContext context;
207 v8::HandleScope scope(context->GetIsolate());
208 Isolate* isolate = CcTest::i_isolate();
209 TestHashSetCausesGC(isolate->factory()->NewOrderedHashSet());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100210}
211#endif
212
213
214#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215template<class HashMap>
216static void TestHashMapCausesGC(Handle<HashMap> table) {
217 Isolate* isolate = CcTest::i_isolate();
218 Factory* factory = isolate->factory();
219
220 Handle<JSObject> key = factory->NewJSArray(0);
221 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
222
223 // Force allocation of hash table backing store for hidden properties.
224 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
225 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
226 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100227
228 // Simulate a full heap so that generating an identity hash code
229 // in subsequent calls will request GC.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 SimulateFullSpace(CcTest::heap()->new_space());
231 SimulateFullSpace(CcTest::heap()->old_pointer_space());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100232
233 // Calling Lookup() should not cause GC ever.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234 CHECK(table->Lookup(key)->IsTheHole());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235
236 // Calling Put() should request GC by returning a failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 int gc_count = isolate->heap()->gc_count();
238 HashMap::Put(table, key, key);
239 CHECK(gc_count < isolate->heap()->gc_count());
240}
241
242
243TEST(ObjectHashTableCausesGC) {
244 i::FLAG_stress_compaction = false;
245 LocalContext context;
246 v8::HandleScope scope(context->GetIsolate());
247 Isolate* isolate = CcTest::i_isolate();
248 TestHashMapCausesGC(ObjectHashTable::New(isolate, 1));
249 TestHashMapCausesGC(isolate->factory()->NewOrderedHashMap());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100250}
251#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252
253
254}