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

@ -13,6 +13,7 @@
#include <AK/TypeCasts.h>
#include <AK/URL.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/AnimationFrameCallbackDriver.h>
@ -123,6 +124,8 @@ public:
// https://html.spec.whatwg.org/multipage/interaction.html#transient-activation
bool has_transient_activation() const;
void initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>);
private:
explicit Window(JS::Realm&);
virtual void initialize(JS::Realm&) override;
@ -170,34 +173,18 @@ public:
Bindings::LocationObject* location_object() { return m_location_object; }
Bindings::LocationObject const* location_object() const { return m_location_object; }
JS::Object* web_prototype(String const& class_name) { return m_prototypes.get(class_name).value_or(nullptr); }
JS::NativeFunction* web_constructor(String const& class_name) { return m_constructors.get(class_name).value_or(nullptr); }
JS::Object& cached_web_prototype(String const& class_name);
JS::Object& cached_web_prototype(String const& class_name) { return Bindings::cached_web_prototype(realm(), 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 = shape().realm();
auto* prototype = heap().allocate<T>(realm, realm);
m_prototypes.set(class_name, prototype);
return *prototype;
return Bindings::ensure_web_prototype<T>(realm(), class_name);
}
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 = shape().realm();
auto* constructor = heap().allocate<T>(realm, realm);
m_constructors.set(class_name, constructor);
define_direct_property(class_name, JS::Value(constructor), JS::Attribute::Writable | JS::Attribute::Configurable);
return *constructor;
return Bindings::ensure_web_constructor<T>(realm(), class_name);
}
virtual JS::ThrowCompletionOr<bool> internal_set_prototype_of(JS::Object* prototype) override;
@ -283,9 +270,6 @@ private:
Bindings::LocationObject* m_location_object { nullptr };
HashMap<String, JS::Object*> m_prototypes;
HashMap<String, JS::NativeFunction*> m_constructors;
// [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;
};