v8  3.14.5(node0.10.28)
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  v8::HandleScope scope;
44  LocalContext context;
45  Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(23);
46  Handle<JSObject> a = FACTORY->NewJSArray(7);
47  Handle<JSObject> b = FACTORY->NewJSArray(11);
48  table = PutIntoObjectHashTable(table, a, b);
49  CHECK_EQ(table->NumberOfElements(), 1);
50  CHECK_EQ(table->Lookup(*a), *b);
51  CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value());
52 
53  // Keys still have to be valid after objects were moved.
54  HEAP->CollectGarbage(NEW_SPACE);
55  CHECK_EQ(table->NumberOfElements(), 1);
56  CHECK_EQ(table->Lookup(*a), *b);
57  CHECK_EQ(table->Lookup(*b), HEAP->the_hole_value());
58 
59  // Keys that are overwritten should not change number of elements.
60  table = PutIntoObjectHashTable(table, a, FACTORY->NewJSArray(13));
61  CHECK_EQ(table->NumberOfElements(), 1);
62  CHECK_NE(table->Lookup(*a), *b);
63 
64  // Keys mapped to the hole should be removed permanently.
65  table = PutIntoObjectHashTable(table, a, FACTORY->the_hole_value());
66  CHECK_EQ(table->NumberOfElements(), 0);
67  CHECK_EQ(table->NumberOfDeletedElements(), 1);
68  CHECK_EQ(table->Lookup(*a), HEAP->the_hole_value());
69 
70  // Keys should map back to their respective values and also should get
71  // an identity hash code generated.
72  for (int i = 0; i < 100; i++) {
73  Handle<JSObject> key = FACTORY->NewJSArray(7);
74  Handle<JSObject> value = FACTORY->NewJSArray(11);
75  table = PutIntoObjectHashTable(table, key, value);
76  CHECK_EQ(table->NumberOfElements(), i + 1);
77  CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound);
78  CHECK_EQ(table->Lookup(*key), *value);
79  CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi());
80  }
81 
82  // Keys never added to the map which already have an identity hash
83  // code should not be found.
84  for (int i = 0; i < 100; i++) {
85  Handle<JSObject> key = FACTORY->NewJSArray(7);
86  CHECK(key->GetIdentityHash(ALLOW_CREATION)->ToObjectChecked()->IsSmi());
87  CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound);
88  CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value());
89  CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi());
90  }
91 
92  // Keys that don't have an identity hash should not be found and also
93  // should not get an identity hash code generated.
94  for (int i = 0; i < 100; i++) {
95  Handle<JSObject> key = FACTORY->NewJSArray(7);
96  CHECK_EQ(table->Lookup(*key), HEAP->the_hole_value());
97  CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value());
98  }
99 }
100 
101 
102 #ifdef DEBUG
103 TEST(ObjectHashSetCausesGC) {
104  v8::HandleScope scope;
105  LocalContext context;
106  Handle<ObjectHashSet> table = FACTORY->NewObjectHashSet(1);
107  Handle<JSObject> key = FACTORY->NewJSArray(0);
109 
110  // Force allocation of hash table backing store for hidden properties.
111  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
112  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
113  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
114 
115  // Simulate a full heap so that generating an identity hash code
116  // in subsequent calls will request GC.
117  FLAG_gc_interval = 0;
118 
119  // Calling Contains() should not cause GC ever.
120  CHECK(!table->Contains(*key));
121 
122  // Calling Remove() should not cause GC ever.
123  CHECK(!table->Remove(*key)->IsFailure());
124 
125  // Calling Add() should request GC by returning a failure.
126  CHECK(table->Add(*key)->IsRetryAfterGC());
127 }
128 #endif
129 
130 
131 #ifdef DEBUG
132 TEST(ObjectHashTableCausesGC) {
133  v8::HandleScope scope;
134  LocalContext context;
135  Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(1);
136  Handle<JSObject> key = FACTORY->NewJSArray(0);
138 
139  // Force allocation of hash table backing store for hidden properties.
140  key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
141  key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
142  key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
143 
144  // Simulate a full heap so that generating an identity hash code
145  // in subsequent calls will request GC.
146  FLAG_gc_interval = 0;
147 
148  // Calling Lookup() should not cause GC ever.
149  CHECK(table->Lookup(*key)->IsTheHole());
150 
151  // Calling Put() should request GC by returning a failure.
152  CHECK(table->Put(*key, *key)->IsRetryAfterGC());
153 }
154 #endif
#define CHECK_EQ(expected, value)
Definition: checks.h:219
#define CHECK(condition)
Definition: checks.h:56
V8EXPORT bool SetHiddenValue(Handle< String > key, Handle< Value > value)
Definition: api.cc:3305
#define CHECK_NE(unexpected, value)
Definition: checks.h:223
Handle< ObjectHashTable > PutIntoObjectHashTable(Handle< ObjectHashTable > table, Handle< Object > key, Handle< Object > value)
Definition: handles.cc:857
static Local< Context > ToLocal(v8::internal::Handle< v8::internal::Context > obj)
#define HEAP
Definition: isolate.h:1433
#define FACTORY
Definition: isolate.h:1434