OLD | NEW |
---|---|
(Empty) | |
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 "global-handles.h" | |
31 #include "snapshot.h" | |
32 #include "cctest.h" | |
33 | |
34 using namespace v8::internal; | |
35 | |
36 | |
37 static Handle<JSWeakMap> AllocateJSWeakMap() { | |
38 Handle<Map> map = FACTORY->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | |
39 Handle<JSObject> weakmap_obj = FACTORY->NewJSObjectFromMap(map); | |
40 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); | |
41 // Do not use handles for the hash table, it would make entries strong. | |
42 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); | |
43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | |
44 weakmap->set_table(table); | |
45 weakmap->set_next(Smi::FromInt(0)); | |
46 return weakmap; | |
47 } | |
48 | |
49 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | |
50 Handle<JSObject> key, | |
51 int value) { | |
52 Handle<ObjectHashTable> table = PutIntoObjectHashTable( | |
53 Handle<ObjectHashTable>(weakmap->table()), | |
54 Handle<JSObject>(JSObject::cast(*key)), | |
55 Handle<Smi>(Smi::FromInt(value))); | |
56 weakmap->set_table(*table); | |
57 } | |
58 | |
59 static int NumberOfWeakCalls = 0; | |
60 static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) { | |
61 ASSERT(id == reinterpret_cast<void*>(1234)); | |
62 NumberOfWeakCalls++; | |
63 handle.Dispose(); | |
64 } | |
65 | |
66 | |
67 TEST(Weakness) { | |
68 LocalContext context; | |
69 v8::HandleScope scope; | |
70 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | |
71 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | |
72 | |
73 // Keep global reference to the key. | |
74 Handle<Object> key; | |
75 { | |
76 v8::HandleScope scope; | |
77 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | |
78 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | |
79 key = global_handles->Create(*object); | |
80 } | |
81 CHECK(!global_handles->IsWeak(key.location())); | |
82 | |
83 // Put entry into weak map. | |
84 { | |
85 v8::HandleScope scope; | |
86 PutIntoWeakMap(weakmap, Handle<JSObject>(JSObject::cast(*key)), 23); | |
87 } | |
88 CHECK_EQ(1, weakmap->table()->NumberOfElements()); | |
89 | |
90 // Force a full GC. | |
91 HEAP->CollectAllGarbage(false); | |
92 CHECK_EQ(0, NumberOfWeakCalls); | |
93 CHECK_EQ(1, weakmap->table()->NumberOfElements()); | |
94 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements()); | |
95 | |
96 // Make the global reference to the key weak. | |
97 { | |
98 v8::HandleScope scope; | |
99 global_handles->MakeWeak(key.location(), | |
100 reinterpret_cast<void*>(1234), | |
101 &WeakPointerCallback); | |
102 } | |
103 CHECK(global_handles->IsWeak(key.location())); | |
104 | |
105 // Force a full GC. | |
106 // Perform two consecutive GCs because the first one will only clear | |
107 // weak references whereas the second one will also clear weak maps. | |
108 HEAP->CollectAllGarbage(false); | |
Vyacheslav Egorov (Chromium)
2011/08/02 16:32:00
You can add a check that key is still in the map a
Michael Starzinger
2011/08/03 08:47:05
Done.
| |
109 HEAP->CollectAllGarbage(false); | |
110 CHECK_EQ(1, NumberOfWeakCalls); | |
111 CHECK_EQ(0, weakmap->table()->NumberOfElements()); | |
112 CHECK_EQ(1, weakmap->table()->NumberOfDeletedElements()); | |
113 } | |
114 | |
115 | |
116 TEST(Shrinking) { | |
117 LocalContext context; | |
118 v8::HandleScope scope; | |
119 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | |
120 | |
121 // Check initial capacity. | |
122 CHECK_EQ(32, weakmap->table()->Capacity()); | |
123 | |
124 // Fill up weak map to trigger capacity change. | |
125 { | |
126 v8::HandleScope scope; | |
127 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | |
128 for (int i = 0; i < 32; i++) { | |
129 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | |
130 PutIntoWeakMap(weakmap, object, i); | |
131 } | |
132 } | |
133 | |
134 // Check increased capacity. | |
135 CHECK_EQ(128, weakmap->table()->Capacity()); | |
136 | |
137 // Force a full GC. | |
138 CHECK_EQ(32, weakmap->table()->NumberOfElements()); | |
139 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements()); | |
140 HEAP->CollectAllGarbage(false); | |
141 CHECK_EQ(0, weakmap->table()->NumberOfElements()); | |
142 CHECK_EQ(32, weakmap->table()->NumberOfDeletedElements()); | |
143 | |
144 // Check shrunk capacity. | |
145 // TODO (mstarzinger): Enable this as soon as we implement shrinking. | |
146 //CHECK_EQ(32, weakmap->table()->Capacity()); | |
Vyacheslav Egorov (Chromium)
2011/08/02 16:32:00
Instead of leaving commented code (which we do not
Michael Starzinger
2011/08/03 08:47:05
Done.
| |
147 } | |
OLD | NEW |