1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibWeb: Add a simple internals objects only available during testing

This object is available as `window.internals` (or just `internals`) and
is only accessible while running in "test mode".

This first version only has one API: gc(), which triggers a garbage
collection immediately.

In the future, we can add more APIs here to help us test parts of the
engine that are hard or impossible to reach via public web APIs.
This commit is contained in:
Andreas Kling 2023-07-21 11:59:49 +02:00
parent ba236e3f21
commit ec24d7555a
14 changed files with 109 additions and 5 deletions

View file

@ -397,6 +397,7 @@ set(SOURCES
Infra/ByteSequences.cpp
Infra/JSON.cpp
Infra/Strings.cpp
Internals/Internals.cpp
IntersectionObserver/IntersectionObserver.cpp
IntersectionObserver/IntersectionObserverEntry.cpp
Layout/AudioBox.cpp

View file

@ -454,6 +454,10 @@ namespace Web::HighResolutionTime {
class Performance;
}
namespace Web::Internals {
class Internals;
}
namespace Web::IntersectionObserver {
class IntersectionObserver;
class IntersectionObserverEntry;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
*
@ -51,6 +51,7 @@
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Infra/Base64.h>
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Internals/Internals.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/RequestIdleCallback/IdleDeadline.h>
@ -805,6 +806,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::CallbackType>> Window::byte_length_
return JS::NonnullGCPtr { *m_byte_length_queuing_strategy_size_function };
}
static bool s_internals_object_exposed = false;
void Window::set_internals_object_exposed(bool exposed)
{
s_internals_object_exposed = exposed;
}
WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironmentSettingsObject>)
{
auto& realm = this->realm();
@ -815,6 +823,9 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
MUST_OR_THROW_OOM(Bindings::WindowGlobalMixin::initialize(realm, *this));
MUST_OR_THROW_OOM(WindowOrWorkerGlobalScopeMixin::initialize(realm));
if (s_internals_object_exposed)
define_direct_property("internals", MUST_OR_THROW_OOM(heap().allocate<Internals::Internals>(realm, realm)), JS::default_attributes);
return {};
}

View file

@ -190,6 +190,8 @@ public:
HighResolutionTime::DOMHighResTimeStamp get_last_activation_timestamp() const { return m_last_activation_timestamp; }
void set_last_activation_timestamp(HighResolutionTime::DOMHighResTimeStamp timestamp) { m_last_activation_timestamp = timestamp; }
static void set_internals_object_exposed(bool);
private:
explicit Window(JS::Realm&);

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/InternalsPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Internals/Internals.h>
namespace Web::Internals {
Internals::Internals(JS::Realm& realm)
: Bindings::PlatformObject(realm)
{
}
Internals::~Internals() = default;
JS::ThrowCompletionOr<void> Internals::initialize(JS::Realm& realm)
{
TRY(Base::initialize(realm));
Object::set_prototype(&Bindings::ensure_web_prototype<Bindings::InternalsPrototype>(realm, "Internals"));
return {};
}
void Internals::gc()
{
vm().heap().collect_garbage();
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/PlatformObject.h>
namespace Web::Internals {
class Internals final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(Internals, Bindings::PlatformObject);
public:
virtual ~Internals() override;
void gc();
private:
explicit Internals(JS::Realm&);
virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
};
}

View file

@ -0,0 +1,5 @@
[Exposed=Nobody] interface Internals {
undefined gc();
};

View file

@ -183,6 +183,7 @@ libweb_js_bindings(HTML/WorkerGlobalScope)
libweb_js_bindings(HTML/WorkerLocation)
libweb_js_bindings(HTML/WorkerNavigator)
libweb_js_bindings(HighResolutionTime/Performance)
libweb_js_bindings(Internals/Internals)
libweb_js_bindings(IntersectionObserver/IntersectionObserver)
libweb_js_bindings(IntersectionObserver/IntersectionObserverEntry)
libweb_js_bindings(NavigationTiming/PerformanceTiming)