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

LibJS: Add all of the FinalizationRegistry.prototype methods

More specifically: cleanupSome, register & unregister.

FinalizationRegistery.prototype.cleanupSome is actually still a stage 2
proposal, but since test262 test cases already exist for it, i decided
to go for it :)
This commit is contained in:
Idan Horowitz 2021-06-15 22:21:24 +03:00 committed by Linus Groh
parent de9fa6622a
commit e1b0719435
7 changed files with 174 additions and 0 deletions

View file

@ -0,0 +1,35 @@
test("length is 0", () => {
expect(FinalizationRegistry.prototype.cleanupSome).toHaveLength(0);
});
function registerInDifferentScope(registry) {
registry.register({}, {});
}
test("basic functionality", () => {
var registry = new FinalizationRegistry(() => {});
var count = 0;
var increment = () => {
count++;
};
registry.cleanupSome(increment);
expect(count).toBe(0);
registerInDifferentScope(registry);
gc();
registry.cleanupSome(increment);
expect(count).toBe(1);
});
test("errors", () => {
var registry = new FinalizationRegistry(() => {});
expect(() => {
registry.cleanupSome(5);
}).toThrowWithMessage(TypeError, "is not a function");
});

View file

@ -0,0 +1,35 @@
test("length is 2", () => {
expect(FinalizationRegistry.prototype.register).toHaveLength(2);
});
test("basic functionality", () => {
var registry = new FinalizationRegistry(() => {});
var target1 = {};
var heldValue1 = {};
registry.register(target1, heldValue1);
var target2 = {};
var heldValue2 = {};
var token = {};
registry.register(target2, heldValue2, token);
});
test("errors", () => {
var registry = new FinalizationRegistry(() => {});
expect(() => {
registry.register(5, {});
}).toThrowWithMessage(TypeError, "is not an object");
expect(() => {
var a = {};
registry.register(a, a);
}).toThrowWithMessage(TypeError, "Target and held value must not be the same");
expect(() => {
registry.register({}, {}, 5);
}).toThrowWithMessage(TypeError, "is not an object");
});

View file

@ -0,0 +1,25 @@
test("length is 2", () => {
expect(FinalizationRegistry.prototype.unregister).toHaveLength(1);
});
test("basic functionality", () => {
var registry = new FinalizationRegistry(() => {});
var target = {};
var heldValue = {};
var token = {};
registry.register(target, heldValue, token);
expect(registry.unregister({})).toBe(false);
expect(registry.unregister(token)).toBe(true);
expect(registry.unregister(token)).toBe(false);
});
test("errors", () => {
var registry = new FinalizationRegistry(() => {});
expect(() => {
registry.unregister(5);
}).toThrowWithMessage(TypeError, "is not an object");
});