Debian Codegraph @

js/src/jsapi-tests/testWeakMap.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "gc/Zone.h"
9 #include "js/Array.h" // JS::GetArrayLength
10 #include "js/Exception.h" // JS_IsExceptionPending
11 #include "js/GlobalObject.h" // JS_NewGlobalObject
12 #include "js/PropertyAndElement.h" // JS_DefineProperty
13 #include "js/WeakMap.h"
14 #include "jsapi-tests/tests.h"
15 #include "vm/Realm.h"
16
17 using namespace js;
18
19 static bool checkSize(JSContext* cx, JS::HandleObject map, uint32_t expected) {
20 JS::RootedObject keys(cx);
21 if (!JS_NondeterministicGetWeakMapKeys(cx, map, &keys)) {
22 return false;
23 }
24
25 uint32_t length;
26 if (!JS::GetArrayLength(cx, keys, &length)) {
27 return false;
28 }
29
30 return length == expected;
31 }
32
33 JSObject* keyDelegate = nullptr;
34
35 BEGIN_TEST(testWeakMap_basicOperations) {
36 JS::RootedObject map(cx, JS::NewWeakMapObject(cx));
37 CHECK(IsWeakMapObject(map));
38
39 JS::RootedValue key(cx, JS::ObjectOrNullValue(newKey()));
40 CHECK(!key.isNull());
41 CHECK(!JS::IsWeakMapObject(&key.toObject()));
42
43 JS::RootedValue r(cx);
44 CHECK(GetWeakMapEntry(cx, map, key, &r));
45 CHECK(r.isUndefined());
46
47 CHECK(checkSize(cx, map, 0));
48
49 JS::RootedValue val(cx, JS::Int32Value(1));
50 CHECK(SetWeakMapEntry(cx, map, key, val));
51
52 CHECK(GetWeakMapEntry(cx, map, key, &r));
53 CHECK(r == val);
54 CHECK(checkSize(cx, map, 1));
55
56 JS_GC(cx);
57
58 CHECK(GetWeakMapEntry(cx, map, key, &r));
59 CHECK(r == val);
60 CHECK(checkSize(cx, map, 1));
61
62 key.setUndefined();
63 JS_GC(cx);
64
65 CHECK(checkSize(cx, map, 0));
66
67 return true;
68 }
69
70 JSObject* newKey() { return JS_NewPlainObject(cx); }
71 END_TEST(testWeakMap_basicOperations)
72
73 BEGIN_TEST(testWeakMap_setWeakMapEntry_invalid_key) {
74 JS::RootedObject map(cx, JS::NewWeakMapObject(cx));
75 CHECK(IsWeakMapObject(map));
76 CHECK(checkSize(cx, map, 0));
77
78 JS::RootedString test(cx, JS_NewStringCopyZ(cx, "test"));
79 // sym is a Symbol in global Symbol registry and hence can't be used as a key.
80 JS::RootedSymbol sym(cx, JS::GetSymbolFor(cx, test));
81 JS::RootedValue key(cx, JS::SymbolValue(sym));
82 CHECK(!key.isUndefined());
83
84 JS::RootedValue val(cx, JS::Int32Value(1));
85
86 CHECK(!JS_IsExceptionPending(cx));
87 CHECK(SetWeakMapEntry(cx, map, key, val) == false);
88
89 CHECK(JS_IsExceptionPending(cx));
90 JS::Rooted<JS::Value> exn(cx);
91 CHECK(JS_GetPendingException(cx, &exn));
92 JS::Rooted<JSObject*> obj(cx, &exn.toObject());
93 JSErrorReport* err = JS_ErrorFromException(cx, obj);
94 CHECK(err->exnType == JSEXN_TYPEERR);
95
96 JS_ClearPendingException(cx);
97
98 JS::RootedValue r(cx);
99 CHECK(GetWeakMapEntry(cx, map, key, &r));
100 CHECK(r == JS::UndefinedValue());
101 CHECK(checkSize(cx, map, 0));
102
103 return true;
104 }
105 END_TEST(testWeakMap_setWeakMapEntry_invalid_key)
106
107 #ifdef NIGHTLY_BUILD
108 BEGIN_TEST(testWeakMap_basicOperations_symbols_as_keys) {
109 JS::RootedObject map(cx, JS::NewWeakMapObject(cx));
110 CHECK(IsWeakMapObject(map));
111
112 JS::RootedString test(cx, JS_NewStringCopyZ(cx, "test"));
113 JS::RootedSymbol sym(cx, JS::NewSymbol(cx, test));
114 CHECK(sym);
115 JS::RootedValue key(cx, JS::SymbolValue(sym));
116
117 JS::RootedValue r(cx);
118 CHECK(GetWeakMapEntry(cx, map, key, &r));
119 CHECK(r.isUndefined());
120
121 CHECK(checkSize(cx, map, 0));
122
123 JS::RootedValue val(cx, JS::Int32Value(1));
124 CHECK(SetWeakMapEntry(cx, map, key, val));
125
126 CHECK(GetWeakMapEntry(cx, map, key, &r));
127 CHECK(r == val);
128 CHECK(checkSize(cx, map, 1));
129
130 JS_GC(cx);
131
132 CHECK(GetWeakMapEntry(cx, map, key, &r));
133 CHECK(r == val);
134 CHECK(checkSize(cx, map, 1));
135
136 sym = nullptr;
137 key.setUndefined();
138 JS_GC(cx);
139
140 CHECK(checkSize(cx, map, 0));
141
142 return true;
143 }
144 END_TEST(testWeakMap_basicOperations_symbols_as_keys)
145 #endif
146
147 BEGIN_TEST(testWeakMap_keyDelegates) {
148 AutoLeaveZeal nozeal(cx);
149
150 AutoGCParameter param(cx, JSGC_INCREMENTAL_GC_ENABLED, true);
151 JS_GC(cx);
152 JS::RootedObject map(cx, JS::NewWeakMapObject(cx));
153 CHECK(map);
154
155 JS::RootedObject delegate(cx, newDelegate());
156 JS::RootedObject key(cx, delegate);
157 if (!JS_WrapObject(cx, &key)) {
158 return false;
159 }
160 CHECK(key);
161 CHECK(delegate);
162
163 keyDelegate = delegate;
164
165 JS::RootedObject delegateRoot(cx);
166 {
167 JSAutoRealm ar(cx, delegate);
168 delegateRoot = JS_NewPlainObject(cx);
169 CHECK(delegateRoot);
170 JS::RootedValue delegateValue(cx, JS::ObjectValue(*delegate));
171 CHECK(JS_DefineProperty(cx, delegateRoot, "delegate", delegateValue, 0));
172 }
173 delegate = nullptr;
174
175 /*
176 * Perform an incremental GC, introducing an unmarked CCW to force the map
177 * zone to finish marking before the delegate zone.
178 */
179 CHECK(newCCW(map, delegateRoot));
180 performIncrementalGC();
181 #ifdef DEBUG
182 CHECK(map->zone()->lastSweepGroupIndex() <
183 delegateRoot->zone()->lastSweepGroupIndex());
184 #endif
185
186 /* Add our entry to the weakmap. */
187 JS::RootedValue keyVal(cx, JS::ObjectValue(*key));
188 JS::RootedValue val(cx, JS::Int32Value(1));
189 CHECK(SetWeakMapEntry(cx, map, keyVal, val));
190 CHECK(checkSize(cx, map, 1));
191
192 /*
193 * Check the delegate keeps the entry alive even if the key is not reachable.
194 */
195 key = nullptr;
196 keyVal.setUndefined();
197 CHECK(newCCW(map, delegateRoot));
198 performIncrementalGC();
199 CHECK(checkSize(cx, map, 1));
200
201 /*
202 * Check that the zones finished marking at the same time, which is
203 * necessary because of the presence of the delegate and the CCW.
204 */
205 #ifdef DEBUG
206 CHECK(map->zone()->lastSweepGroupIndex() ==
207 delegateRoot->zone()->lastSweepGroupIndex());
208 #endif
209
210 /* Check that when the delegate becomes unreachable the entry is removed. */
211 delegateRoot = nullptr;
212 keyDelegate = nullptr;
213 JS_GC(cx);
214 CHECK(checkSize(cx, map, 0));
215
216 return true;
217 }
218
219 static size_t DelegateObjectMoved(JSObject* obj, JSObject* old) {
220 if (!keyDelegate) {
221 return 0; // Object got moved before we set keyDelegate to point to it.
222 }
223
224 MOZ_RELEASE_ASSERT(keyDelegate == old);
225 keyDelegate = obj;
226 return 0;
227 }
228
229 JSObject* newKey() {
230 static const JSClass keyClass = {
231 "keyWithDelegate", JSCLASS_HAS_RESERVED_SLOTS(1),
232 JS_NULL_CLASS_OPS, JS_NULL_CLASS_SPEC,
233 JS_NULL_CLASS_EXT, JS_NULL_OBJECT_OPS,
234 };
235
236 JS::RootedObject key(cx, JS_NewObject(cx, &keyClass));
237 if (!key) {
238 return nullptr;
239 }
240
241 return key;
242 }
243
244 JSObject* newCCW(JS::HandleObject sourceZone, JS::HandleObject destZone) {
245 /*
246 * Now ensure that this zone will be swept first by adding a cross
247 * compartment wrapper to a new object in the same zone as the
248 * delegate object.
249 */
250 JS::RootedObject object(cx);
251 {
252 JSAutoRealm ar(cx, destZone);
253 object = JS_NewPlainObject(cx);
254 if (!object) {
255 return nullptr;
256 }
257 }
258 {
259 JSAutoRealm ar(cx, sourceZone);
260 if (!JS_WrapObject(cx, &object)) {
261 return nullptr;
262 }
263 }
264
265 // In order to test the SCC algorithm, we need the wrapper/wrappee to be
266 // tenured.
267 cx->runtime()->gc.evictNursery();
268
269 return object;
270 }
271
272 JSObject* newDelegate() {
273 static const JSClassOps delegateClassOps = {
274 nullptr, // addProperty
275 nullptr, // delProperty
276 nullptr, // enumerate
277 nullptr, // newEnumerate
278 nullptr, // resolve
279 nullptr, // mayResolve
280 nullptr, // finalize
281 nullptr, // call
282 nullptr, // construct
283 JS_GlobalObjectTraceHook, // trace
284 };
285
286 static const js::ClassExtension delegateClassExtension = {
287 DelegateObjectMoved, // objectMovedOp
288 };
289
290 static const JSClass delegateClass = {
291 "delegate",
292 JSCLASS_GLOBAL_FLAGS | JSCLASS_HAS_RESERVED_SLOTS(1),
293 &delegateClassOps,
294 JS_NULL_CLASS_SPEC,
295 &delegateClassExtension,
296 JS_NULL_OBJECT_OPS,
297 };
298
299 /* Create the global object. */
300 JS::RealmOptions options;
301 JS::RootedObject global(cx,
302 JS_NewGlobalObject(cx, &delegateClass, nullptr,
303 JS::FireOnNewGlobalHook, options));
304 if (!global) {
305 return nullptr;
306 }
307
308 JS_SetReservedSlot(global, 0, JS::Int32Value(42));
309 return global;
310 }
311
312 void performIncrementalGC() {
313 JSRuntime* rt = cx->runtime();
314 JS::SliceBudget budget(JS::WorkBudget(1000));
315 rt->gc.startDebugGC(JS::GCOptions::Normal, budget);
316
317 // Wait until we've started marking before finishing the GC
318 // non-incrementally.
319 while (rt->gc.state() == gc::State::Prepare) {
320 rt->gc.debugGCSlice(budget);
321 }
322 if (JS::IsIncrementalGCInProgress(cx)) {
323 rt->gc.finishGC(JS::GCReason::DEBUG_GC);
324 }
325 }
326 END_TEST(testWeakMap_keyDelegates)