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

LibJS: Setup host hooks and have promise jobs work out the realm

This allows the host of LibJS (notably LibWeb in this case) to override
certain functions such as HostEnqueuePromiseJob, so it can do it's own
thing in certain situations. Notably, LibWeb will override
HostEnqueuePromiseJob to put promise jobs on the microtask queue.

This also makes promise jobs use AK::Function instead of
JS::NativeFunction. This removes the need to go through a JavaScript
function and it more closely matches the spec's idea of "abstract
closures"
This commit is contained in:
Luke Wilde 2022-02-06 03:46:45 +00:00 committed by Linus Groh
parent 5aacec65ab
commit 4c1c6ef91c
12 changed files with 202 additions and 172 deletions

View file

@ -9,6 +9,7 @@
#include <AK/SinglyLinkedList.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/JobCallback.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Value.h>
#include <LibJS/Runtime/WeakContainer.h>
@ -21,21 +22,26 @@ class FinalizationRegistry final
JS_OBJECT(FinalizationRegistry, Object);
public:
static FinalizationRegistry* create(GlobalObject&, FunctionObject&);
explicit FinalizationRegistry(FunctionObject&, Object& prototype);
explicit FinalizationRegistry(Realm&, JS::JobCallback, Object& prototype);
virtual ~FinalizationRegistry() override;
void add_finalization_record(Cell& target, Value held_value, Object* unregister_token);
bool remove_by_token(Object& unregister_token);
ThrowCompletionOr<void> cleanup(FunctionObject* callback = nullptr);
ThrowCompletionOr<void> cleanup(Optional<JobCallback> = {});
virtual void remove_dead_cells(Badge<Heap>) override;
Realm& realm() { return *m_realm.cell(); }
Realm const& realm() const { return *m_realm.cell(); }
JobCallback& cleanup_callback() { return m_cleanup_callback; }
JobCallback const& cleanup_callback() const { return m_cleanup_callback; }
private:
virtual void visit_edges(Visitor& visitor) override;
FunctionObject* m_cleanup_callback { nullptr };
Handle<Realm> m_realm;
JS::JobCallback m_cleanup_callback;
struct FinalizationRecord {
Cell* target { nullptr };