1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:07:35 +00:00

LibJS: Remove "uprooting" mechanism from garbage collector

The Heap::uproot_cell() API was used to implement markAsGarbage() which
was used in 3 tests to forcibly destroy a value, even if it had
references on the stack or elsewhere.

This patch rewrites the 3 tests that used this mechanism to be
structured in a way that allows garbage collection to collect the values
as intended without hacks. And now that the uprooting mechanism is no
longer needed, it's uprooted as well.

This fixes 3 test-js tests in bytecode mode. :^)
This commit is contained in:
Andreas Kling 2023-07-21 10:09:22 +02:00
parent 9054b1bc14
commit 6232ad3a0d
6 changed files with 28 additions and 65 deletions

View file

@ -3,9 +3,7 @@ test("length is 0", () => {
});
function registerInDifferentScope(registry) {
const target = {};
registry.register(target, {});
return target;
registry.register({}, {});
}
test("basic functionality", () => {
@ -20,8 +18,7 @@ test("basic functionality", () => {
expect(count).toBe(0);
const target = registerInDifferentScope(registry);
markAsGarbage("target");
registerInDifferentScope(registry);
gc();
registry.cleanupSome(increment);

View file

@ -1,3 +1,9 @@
function registerInDifferentScope(registry) {
const target = {};
registry.register(target, {});
eval("");
}
test("basic functionality", () => {
expect(WeakMap.prototype.set).toHaveLength(2);
@ -21,25 +27,26 @@ test("invalid values", () => {
});
});
function setObjectKey(weakMap) {
expect(weakMap.set({ e: 3 }, 1)).toBe(weakMap);
}
function setSymbolKey(weakMap) {
expect(weakMap.set(Symbol("foo"), "bar")).toBe(weakMap);
}
test("automatic removal of garbage-collected values", () => {
const weakMap = new WeakMap();
const objectKey = { e: 3 };
expect(weakMap.set(objectKey, 1)).toBe(weakMap);
setObjectKey(weakMap);
expect(getWeakMapSize(weakMap)).toBe(1);
markAsGarbage("objectKey");
gc();
expect(getWeakMapSize(weakMap)).toBe(0);
const symbolKey = Symbol("foo");
expect(weakMap.set(symbolKey, "bar")).toBe(weakMap);
setSymbolKey(weakMap);
expect(getWeakMapSize(weakMap)).toBe(1);
markAsGarbage("symbolKey");
gc();
expect(getWeakMapSize(weakMap)).toBe(0);
});

View file

@ -16,25 +16,26 @@ test("invalid values", () => {
});
});
function addObjectItem(weakSet) {
weakSet.add({ a: 1 });
}
function addSymbolItem(weakSet) {
weakSet.add(Symbol("foo"));
}
test("automatic removal of garbage-collected values", () => {
const weakSet = new WeakSet();
const objectItem = { a: 1 };
expect(weakSet.add(objectItem)).toBe(weakSet);
addObjectItem(weakSet);
expect(getWeakSetSize(weakSet)).toBe(1);
markAsGarbage("objectItem");
gc();
expect(getWeakSetSize(weakSet)).toBe(0);
const symbolItem = Symbol("foo");
expect(weakSet.add(symbolItem)).toBe(weakSet);
addSymbolItem(weakSet);
expect(getWeakSetSize(weakSet)).toBe(1);
markAsGarbage("symbolItem");
gc();
expect(getWeakSetSize(weakSet)).toBe(0);
});