diff options
author | Luke Wilde <lukew@serenityos.org> | 2021-10-14 16:12:53 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-08 17:47:44 +0000 |
commit | f71f404e0c56497bdf1b65a4a69b45de51e8e42c (patch) | |
tree | b02b0d04c3b34c3a167f1ed95ab6d22431d35519 /Userland/Services/WebContent | |
parent | 4db5406d6234bad17d34fe8a0b461dea766271e6 (diff) | |
download | serenity-f71f404e0c56497bdf1b65a4a69b45de51e8e42c.zip |
LibWeb: Introduce the Environment Settings Object
The environment settings object is effectively the context a piece of
script is running under, for example, it contains the origin,
responsible document, realm, global object and event loop for the
current context. This effectively replaces ScriptExecutionContext, but
it cannot be removed in this commit as EventTarget still depends on it.
https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
Diffstat (limited to 'Userland/Services/WebContent')
-rw-r--r-- | Userland/Services/WebContent/ClientConnection.cpp | 26 | ||||
-rw-r--r-- | Userland/Services/WebContent/WebContentConsoleClient.cpp | 37 |
2 files changed, 32 insertions, 31 deletions
diff --git a/Userland/Services/WebContent/ClientConnection.cpp b/Userland/Services/WebContent/ClientConnection.cpp index 843d14ff7a..fcefebfbaf 100644 --- a/Userland/Services/WebContent/ClientConnection.cpp +++ b/Userland/Services/WebContent/ClientConnection.cpp @@ -19,6 +19,7 @@ #include <LibWeb/DOM/Document.h> #include <LibWeb/Dump.h> #include <LibWeb/HTML/BrowsingContext.h> +#include <LibWeb/HTML/Scripting/ClassicScript.h> #include <LibWeb/Layout/InitialContainingBlock.h> #include <LibWeb/Loader/ContentFilter.h> #include <LibWeb/Loader/ResourceLoader.h> @@ -333,20 +334,29 @@ void ClientConnection::js_console_input(const String& js_source) void ClientConnection::run_javascript(String const& js_source) { - if (!page().top_level_browsing_context().active_document()) + auto* active_document = page().top_level_browsing_context().active_document(); + + if (!active_document) return; - auto& interpreter = page().top_level_browsing_context().active_document()->interpreter(); + // This is partially based on "execute a javascript: URL request" https://html.spec.whatwg.org/multipage/browsing-the-web.html#javascript-protocol - auto script_or_error = JS::Script::parse(js_source, interpreter.realm(), ""); - if (script_or_error.is_error()) - return; + // Let settings be browsingContext's active document's relevant settings object. + auto& settings = active_document->relevant_settings_object(); + + // Let baseURL be settings's API base URL. + auto base_url = settings.api_base_url(); - auto result = interpreter.run(script_or_error.value()); + // Let script be the result of creating a classic script given scriptSource, settings, baseURL, and the default classic script fetch options. + // FIXME: This doesn't pass in "default classic script fetch options" + // FIXME: What should the filename be here? + auto script = Web::HTML::ClassicScript::create("(client connection run_javascript)", js_source, settings, move(base_url)); - if (result.is_error()) { + // Let evaluationStatus be the result of running the classic script script. + auto evaluation_status = script->run(); + + if (evaluation_status.is_error()) dbgln("Exception :("); - } } void ClientConnection::js_console_request_messages(i32 start_index) diff --git a/Userland/Services/WebContent/WebContentConsoleClient.cpp b/Userland/Services/WebContent/WebContentConsoleClient.cpp index 6bbbfe3478..9ae9986c3d 100644 --- a/Userland/Services/WebContent/WebContentConsoleClient.cpp +++ b/Userland/Services/WebContent/WebContentConsoleClient.cpp @@ -9,8 +9,11 @@ #include "WebContentConsoleClient.h" #include <LibJS/Interpreter.h> #include <LibJS/MarkupGenerator.h> +#include <LibJS/Parser.h> #include <LibJS/Script.h> #include <LibWeb/Bindings/WindowObject.h> +#include <LibWeb/HTML/Scripting/ClassicScript.h> +#include <LibWeb/HTML/Scripting/Environments.h> #include <WebContent/ConsoleGlobalObject.h> namespace WebContent { @@ -28,31 +31,18 @@ WebContentConsoleClient::WebContentConsoleClient(JS::Console& console, WeakPtr<J void WebContentConsoleClient::handle_input(String const& js_source) { - auto script_or_error = JS::Script::parse(js_source, m_interpreter->realm(), ""); + auto& settings = verify_cast<Web::HTML::EnvironmentSettingsObject>(*m_interpreter->realm().host_defined()); + auto script = Web::HTML::ClassicScript::create("(console)", js_source, settings, settings.api_base_url()); + + // FIXME: Add parse error printouts back once ClassicScript can report parse errors. + + auto result = script->run(); + StringBuilder output_html; - auto result = JS::ThrowCompletionOr<JS::Value> { JS::js_undefined() }; - if (script_or_error.is_error()) { - auto error = script_or_error.error()[0]; - auto hint = error.source_location_hint(js_source); - if (!hint.is_empty()) - output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint))); - result = m_interpreter->vm().throw_completion<JS::SyntaxError>(*m_console_global_object.cell(), error.to_string()); - } else { - // FIXME: This is not the correct way to do this, we probably want to have - // multiple execution contexts we switch between. - auto& global_object_before = m_interpreter->realm().global_object(); - VERIFY(is<Web::Bindings::WindowObject>(global_object_before)); - auto& this_value_before = m_interpreter->realm().global_environment().global_this_value(); - m_interpreter->realm().set_global_object(*m_console_global_object.cell(), &global_object_before); - - result = m_interpreter->run(script_or_error.value()); - - m_interpreter->realm().set_global_object(global_object_before, &this_value_before); - } - if (result.is_error()) { + if (result.is_abrupt()) { output_html.append("Uncaught exception: "); - auto error = *result.throw_completion().value(); + auto error = *result.release_error().value(); if (error.is_object()) output_html.append(JS::MarkupGenerator::html_from_error(error.as_object())); else @@ -61,7 +51,8 @@ void WebContentConsoleClient::handle_input(String const& js_source) return; } - print_html(JS::MarkupGenerator::html_from_value(result.value())); + if (result.value().has_value()) + print_html(JS::MarkupGenerator::html_from_value(*result.value())); } void WebContentConsoleClient::print_html(String const& line) |