LibGC+Everywhere: Factor out a LibGC from LibJS

Resulting in a massive rename across almost everywhere! Alongside the
namespace change, we now have the following names:

 * JS::NonnullGCPtr -> GC::Ref
 * JS::GCPtr -> GC::Ptr
 * JS::HeapFunction -> GC::Function
 * JS::CellImpl -> GC::Cell
 * JS::Handle -> GC::Root
This commit is contained in:
Shannon Booth 2024-11-15 04:01:23 +13:00 committed by Andreas Kling
commit f87041bf3a
Notes: github-actions[bot] 2024-11-15 13:50:17 +00:00
1722 changed files with 9939 additions and 9906 deletions

View file

@ -11,7 +11,7 @@
namespace Web::IndexedDB {
JS_DEFINE_ALLOCATOR(IDBDatabase);
GC_DEFINE_ALLOCATOR(IDBDatabase);
IDBDatabase::IDBDatabase(JS::Realm& realm, Database& db)
: EventTarget(realm)
@ -24,7 +24,7 @@ IDBDatabase::IDBDatabase(JS::Realm& realm, Database& db)
IDBDatabase::~IDBDatabase() = default;
JS::NonnullGCPtr<IDBDatabase> IDBDatabase::create(JS::Realm& realm, Database& db)
GC::Ref<IDBDatabase> IDBDatabase::create(JS::Realm& realm, Database& db)
{
return realm.create<IDBDatabase>(realm, db);
}

View file

@ -6,7 +6,7 @@
#pragma once
#include <LibJS/Heap/GCPtr.h>
#include <LibGC/Ptr.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HTML/DOMStringList.h>
#include <LibWeb/IndexedDB/IDBRequest.h>
@ -21,7 +21,7 @@ namespace Web::IndexedDB {
// https://www.w3.org/TR/IndexedDB/#database-connection
class IDBDatabase : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(IDBDatabase, DOM::EventTarget);
JS_DECLARE_ALLOCATOR(IDBDatabase);
GC_DECLARE_ALLOCATOR(IDBDatabase);
enum ConnectionState {
Open,
@ -31,17 +31,17 @@ class IDBDatabase : public DOM::EventTarget {
public:
virtual ~IDBDatabase() override;
[[nodiscard]] static JS::NonnullGCPtr<IDBDatabase> create(JS::Realm&, Database&);
[[nodiscard]] static GC::Ref<IDBDatabase> create(JS::Realm&, Database&);
void set_version(u64 version) { m_version = version; }
void set_close_pending(bool close_pending) { m_close_pending = close_pending; }
[[nodiscard]] JS::NonnullGCPtr<HTML::DOMStringList> object_store_names() { return m_object_store_names; }
[[nodiscard]] GC::Ref<HTML::DOMStringList> object_store_names() { return m_object_store_names; }
[[nodiscard]] String name() const { return m_name; }
[[nodiscard]] u64 version() const { return m_version; }
[[nodiscard]] bool close_pending() const { return m_close_pending; }
[[nodiscard]] ConnectionState state() const { return m_state; }
[[nodiscard]] JS::NonnullGCPtr<Database> associated_database() { return m_associated_database; }
[[nodiscard]] GC::Ref<Database> associated_database() { return m_associated_database; }
void set_onabort(WebIDL::CallbackType*);
WebIDL::CallbackType* onabort();
@ -61,7 +61,7 @@ protected:
private:
u64 m_version { 0 };
String m_name;
JS::NonnullGCPtr<HTML::DOMStringList> m_object_store_names;
GC::Ref<HTML::DOMStringList> m_object_store_names;
// Each connection has a close pending flag which is initially false.
bool m_close_pending { false };
@ -70,7 +70,7 @@ private:
// NOTE: There is an associated database in the spec, but there is no mention where it is assigned, nor where its from
// So we stash the one we have when opening a connection.
JS::NonnullGCPtr<Database> m_associated_database;
GC::Ref<Database> m_associated_database;
};
}

View file

@ -18,7 +18,7 @@
namespace Web::IndexedDB {
JS_DEFINE_ALLOCATOR(IDBFactory);
GC_DEFINE_ALLOCATOR(IDBFactory);
IDBFactory::IDBFactory(JS::Realm& realm)
: Bindings::PlatformObject(realm)
@ -34,7 +34,7 @@ void IDBFactory::initialize(JS::Realm& realm)
}
// https://w3c.github.io/IndexedDB/#dom-idbfactory-open
WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBOpenDBRequest>> IDBFactory::open(String const& name, Optional<u64> version)
WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> IDBFactory::open(String const& name, Optional<u64> version)
{
auto& realm = this->realm();
@ -55,21 +55,21 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBOpenDBRequest>> IDBFactory::open(String
auto request = IDBOpenDBRequest::create(realm);
// 5. Run these steps in parallel:
Platform::EventLoopPlugin::the().deferred_invoke(JS::create_heap_function(realm.heap(), [&realm, storage_key, name, version, request] {
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(realm.heap(), [&realm, storage_key, name, version, request] {
HTML::TemporaryExecutionContext context(realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes);
// 1. Let result be the result of opening a database connection, with storageKey, name, version if given and undefined otherwise, and request.
auto result = open_a_database_connection(realm, storage_key.value(), name, version, request);
// 2. Queue a task to run these steps:
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, JS::create_heap_function(realm.heap(), [&realm, &request, result = move(result)]() mutable {
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, GC::create_function(realm.heap(), [&realm, &request, result = move(result)]() mutable {
// 1. If result is an error, then:
if (result.is_error()) {
// 1. Set requests result to undefined.
request->set_result(JS::js_undefined());
// 2. Set requests error to result.
request->set_error(result.exception().get<JS::NonnullGCPtr<WebIDL::DOMException>>());
request->set_error(result.exception().get<GC::Ref<WebIDL::DOMException>>());
// 3. Set requests done flag to true.
request->set_done(true);

View file

@ -15,12 +15,12 @@ namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#idbfactory
class IDBFactory : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(IDBFactory, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(IDBFactory);
GC_DECLARE_ALLOCATOR(IDBFactory);
public:
virtual ~IDBFactory() override;
WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBOpenDBRequest>> open(String const& name, Optional<u64> version);
WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> open(String const& name, Optional<u64> version);
protected:
explicit IDBFactory(JS::Realm&);

View file

@ -12,7 +12,7 @@
namespace Web::IndexedDB {
JS_DEFINE_ALLOCATOR(IDBOpenDBRequest);
GC_DEFINE_ALLOCATOR(IDBOpenDBRequest);
IDBOpenDBRequest::~IDBOpenDBRequest() = default;
@ -27,7 +27,7 @@ void IDBOpenDBRequest::initialize(JS::Realm& realm)
WEB_SET_PROTOTYPE_FOR_INTERFACE(IDBOpenDBRequest);
}
JS::NonnullGCPtr<IDBOpenDBRequest> IDBOpenDBRequest::create(JS::Realm& realm)
GC::Ref<IDBOpenDBRequest> IDBOpenDBRequest::create(JS::Realm& realm)
{
return realm.create<IDBOpenDBRequest>(realm);
}

View file

@ -15,12 +15,12 @@ namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#idbopendbrequest
class IDBOpenDBRequest : public IDBRequest {
WEB_PLATFORM_OBJECT(IDBOpenDBRequest, IDBRequest);
JS_DECLARE_ALLOCATOR(IDBOpenDBRequest);
GC_DECLARE_ALLOCATOR(IDBOpenDBRequest);
public:
virtual ~IDBOpenDBRequest();
[[nodiscard]] static JS::NonnullGCPtr<IDBOpenDBRequest> create(JS::Realm&);
[[nodiscard]] static GC::Ref<IDBOpenDBRequest> create(JS::Realm&);
void set_onblocked(WebIDL::CallbackType*);
WebIDL::CallbackType* onblocked();

View file

@ -12,7 +12,7 @@
namespace Web::IndexedDB {
JS_DEFINE_ALLOCATOR(IDBRequest);
GC_DEFINE_ALLOCATOR(IDBRequest);
IDBRequest::~IDBRequest() = default;

View file

@ -13,19 +13,19 @@ namespace Web::IndexedDB {
class IDBRequest : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(IDBRequest, DOM::EventTarget);
JS_DECLARE_ALLOCATOR(IDBRequest);
GC_DECLARE_ALLOCATOR(IDBRequest);
public:
virtual ~IDBRequest() override;
[[nodiscard]] JS::Value result() const { return m_result; }
[[nodiscard]] JS::GCPtr<WebIDL::DOMException> error() const { return m_error; }
[[nodiscard]] GC::Ptr<WebIDL::DOMException> error() const { return m_error; }
[[nodiscard]] bool done() const { return m_done; }
[[nodiscard]] bool processed() const { return m_processed; }
void set_done(bool done) { m_done = done; }
void set_result(JS::Value result) { m_result = result; }
void set_error(JS::GCPtr<WebIDL::DOMException> error) { m_error = error; }
void set_error(GC::Ptr<WebIDL::DOMException> error) { m_error = error; }
void set_processed(bool processed) { m_processed = processed; }
void set_onsuccess(WebIDL::CallbackType*);
@ -46,7 +46,7 @@ private:
bool m_done { false };
// A request has a result and an error
JS::Value m_result;
JS::GCPtr<WebIDL::DOMException> m_error;
GC::Ptr<WebIDL::DOMException> m_error;
// FIXME: A request has a source object.
// FIXME: A request has a transaction which is initially null.
};

View file

@ -11,9 +11,9 @@
namespace Web::IndexedDB {
JS_DEFINE_ALLOCATOR(IDBVersionChangeEvent);
GC_DEFINE_ALLOCATOR(IDBVersionChangeEvent);
JS::NonnullGCPtr<IDBVersionChangeEvent> IDBVersionChangeEvent::create(JS::Realm& realm, FlyString const& event_name, IDBVersionChangeEventInit const& event_init)
GC::Ref<IDBVersionChangeEvent> IDBVersionChangeEvent::create(JS::Realm& realm, FlyString const& event_name, IDBVersionChangeEventInit const& event_init)
{
return realm.create<IDBVersionChangeEvent>(realm, event_name, event_init);
}

View file

@ -6,7 +6,7 @@
#pragma once
#include <LibJS/Heap/GCPtr.h>
#include <LibGC/Ptr.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/Forward.h>
@ -20,12 +20,12 @@ struct IDBVersionChangeEventInit : public DOM::EventInit {
// https://w3c.github.io/IndexedDB/#events
class IDBVersionChangeEvent : public DOM::Event {
WEB_PLATFORM_OBJECT(IDBVersionChangeEvent, DOM::Event);
JS_DECLARE_ALLOCATOR(IDBVersionChangeEvent);
GC_DECLARE_ALLOCATOR(IDBVersionChangeEvent);
public:
virtual ~IDBVersionChangeEvent() override;
static JS::NonnullGCPtr<IDBVersionChangeEvent> create(JS::Realm&, FlyString const&, IDBVersionChangeEventInit const&);
static GC::Ref<IDBVersionChangeEvent> create(JS::Realm&, FlyString const&, IDBVersionChangeEventInit const&);
u64 old_version() const { return m_old_version; }
Optional<u64> new_version() const { return m_new_version; }

View file

@ -17,7 +17,7 @@
namespace Web::IndexedDB {
// https://w3c.github.io/IndexedDB/#open-a-database-connection
WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS::Realm& realm, StorageAPI::StorageKey storage_key, String name, Optional<u64> maybe_version, JS::NonnullGCPtr<IDBRequest> request)
WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm& realm, StorageAPI::StorageKey storage_key, String name, Optional<u64> maybe_version, GC::Ref<IDBRequest> request)
{
// 1. Let queue be the connection queue for storageKey and name.
auto& queue = ConnectionQueueHandler::for_key_and_name(storage_key, name);
@ -26,13 +26,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS
queue.append(request);
// 3. Wait until all previous requests in queue have been processed.
HTML::main_thread_event_loop().spin_until(JS::create_heap_function(realm.vm().heap(), [queue, request]() {
HTML::main_thread_event_loop().spin_until(GC::create_function(realm.vm().heap(), [queue, request]() {
return queue.all_previous_requests_processed(request);
}));
// 4. Let db be the database named name in storageKey, or null otherwise.
auto maybe_db = Database::for_key_and_name(storage_key, name);
JS::GCPtr<Database> db;
GC::Ptr<Database> db;
// 5. If version is undefined, let version be 1 if db is null, or dbs version otherwise.
auto version = maybe_version.value_or(maybe_db.has_value() ? maybe_db.value()->version() : 1);
@ -69,7 +69,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS
// queue a task to fire a version change event named versionchange at entry with dbs version and version.
for (auto& entry : open_connections) {
if (!entry->close_pending()) {
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, JS::create_heap_function(realm.vm().heap(), [&realm, entry, db, version]() {
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, GC::create_function(realm.vm().heap(), [&realm, entry, db, version]() {
fire_a_version_change_event(realm, HTML::EventNames::versionchange, *entry, db->version(), version);
}));
}
@ -81,14 +81,14 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS
// queue a task to fire a version change event named blocked at request with dbs version and version.
for (auto& entry : open_connections) {
if (entry->state() != IDBDatabase::ConnectionState::Closed) {
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, JS::create_heap_function(realm.vm().heap(), [&realm, entry, db, version]() {
HTML::queue_a_task(HTML::Task::Source::DatabaseAccess, nullptr, nullptr, GC::create_function(realm.vm().heap(), [&realm, entry, db, version]() {
fire_a_version_change_event(realm, HTML::EventNames::blocked, *entry, db->version(), version);
}));
}
}
// 5. Wait until all connections in openConnections are closed.
HTML::main_thread_event_loop().spin_until(JS::create_heap_function(realm.vm().heap(), [open_connections]() {
HTML::main_thread_event_loop().spin_until(GC::create_function(realm.vm().heap(), [open_connections]() {
for (auto const& entry : open_connections) {
if (entry->state() != IDBDatabase::ConnectionState::Closed) {
return false;
@ -115,7 +115,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS
return connection;
}
bool fire_a_version_change_event(JS::Realm& realm, FlyString const& event_name, JS::NonnullGCPtr<DOM::EventTarget> target, u64 old_version, Optional<u64> new_version)
bool fire_a_version_change_event(JS::Realm& realm, FlyString const& event_name, GC::Ref<DOM::EventTarget> target, u64 old_version, Optional<u64> new_version)
{
IDBVersionChangeEventInit event_init = {};
// 4. Set events oldVersion attribute to oldVersion.

View file

@ -12,7 +12,7 @@
namespace Web::IndexedDB {
WebIDL::ExceptionOr<JS::NonnullGCPtr<IDBDatabase>> open_a_database_connection(JS::Realm&, StorageAPI::StorageKey, String, Optional<u64>, JS::NonnullGCPtr<IDBRequest>);
bool fire_a_version_change_event(JS::Realm&, FlyString const&, JS::NonnullGCPtr<DOM::EventTarget>, u64, Optional<u64>);
WebIDL::ExceptionOr<GC::Ref<IDBDatabase>> open_a_database_connection(JS::Realm&, StorageAPI::StorageKey, String, Optional<u64>, GC::Ref<IDBRequest>);
bool fire_a_version_change_event(JS::Realm&, FlyString const&, GC::Ref<DOM::EventTarget>, u64, Optional<u64>);
}

View file

@ -7,16 +7,16 @@
#pragma once
#include <AK/HashMap.h>
#include <LibJS/Heap/Handle.h>
#include <LibGC/Root.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/IndexedDB/IDBRequest.h>
#include <LibWeb/StorageAPI/StorageKey.h>
namespace Web::IndexedDB {
class ConnectionQueue : public AK::Vector<JS::Handle<IDBRequest>> {
class ConnectionQueue : public AK::Vector<GC::Root<IDBRequest>> {
public:
bool all_previous_requests_processed(JS::NonnullGCPtr<IDBRequest> const& request) const
bool all_previous_requests_processed(GC::Ref<IDBRequest> const& request) const
{
for (auto const& entry : *this) {
if (entry == request)

View file

@ -9,14 +9,14 @@
namespace Web::IndexedDB {
using IDBDatabaseMapping = HashMap<StorageAPI::StorageKey, HashMap<String, JS::Handle<Database>>>;
using IDBDatabaseMapping = HashMap<StorageAPI::StorageKey, HashMap<String, GC::Root<Database>>>;
static IDBDatabaseMapping m_databases;
JS_DEFINE_ALLOCATOR(Database);
GC_DEFINE_ALLOCATOR(Database);
Database::~Database() = default;
JS::NonnullGCPtr<Database> Database::create(JS::Realm& realm, String const& name)
GC::Ref<Database> Database::create(JS::Realm& realm, String const& name)
{
return realm.create<Database>(realm, name);
}
@ -37,18 +37,18 @@ ConnectionQueue& ConnectionQueueHandler::for_key_and_name(StorageAPI::StorageKey
});
}
Optional<JS::Handle<Database>> Database::for_key_and_name(StorageAPI::StorageKey& key, String& name)
Optional<GC::Root<Database>> Database::for_key_and_name(StorageAPI::StorageKey& key, String& name)
{
return m_databases.ensure(key, [] {
return HashMap<String, JS::Handle<Database>>();
return HashMap<String, GC::Root<Database>>();
})
.get(name);
}
ErrorOr<JS::Handle<Database>> Database::create_for_key_and_name(JS::Realm& realm, StorageAPI::StorageKey& key, String& name)
ErrorOr<GC::Root<Database>> Database::create_for_key_and_name(JS::Realm& realm, StorageAPI::StorageKey& key, String& name)
{
auto database_mapping = TRY(m_databases.try_ensure(key, [] {
return HashMap<String, JS::Handle<Database>>();
return HashMap<String, GC::Root<Database>>();
}));
return database_mapping.try_ensure(name, [&] {

View file

@ -7,7 +7,7 @@
#pragma once
#include <AK/HashMap.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibGC/Ptr.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/IndexedDB/IDBDatabase.h>
#include <LibWeb/IndexedDB/IDBRequest.h>
@ -18,18 +18,18 @@ namespace Web::IndexedDB {
// https://www.w3.org/TR/IndexedDB/#database-construct
class Database : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Database, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(Database);
GC_DECLARE_ALLOCATOR(Database);
public:
void set_version(u64 version) { m_version = version; }
u64 version() const { return m_version; }
String name() const { return m_name; }
void associate(JS::NonnullGCPtr<IDBDatabase> connection) { m_associated_connections.append(connection); }
ReadonlySpan<JS::NonnullGCPtr<IDBDatabase>> associated_connections() { return m_associated_connections; }
Vector<JS::Handle<IDBDatabase>> associated_connections_except(IDBDatabase& connection)
void associate(GC::Ref<IDBDatabase> connection) { m_associated_connections.append(connection); }
ReadonlySpan<GC::Ref<IDBDatabase>> associated_connections() { return m_associated_connections; }
Vector<GC::Root<IDBDatabase>> associated_connections_except(IDBDatabase& connection)
{
Vector<JS::Handle<IDBDatabase>> connections;
Vector<GC::Root<IDBDatabase>> connections;
for (auto& associated_connection : m_associated_connections) {
if (associated_connection != &connection)
connections.append(associated_connection);
@ -37,11 +37,11 @@ public:
return connections;
}
[[nodiscard]] static Optional<JS::Handle<Database>> for_key_and_name(StorageAPI::StorageKey&, String&);
[[nodiscard]] static ErrorOr<JS::Handle<Database>> create_for_key_and_name(JS::Realm&, StorageAPI::StorageKey&, String&);
[[nodiscard]] static Optional<GC::Root<Database>> for_key_and_name(StorageAPI::StorageKey&, String&);
[[nodiscard]] static ErrorOr<GC::Root<Database>> create_for_key_and_name(JS::Realm&, StorageAPI::StorageKey&, String&);
[[nodiscard]] static ErrorOr<void> delete_for_key_and_name(StorageAPI::StorageKey&, String&);
[[nodiscard]] static JS::NonnullGCPtr<Database> create(JS::Realm&, String const&);
[[nodiscard]] static GC::Ref<Database> create(JS::Realm&, String const&);
virtual ~Database();
protected:
@ -56,7 +56,7 @@ protected:
virtual void visit_edges(Visitor&) override;
private:
Vector<JS::NonnullGCPtr<IDBDatabase>> m_associated_connections;
Vector<GC::Ref<IDBDatabase>> m_associated_connections;
// FIXME: A database has zero or more object stores which hold the data stored in the database.