/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// A function for allocating a Kennel and a barker. Only allocating // PersistentRooteds on the heap, and in this function, helps ensure that the // conservative GC doesn't find stray references to the barker. Ugh.
MOZ_NEVER_INLINE static Kennel* Allocate(JSContext* cx) {
RootedObject barker(cx, JS_NewObject(cx, &BarkWhenTracedClass::class_)); if (!barker) { return nullptr;
}
returnnew Kennel(cx, barker);
}
// Do a GC, expecting |n| barkers to be finalized. staticbool GCFinalizesNBarkers(JSContext* cx, int n) { int preGCTrace = BarkWhenTracedClass::traceCount; int preGCFinalize = BarkWhenTracedClass::finalizeCount;
// Now that kennel and nowKennel are both deallocated, GC should not be // able to find the barker.
JS_GC(cx);
CHECK(BarkWhenTracedClass::finalizeCount == 1);
// Allocate a new, empty kennel.
mozilla::UniquePtr<Kennel> kennel2(new Kennel(cx));
// Assignment! ASTONISHING!
*kennel2 = *kennel;
// With both kennels referring to the same barker, it is held alive.
CHECK(GCFinalizesNBarkers(cx, 0));
kennel2 = nullptr;
// The destination of the assignment alone holds the barker alive.
CHECK(GCFinalizesNBarkers(cx, 0));
// Allocate a second barker.
kennel2 = mozilla::UniquePtr<Kennel>(Allocate(cx));
CHECK(kennel2.get());
*kennel = *kennel2;
// Nothing refers to the first kennel any more.
CHECK(GCFinalizesNBarkers(cx, 1));
kennel = nullptr;
kennel2 = nullptr;
// Now that kennel and kennel2 are both deallocated, GC should not be // able to find the barker.
JS_GC(cx);
CHECK(BarkWhenTracedClass::finalizeCount == 2);
// PersistentRooted instances can initialized in a separate step to allow for // global PersistentRooteds.
BEGIN_TEST(test_GlobalPersistentRooted) {
BarkWhenTracedClass::reset();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.