Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/cctest/test-weakmaps.cc

Issue 7553012: Prototype of mark-and-compact support for Harmony weak maps. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Removed commented stress test. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/cctest/cctest.status ('k') | test/mjsunit/harmony/weakmaps.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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);
109 CHECK_EQ(1, NumberOfWeakCalls);
110 CHECK_EQ(1, weakmap->table()->NumberOfElements());
111 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements());
112 HEAP->CollectAllGarbage(false);
113 CHECK_EQ(1, NumberOfWeakCalls);
114 CHECK_EQ(0, weakmap->table()->NumberOfElements());
115 CHECK_EQ(1, weakmap->table()->NumberOfDeletedElements());
116 }
117
118
119 TEST(Shrinking) {
120 LocalContext context;
121 v8::HandleScope scope;
122 Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
123
124 // Check initial capacity.
125 CHECK_EQ(32, weakmap->table()->Capacity());
126
127 // Fill up weak map to trigger capacity change.
128 {
129 v8::HandleScope scope;
130 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
131 for (int i = 0; i < 32; i++) {
132 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map);
133 PutIntoWeakMap(weakmap, object, i);
134 }
135 }
136
137 // Check increased capacity.
138 CHECK_EQ(128, weakmap->table()->Capacity());
139
140 // Force a full GC.
141 CHECK_EQ(32, weakmap->table()->NumberOfElements());
142 CHECK_EQ(0, weakmap->table()->NumberOfDeletedElements());
143 HEAP->CollectAllGarbage(false);
144 CHECK_EQ(0, weakmap->table()->NumberOfElements());
145 CHECK_EQ(32, weakmap->table()->NumberOfDeletedElements());
146
147 // Check shrunk capacity.
148 CHECK_EQ(32, weakmap->table()->Capacity());
149 }
OLDNEW
« no previous file with comments | « test/cctest/cctest.status ('k') | test/mjsunit/harmony/weakmaps.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698