1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 01:17:35 +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

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,26 +8,38 @@
namespace JS { namespace JS {
WeakRef* WeakRef::create(GlobalObject& global_object, Object* object) WeakRef* WeakRef::create(GlobalObject& global_object, Object& value)
{ {
return global_object.heap().allocate<WeakRef>(global_object, object, *global_object.weak_ref_prototype()); return global_object.heap().allocate<WeakRef>(global_object, value, *global_object.weak_ref_prototype());
} }
WeakRef::WeakRef(Object* object, Object& prototype) WeakRef* WeakRef::create(GlobalObject& global_object, Symbol& value)
{
return global_object.heap().allocate<WeakRef>(global_object, value, *global_object.weak_ref_prototype());
}
WeakRef::WeakRef(Object& value, Object& prototype)
: Object(prototype) : Object(prototype)
, WeakContainer(heap()) , WeakContainer(heap())
, m_value(object) , m_value(&value)
, m_last_execution_generation(vm().execution_generation())
{
}
WeakRef::WeakRef(Symbol& value, Object& prototype)
: Object(prototype)
, WeakContainer(heap())
, m_value(&value)
, m_last_execution_generation(vm().execution_generation()) , m_last_execution_generation(vm().execution_generation())
{ {
} }
void WeakRef::remove_dead_cells(Badge<Heap>) void WeakRef::remove_dead_cells(Badge<Heap>)
{ {
VERIFY(m_value); if (m_value.visit([](Cell* cell) -> bool { return cell->state() == Cell::State::Live; }, [](Empty) -> bool { VERIFY_NOT_REACHED(); }))
if (m_value->state() == Cell::State::Live)
return; return;
m_value = nullptr; m_value = Empty {};
// This is an optimization, we deregister from the garbage collector early (even if we were not garbage collected ourself yet) // This is an optimization, we deregister from the garbage collector early (even if we were not garbage collected ourself yet)
// to reduce the garbage collection overhead, which we can do because a cleared weak ref cannot be reused. // to reduce the garbage collection overhead, which we can do because a cleared weak ref cannot be reused.
WeakContainer::deregister(); WeakContainer::deregister();
@ -37,8 +49,10 @@ void WeakRef::visit_edges(Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);
if (vm().execution_generation() == m_last_execution_generation) if (vm().execution_generation() == m_last_execution_generation) {
visitor.visit(m_value); auto* cell = m_value.visit([](Cell* cell) -> Cell* { return cell; }, [](Empty) -> Cell* { return nullptr; });
visitor.visit(cell);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -18,12 +18,14 @@ class WeakRef final
JS_OBJECT(WeakRef, Object); JS_OBJECT(WeakRef, Object);
public: public:
static WeakRef* create(GlobalObject&, Object*); static WeakRef* create(GlobalObject&, Object&);
static WeakRef* create(GlobalObject&, Symbol&);
explicit WeakRef(Object*, Object& prototype); explicit WeakRef(Object&, Object& prototype);
explicit WeakRef(Symbol&, Object& prototype);
virtual ~WeakRef() override = default; virtual ~WeakRef() override = default;
Object* value() const { return m_value; }; auto const& value() const { return m_value; };
void update_execution_generation() { m_last_execution_generation = vm().execution_generation(); }; void update_execution_generation() { m_last_execution_generation = vm().execution_generation(); };
@ -32,7 +34,7 @@ public:
private: private:
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;
Object* m_value { nullptr }; Variant<Object*, Symbol*, Empty> m_value;
u32 m_last_execution_generation { 0 }; u32 m_last_execution_generation { 0 };
}; };

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -42,9 +42,13 @@ ThrowCompletionOr<Object*> WeakRefConstructor::construct(FunctionObject& new_tar
auto& global_object = this->global_object(); auto& global_object = this->global_object();
auto target = vm.argument(0); auto target = vm.argument(0);
if (!target.is_object()) if (!can_be_held_weakly(target))
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, target.to_string_without_side_effects()); return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, target.to_string_without_side_effects());
return TRY(ordinary_create_from_constructor<WeakRef>(global_object, new_target, &GlobalObject::weak_ref_prototype, &target.as_object()));
if (target.is_object())
return TRY(ordinary_create_from_constructor<WeakRef>(global_object, new_target, &GlobalObject::weak_ref_prototype, target.as_object()));
VERIFY(target.is_symbol());
return TRY(ordinary_create_from_constructor<WeakRef>(global_object, new_target, &GlobalObject::weak_ref_prototype, target.as_symbol()));
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> * Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -30,7 +30,9 @@ JS_DEFINE_NATIVE_FUNCTION(WeakRefPrototype::deref)
auto* weak_ref = TRY(typed_this_object(global_object)); auto* weak_ref = TRY(typed_this_object(global_object));
weak_ref->update_execution_generation(); weak_ref->update_execution_generation();
return weak_ref->value() ?: js_undefined(); return weak_ref->value().visit(
[](Empty) -> Value { return js_undefined(); },
[](auto* value) -> Value { return value; });
} }
} }

View file

@ -8,7 +8,7 @@ describe("errors", () => {
[-100, Infinity, NaN, 152n, undefined].forEach(value => { [-100, Infinity, NaN, 152n, undefined].forEach(value => {
expect(() => { expect(() => {
new WeakRef(value); new WeakRef(value);
}).toThrowWithMessage(TypeError, "is not an object"); }).toThrowWithMessage(TypeError, "cannot be held weakly");
}); });
}); });
test("called without new", () => { test("called without new", () => {
@ -27,4 +27,9 @@ describe("normal behavior", () => {
var a = new WeakRef({}); var a = new WeakRef({});
expect(a instanceof WeakRef).toBeTrue(); 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", () => { test("basic functionality", () => {
var original = { a: 1 }; var originalObject = { a: 1 };
var weakRef = new WeakRef(original); 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; var weakRef;
{ {
weakRef = new WeakRef({ a: 1 }); weakRef = new WeakRef({ a: 1 });
@ -19,3 +24,14 @@ test("kept alive for current synchronous execution sequence", () => {
// This is fine 🔥 // This is fine 🔥
expect(weakRef.deref()).not.toBe(undefined); 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);
});