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

LibWeb: Start implementing "create and initialize a Document" from HTML

The way we've been creating DOM::Document has been pretty far from what
the spec tells us to do, and this is a first big step towards getting us
closer to spec.

The new Document::create_and_initialize() is called by FrameLoader after
loading a "text/html" resource.

We create the JS Realm and the Window object when creating the Document
(previously, we'd do it on first access to Document::interpreter().)

The realm execution context is owned by the Environment Settings Object.
This commit is contained in:
Andreas Kling 2022-08-04 21:30:33 +02:00
parent 0781bdb23e
commit 602f927982
13 changed files with 315 additions and 78 deletions

View file

@ -16,8 +16,8 @@
namespace Web::HTML {
EnvironmentSettingsObject::EnvironmentSettingsObject(JS::ExecutionContext& realm_execution_context)
: m_realm_execution_context(realm_execution_context)
EnvironmentSettingsObject::EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext> realm_execution_context)
: m_realm_execution_context(move(realm_execution_context))
{
// Register with the responsible event loop so we can perform step 4 of "perform a microtask checkpoint".
responsible_event_loop().register_environment_settings_object({}, *this);
@ -31,7 +31,7 @@ EnvironmentSettingsObject::~EnvironmentSettingsObject()
JS::ExecutionContext& EnvironmentSettingsObject::realm_execution_context()
{
// NOTE: All environment settings objects are created with a realm execution context, so it's stored and returned here in the base class.
return m_realm_execution_context;
return *m_realm_execution_context;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object%27s-realm

View file

@ -105,10 +105,10 @@ struct EnvironmentSettingsObject
bool is_scripting_disabled() const;
protected:
explicit EnvironmentSettingsObject(JS::ExecutionContext& realm_execution_context);
explicit EnvironmentSettingsObject(NonnullOwnPtr<JS::ExecutionContext>);
private:
JS::ExecutionContext& m_realm_execution_context;
NonnullOwnPtr<JS::ExecutionContext> m_realm_execution_context;
EventLoop* m_responsible_event_loop { nullptr };
// https://html.spec.whatwg.org/multipage/webappapis.html#outstanding-rejected-promises-weak-set

View file

@ -10,17 +10,17 @@
namespace Web::HTML {
WindowEnvironmentSettingsObject::WindowEnvironmentSettingsObject(Window& window, JS::ExecutionContext& execution_context)
: EnvironmentSettingsObject(execution_context)
WindowEnvironmentSettingsObject::WindowEnvironmentSettingsObject(Window& window, NonnullOwnPtr<JS::ExecutionContext> execution_context)
: EnvironmentSettingsObject(move(execution_context))
, m_window(window)
{
}
// https://html.spec.whatwg.org/multipage/window-object.html#set-up-a-window-environment-settings-object
void WindowEnvironmentSettingsObject::setup(AK::URL& creation_url, JS::ExecutionContext& execution_context)
void WindowEnvironmentSettingsObject::setup(AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext> execution_context, Optional<Environment> reserved_environment, AK::URL top_level_creation_url, Origin top_level_origin)
{
// 1. Let realm be the value of execution context's Realm component.
auto* realm = execution_context.realm;
auto* realm = execution_context->realm;
VERIFY(realm);
// 2. Let window be realm's global object.
@ -29,17 +29,32 @@ void WindowEnvironmentSettingsObject::setup(AK::URL& creation_url, JS::Execution
// 3. Let settings object be a new environment settings object whose algorithms are defined as follows:
// NOTE: See the functions defined for this class.
auto settings_object = adopt_own(*new WindowEnvironmentSettingsObject(window, execution_context));
auto settings_object = adopt_own(*new WindowEnvironmentSettingsObject(window, move(execution_context)));
// FIXME: 4. If reservedEnvironment is non-null, then:
// FIXME: 1. Set settings object's id to reservedEnvironment's id, target browsing context to reservedEnvironment's target browsing context, and active service worker to reservedEnvironment's active service worker.
// FIXME: 2. Set reservedEnvironment's id to the empty string.
// 4. If reservedEnvironment is non-null, then:
if (reserved_environment.has_value()) {
// FIXME: 1. Set settings object's id to reservedEnvironment's id,
// target browsing context to reservedEnvironment's target browsing context,
// and active service worker to reservedEnvironment's active service worker.
settings_object->target_browsing_context = reserved_environment->target_browsing_context;
// FIXME: 5. Otherwise, set settings object's id to a new unique opaque string, settings object's target browsing context to null, and settings object's active service worker to null.
settings_object->target_browsing_context = nullptr;
// FIXME: 2. Set reservedEnvironment's id to the empty string.
}
// FIXME: 6. Set settings object's creation URL to creationURL, settings object's top-level creation URL to topLevelCreationURL, and settings object's top-level origin to topLevelOrigin.
// 5. Otherwise, ...
else {
// FIXME: ...set settings object's id to a new unique opaque string,
// settings object's target browsing context to null,
// and settings object's active service worker to null.
settings_object->target_browsing_context = nullptr;
}
// 6. Set settings object's creation URL to creationURL,
// settings object's top-level creation URL to topLevelCreationURL,
// and settings object's top-level origin to topLevelOrigin.
settings_object->creation_url = creation_url;
settings_object->top_level_creation_url = top_level_creation_url;
settings_object->top_level_origin = top_level_origin;
// 7. Set realm's [[HostDefined]] field to settings object.
realm->set_host_defined(move(settings_object));

View file

@ -13,7 +13,7 @@ namespace Web::HTML {
class WindowEnvironmentSettingsObject final : public EnvironmentSettingsObject {
public:
static void setup(AK::URL& creation_url, JS::ExecutionContext& execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */);
static void setup(AK::URL const& creation_url, NonnullOwnPtr<JS::ExecutionContext>, Optional<Environment>, AK::URL top_level_creation_url, Origin top_level_origin);
virtual ~WindowEnvironmentSettingsObject() override = default;
@ -24,7 +24,7 @@ public:
virtual CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() override;
private:
WindowEnvironmentSettingsObject(Window&, JS::ExecutionContext& execution_context);
WindowEnvironmentSettingsObject(Window&, NonnullOwnPtr<JS::ExecutionContext>);
NonnullRefPtr<Window> m_window;
};

View file

@ -18,17 +18,17 @@ class WorkerEnvironmentSettingsObject final
: public EnvironmentSettingsObject
, public Weakable<WorkerEnvironmentSettingsObject> {
public:
WorkerEnvironmentSettingsObject(DOM::Document& document, JS::ExecutionContext& execution_context)
: EnvironmentSettingsObject(execution_context)
WorkerEnvironmentSettingsObject(DOM::Document& document, NonnullOwnPtr<JS::ExecutionContext> execution_context)
: EnvironmentSettingsObject(move(execution_context))
, m_document(document)
{
}
static WeakPtr<WorkerEnvironmentSettingsObject> setup(DOM::Document& document, JS::ExecutionContext& execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */)
static WeakPtr<WorkerEnvironmentSettingsObject> setup(DOM::Document& document, NonnullOwnPtr<JS::ExecutionContext> execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */)
{
auto* realm = execution_context.realm;
auto* realm = execution_context->realm;
VERIFY(realm);
auto settings_object = adopt_own(*new WorkerEnvironmentSettingsObject(document, execution_context));
auto settings_object = adopt_own(*new WorkerEnvironmentSettingsObject(document, move(execution_context)));
settings_object->target_browsing_context = nullptr;
realm->set_host_defined(move(settings_object));

View file

@ -24,7 +24,6 @@ Worker::Worker(FlyString const& script_url, WorkerOptions const options, DOM::Do
, m_worker_vm(JS::VM::create(adopt_own(m_custom_data)))
, m_interpreter(JS::Interpreter::create<JS::GlobalObject>(m_worker_vm))
, m_interpreter_scope(*m_interpreter)
, m_execution_context(m_worker_vm->heap())
, m_implicit_port(MessagePort::create())
{
}
@ -153,22 +152,23 @@ void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_setti
// FIXME: This is because I don't know all the libraries well enough to properly setup the environment to spec
// let alone making it a parallel implementation.
m_execution_context.current_node = nullptr;
m_execution_context.this_value = m_worker_scope;
m_execution_context.function_name = "(global execution context)"sv;
m_execution_context.lexical_environment = &m_worker_realm->global_environment();
m_execution_context.variable_environment = &m_worker_realm->global_environment();
m_execution_context.realm = m_worker_realm;
auto execution_context = make<JS::ExecutionContext>(m_worker_vm->heap());
execution_context->current_node = nullptr;
execution_context->this_value = m_worker_scope;
execution_context->function_name = "(global execution context)"sv;
execution_context->lexical_environment = &m_worker_realm->global_environment();
execution_context->variable_environment = &m_worker_realm->global_environment();
execution_context->realm = m_worker_realm;
m_worker_vm->push_execution_context(m_execution_context);
m_worker_realm->set_global_object(*m_worker_scope, m_worker_scope);
m_worker_vm->push_execution_context(*execution_context);
m_worker_realm->set_global_object(m_worker_scope, m_worker_scope);
// 8. Let worker global scope be the global object of realm execution context's Realm component.
// NOTE: This is the DedicatedWorkerGlobalScope or SharedWorkerGlobalScope object created in the previous step.
// 9. Set up a worker environment settings object with realm execution context,
// outside settings, and unsafeWorkerCreationTime, and let inside settings be the result.
m_inner_settings = WorkerEnvironmentSettingsObject::setup(*m_document, m_execution_context);
m_inner_settings = WorkerEnvironmentSettingsObject::setup(*m_document, move(execution_context));
// 10. Set worker global scope's name to the value of options's name member.
// FIXME: name property requires the SharedWorkerGlobalScope or DedicatedWorkerGlobalScope child class to be used

View file

@ -88,7 +88,6 @@ private:
NonnullOwnPtr<JS::Interpreter> m_interpreter;
WeakPtr<WorkerEnvironmentSettingsObject> m_inner_settings;
JS::VM::InterpreterExecutionScope m_interpreter_scope;
JS::ExecutionContext m_execution_context;
WeakPtr<JS::Realm> m_worker_realm;
RefPtr<WorkerDebugConsoleClient> m_console;
JS::GlobalObject* m_worker_scope;