1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Implement WeakRef changes from 'Symbol as WeakMap Keys Proposal'

This commit is contained in:
Idan Horowitz 2022-06-22 23:09:59 +03:00
parent dbd0110721
commit 53ed8decaf
6 changed files with 69 additions and 26 deletions

View file

@ -8,7 +8,7 @@ describe("errors", () => {
[-100, Infinity, NaN, 152n, undefined].forEach(value => {
expect(() => {
new WeakRef(value);
}).toThrowWithMessage(TypeError, "is not an object");
}).toThrowWithMessage(TypeError, "cannot be held weakly");
});
});
test("called without new", () => {
@ -27,4 +27,9 @@ describe("normal behavior", () => {
var a = new WeakRef({});
expect(a instanceof WeakRef).toBeTrue();
});
test("constructor with single symbol argument", () => {
var a = new WeakRef(Symbol());
expect(a instanceof WeakRef).toBeTrue();
});
});

View file

@ -3,13 +3,18 @@ test("length is 0", () => {
});
test("basic functionality", () => {
var original = { a: 1 };
var weakRef = new WeakRef(original);
var originalObject = { a: 1 };
var objectWeakRef = new WeakRef(originalObject);
expect(weakRef.deref()).toBe(original);
expect(objectWeakRef.deref()).toBe(originalObject);
var originalSymbol = { a: 1 };
var symbolWeakRef = new WeakRef(originalSymbol);
expect(symbolWeakRef.deref()).toBe(originalSymbol);
});
test("kept alive for current synchronous execution sequence", () => {
test("object kept alive for current synchronous execution sequence", () => {
var weakRef;
{
weakRef = new WeakRef({ a: 1 });
@ -19,3 +24,14 @@ test("kept alive for current synchronous execution sequence", () => {
// This is fine 🔥
expect(weakRef.deref()).not.toBe(undefined);
});
test("symbol kept alive for current synchronous execution sequence", () => {
var weakRef;
{
weakRef = new WeakRef(Symbol("foo"));
}
weakRef.deref();
gc();
// This is fine 🔥
expect(weakRef.deref()).not.toBe(undefined);
});