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
34using namespace v8::internal;
35
36
37static 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
49static void PutIntoWeakMap(Handle<JSWeakMap> weakmap,
50                           Handle<JSObject> key,
51                           int value) {
52  Handle<ObjectHashTable> table = PutIntoObjectHashTable(
53      Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())),
54      Handle<JSObject>(JSObject::cast(*key)),
55      Handle<Smi>(Smi::FromInt(value)));
56  weakmap->set_table(*table);
57}
58
59static int NumberOfWeakCalls = 0;
60static void WeakPointerCallback(v8::Persistent<v8::Value> handle, void* id) {
61  ASSERT(id == reinterpret_cast<void*>(1234));
62  NumberOfWeakCalls++;
63  handle.Dispose();
64}
65
66
67TEST(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, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
89
90  // Force a full GC.
91  HEAP->CollectAllGarbage(false);
92  CHECK_EQ(0, NumberOfWeakCalls);
93  CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
94  CHECK_EQ(
95      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
96
97  // Make the global reference to the key weak.
98  {
99    v8::HandleScope scope;
100    global_handles->MakeWeak(key.location(),
101                             reinterpret_cast<void*>(1234),
102                             &WeakPointerCallback);
103  }
104  CHECK(global_handles->IsWeak(key.location()));
105
106  // Force a full GC.
107  // Perform two consecutive GCs because the first one will only clear
108  // weak references whereas the second one will also clear weak maps.
109  HEAP->CollectAllGarbage(false);
110  CHECK_EQ(1, NumberOfWeakCalls);
111  CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
112  CHECK_EQ(
113      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
114  HEAP->CollectAllGarbage(false);
115  CHECK_EQ(1, NumberOfWeakCalls);
116  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
117  CHECK_EQ(
118      1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
119}
120
121
122TEST(Shrinking) {
123  LocalContext context;
124  v8::HandleScope scope;
125  Handle<JSWeakMap> weakmap = AllocateJSWeakMap();
126
127  // Check initial capacity.
128  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
129
130  // Fill up weak map to trigger capacity change.
131  {
132    v8::HandleScope scope;
133    Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
134    for (int i = 0; i < 32; i++) {
135      Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map);
136      PutIntoWeakMap(weakmap, object, i);
137    }
138  }
139
140  // Check increased capacity.
141  CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
142
143  // Force a full GC.
144  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
145  CHECK_EQ(
146      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
147  HEAP->CollectAllGarbage(false);
148  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
149  CHECK_EQ(
150      32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
151
152  // Check shrunk capacity.
153  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
154}
155