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
Notes: sideshowbarker 2024-07-17 19:09:28 +09:00
29 changed files with 883 additions and 86 deletions
Userland/Libraries/LibWeb/HTML/Scripting

View file

@ -7,13 +7,16 @@
#include <AK/Debug.h>
#include <LibCore/ElapsedTimer.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/DOM/DOMException.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, JS::Realm& realm, AK::URL base_url, MutedErrors muted_errors)
NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, EnvironmentSettingsObject& environment_settings_object, AK::URL base_url, MutedErrors muted_errors)
{
// 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.)
@ -24,9 +27,9 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
// FIXME: 3. If scripting is disabled for settings, then set source to the empty string.
// 4. Let script be a new classic script that this algorithm will subsequently initialize.
auto script = adopt_ref(*new ClassicScript(move(base_url), move(filename)));
auto script = adopt_ref(*new ClassicScript(move(base_url), move(filename), environment_settings_object));
// FIXME: 5. Set script's settings object to settings.
// 5. Set script's settings object to settings. (NOTE: This was already done when constructing.)
// 6. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
@ -36,16 +39,21 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
script->m_muted_errors = muted_errors;
// FIXME: 9. Set script's parse error and error to rethrow to null.
// NOTE: Error to rethrow was set to null in the construction of ClassicScript. We do not have parse error as it would currently go unused.
// 10. Let result be ParseScript(source, settings's Realm, script).
auto parse_timer = Core::ElapsedTimer::start_new();
auto result = JS::Script::parse(source, realm, script->filename());
auto result = JS::Script::parse(source, environment_settings_object.realm(), script->filename());
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed());
// 11. If result is a list of errors, then:
if (result.is_error()) {
// FIXME: 1. Set script's parse error and its error to rethrow to result[0].
auto& parse_error = result.error().first();
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to parse: {}", parse_error.to_string());
// FIXME: 1. Set script's parse error and its error to rethrow to result[0].
// We do not have parse error as it would currently go unused.
script->m_error_to_rethrow = parse_error;
// 2. Return script.
return script;
@ -59,37 +67,88 @@ NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView s
}
// https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
{
if (!m_script_record) {
// FIXME: Throw a SyntaxError per the spec.
dbgln("ClassicScript: Unable to run script {}", filename());
return {};
auto& global_object = m_settings_object.global_object();
auto& vm = global_object.vm();
// 1. Let settings be the settings object of script. (NOTE: Not necessary)
// 2. Check if we can run script with settings. If this returns "do not run" then return NormalCompletion(empty).
if (m_settings_object.can_run_script() == RunScriptDecision::DoNotRun)
return JS::normal_completion({});
// 3. Prepare to run script given settings.
m_settings_object.prepare_to_run_script();
// 4. Let evaluationStatus be null.
JS::Completion evaluation_status;
// 5. If script's error to rethrow is not null, then set evaluationStatus to Completion { [[Type]]: throw, [[Value]]: script's error to rethrow, [[Target]]: empty }.
if (m_error_to_rethrow.has_value()) {
evaluation_status = vm.throw_completion<JS::SyntaxError>(global_object, m_error_to_rethrow.value().to_string());
} else {
auto timer = Core::ElapsedTimer::start_new();
// 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
evaluation_status = interpreter->run(*m_script_record);
// FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
}
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Running script {}", filename());
(void)rethrow_errors;
// 7. If evaluationStatus is an abrupt completion, then:
if (evaluation_status.is_abrupt()) {
// 1. If rethrow errors is true and script's muted errors is false, then:
if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::No) {
// 1. Clean up after running script with settings.
m_settings_object.clean_up_after_running_script();
auto timer = Core::ElapsedTimer::start_new();
// 2. Rethrow evaluationStatus.[[Value]].
return JS::throw_completion(*evaluation_status.value());
}
// 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
// 2. If rethrow errors is true and script's muted errors is true, then:
if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::Yes) {
// 1. Clean up after running script with settings.
m_settings_object.clean_up_after_running_script();
auto evaluation_status = interpreter->run(*m_script_record);
// 2. Throw a "NetworkError" DOMException.
return Bindings::throw_dom_exception_if_needed(global_object, [] {
return DOM::NetworkError::create("Script error.");
}).release_error();
}
// FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.
// 3. Otherwise, rethrow errors is false. Perform the following steps:
VERIFY(rethrow_errors == RethrowErrors::No);
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
if (evaluation_status.is_error()) {
// FIXME: Propagate error according to the spec.
// 1. Report the exception given by evaluationStatus.[[Value]] for script.
report_exception(evaluation_status);
return {};
// 2. Clean up after running script with settings.
m_settings_object.clean_up_after_running_script();
// 3. Return evaluationStatus.
return evaluation_status;
}
return evaluation_status.value();
// 8. Clean up after running script with settings.
m_settings_object.clean_up_after_running_script();
// 9. If evaluationStatus is a normal completion, then return evaluationStatus.
VERIFY(!evaluation_status.is_abrupt());
return evaluation_status;
// FIXME: 10. If we've reached this point, evaluationStatus was left as null because the script was aborted prematurely during evaluation.
// Return Completion { [[Type]]: throw, [[Value]]: a new "QuotaExceededError" DOMException, [[Target]]: empty }.
}
ClassicScript::ClassicScript(AK::URL base_url, String filename)
ClassicScript::ClassicScript(AK::URL base_url, String filename, EnvironmentSettingsObject& environment_settings_object)
: Script(move(base_url), move(filename))
, m_settings_object(environment_settings_object)
{
}