| 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 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
// Implementation of JS FinalizationRegistry objects. |
| 8 |
|
| 9 |
#include "builtin/FinalizationRegistryObject.h" |
| 10 |
|
| 11 |
#include "mozilla/ScopeExit.h" |
| 12 |
|
| 13 |
#include "jsapi.h" |
| 14 |
|
| 15 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 16 |
#include "vm/GlobalObject.h" |
| 17 |
#include "vm/Interpreter.h" |
| 18 |
|
| 19 |
#include "gc/GCContext-inl.h" |
| 20 |
#include "vm/JSObject-inl.h" |
| 21 |
#include "vm/NativeObject-inl.h" |
| 22 |
|
| 23 |
using namespace js; |
| 24 |
|
| 25 |
/////////////////////////////////////////////////////////////////////////// |
| 26 |
// FinalizationRecordObject |
| 27 |
|
| 28 |
const JSClass FinalizationRecordObject::class_ = { |
| 29 |
"FinalizationRecord", |
| 30 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount), |
| 31 |
}; |
| 32 |
|
| 33 |
/* static */ |
| 34 |
FinalizationRecordObject* FinalizationRecordObject::create( |
| 35 |
JSContext* cx, HandleFinalizationQueueObject queue, HandleValue heldValue) { |
| 36 |
MOZ_ASSERT(queue); |
| 37 |
|
| 38 |
auto record = NewObjectWithGivenProto<FinalizationRecordObject>(cx, nullptr); |
| 39 |
if (!record) { |
| 40 |
return nullptr; |
| 41 |
} |
| 42 |
|
| 43 |
MOZ_ASSERT(queue->compartment() == record->compartment()); |
| 44 |
|
| 45 |
record->initReservedSlot(QueueSlot, ObjectValue(*queue)); |
| 46 |
record->initReservedSlot(HeldValueSlot, heldValue); |
| 47 |
record->initReservedSlot(InMapSlot, BooleanValue(false)); |
| 48 |
|
| 49 |
return record; |
| 50 |
} |
| 51 |
|
| 52 |
FinalizationQueueObject* FinalizationRecordObject::queue() const { |
| 53 |
Value value = getReservedSlot(QueueSlot); |
| 54 |
if (value.isUndefined()) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
return &value.toObject().as<FinalizationQueueObject>(); |
| 58 |
} |
| 59 |
|
| 60 |
Value FinalizationRecordObject::heldValue() const { |
| 61 |
return getReservedSlot(HeldValueSlot); |
| 62 |
} |
| 63 |
|
| 64 |
bool FinalizationRecordObject::isRegistered() const { |
| 65 |
MOZ_ASSERT_IF(!queue(), heldValue().isUndefined()); |
| 66 |
return queue(); |
| 67 |
} |
| 68 |
|
| 69 |
bool FinalizationRecordObject::isInRecordMap() const { |
| 70 |
return getReservedSlot(InMapSlot).toBoolean(); |
| 71 |
} |
| 72 |
|
| 73 |
void FinalizationRecordObject::setInRecordMap(bool newValue) { |
| 74 |
MOZ_ASSERT(newValue != isInRecordMap()); |
| 75 |
setReservedSlot(InMapSlot, BooleanValue(newValue)); |
| 76 |
} |
| 77 |
|
| 78 |
void FinalizationRecordObject::clear() { |
| 79 |
MOZ_ASSERT(queue()); |
| 80 |
setReservedSlot(QueueSlot, UndefinedValue()); |
| 81 |
setReservedSlot(HeldValueSlot, UndefinedValue()); |
| 82 |
MOZ_ASSERT(!isRegistered()); |
| 83 |
} |
| 84 |
|
| 85 |
/////////////////////////////////////////////////////////////////////////// |
| 86 |
// FinalizationRegistrationsObject |
| 87 |
|
| 88 |
const JSClass FinalizationRegistrationsObject::class_ = { |
| 89 |
"FinalizationRegistrations", |
| 90 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_BACKGROUND_FINALIZE, |
| 91 |
&classOps_, |
| 92 |
JS_NULL_CLASS_SPEC, |
| 93 |
}; |
| 94 |
|
| 95 |
const JSClassOps FinalizationRegistrationsObject::classOps_ = { |
| 96 |
nullptr, // addProperty |
| 97 |
nullptr, // delProperty |
| 98 |
nullptr, // enumerate |
| 99 |
nullptr, // newEnumerate |
| 100 |
nullptr, // resolve |
| 101 |
nullptr, // mayResolve |
| 102 |
FinalizationRegistrationsObject::finalize, // finalize |
| 103 |
nullptr, // call |
| 104 |
nullptr, // construct |
| 105 |
FinalizationRegistrationsObject::trace, // trace |
| 106 |
}; |
| 107 |
|
| 108 |
/* static */ |
| 109 |
FinalizationRegistrationsObject* FinalizationRegistrationsObject::create( |
| 110 |
JSContext* cx) { |
| 111 |
auto records = cx->make_unique<WeakFinalizationRecordVector>(cx->zone()); |
| 112 |
if (!records) { |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
auto object = |
| 117 |
NewObjectWithGivenProto<FinalizationRegistrationsObject>(cx, nullptr); |
| 118 |
if (!object) { |
| 119 |
return nullptr; |
| 120 |
} |
| 121 |
|
| 122 |
InitReservedSlot(object, RecordsSlot, records.release(), |
| 123 |
MemoryUse::FinalizationRecordVector); |
| 124 |
|
| 125 |
return object; |
| 126 |
} |
| 127 |
|
| 128 |
/* static */ |
| 129 |
void FinalizationRegistrationsObject::trace(JSTracer* trc, JSObject* obj) { |
| 130 |
if (!trc->traceWeakEdges()) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
auto* self = &obj->as<FinalizationRegistrationsObject>(); |
| 135 |
if (WeakFinalizationRecordVector* records = self->records()) { |
| 136 |
TraceRange(trc, records->length(), records->begin(), |
| 137 |
"FinalizationRegistrationsObject records"); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* static */ |
| 142 |
void FinalizationRegistrationsObject::finalize(JS::GCContext* gcx, |
| 143 |
JSObject* obj) { |
| 144 |
auto* self = &obj->as<FinalizationRegistrationsObject>(); |
| 145 |
gcx->delete_(obj, self->records(), MemoryUse::FinalizationRecordVector); |
| 146 |
} |
| 147 |
|
| 148 |
inline WeakFinalizationRecordVector* |
| 149 |
FinalizationRegistrationsObject::records() { |
| 150 |
return static_cast<WeakFinalizationRecordVector*>(privatePtr()); |
| 151 |
} |
| 152 |
|
| 153 |
inline const WeakFinalizationRecordVector* |
| 154 |
FinalizationRegistrationsObject::records() const { |
| 155 |
return static_cast<const WeakFinalizationRecordVector*>(privatePtr()); |
| 156 |
} |
| 157 |
|
| 158 |
inline void* FinalizationRegistrationsObject::privatePtr() const { |
| 159 |
Value value = getReservedSlot(RecordsSlot); |
| 160 |
if (value.isUndefined()) { |
| 161 |
return nullptr; |
| 162 |
} |
| 163 |
void* ptr = value.toPrivate(); |
| 164 |
MOZ_ASSERT(ptr); |
| 165 |
return ptr; |
| 166 |
} |
| 167 |
|
| 168 |
inline bool FinalizationRegistrationsObject::isEmpty() const { |
| 169 |
MOZ_ASSERT(records()); |
| 170 |
return records()->empty(); |
| 171 |
} |
| 172 |
|
| 173 |
inline bool FinalizationRegistrationsObject::append( |
| 174 |
HandleFinalizationRecordObject record) { |
| 175 |
MOZ_ASSERT(records()); |
| 176 |
return records()->append(record); |
| 177 |
} |
| 178 |
|
| 179 |
inline void FinalizationRegistrationsObject::remove( |
| 180 |
HandleFinalizationRecordObject record) { |
| 181 |
MOZ_ASSERT(records()); |
| 182 |
records()->eraseIfEqual(record); |
| 183 |
} |
| 184 |
|
| 185 |
inline bool FinalizationRegistrationsObject::traceWeak(JSTracer* trc) { |
| 186 |
MOZ_ASSERT(records()); |
| 187 |
return records()->traceWeak(trc); |
| 188 |
} |
| 189 |
|
| 190 |
/////////////////////////////////////////////////////////////////////////// |
| 191 |
// FinalizationRegistryObject |
| 192 |
|
| 193 |
// Bug 1600300: FinalizationRegistryObject is foreground finalized so that |
| 194 |
// HeapPtr destructors never see referents with released arenas. When this is |
| 195 |
// fixed we may be able to make this background finalized again. |
| 196 |
const JSClass FinalizationRegistryObject::class_ = { |
| 197 |
"FinalizationRegistry", |
| 198 |
JSCLASS_HAS_CACHED_PROTO(JSProto_FinalizationRegistry) | |
| 199 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_FOREGROUND_FINALIZE, |
| 200 |
&classOps_, |
| 201 |
&classSpec_, |
| 202 |
}; |
| 203 |
|
| 204 |
const JSClass FinalizationRegistryObject::protoClass_ = { |
| 205 |
"FinalizationRegistry.prototype", |
| 206 |
JSCLASS_HAS_CACHED_PROTO(JSProto_FinalizationRegistry), |
| 207 |
JS_NULL_CLASS_OPS, |
| 208 |
&classSpec_, |
| 209 |
}; |
| 210 |
|
| 211 |
const JSClassOps FinalizationRegistryObject::classOps_ = { |
| 212 |
nullptr, // addProperty |
| 213 |
nullptr, // delProperty |
| 214 |
nullptr, // enumerate |
| 215 |
nullptr, // newEnumerate |
| 216 |
nullptr, // resolve |
| 217 |
nullptr, // mayResolve |
| 218 |
FinalizationRegistryObject::finalize, // finalize |
| 219 |
nullptr, // call |
| 220 |
nullptr, // construct |
| 221 |
FinalizationRegistryObject::trace, // trace |
| 222 |
}; |
| 223 |
|
| 224 |
const ClassSpec FinalizationRegistryObject::classSpec_ = { |
| 225 |
GenericCreateConstructor<construct, 1, gc::AllocKind::FUNCTION>, |
| 226 |
GenericCreatePrototype<FinalizationRegistryObject>, |
| 227 |
nullptr, |
| 228 |
nullptr, |
| 229 |
methods_, |
| 230 |
properties_, |
| 231 |
}; |
| 232 |
|
| 233 |
const JSFunctionSpec FinalizationRegistryObject::methods_[] = { |
| 234 |
JS_FN("register", register_, 2, 0), |
| 235 |
JS_FN("unregister", unregister, 1, 0), |
| 236 |
JS_FN("cleanupSome", cleanupSome, 0, 0), |
| 237 |
JS_FS_END, |
| 238 |
}; |
| 239 |
|
| 240 |
const JSPropertySpec FinalizationRegistryObject::properties_[] = { |
| 241 |
JS_STRING_SYM_PS(toStringTag, "FinalizationRegistry", JSPROP_READONLY), |
| 242 |
JS_PS_END, |
| 243 |
}; |
| 244 |
|
| 245 |
/* static */ |
| 246 |
bool FinalizationRegistryObject::construct(JSContext* cx, unsigned argc, |
| 247 |
Value* vp) { |
| 248 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 249 |
|
| 250 |
if (!ThrowIfNotConstructing(cx, args, "FinalizationRegistry")) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
RootedObject cleanupCallback( |
| 255 |
cx, ValueToCallable(cx, args.get(0), 1, NO_CONSTRUCT)); |
| 256 |
if (!cleanupCallback) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
RootedObject proto(cx); |
| 261 |
if (!GetPrototypeFromBuiltinConstructor( |
| 262 |
cx, args, JSProto_FinalizationRegistry, &proto)) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
Rooted<UniquePtr<ObjectWeakMap>> registrations( |
| 267 |
cx, cx->make_unique<ObjectWeakMap>(cx)); |
| 268 |
if (!registrations) { |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
RootedFinalizationQueueObject queue( |
| 273 |
cx, FinalizationQueueObject::create(cx, cleanupCallback)); |
| 274 |
if (!queue) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
RootedFinalizationRegistryObject registry( |
| 279 |
cx, NewObjectWithClassProto<FinalizationRegistryObject>(cx, proto)); |
| 280 |
if (!registry) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
registry->initReservedSlot(QueueSlot, ObjectValue(*queue)); |
| 285 |
InitReservedSlot(registry, RegistrationsSlot, registrations.release(), |
| 286 |
MemoryUse::FinalizationRegistryRegistrations); |
| 287 |
|
| 288 |
if (!cx->runtime()->gc.addFinalizationRegistry(cx, registry)) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
queue->setHasRegistry(true); |
| 293 |
|
| 294 |
args.rval().setObject(*registry); |
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
/* static */ |
| 299 |
void FinalizationRegistryObject::trace(JSTracer* trc, JSObject* obj) { |
| 300 |
// Trace the registrations weak map. At most this traces the |
| 301 |
// FinalizationRegistrationsObject values of the map; the contents of those |
| 302 |
// objects are weakly held and are not traced by this method. |
| 303 |
|
| 304 |
auto* registry = &obj->as<FinalizationRegistryObject>(); |
| 305 |
if (ObjectWeakMap* registrations = registry->registrations()) { |
| 306 |
registrations->trace(trc); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void FinalizationRegistryObject::traceWeak(JSTracer* trc) { |
| 311 |
// Trace and update the contents of the registrations weak map's values, which |
| 312 |
// are weakly held. |
| 313 |
|
| 314 |
MOZ_ASSERT(registrations()); |
| 315 |
for (ObjectWeakMap::Enum e(*registrations()); !e.empty(); e.popFront()) { |
| 316 |
auto* registrations = |
| 317 |
&e.front().value()->as<FinalizationRegistrationsObject>(); |
| 318 |
if (!registrations->traceWeak(trc)) { |
| 319 |
e.removeFront(); |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/* static */ |
| 325 |
void FinalizationRegistryObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 326 |
auto registry = &obj->as<FinalizationRegistryObject>(); |
| 327 |
|
| 328 |
// The queue's flag should have been updated by |
| 329 |
// GCRuntime::sweepFinalizationRegistries. |
| 330 |
MOZ_ASSERT_IF(registry->queue(), !registry->queue()->hasRegistry()); |
| 331 |
|
| 332 |
gcx->delete_(obj, registry->registrations(), |
| 333 |
MemoryUse::FinalizationRegistryRegistrations); |
| 334 |
} |
| 335 |
|
| 336 |
FinalizationQueueObject* FinalizationRegistryObject::queue() const { |
| 337 |
Value value = getReservedSlot(QueueSlot); |
| 338 |
if (value.isUndefined()) { |
| 339 |
return nullptr; |
| 340 |
} |
| 341 |
return &value.toObject().as<FinalizationQueueObject>(); |
| 342 |
} |
| 343 |
|
| 344 |
ObjectWeakMap* FinalizationRegistryObject::registrations() const { |
| 345 |
Value value = getReservedSlot(RegistrationsSlot); |
| 346 |
if (value.isUndefined()) { |
| 347 |
return nullptr; |
| 348 |
} |
| 349 |
return static_cast<ObjectWeakMap*>(value.toPrivate()); |
| 350 |
} |
| 351 |
|
| 352 |
// FinalizationRegistry.prototype.register(target, heldValue [, unregisterToken |
| 353 |
// ]) |
| 354 |
// https://tc39.es/proposal-weakrefs/#sec-finalization-registry.prototype.register |
| 355 |
/* static */ |
| 356 |
bool FinalizationRegistryObject::register_(JSContext* cx, unsigned argc, |
| 357 |
Value* vp) { |
| 358 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 359 |
|
| 360 |
// 1. Let finalizationRegistry be the this value. |
| 361 |
// 2. If Type(finalizationRegistry) is not Object, throw a TypeError |
| 362 |
// exception. |
| 363 |
// 3. If finalizationRegistry does not have a [[Cells]] internal slot, throw a |
| 364 |
// TypeError exception. |
| 365 |
if (!args.thisv().isObject() || |
| 366 |
!args.thisv().toObject().is<FinalizationRegistryObject>()) { |
| 367 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 368 |
JSMSG_NOT_A_FINALIZATION_REGISTRY, |
| 369 |
"Receiver of FinalizationRegistry.register call"); |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
RootedFinalizationRegistryObject registry( |
| 374 |
cx, &args.thisv().toObject().as<FinalizationRegistryObject>()); |
| 375 |
|
| 376 |
// 4. If Type(target) is not Object, throw a TypeError exception. |
| 377 |
if (!args.get(0).isObject()) { |
| 378 |
JS_ReportErrorNumberASCII( |
| 379 |
cx, GetErrorMessage, nullptr, JSMSG_OBJECT_REQUIRED, |
| 380 |
"target argument to FinalizationRegistry.register"); |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
RootedObject target(cx, &args[0].toObject()); |
| 385 |
|
| 386 |
// 5. If SameValue(target, heldValue), throw a TypeError exception. |
| 387 |
if (args.get(1).isObject() && &args.get(1).toObject() == target) { |
| 388 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 389 |
JSMSG_BAD_HELD_VALUE); |
| 390 |
return false; |
| 391 |
} |
| 392 |
|
| 393 |
HandleValue heldValue = args.get(1); |
| 394 |
|
| 395 |
// 6. If Type(unregisterToken) is not Object, |
| 396 |
// a. If unregisterToken is not undefined, throw a TypeError exception. |
| 397 |
if (!args.get(2).isUndefined() && !args.get(2).isObject()) { |
| 398 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 399 |
JSMSG_BAD_UNREGISTER_TOKEN, |
| 400 |
"FinalizationRegistry.register"); |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
RootedObject unregisterToken(cx); |
| 405 |
if (!args.get(2).isUndefined()) { |
| 406 |
unregisterToken = &args[2].toObject(); |
| 407 |
} |
| 408 |
|
| 409 |
// Create the finalization record representing this target and heldValue. |
| 410 |
Rooted<FinalizationQueueObject*> queue(cx, registry->queue()); |
| 411 |
Rooted<FinalizationRecordObject*> record( |
| 412 |
cx, FinalizationRecordObject::create(cx, queue, heldValue)); |
| 413 |
if (!record) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
// Add the record to the registrations if an unregister token was supplied. |
| 418 |
if (unregisterToken && |
| 419 |
!addRegistration(cx, registry, unregisterToken, record)) { |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
auto registrationsGuard = mozilla::MakeScopeExit([&] { |
| 424 |
if (unregisterToken) { |
| 425 |
removeRegistrationOnError(registry, unregisterToken, record); |
| 426 |
} |
| 427 |
}); |
| 428 |
|
| 429 |
// Fully unwrap the target to pass it to the GC. |
| 430 |
RootedObject unwrappedTarget(cx); |
| 431 |
unwrappedTarget = CheckedUnwrapDynamic(target, cx); |
| 432 |
if (!unwrappedTarget) { |
| 433 |
ReportAccessDenied(cx); |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
// If the target is a DOM wrapper, preserve it. |
| 438 |
if (!preserveDOMWrapper(cx, target)) { |
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
// Wrap the record into the compartment of the target. |
| 443 |
RootedObject wrappedRecord(cx, record); |
| 444 |
AutoRealm ar(cx, unwrappedTarget); |
| 445 |
if (!JS_WrapObject(cx, &wrappedRecord)) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
if (JS_IsDeadWrapper(wrappedRecord)) { |
| 450 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_DEAD_OBJECT); |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
// Register the record with the target. |
| 455 |
gc::GCRuntime* gc = &cx->runtime()->gc; |
| 456 |
if (!gc->registerWithFinalizationRegistry(cx, unwrappedTarget, |
| 457 |
wrappedRecord)) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
registrationsGuard.release(); |
| 462 |
args.rval().setUndefined(); |
| 463 |
return true; |
| 464 |
} |
| 465 |
|
| 466 |
/* static */ |
| 467 |
bool FinalizationRegistryObject::preserveDOMWrapper(JSContext* cx, |
| 468 |
HandleObject obj) { |
| 469 |
if (!MaybePreserveDOMWrapper(cx, obj)) { |
| 470 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 471 |
JSMSG_BAD_FINALIZATION_REGISTRY_OBJECT); |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
return true; |
| 476 |
} |
| 477 |
|
| 478 |
/* static */ |
| 479 |
bool FinalizationRegistryObject::addRegistration( |
| 480 |
JSContext* cx, HandleFinalizationRegistryObject registry, |
| 481 |
HandleObject unregisterToken, HandleFinalizationRecordObject record) { |
| 482 |
// Add the record to the list of records associated with this unregister |
| 483 |
// token. |
| 484 |
|
| 485 |
MOZ_ASSERT(unregisterToken); |
| 486 |
MOZ_ASSERT(registry->registrations()); |
| 487 |
|
| 488 |
auto& map = *registry->registrations(); |
| 489 |
Rooted<FinalizationRegistrationsObject*> recordsObject(cx); |
| 490 |
JSObject* obj = map.get(unregisterToken); |
| 491 |
if (obj) { |
| 492 |
recordsObject = &obj->as<FinalizationRegistrationsObject>(); |
| 493 |
} else { |
| 494 |
recordsObject = FinalizationRegistrationsObject::create(cx); |
| 495 |
if (!recordsObject || !map.put(unregisterToken, recordsObject)) { |
| 496 |
ReportOutOfMemory(cx); |
| 497 |
return false; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
if (!recordsObject->append(record)) { |
| 502 |
ReportOutOfMemory(cx); |
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
/* static */ void FinalizationRegistryObject::removeRegistrationOnError( |
| 510 |
HandleFinalizationRegistryObject registry, HandleObject unregisterToken, |
| 511 |
HandleFinalizationRecordObject record) { |
| 512 |
// Remove a registration if something went wrong before we added it to the |
| 513 |
// target zone's map. Note that this can't remove a registration after that |
| 514 |
// point. |
| 515 |
|
| 516 |
MOZ_ASSERT(unregisterToken); |
| 517 |
MOZ_ASSERT(registry->registrations()); |
| 518 |
JS::AutoAssertNoGC nogc; |
| 519 |
|
| 520 |
auto& map = *registry->registrations(); |
| 521 |
JSObject* obj = map.get(unregisterToken); |
| 522 |
MOZ_ASSERT(obj); |
| 523 |
auto records = &obj->as<FinalizationRegistrationsObject>(); |
| 524 |
records->remove(record); |
| 525 |
|
| 526 |
if (records->empty()) { |
| 527 |
map.remove(unregisterToken); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
// FinalizationRegistry.prototype.unregister ( unregisterToken ) |
| 532 |
// https://tc39.es/proposal-weakrefs/#sec-finalization-registry.prototype.unregister |
| 533 |
/* static */ |
| 534 |
bool FinalizationRegistryObject::unregister(JSContext* cx, unsigned argc, |
| 535 |
Value* vp) { |
| 536 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 537 |
|
| 538 |
// 1. Let finalizationRegistry be the this value. |
| 539 |
// 2. If Type(finalizationRegistry) is not Object, throw a TypeError |
| 540 |
// exception. |
| 541 |
// 3. If finalizationRegistry does not have a [[Cells]] internal slot, throw a |
| 542 |
// TypeError exception. |
| 543 |
if (!args.thisv().isObject() || |
| 544 |
!args.thisv().toObject().is<FinalizationRegistryObject>()) { |
| 545 |
JS_ReportErrorNumberASCII( |
| 546 |
cx, GetErrorMessage, nullptr, JSMSG_NOT_A_FINALIZATION_REGISTRY, |
| 547 |
"Receiver of FinalizationRegistry.unregister call"); |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
RootedFinalizationRegistryObject registry( |
| 552 |
cx, &args.thisv().toObject().as<FinalizationRegistryObject>()); |
| 553 |
|
| 554 |
// 4. If Type(unregisterToken) is not Object, throw a TypeError exception. |
| 555 |
if (!args.get(0).isObject()) { |
| 556 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 557 |
JSMSG_BAD_UNREGISTER_TOKEN, |
| 558 |
"FinalizationRegistry.unregister"); |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
RootedObject unregisterToken(cx, &args[0].toObject()); |
| 563 |
|
| 564 |
// 5. Let removed be false. |
| 565 |
bool removed = false; |
| 566 |
|
| 567 |
// 6. For each Record { [[Target]], [[HeldValue]], [[UnregisterToken]] } cell |
| 568 |
// that is an element of finalizationRegistry.[[Cells]], do |
| 569 |
// a. If SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then |
| 570 |
// i. Remove cell from finalizationRegistry.[[Cells]]. |
| 571 |
// ii. Set removed to true. |
| 572 |
|
| 573 |
RootedObject obj(cx, registry->registrations()->get(unregisterToken)); |
| 574 |
if (obj) { |
| 575 |
auto* records = obj->as<FinalizationRegistrationsObject>().records(); |
| 576 |
MOZ_ASSERT(records); |
| 577 |
MOZ_ASSERT(!records->empty()); |
| 578 |
for (FinalizationRecordObject* record : *records) { |
| 579 |
if (unregisterRecord(record)) { |
| 580 |
removed = true; |
| 581 |
} |
| 582 |
} |
| 583 |
registry->registrations()->remove(unregisterToken); |
| 584 |
} |
| 585 |
|
| 586 |
// 7. Return removed. |
| 587 |
args.rval().setBoolean(removed); |
| 588 |
return true; |
| 589 |
} |
| 590 |
|
| 591 |
/* static */ |
| 592 |
bool FinalizationRegistryObject::unregisterRecord( |
| 593 |
FinalizationRecordObject* record) { |
| 594 |
if (!record->isRegistered()) { |
| 595 |
return false; |
| 596 |
} |
| 597 |
|
| 598 |
// Clear the fields of this record; it will be removed from the target's |
| 599 |
// list when it is next swept. |
| 600 |
record->clear(); |
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
// FinalizationRegistry.prototype.cleanupSome ( [ callback ] ) |
| 605 |
// https://tc39.es/proposal-weakrefs/#sec-finalization-registry.prototype.cleanupSome |
| 606 |
bool FinalizationRegistryObject::cleanupSome(JSContext* cx, unsigned argc, |
| 607 |
Value* vp) { |
| 608 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 609 |
|
| 610 |
// 1. Let finalizationRegistry be the this value. |
| 611 |
// 2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]). |
| 612 |
if (!args.thisv().isObject() || |
| 613 |
!args.thisv().toObject().is<FinalizationRegistryObject>()) { |
| 614 |
JS_ReportErrorNumberASCII( |
| 615 |
cx, GetErrorMessage, nullptr, JSMSG_NOT_A_FINALIZATION_REGISTRY, |
| 616 |
"Receiver of FinalizationRegistry.cleanupSome call"); |
| 617 |
return false; |
| 618 |
} |
| 619 |
|
| 620 |
RootedFinalizationRegistryObject registry( |
| 621 |
cx, &args.thisv().toObject().as<FinalizationRegistryObject>()); |
| 622 |
|
| 623 |
// 3. If callback is not undefined and IsCallable(callback) is false, throw a |
| 624 |
// TypeError exception. |
| 625 |
RootedObject cleanupCallback(cx); |
| 626 |
if (!args.get(0).isUndefined()) { |
| 627 |
cleanupCallback = ValueToCallable(cx, args.get(0), -1, NO_CONSTRUCT); |
| 628 |
if (!cleanupCallback) { |
| 629 |
return false; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
RootedFinalizationQueueObject queue(cx, registry->queue()); |
| 634 |
if (!FinalizationQueueObject::cleanupQueuedRecords(cx, queue, |
| 635 |
cleanupCallback)) { |
| 636 |
return false; |
| 637 |
} |
| 638 |
|
| 639 |
args.rval().setUndefined(); |
| 640 |
return true; |
| 641 |
} |
| 642 |
|
| 643 |
/////////////////////////////////////////////////////////////////////////// |
| 644 |
// FinalizationQueueObject |
| 645 |
|
| 646 |
// Bug 1600300: FinalizationQueueObject is foreground finalized so that |
| 647 |
// HeapPtr destructors never see referents with released arenas. When this is |
| 648 |
// fixed we may be able to make this background finalized again. |
| 649 |
const JSClass FinalizationQueueObject::class_ = { |
| 650 |
"FinalizationQueue", |
| 651 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_FOREGROUND_FINALIZE, |
| 652 |
&classOps_, |
| 653 |
}; |
| 654 |
|
| 655 |
const JSClassOps FinalizationQueueObject::classOps_ = { |
| 656 |
nullptr, // addProperty |
| 657 |
nullptr, // delProperty |
| 658 |
nullptr, // enumerate |
| 659 |
nullptr, // newEnumerate |
| 660 |
nullptr, // resolve |
| 661 |
nullptr, // mayResolve |
| 662 |
FinalizationQueueObject::finalize, // finalize |
| 663 |
nullptr, // call |
| 664 |
nullptr, // construct |
| 665 |
FinalizationQueueObject::trace, // trace |
| 666 |
}; |
| 667 |
|
| 668 |
/* static */ |
| 669 |
FinalizationQueueObject* FinalizationQueueObject::create( |
| 670 |
JSContext* cx, HandleObject cleanupCallback) { |
| 671 |
MOZ_ASSERT(cleanupCallback); |
| 672 |
|
| 673 |
Rooted<UniquePtr<FinalizationRecordVector>> recordsToBeCleanedUp( |
| 674 |
cx, cx->make_unique<FinalizationRecordVector>(cx->zone())); |
| 675 |
if (!recordsToBeCleanedUp) { |
| 676 |
return nullptr; |
| 677 |
} |
| 678 |
|
| 679 |
Handle<PropertyName*> funName = cx->names().empty_; |
| 680 |
RootedFunction doCleanupFunction( |
| 681 |
cx, NewNativeFunction(cx, doCleanup, 0, funName, |
| 682 |
gc::AllocKind::FUNCTION_EXTENDED)); |
| 683 |
if (!doCleanupFunction) { |
| 684 |
return nullptr; |
| 685 |
} |
| 686 |
|
| 687 |
// It's problematic storing a CCW to a global in another compartment because |
| 688 |
// you don't know how far to unwrap it to get the original object |
| 689 |
// back. Instead store a CCW to a plain object in the same compartment as the |
| 690 |
// global (this uses Object.prototype). |
| 691 |
Rooted<JSObject*> hostDefinedData(cx); |
| 692 |
if (!GetObjectFromHostDefinedData(cx, &hostDefinedData)) { |
| 693 |
return nullptr; |
| 694 |
} |
| 695 |
|
| 696 |
FinalizationQueueObject* queue = |
| 697 |
NewObjectWithGivenProto<FinalizationQueueObject>(cx, nullptr); |
| 698 |
if (!queue) { |
| 699 |
return nullptr; |
| 700 |
} |
| 701 |
|
| 702 |
queue->initReservedSlot(CleanupCallbackSlot, ObjectValue(*cleanupCallback)); |
| 703 |
queue->initReservedSlot(HostDefinedDataSlot, |
| 704 |
JS::ObjectOrNullValue(hostDefinedData)); |
| 705 |
InitReservedSlot(queue, RecordsToBeCleanedUpSlot, |
| 706 |
recordsToBeCleanedUp.release(), |
| 707 |
MemoryUse::FinalizationRegistryRecordVector); |
| 708 |
queue->initReservedSlot(IsQueuedForCleanupSlot, BooleanValue(false)); |
| 709 |
queue->initReservedSlot(DoCleanupFunctionSlot, |
| 710 |
ObjectValue(*doCleanupFunction)); |
| 711 |
queue->initReservedSlot(HasRegistrySlot, BooleanValue(false)); |
| 712 |
|
| 713 |
doCleanupFunction->setExtendedSlot(DoCleanupFunction_QueueSlot, |
| 714 |
ObjectValue(*queue)); |
| 715 |
|
| 716 |
return queue; |
| 717 |
} |
| 718 |
|
| 719 |
/* static */ |
| 720 |
void FinalizationQueueObject::trace(JSTracer* trc, JSObject* obj) { |
| 721 |
auto queue = &obj->as<FinalizationQueueObject>(); |
| 722 |
|
| 723 |
if (FinalizationRecordVector* records = queue->recordsToBeCleanedUp()) { |
| 724 |
records->trace(trc); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/* static */ |
| 729 |
void FinalizationQueueObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 730 |
auto queue = &obj->as<FinalizationQueueObject>(); |
| 731 |
|
| 732 |
gcx->delete_(obj, queue->recordsToBeCleanedUp(), |
| 733 |
MemoryUse::FinalizationRegistryRecordVector); |
| 734 |
} |
| 735 |
|
| 736 |
void FinalizationQueueObject::setHasRegistry(bool newValue) { |
| 737 |
MOZ_ASSERT(hasRegistry() != newValue); |
| 738 |
|
| 739 |
// Suppress our assertions about touching grey things. It's OK for us to set a |
| 740 |
// boolean slot even if this object is gray. |
| 741 |
AutoTouchingGrayThings atgt; |
| 742 |
|
| 743 |
setReservedSlot(HasRegistrySlot, BooleanValue(newValue)); |
| 744 |
} |
| 745 |
|
| 746 |
bool FinalizationQueueObject::hasRegistry() const { |
| 747 |
return getReservedSlot(HasRegistrySlot).toBoolean(); |
| 748 |
} |
| 749 |
|
| 750 |
inline JSObject* FinalizationQueueObject::cleanupCallback() const { |
| 751 |
Value value = getReservedSlot(CleanupCallbackSlot); |
| 752 |
if (value.isUndefined()) { |
| 753 |
return nullptr; |
| 754 |
} |
| 755 |
return &value.toObject(); |
| 756 |
} |
| 757 |
|
| 758 |
JSObject* FinalizationQueueObject::getHostDefinedData() const { |
| 759 |
Value value = getReservedSlot(HostDefinedDataSlot); |
| 760 |
if (value.isUndefined()) { |
| 761 |
return nullptr; |
| 762 |
} |
| 763 |
return value.toObjectOrNull(); |
| 764 |
} |
| 765 |
|
| 766 |
FinalizationRecordVector* FinalizationQueueObject::recordsToBeCleanedUp() |
| 767 |
const { |
| 768 |
Value value = getReservedSlot(RecordsToBeCleanedUpSlot); |
| 769 |
if (value.isUndefined()) { |
| 770 |
return nullptr; |
| 771 |
} |
| 772 |
return static_cast<FinalizationRecordVector*>(value.toPrivate()); |
| 773 |
} |
| 774 |
|
| 775 |
bool FinalizationQueueObject::isQueuedForCleanup() const { |
| 776 |
return getReservedSlot(IsQueuedForCleanupSlot).toBoolean(); |
| 777 |
} |
| 778 |
|
| 779 |
JSFunction* FinalizationQueueObject::doCleanupFunction() const { |
| 780 |
Value value = getReservedSlot(DoCleanupFunctionSlot); |
| 781 |
if (value.isUndefined()) { |
| 782 |
return nullptr; |
| 783 |
} |
| 784 |
return &value.toObject().as<JSFunction>(); |
| 785 |
} |
| 786 |
|
| 787 |
void FinalizationQueueObject::queueRecordToBeCleanedUp( |
| 788 |
FinalizationRecordObject* record) { |
| 789 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 790 |
if (!recordsToBeCleanedUp()->append(record)) { |
| 791 |
oomUnsafe.crash("FinalizationQueueObject::queueRecordsToBeCleanedUp"); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
void FinalizationQueueObject::setQueuedForCleanup(bool value) { |
| 796 |
MOZ_ASSERT(value != isQueuedForCleanup()); |
| 797 |
setReservedSlot(IsQueuedForCleanupSlot, BooleanValue(value)); |
| 798 |
} |
| 799 |
|
| 800 |
/* static */ |
| 801 |
bool FinalizationQueueObject::doCleanup(JSContext* cx, unsigned argc, |
| 802 |
Value* vp) { |
| 803 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 804 |
|
| 805 |
RootedFunction callee(cx, &args.callee().as<JSFunction>()); |
| 806 |
|
| 807 |
Value value = callee->getExtendedSlot(DoCleanupFunction_QueueSlot); |
| 808 |
RootedFinalizationQueueObject queue( |
| 809 |
cx, &value.toObject().as<FinalizationQueueObject>()); |
| 810 |
|
| 811 |
queue->setQueuedForCleanup(false); |
| 812 |
return cleanupQueuedRecords(cx, queue); |
| 813 |
} |
| 814 |
|
| 815 |
// CleanupFinalizationRegistry ( finalizationRegistry [ , callback ] ) |
| 816 |
// https://tc39.es/proposal-weakrefs/#sec-cleanup-finalization-registry |
| 817 |
/* static */ |
| 818 |
bool FinalizationQueueObject::cleanupQueuedRecords( |
| 819 |
JSContext* cx, HandleFinalizationQueueObject queue, |
| 820 |
HandleObject callbackArg) { |
| 821 |
MOZ_ASSERT(cx->compartment() == queue->compartment()); |
| 822 |
|
| 823 |
// 2. If callback is undefined, set callback to |
| 824 |
// finalizationRegistry.[[CleanupCallback]]. |
| 825 |
RootedValue callback(cx); |
| 826 |
if (callbackArg) { |
| 827 |
callback.setObject(*callbackArg); |
| 828 |
} else { |
| 829 |
JSObject* cleanupCallback = queue->cleanupCallback(); |
| 830 |
MOZ_ASSERT(cleanupCallback); |
| 831 |
callback.setObject(*cleanupCallback); |
| 832 |
} |
| 833 |
|
| 834 |
// 3. While finalizationRegistry.[[Cells]] contains a Record cell such that |
| 835 |
// cell.[[WeakRefTarget]] is empty, then an implementation may perform the |
| 836 |
// following steps, |
| 837 |
// a. Choose any such cell. |
| 838 |
// b. Remove cell from finalizationRegistry.[[Cells]]. |
| 839 |
// c. Perform ? Call(callback, undefined, « cell.[[HeldValue]] »). |
| 840 |
|
| 841 |
RootedValue heldValue(cx); |
| 842 |
RootedValue rval(cx); |
| 843 |
FinalizationRecordVector* records = queue->recordsToBeCleanedUp(); |
| 844 |
while (!records->empty()) { |
| 845 |
FinalizationRecordObject* record = records->popCopy(); |
| 846 |
|
| 847 |
// Skip over records that have been unregistered. |
| 848 |
if (!record->isRegistered()) { |
| 849 |
continue; |
| 850 |
} |
| 851 |
|
| 852 |
heldValue.set(record->heldValue()); |
| 853 |
|
| 854 |
record->clear(); |
| 855 |
|
| 856 |
if (!Call(cx, callback, UndefinedHandleValue, heldValue, &rval)) { |
| 857 |
return false; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
return true; |
| 862 |
} |