v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-dictionary.cc
Go to the documentation of this file.
1 // 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 
28 #include "v8.h"
29 
30 #include "api.h"
31 #include "debug.h"
32 #include "execution.h"
33 #include "factory.h"
34 #include "macro-assembler.h"
35 #include "objects.h"
36 #include "global-handles.h"
37 #include "cctest.h"
38 
39 using namespace v8::internal;
40 
41 
43  LocalContext context;
44  Isolate* isolate = CcTest::i_isolate();
45  Factory* factory = isolate->factory();
46  v8::HandleScope scope(context->GetIsolate());
47  Handle<ObjectHashTable> table = factory->NewObjectHashTable(23);
48  Handle<JSObject> a = factory->NewJSArray(7);
49  Handle<JSObject> b = factory->NewJSArray(11);
50  table = ObjectHashTable::Put(table, a, b);
51  CHECK_EQ(table->NumberOfElements(), 1);
52  CHECK_EQ(table->Lookup(*a), *b);
53  CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value());
54 
55  // Keys still have to be valid after objects were moved.
57  CHECK_EQ(table->NumberOfElements(), 1);
58  CHECK_EQ(table->Lookup(*a), *b);
59  CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value());
60 
61  // Keys that are overwritten should not change number of elements.
62  table = ObjectHashTable::Put(table, a, factory->NewJSArray(13));
63  CHECK_EQ(table->NumberOfElements(), 1);
64  CHECK_NE(table->Lookup(*a), *b);
65 
66  // Keys mapped to the hole should be removed permanently.
67  table = ObjectHashTable::Put(table, a, factory->the_hole_value());
68  CHECK_EQ(table->NumberOfElements(), 0);
69  CHECK_EQ(table->NumberOfDeletedElements(), 1);
70  CHECK_EQ(table->Lookup(*a), CcTest::heap()->the_hole_value());
71 
72  // Keys should map back to their respective values and also should get
73  // an identity hash code generated.
74  for (int i = 0; i < 100; i++) {
75  Handle<JSReceiver> key = factory->NewJSArray(7);
76  Handle<JSObject> value = factory->NewJSArray(11);
77  table = ObjectHashTable::Put(table, key, value);
78  CHECK_EQ(table->NumberOfElements(), i + 1);
79  CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound);
80  CHECK_EQ(table->Lookup(*key), *value);
81  CHECK(key->GetIdentityHash()->IsSmi());
82  }
83 
84  // Keys never added to the map which already have an identity hash
85  // code should not be found.
86  for (int i = 0; i < 100; i++) {
87  Handle<JSReceiver> key = factory->NewJSArray(7);
89  CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound);
90  CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value());
91  CHECK(key->GetIdentityHash()->IsSmi());
92  }
93 
94  // Keys that don't have an identity hash should not be found and also
95  // should not get an identity hash code generated.
96  for (int i = 0; i < 100; i++) {
97  Handle<JSReceiver> key = factory->NewJSArray(7);
98  CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value());
99  CHECK_EQ(key->GetIdentityHash(),
100  CcTest::heap()->undefined_value());
101  }
102 }
103 
104 
106  public:
107  void insert(int entry, int key, int value) {
108  set(EntryToIndex(entry), Smi::FromInt(key));
109  set(EntryToIndex(entry) + 1, Smi::FromInt(value));
110  }
111 
112  int lookup(int key) {
113  return Smi::cast(Lookup(Smi::FromInt(key)))->value();
114  }
115 
116  int capacity() {
117  return Capacity();
118  }
119 };
120 
121 
122 TEST(HashTableRehash) {
123  LocalContext context;
124  Isolate* isolate = CcTest::i_isolate();
125  Factory* factory = isolate->factory();
126  v8::HandleScope scope(context->GetIsolate());
127  // Test almost filled table.
128  {
129  Handle<ObjectHashTable> table = factory->NewObjectHashTable(100);
130  ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
131  int capacity = t->capacity();
132  for (int i = 0; i < capacity - 1; i++) {
133  t->insert(i, i * i, i);
134  }
135  t->Rehash(Smi::FromInt(0));
136  for (int i = 0; i < capacity - 1; i++) {
137  CHECK_EQ(i, t->lookup(i * i));
138  }
139  }
140  // Test half-filled table.
141  {
142  Handle<ObjectHashTable> table = factory->NewObjectHashTable(100);
143  ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
144  int capacity = t->capacity();
145  for (int i = 0; i < capacity / 2; i++) {
146  t->insert(i, i * i, i);
147  }
148  t->Rehash(Smi::FromInt(0));
149  for (int i = 0; i < capacity / 2; i++) {
150  CHECK_EQ(i, t->lookup(i * i));
151  }
152  }
153 }
154 
155 
156 #ifdef DEBUG
157 TEST(ObjectHashSetCausesGC) {
158  i::FLAG_stress_compaction = false;
159  LocalContext context;
160  Isolate* isolate = CcTest::i_isolate();
161  Factory* factory = isolate->factory();
162  v8::HandleScope scope(context->GetIsolate());
163  Handle<ObjectHashSet> table = factory->NewObjectHashSet(1);
164  Handle<JSObject> key = factory->NewJSArray(0);
166 
167  // Force allocation of hash table backing store for hidden properties.
168  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
169  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
170  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
171 
172  // Simulate a full heap so that generating an identity hash code
173  // in subsequent calls will request GC.
174  SimulateFullSpace(CcTest::heap()->new_space());
175  SimulateFullSpace(CcTest::heap()->old_pointer_space());
176 
177  // Calling Contains() should not cause GC ever.
178  int gc_count = isolate->heap()->gc_count();
179  CHECK(!table->Contains(*key));
180  CHECK(gc_count == isolate->heap()->gc_count());
181 
182  // Calling Remove() will not cause GC in this case.
183  table = ObjectHashSet::Remove(table, key);
184  CHECK(gc_count == isolate->heap()->gc_count());
185 
186  // Calling Add() should cause GC.
187  table = ObjectHashSet::Add(table, key);
188  CHECK(gc_count < isolate->heap()->gc_count());
189 }
190 #endif
191 
192 
193 #ifdef DEBUG
194 TEST(ObjectHashTableCausesGC) {
195  i::FLAG_stress_compaction = false;
196  LocalContext context;
197  Isolate* isolate = CcTest::i_isolate();
198  Factory* factory = isolate->factory();
199  v8::HandleScope scope(context->GetIsolate());
200  Handle<ObjectHashTable> table = factory->NewObjectHashTable(1);
201  Handle<JSObject> key = factory->NewJSArray(0);
203 
204  // Force allocation of hash table backing store for hidden properties.
205  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
206  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
207  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
208 
209  // Simulate a full heap so that generating an identity hash code
210  // in subsequent calls will request GC.
211  SimulateFullSpace(CcTest::heap()->new_space());
212  SimulateFullSpace(CcTest::heap()->old_pointer_space());
213 
214  // Calling Lookup() should not cause GC ever.
215  CHECK(table->Lookup(*key)->IsTheHole());
216 
217  // Calling Put() should request GC by returning a failure.
218  int gc_count = isolate->heap()->gc_count();
219  ObjectHashTable::Put(table, key, key);
220  CHECK(gc_count < isolate->heap()->gc_count());
221 }
222 #endif
Handle< ObjectHashTable > NewObjectHashTable(int at_least_space_for, MinimumCapacity capacity_option=USE_DEFAULT_MINIMUM_CAPACITY)
Definition: factory.cc:145
#define CHECK_EQ(expected, value)
Definition: checks.h:252
static Smi * FromInt(int value)
Definition: objects-inl.h:1209
static i::Heap * heap()
Definition: cctest.h:106
#define CHECK(condition)
Definition: checks.h:75
Factory * factory()
Definition: isolate.h:995
static Smi * cast(Object *object)
v8::Isolate * GetIsolate()
Definition: api.cc:5233
static Handle< ObjectHashSet > Add(Handle< ObjectHashSet > table, Handle< Object > key)
Definition: objects.cc:15697
void insert(int entry, int key, int value)
static Handle< ObjectHashSet > Remove(Handle< ObjectHashSet > table, Handle< Object > key)
Definition: objects.cc:15720
static Handle< Object > GetOrCreateIdentityHash(Handle< JSReceiver > object)
Definition: objects-inl.h:6342
static i::Isolate * i_isolate()
Definition: cctest.h:102
bool SetHiddenValue(Handle< String > key, Handle< Value > value)
Definition: api.cc:3661
Definition: v8.h:123
#define CHECK_NE(unexpected, value)
Definition: checks.h:256
bool CollectGarbage(AllocationSpace space, const char *gc_reason=NULL, const GCCallbackFlags gc_callback_flags=kNoGCCallbackFlags)
Definition: heap-inl.h:554
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
void Rehash(Key key)
Definition: objects.cc:14007
int gc_count() const
Definition: heap.h:1822
static Handle< ObjectHashTable > Put(Handle< ObjectHashTable > table, Handle< Object > key, Handle< Object > value)
Definition: objects.cc:15775
Handle< ObjectHashSet > NewObjectHashSet(int at_least_space_for)
Definition: factory.cc:136
Handle< JSArray > NewJSArray(ElementsKind elements_kind, int length, int capacity, ArrayStorageAllocationMode mode=INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE, PretenureFlag pretenure=NOT_TENURED)
Definition: factory.cc:1437