1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +00:00

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
This commit is contained in:
Luke Wilde 2021-10-14 16:12:53 +01:00 committed by Linus Groh
parent 4db5406d62
commit f71f404e0c
29 changed files with 883 additions and 86 deletions

View file

@ -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();
auto result = interpreter.run(script_or_error.value());
// Let baseURL be settings's API base URL.
auto base_url = settings.api_base_url();
if (result.is_error()) {
// 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));
// 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)

View file

@ -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)