LibWeb: Implement clean_up_after_running_script on a Realm

Taking further steps towards implementing the shadow realm spec :^)
This commit is contained in:
Shannon Booth 2024-10-21 16:18:25 +13:00 committed by Andrew Kaster
commit d6fdaf6b26
Notes: github-actions[bot] 2024-11-01 19:16:25 +00:00
10 changed files with 35 additions and 31 deletions

View file

@ -112,8 +112,8 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
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.
settings.clean_up_after_running_script();
// 1. Clean up after running script with realm.
clean_up_after_running_script(realm);
// 2. Rethrow evaluationStatus.[[Value]].
return JS::throw_completion(*evaluation_status.value());
@ -121,8 +121,8 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
// 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.
settings.clean_up_after_running_script();
// 1. Clean up after running script with realm.
clean_up_after_running_script(realm);
// 2. Throw a "NetworkError" DOMException.
return throw_completion(WebIDL::NetworkError::create(realm, "Script error."_string));
@ -136,15 +136,15 @@ JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
VERIFY(window_or_worker);
window_or_worker->report_an_exception(*evaluation_status.value());
// 2. Clean up after running script with settings.
settings.clean_up_after_running_script();
// 2. Clean up after running script with realm.
clean_up_after_running_script(realm);
// 3. Return evaluationStatus.
return evaluation_status;
}
// 8. Clean up after running script with settings.
settings.clean_up_after_running_script();
// 8. Clean up after running script with realm.
clean_up_after_running_script(realm);
// 9. If evaluationStatus is a normal completion, then return evaluationStatus.
VERIFY(!evaluation_status.is_abrupt());

View file

@ -2,6 +2,7 @@
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, networkException <networkexception@serenityos.org>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -140,19 +141,20 @@ JS::ExecutionContext const& execution_context_of_realm(JS::Realm const& realm)
}
// https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-script
void EnvironmentSettingsObject::clean_up_after_running_script()
// https://whatpr.org/html/9893/webappapis.html#clean-up-after-running-script
void clean_up_after_running_script(JS::Realm const& realm)
{
auto& vm = global_object().vm();
auto& vm = realm.global_object().vm();
// 1. Assert: settings's realm execution context is the running JavaScript execution context.
VERIFY(&realm_execution_context() == &vm.running_execution_context());
// 1. Assert: realm's execution context is the running JavaScript execution context.
VERIFY(&execution_context_of_realm(realm) == &vm.running_execution_context());
// 2. Remove settings's realm execution context from the JavaScript execution context stack.
// 2. Remove realm's execution context from the JavaScript execution context stack.
vm.pop_execution_context();
// 3. If the JavaScript execution context stack is now empty, perform a microtask checkpoint. (If this runs scripts, these algorithms will be invoked reentrantly.)
if (vm.execution_context_stack().is_empty())
responsible_event_loop().perform_a_microtask_checkpoint();
main_thread_event_loop().perform_a_microtask_checkpoint();
}
static JS::ExecutionContext* top_most_script_having_execution_context(JS::VM& vm)

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -96,7 +97,6 @@ public:
Vector<JS::NonnullGCPtr<Fetch::Infrastructure::FetchRecord>>& fetch_group() { return m_fetch_group; }
void prepare_to_run_script();
void clean_up_after_running_script();
void prepare_to_run_callback();
void clean_up_after_running_callback();
@ -141,6 +141,7 @@ JS::ExecutionContext const& execution_context_of_realm(JS::Realm const&);
RunScriptDecision can_run_script(JS::Realm const&);
bool is_scripting_enabled(JS::Realm const&);
bool is_scripting_disabled(JS::Realm const&);
void clean_up_after_running_script(JS::Realm const&);
EnvironmentSettingsObject& incumbent_settings_object();
JS::Realm& incumbent_realm();

View file

@ -177,8 +177,8 @@ JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting)
// FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
// 8. Clean up after running script with settings.
settings.clean_up_after_running_script();
// 8. Clean up after running script with realm.
clean_up_after_running_script(realm);
// 9. Return evaluationPromise.
return evaluation_promise;

View file

@ -20,7 +20,7 @@ TemporaryExecutionContext::TemporaryExecutionContext(EnvironmentSettingsObject&
TemporaryExecutionContext::~TemporaryExecutionContext()
{
m_environment_settings->clean_up_after_running_script();
clean_up_after_running_script(m_environment_settings->realm());
if (m_callbacks_enabled == CallbacksEnabled::Yes)
m_environment_settings->clean_up_after_running_callback();
}