1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:57:45 +00:00

LibWeb: Move Web prototypes and constructors to new Intrinsics object

This Intrinsics object hangs off of a new HostDefined struct that takes
the place of EnvironmentSettingsObject as the true [[HostDefined]] slot
on JS::Realm objects created by LibWeb.

This gets the intrinsics off of the GlobalObject, Window, similar to the
previous refactor of LibJS to move the intrinsics into the Realm's
[[Intrinics]] internal slot.

A side effect of this change is that we cannot fully initialize a Window
object until the [[HostDefined]] slot has been installed into the realm,
which happens with the creation of the WindowEnvironmentSettingsObject.

As such, any Window usage that has not been funned through a WindowESO
will not have any cached Web prototyped or constructors, and will not
have Window APIs available to javascript code. Currently this seems
limited to usage of Window in the CSS parser, but a subsequent commit
will clean those up to take Realm as well. However, this commit compiles
so let's cut it off here :^).
This commit is contained in:
Andrew Kaster 2022-09-24 15:39:23 -06:00 committed by Linus Groh
parent 01c2cffcca
commit c61a4f35dc
22 changed files with 240 additions and 60 deletions

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/HostDefined.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/Scripting/Environments.h>
namespace Web::Bindings {
void HostDefined::visit_edges(JS::Cell::Visitor& visitor)
{
JS::Realm::HostDefined::visit_edges(visitor);
visitor.visit(environment_settings_object);
visitor.visit(*intrinsics);
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/TypeCasts.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
struct HostDefined : public JS::Realm::HostDefined {
HostDefined(JS::GCPtr<HTML::EnvironmentSettingsObject> eso, JS::NonnullGCPtr<Intrinsics> intrinsics)
: environment_settings_object(eso)
, intrinsics(intrinsics)
{
}
virtual ~HostDefined() override = default;
virtual void visit_edges(JS::Cell::Visitor& visitor) override;
// NOTE: Only the root execution environment in the main thread VM ever sets this to nullptr
JS::GCPtr<HTML::EnvironmentSettingsObject> environment_settings_object;
JS::NonnullGCPtr<Intrinsics> intrinsics;
};
inline HTML::EnvironmentSettingsObject& host_defined_environment_settings_object(JS::Realm& realm)
{
return *verify_cast<HostDefined>(realm.host_defined())->environment_settings_object;
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <AK/String.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
#include <LibWeb/Bindings/Intrinsics.h>
namespace Web::Bindings {
JS::Object& Intrinsics::cached_web_prototype(String const& class_name)
{
auto it = m_prototypes.find(class_name);
if (it == m_prototypes.end()) {
dbgln("Missing prototype: {}", class_name);
}
VERIFY(it != m_prototypes.end());
return *it->value;
}
void Intrinsics::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto& it : m_prototypes)
visitor.visit(it.value);
for (auto& it : m_constructors)
visitor.visit(it.value);
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/HashMap.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/HostDefined.h>
namespace Web::Bindings {
class Intrinsics final : public JS::Cell {
JS_CELL(Intrinsics, JS::Cell);
public:
Intrinsics(JS::Realm& realm)
: m_realm(realm)
{
}
JS::Object& cached_web_prototype(String const& class_name);
template<typename T>
JS::Object& ensure_web_prototype(String const& class_name)
{
auto it = m_prototypes.find(class_name);
if (it != m_prototypes.end())
return *it->value;
auto& realm = *m_realm;
auto* prototype = heap().allocate<T>(realm, realm);
m_prototypes.set(class_name, prototype);
return *prototype;
}
template<typename T>
JS::NativeFunction& ensure_web_constructor(String const& class_name)
{
auto it = m_constructors.find(class_name);
if (it != m_constructors.end())
return *it->value;
auto& realm = *m_realm;
auto* constructor = heap().allocate<T>(realm, realm);
m_constructors.set(class_name, constructor);
return *constructor;
}
private:
virtual void visit_edges(JS::Cell::Visitor&) override;
HashMap<String, JS::Object*> m_prototypes;
HashMap<String, JS::NativeFunction*> m_constructors;
JS::NonnullGCPtr<JS::Realm> m_realm;
};
inline Intrinsics& host_defined_intrinsics(JS::Realm& realm)
{
return *verify_cast<HostDefined>(realm.host_defined())->intrinsics;
}
template<typename T>
JS::Object& ensure_web_prototype(JS::Realm& realm, String const& class_name)
{
return host_defined_intrinsics(realm).ensure_web_prototype<T>(class_name);
}
template<typename T>
JS::NativeFunction& ensure_web_constructor(JS::Realm& realm, String const& class_name)
{
return host_defined_intrinsics(realm).ensure_web_constructor<T>(class_name);
}
inline JS::Object& cached_web_prototype(JS::Realm& realm, String const& class_name)
{
return host_defined_intrinsics(realm).cached_web_prototype(class_name);
}
}

View file

@ -11,6 +11,7 @@
#include <LibJS/Runtime/FinalizationRegistry.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
@ -181,7 +182,7 @@ JS::VM& main_thread_vm()
// 2. Queue a global task on the JavaScript engine task source given global to perform the following steps:
HTML::queue_global_task(HTML::Task::Source::JavaScriptEngine, global, [&finalization_registry]() mutable {
// 1. Let entry be finalizationRegistry.[[CleanupCallback]].[[Callback]].[[Realm]]'s environment settings object.
auto& entry = verify_cast<HTML::EnvironmentSettingsObject>(*finalization_registry.cleanup_callback().callback.cell()->realm()->host_defined());
auto& entry = host_defined_environment_settings_object(*finalization_registry.cleanup_callback().callback.cell()->realm());
// 2. Check if we can run script with entry. If this returns "do not run", then return.
if (entry.can_run_script() == HTML::RunScriptDecision::DoNotRun)
@ -207,7 +208,7 @@ JS::VM& main_thread_vm()
// 1. If realm is not null, then let job settings be the settings object for realm. Otherwise, let job settings be null.
HTML::EnvironmentSettingsObject* job_settings { nullptr };
if (realm)
job_settings = verify_cast<HTML::EnvironmentSettingsObject>(realm->host_defined());
job_settings = &host_defined_environment_settings_object(*realm);
// IMPLEMENTATION DEFINED: The JS spec says we must take implementation defined steps to make the currently active script or module at the time of HostEnqueuePromiseJob being invoked
// also be the active script or module of the job at the time of its invocation.
@ -312,6 +313,11 @@ JS::VM& main_thread_vm()
},
nullptr));
auto* root_realm = custom_data.root_execution_context->realm;
auto* intrinsics = root_realm->heap().allocate<Intrinsics>(*root_realm, *root_realm);
auto host_defined = make<HostDefined>(nullptr, *intrinsics);
root_realm->set_host_defined(move(host_defined));
vm->push_execution_context(*custom_data.root_execution_context);
}
return *vm;

View file

@ -394,8 +394,9 @@
#define ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(interface_name, constructor_name, prototype_name) \
{ \
auto& prototype = ensure_web_prototype<Bindings::prototype_name>(#interface_name); \
auto& constructor = ensure_web_constructor<Bindings::constructor_name>(#interface_name); \
auto& prototype = Bindings::ensure_web_prototype<Bindings::prototype_name>(realm, #interface_name); \
auto& constructor = Bindings::ensure_web_constructor<Bindings::constructor_name>(realm, #interface_name); \
define_direct_property(#interface_name, &constructor, JS::Attribute::Writable | JS::Attribute::Configurable); \
prototype.define_direct_property(vm.names.constructor, &constructor, JS::Attribute::Writable | JS::Attribute::Configurable); \
constructor.define_direct_property(vm.names.name, js_string(vm, #interface_name), JS::Attribute::Configurable); \
}
@ -405,6 +406,7 @@
#define ADD_WINDOW_OBJECT_INTERFACES \
auto& vm = this->vm(); \
auto& realm = this->realm(); \
ADD_WINDOW_OBJECT_INTERFACE(AbortController) \
ADD_WINDOW_OBJECT_INTERFACE(AbortSignal) \
ADD_WINDOW_OBJECT_INTERFACE(AbstractRange) \