Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb: Shadow Realms Part 5 - Add support for UniversalGlobalScope mixin #2220

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Meta/CMake/libweb_generators.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function (generate_js_bindings target)
IntrinsicDefinitions.cpp
DedicatedWorkerExposedInterfaces.cpp DedicatedWorkerExposedInterfaces.h
SharedWorkerExposedInterfaces.cpp SharedWorkerExposedInterfaces.h
ShadowRealmExposedInterfaces.cpp ShadowRealmExposedInterfaces.h
WindowExposedInterfaces.cpp WindowExposedInterfaces.h)
list(TRANSFORM exposed_interface_sources PREPEND "Bindings/")
add_custom_command(
Expand All @@ -206,6 +207,8 @@ function (generate_js_bindings target)
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/DedicatedWorkerExposedInterfaces.cpp "Bindings/DedicatedWorkerExposedInterfaces.cpp"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/SharedWorkerExposedInterfaces.h "Bindings/SharedWorkerExposedInterfaces.h"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/SharedWorkerExposedInterfaces.cpp "Bindings/SharedWorkerExposedInterfaces.cpp"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/ShadowRealmExposedInterfaces.h "Bindings/ShadowRealmExposedInterfaces.h"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/ShadowRealmExposedInterfaces.cpp "Bindings/ShadowRealmExposedInterfaces.cpp"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/WindowExposedInterfaces.h "Bindings/WindowExposedInterfaces.h"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different tmp/WindowExposedInterfaces.cpp "Bindings/WindowExposedInterfaces.cpp"
COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_CURRENT_BINARY_DIR}/tmp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <LibIDL/Types.h>
#include <LibMain/Main.h>

static ErrorOr<void> add_to_interface_sets(IDL::Interface&, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed);
static ErrorOr<void> add_to_interface_sets(IDL::Interface&, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed, Vector<IDL::Interface&>& shadow_realm_exposed);
static ByteString s_error_string;

struct LegacyConstructor {
Expand Down Expand Up @@ -367,6 +367,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Vector<IDL::Interface&> window_exposed;
Vector<IDL::Interface&> dedicated_worker_exposed;
Vector<IDL::Interface&> shared_worker_exposed;
Vector<IDL::Interface&> shadow_realm_exposed;
// TODO: service_worker_exposed

for (size_t i = 0; i < paths.size(); ++i) {
Expand All @@ -378,7 +379,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return Error::from_string_view(s_error_string.view());
}

TRY(add_to_interface_sets(interface, intrinsics, window_exposed, dedicated_worker_exposed, shared_worker_exposed));
TRY(add_to_interface_sets(interface, intrinsics, window_exposed, dedicated_worker_exposed, shared_worker_exposed, shadow_realm_exposed));
parsers.append(move(parser));
}

Expand All @@ -387,11 +388,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(generate_exposed_interface_header("Window"sv, output_path));
TRY(generate_exposed_interface_header("DedicatedWorker"sv, output_path));
TRY(generate_exposed_interface_header("SharedWorker"sv, output_path));
TRY(generate_exposed_interface_header("ShadowRealm"sv, output_path));
// TODO: ServiceWorkerExposed.h

TRY(generate_exposed_interface_implementation("Window"sv, output_path, window_exposed));
TRY(generate_exposed_interface_implementation("DedicatedWorker"sv, output_path, dedicated_worker_exposed));
TRY(generate_exposed_interface_implementation("SharedWorker"sv, output_path, shared_worker_exposed));
TRY(generate_exposed_interface_implementation("ShadowRealm"sv, output_path, shadow_realm_exposed));
// TODO: ServiceWorkerExposed.cpp

return 0;
Expand All @@ -404,8 +407,9 @@ enum ExposedTo {
ServiceWorker = 0x4,
AudioWorklet = 0x8,
Window = 0x10,
AllWorkers = 0xF, // FIXME: Is "AudioWorklet" a Worker? We'll assume it is for now
All = 0x1F,
ShadowRealm = 0x20,
AllWorkers = DedicatedWorker | SharedWorker | ServiceWorker | AudioWorklet, // FIXME: Is "AudioWorklet" a Worker? We'll assume it is for now (here, and line below)
All = AllWorkers | Window | ShadowRealm
};
AK_ENUM_BITWISE_OPERATORS(ExposedTo);

Expand Down Expand Up @@ -436,6 +440,8 @@ static ErrorOr<ExposedTo> parse_exposure_set(IDL::Interface& interface)
return ExposedTo::ServiceWorker;
if (exposed == "AudioWorklet"sv)
return ExposedTo::AudioWorklet;
if (exposed == "ShadowRealm"sv)
return ExposedTo::ShadowRealm;

if (exposed[0] == '(') {
ExposedTo whom = Nobody;
Expand All @@ -453,6 +459,8 @@ static ErrorOr<ExposedTo> parse_exposure_set(IDL::Interface& interface)
whom |= ExposedTo::ServiceWorker;
} else if (candidate == "AudioWorklet"sv) {
whom |= ExposedTo::AudioWorklet;
} else if (candidate == "ShadowRealm"sv) {
whom |= ExposedTo::ShadowRealm;
} else {
s_error_string = ByteString::formatted("Unknown Exposed attribute candidate {} in {} in {}", candidate, exposed, interface.name);
return Error::from_string_view(s_error_string.view());
Expand All @@ -469,7 +477,7 @@ static ErrorOr<ExposedTo> parse_exposure_set(IDL::Interface& interface)
return Error::from_string_view(s_error_string.view());
}

ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed)
ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Interface&>& intrinsics, Vector<IDL::Interface&>& window_exposed, Vector<IDL::Interface&>& dedicated_worker_exposed, Vector<IDL::Interface&>& shared_worker_exposed, Vector<IDL::Interface&>& shadow_realm_exposed)
{
// TODO: Add service worker exposed and audio worklet exposed
auto whom = TRY(parse_exposure_set(interface));
Expand All @@ -485,5 +493,8 @@ ErrorOr<void> add_to_interface_sets(IDL::Interface& interface, Vector<IDL::Inter
if (whom & ExposedTo::SharedWorker)
shared_worker_exposed.append(interface);

if (whom & ExposedTo::ShadowRealm)
shadow_realm_exposed.append(interface);

return {};
}
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/HTML/shadow-realm-btoa.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shadow realm evaluation returned: SGVsbG8sIHdvcmxkIQ==
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/HTML/shadow-realm-url.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Shadow realm evaluation returned: example.com
1 change: 0 additions & 1 deletion Tests/LibWeb/Text/expected/all-window-properties.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ ServiceWorkerContainer
ServiceWorkerRegistration
Set
ShadowRealm
ShadowRealmGlobalScope
ShadowRoot
SharedArrayBuffer
SourceBuffer
Expand Down
8 changes: 8 additions & 0 deletions Tests/LibWeb/Text/input/HTML/shadow-realm-btoa.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="../include.js"></script>
<script>
test(() => {
const realm = new ShadowRealm();
const result = realm.evaluate(`() => self.btoa('Hello, world!')`)();
println(`Shadow realm evaluation returned: ${result}`);
});
</script>
8 changes: 8 additions & 0 deletions Tests/LibWeb/Text/input/HTML/shadow-realm-url.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="../include.js"></script>
<script>
test(() => {
const realm = new ShadowRealm();
const result = realm.evaluate(`() => new URL('https://example.com/path?name=value').hostname`)();
println(`Shadow realm evaluation returned: ${result}`);
});
</script>
18 changes: 18 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/HostDefined.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024, Shannon Booth <[email protected]>
*
* 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);
}

}
26 changes: 26 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/HostDefined.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024, Shannon Booth <[email protected]>
*
* 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;
};

}
8 changes: 8 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/Intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/SyntheticHostDefined.h>

namespace Web::Bindings {

Expand All @@ -28,4 +30,10 @@ bool Intrinsics::is_exposed(StringView name) const
return m_constructors.contains(name) || m_prototypes.contains(name) || m_namespaces.contains(name);
}

Intrinsics& host_defined_intrinsics(JS::Realm& realm)
{
VERIFY(realm.host_defined());
return verify_cast<Bindings::HostDefined>(*realm.host_defined()).intrinsics;
}

}
6 changes: 1 addition & 5 deletions Userland/Libraries/LibWeb/Bindings/Intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>

#define WEB_SET_PROTOTYPE_FOR_INTERFACE_WITH_CUSTOM_NAME(interface_class, interface_name) \
do { \
Expand Down Expand Up @@ -82,10 +81,7 @@ class Intrinsics final : public JS::Cell {
JS::NonnullGCPtr<JS::Realm> m_realm;
};

[[nodiscard]] inline Intrinsics& host_defined_intrinsics(JS::Realm& realm)
{
return *verify_cast<PrincipalHostDefined>(realm.host_defined())->intrinsics;
}
Intrinsics& host_defined_intrinsics(JS::Realm& realm);

template<typename T>
[[nodiscard]] JS::Object& ensure_web_namespace(JS::Realm& realm, FlyString const& namespace_name)
Expand Down
7 changes: 5 additions & 2 deletions Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,20 @@ 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);

// NOTE: This needs to be done after initialization so that the realm has an intrinsics in its [[HostDefined]]
global_object->initialize_web_interfaces();

// 12. Return NormalCompletion(unused).
return {};
};
Expand Down
3 changes: 1 addition & 2 deletions Userland/Libraries/LibWeb/Bindings/PrincipalHostDefined.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 4 additions & 4 deletions Userland/Libraries/LibWeb/Bindings/PrincipalHostDefined.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
#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)
{
}
virtual ~PrincipalHostDefined() override = default;
virtual void visit_edges(JS::Cell::Visitor& visitor) override;

JS::NonnullGCPtr<HTML::EnvironmentSettingsObject> environment_settings_object;
JS::NonnullGCPtr<Intrinsics> intrinsics;
JS::NonnullGCPtr<Page> page;
};

Expand Down
8 changes: 5 additions & 3 deletions Userland/Libraries/LibWeb/Bindings/SyntheticHostDefined.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
}

Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(SOURCES
Bindings/MainThreadVM.cpp
Bindings/OptionConstructor.cpp
Bindings/PlatformObject.cpp
Bindings/HostDefined.cpp
Bindings/PrincipalHostDefined.cpp
Bindings/SyntheticHostDefined.cpp
Clipboard/Clipboard.cpp
Expand Down Expand Up @@ -481,6 +482,7 @@ set(SOURCES
HTML/TokenizedFeatures.cpp
HTML/TrackEvent.cpp
HTML/TraversableNavigable.cpp
HTML/UniversalGlobalScope.cpp
HTML/UserActivation.cpp
HTML/VideoTrack.cpp
HTML/VideoTrackList.cpp
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <LibWeb/Animations/DocumentTimeline.h>
#include <LibWeb/Bindings/DocumentPrototype.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/CSS/AnimationEvent.h>
#include <LibWeb/CSS/CSSAnimation.h>
#include <LibWeb/CSS/CSSImportRule.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/DOM/EventTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/EventTargetPrototype.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/DOM/DOMEventListener.h>
#include <LibWeb/DOM/Document.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <AK/ScopeGuard.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOMURL/DOMURL.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/FileAPI/Blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibWeb/Bindings/BlobPrototype.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/StructuredSerialize.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Event.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <LibJS/Runtime/NativeFunction.h>
#include <LibWeb/Bindings/HTMLDialogElementPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/IDLEventListener.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/NativeFunction.h>
#include <LibWeb/Bindings/HTMLInputElementPrototype.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWeb/HTML/Scripting/Environments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/Bindings/SyntheticHostDefined.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Fetch/Infrastructure/FetchRecord.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <LibWeb/Bindings/PrincipalHostDefined.h>
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
Expand Down
Loading
Loading