Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions tests/finalization_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ class Foo {
}
}

let cleaned = "";
const DROPPED = 200;
const KEPT = -1;

// how many times the callback has run for each held value; KEPT's count is in keptCalls
let calls: int[] = [];
let keptCalls = 0;
let total = 0;

function onCleanup(held: int) {
if (held == KEPT) {
keptCalls++;
} else {
calls[held] = calls[held] + 1;
}

total++;
}

// in a function of its own, so no pointer to the object stays in main's frame for the collector's
// conservative stack scan to find (see weakref_basic.ts)
function registerDropped(registry: FinalizationRegistry<string, Foo>, held: string, withToken: boolean) {
const obj = new Foo(1);
function registerDropped(registry: FinalizationRegistry<int, Foo>, held: int, withToken: boolean) {
const obj = new Foo(held);
if (withToken) {
registry.register(obj, held, obj);
} else {
Expand All @@ -27,31 +43,50 @@ function clearStack(depth: int): int {
return depth > 0 ? clearStack(depth - 1) + a + b + c + d : 0;
}

function countFreed(): int {
let freed = 0;
for (let i = 0; i < DROPPED; i++) {
assert(calls[i] <= 1, "a callback for each freed target, once");
if (calls[i] == 1) freed++;
}

return freed;
}

function main() {
const registry = new FinalizationRegistry<string, Foo>((held: string) => { cleaned += held; });
registerDropped(registry, "a", false);
registerDropped(registry, "b", true);
for (let i = 0; i < DROPPED; i++) {
calls.push(0);
}

const kept = new Foo(2);
registry.register(kept, "k", kept);
const registry = new FinalizationRegistry<int, Foo>(onCleanup);
// every other one with itself as the unregister token
for (let i = 0; i < DROPPED; i++) {
registerDropped(registry, i, i % 2 == 1);
}

// nothing has collected yet
registry.cleanupSome();
assert(cleaned == "", "no callback before a collection");
const kept = new Foo(2);
registry.register(kept, KEPT, kept);

clearStack(200);
const before = total;
GC_gcollect();
GC_gcollect();

// the callbacks run when the registry is next used, never on their own
assert(cleaned == "", "no callback until the registry is used");
assert(total == before, "no callback until the registry is used");
registry.cleanupSome();
assert(cleaned.length == 2 && cleaned.indexOf("a") >= 0 && cleaned.indexOf("b") >= 0,
"a callback for each freed target, once");

// not all of them: the collector scans the stack conservatively, and a stray word there can keep
// one alive (see weakmap_weakset.ts)
const freed = countFreed();
assert(freed >= DROPPED * 9 / 10, "a callback for the freed targets");
assert(total == freed, "and for nothing else");

registry.cleanupSome();
assert(cleaned.length == 2, "not again");
assert(total == freed && countFreed() == freed, "not again");

// a held object is not freed, and unregister forgets it
assert(keptCalls == 0, "no callback for a held target");
assert(registry.unregister(kept), "unregister finds the token");
assert(!registry.unregister(kept), "and has nothing more for it");
assert(kept.value == 2);
Expand Down
Loading