OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "global-handles.h" | 30 #include "global-handles.h" |
31 #include "snapshot.h" | 31 #include "snapshot.h" |
32 #include "cctest.h" | 32 #include "cctest.h" |
33 | 33 |
34 using namespace v8::internal; | 34 using namespace v8::internal; |
35 | 35 |
36 | 36 |
| 37 static Isolate* GetIsolateFrom(LocalContext* context) { |
| 38 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
| 39 } |
| 40 |
| 41 |
37 static Handle<JSWeakMap> AllocateJSWeakMap() { | 42 static Handle<JSWeakMap> AllocateJSWeakMap() { |
38 Handle<Map> map = FACTORY->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 43 Handle<Map> map = FACTORY->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
39 Handle<JSObject> weakmap_obj = FACTORY->NewJSObjectFromMap(map); | 44 Handle<JSObject> weakmap_obj = FACTORY->NewJSObjectFromMap(map); |
40 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); | 45 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); |
41 // Do not use handles for the hash table, it would make entries strong. | 46 // Do not use handles for the hash table, it would make entries strong. |
42 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); | 47 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); |
43 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | 48 ObjectHashTable* table = ObjectHashTable::cast(table_obj); |
44 weakmap->set_table(table); | 49 weakmap->set_table(table); |
45 weakmap->set_next(Smi::FromInt(0)); | 50 weakmap->set_next(Smi::FromInt(0)); |
46 return weakmap; | 51 return weakmap; |
(...skipping 17 matching lines...) Expand all Loading... |
64 NumberOfWeakCalls++; | 69 NumberOfWeakCalls++; |
65 handle.Dispose(isolate); | 70 handle.Dispose(isolate); |
66 } | 71 } |
67 | 72 |
68 | 73 |
69 TEST(Weakness) { | 74 TEST(Weakness) { |
70 FLAG_incremental_marking = false; | 75 FLAG_incremental_marking = false; |
71 LocalContext context; | 76 LocalContext context; |
72 v8::HandleScope scope; | 77 v8::HandleScope scope; |
73 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 78 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); |
74 GlobalHandles* global_handles = Isolate::Current()->global_handles(); | 79 GlobalHandles* global_handles = GetIsolateFrom(&context)->global_handles(); |
75 | 80 |
76 // Keep global reference to the key. | 81 // Keep global reference to the key. |
77 Handle<Object> key; | 82 Handle<Object> key; |
78 { | 83 { |
79 v8::HandleScope scope; | 84 v8::HandleScope scope; |
80 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 85 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
81 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 86 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
82 key = global_handles->Create(*object); | 87 key = global_handles->Create(*object); |
83 } | 88 } |
84 CHECK(!global_handles->IsWeak(key.location())); | 89 CHECK(!global_handles->IsWeak(key.location())); |
85 | 90 |
86 // Put entry into weak map. | 91 // Put entry into weak map. |
87 { | 92 { |
88 v8::HandleScope scope; | 93 v8::HandleScope scope; |
89 PutIntoWeakMap(weakmap, | 94 PutIntoWeakMap(weakmap, |
90 Handle<JSObject>(JSObject::cast(*key)), | 95 Handle<JSObject>(JSObject::cast(*key)), |
91 Handle<Smi>(Smi::FromInt(23))); | 96 Handle<Smi>(Smi::FromInt(23), GetIsolateFrom(&context))); |
92 } | 97 } |
93 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 98 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
94 | 99 |
95 // Force a full GC. | 100 // Force a full GC. |
96 HEAP->CollectAllGarbage(false); | 101 HEAP->CollectAllGarbage(false); |
97 CHECK_EQ(0, NumberOfWeakCalls); | 102 CHECK_EQ(0, NumberOfWeakCalls); |
98 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 103 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
99 CHECK_EQ( | 104 CHECK_EQ( |
100 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 105 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
101 | 106 |
(...skipping 30 matching lines...) Expand all Loading... |
132 | 137 |
133 // Check initial capacity. | 138 // Check initial capacity. |
134 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 139 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
135 | 140 |
136 // Fill up weak map to trigger capacity change. | 141 // Fill up weak map to trigger capacity change. |
137 { | 142 { |
138 v8::HandleScope scope; | 143 v8::HandleScope scope; |
139 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 144 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
140 for (int i = 0; i < 32; i++) { | 145 for (int i = 0; i < 32; i++) { |
141 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 146 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); |
142 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i))); | 147 PutIntoWeakMap(weakmap, object, |
| 148 Handle<Smi>(Smi::FromInt(i), GetIsolateFrom(&context))); |
143 } | 149 } |
144 } | 150 } |
145 | 151 |
146 // Check increased capacity. | 152 // Check increased capacity. |
147 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 153 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
148 | 154 |
149 // Force a full GC. | 155 // Force a full GC. |
150 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 156 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
151 CHECK_EQ( | 157 CHECK_EQ( |
152 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 158 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 217 |
212 // Fill up weak map with keys on an evacuation candidate. | 218 // Fill up weak map with keys on an evacuation candidate. |
213 Handle<JSObject> keys[32]; | 219 Handle<JSObject> keys[32]; |
214 for (int i = 0; i < 32; i++) { | 220 for (int i = 0; i < 32; i++) { |
215 keys[i] = FACTORY->NewJSObject(function, TENURED); | 221 keys[i] = FACTORY->NewJSObject(function, TENURED); |
216 CHECK(!HEAP->InNewSpace(keys[i]->address())); | 222 CHECK(!HEAP->InNewSpace(keys[i]->address())); |
217 CHECK(!first_page->Contains(keys[i]->address())); | 223 CHECK(!first_page->Contains(keys[i]->address())); |
218 } | 224 } |
219 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 225 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); |
220 for (int i = 0; i < 32; i++) { | 226 for (int i = 0; i < 32; i++) { |
221 PutIntoWeakMap(weakmap, keys[i], Handle<Smi>(Smi::FromInt(i))); | 227 PutIntoWeakMap(weakmap, |
| 228 keys[i], |
| 229 Handle<Smi>(Smi::FromInt(i), GetIsolateFrom(&context))); |
222 } | 230 } |
223 | 231 |
224 // Force compacting garbage collection. The subsequent collections are used | 232 // Force compacting garbage collection. The subsequent collections are used |
225 // to verify that key references were actually updated. | 233 // to verify that key references were actually updated. |
226 CHECK(FLAG_always_compact); | 234 CHECK(FLAG_always_compact); |
227 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 235 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
228 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 236 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
229 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 237 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
230 } | 238 } |
OLD | NEW |