LibWeb: Add support for intrinsics on a synthetic realm

This commit is contained in:
Shannon Booth 2024-11-08 16:07:04 +13:00 committed by Andrew Kaster
parent 9ba68c5776
commit fc2104f099
Notes: github-actions[bot] 2024-11-09 19:37:23 +00:00
8 changed files with 59 additions and 12 deletions

View file

@ -0,0 +1,18 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/HostDefined.h>
#include <LibWeb/Bindings/Intrinsics.h>
namespace Web::Bindings {
void HostDefined::visit_edges(JS::Cell::Visitor& visitor)
{
JS::Realm::HostDefined::visit_edges(visitor);
visitor.visit(intrinsics);
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
struct HostDefined : public JS::Realm::HostDefined {
explicit HostDefined(JS::NonnullGCPtr<Intrinsics> intrinsics)
: intrinsics(intrinsics)
{
}
virtual ~HostDefined() override = default;
virtual void visit_edges(JS::Cell::Visitor& visitor) override;
JS::NonnullGCPtr<Intrinsics> intrinsics;
};
}

View file

@ -10,6 +10,7 @@
#include <LibJS/Runtime/Object.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/SyntheticHostDefined.h>
namespace Web::Bindings {
@ -32,7 +33,7 @@ bool Intrinsics::is_exposed(StringView name) const
Intrinsics& host_defined_intrinsics(JS::Realm& realm)
{
VERIFY(realm.host_defined());
return verify_cast<Bindings::PrincipalHostDefined>(*realm.host_defined()).intrinsics;
return verify_cast<Bindings::HostDefined>(*realm.host_defined()).intrinsics;
}
}

View file

@ -589,13 +589,13 @@ ErrorOr<void> initialize_main_thread_vm(HTML::EventLoop::Type type)
};
// 8. Set realm.[[HostDefined]] to settings.
realm.set_host_defined(make<Bindings::SyntheticHostDefined>(move(settings)));
realm.set_host_defined(make<Bindings::SyntheticHostDefined>(move(settings), realm.heap().allocate<Bindings::Intrinsics>(realm, realm)));
// 9. Set realm.[[GlobalObject]] to globalObject.
realm.set_global_object(global_object);
// 10. Set realm.[[GlobalEnv]] to NewGlobalEnvironment(globalObject, globalObject).
realm.set_global_environment(realm.vm().heap().allocate_without_realm<JS::GlobalEnvironment>(global_object, global_object));
realm.set_global_environment(realm.heap().allocate_without_realm<JS::GlobalEnvironment>(global_object, global_object));
// 11. Perform ? SetDefaultGlobalBindings(realm).
set_default_global_bindings(realm);

View file

@ -15,9 +15,8 @@ namespace Web::Bindings {
void PrincipalHostDefined::visit_edges(JS::Cell::Visitor& visitor)
{
JS::Realm::HostDefined::visit_edges(visitor);
HostDefined::visit_edges(visitor);
visitor.visit(environment_settings_object);
visitor.visit(intrinsics);
visitor.visit(page);
}

View file

@ -9,14 +9,15 @@
#include <AK/TypeCasts.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/HostDefined.h>
#include <LibWeb/Forward.h>
namespace Web::Bindings {
struct PrincipalHostDefined : public JS::Realm::HostDefined {
struct PrincipalHostDefined : public HostDefined {
PrincipalHostDefined(JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> eso, JS::NonnullGCPtr<Intrinsics> intrinsics, JS::NonnullGCPtr<Page> page)
: environment_settings_object(eso)
, intrinsics(intrinsics)
: HostDefined(intrinsics)
, environment_settings_object(eso)
, page(page)
{
}
@ -24,7 +25,6 @@ struct PrincipalHostDefined : public JS::Realm::HostDefined {
virtual void visit_edges(JS::Cell::Visitor& visitor) override;
JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> environment_settings_object;
JS::NonnullGCPtr<Intrinsics> intrinsics;
JS::NonnullGCPtr<Page> page;
};

View file

@ -7,14 +7,16 @@
#pragma once
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/HostDefined.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/SyntheticRealmSettings.h>
namespace Web::Bindings {
struct SyntheticHostDefined : public JS::Realm::HostDefined {
SyntheticHostDefined(HTML::SyntheticRealmSettings synthetic_realm_settings)
: synthetic_realm_settings(move(synthetic_realm_settings))
struct SyntheticHostDefined : public HostDefined {
SyntheticHostDefined(HTML::SyntheticRealmSettings synthetic_realm_settings, JS::NonnullGCPtr<Intrinsics> intrinsics)
: HostDefined(intrinsics)
, synthetic_realm_settings(move(synthetic_realm_settings))
{
}

View file

@ -23,6 +23,7 @@ set(SOURCES
Bindings/MainThreadVM.cpp
Bindings/OptionConstructor.cpp
Bindings/PlatformObject.cpp
Bindings/HostDefined.cpp
Bindings/PrincipalHostDefined.cpp
Bindings/SyntheticHostDefined.cpp
Clipboard/Clipboard.cpp