ladybird/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp
davidot 9264f9d24e LibJS+Everywhere: Remove VM::exception() and most related functions
This commit removes all exception related code:
Remove VM::exception(), VM::throw_exception() etc. Any leftover
throw_exception calls are moved to throw_completion.
The one method left is clear_exception() which is now a no-op. Most of
these calls are just to clear whatever exception might have been thrown
when handling a Completion. So to have a cleaner commit this will be
removed in a next commit.

It also removes the actual Exception and TemporaryClearException classes
since these are no longer used.

In any spot where the exception was actually used an attempt was made to
preserve that behavior. However since it is no longer tracked by the VM
we cannot access exceptions which were thrown in previous calls.
There are two such cases which might have different behavior:
- In Web::DOM::Document::interpreter() the on_call_stack_emptied hook
  used to print any uncaught exception but this is now no longer
  possible as the VM does not store uncaught exceptions.
- In js the code used to be interruptable by throwing an exception on
  the VM. This is no longer possible but was already somewhat fragile
  before as you could happen to throw an exception just before a VERIFY.
2022-02-08 09:12:42 +00:00

100 lines
3.4 KiB
C++

/*
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Debug.h>
#include <LibCore/ElapsedTimer.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/HTML/Scripting/ClassicScript.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)
{
// 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.)
// 2. If muted errors is true, then set baseURL to about:blank.
if (muted_errors == MutedErrors::Yes)
base_url = "about:blank";
// 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)));
// FIXME: 5. Set script's settings object to settings.
// 6. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
// FIXME: 7. Set script's fetch options to options.
// 8. Set script's muted errors to muted errors.
script->m_muted_errors = muted_errors;
// FIXME: 9. Set script's parse error and error to rethrow to null.
// 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());
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].
// 2. Return script.
return script;
}
// 12. Set script's record to result.
script->m_script_record = result.release_value();
// 13. Return script.
return script;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
{
if (!m_script_record) {
// FIXME: Throw a SyntaxError per the spec.
dbgln("ClassicScript: Unable to run script {}", filename());
return {};
}
dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Running script {}", filename());
(void)rethrow_errors;
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());
auto 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());
if (evaluation_status.is_error()) {
// FIXME: Propagate error according to the spec.
report_exception(evaluation_status);
return {};
}
return evaluation_status.value();
}
ClassicScript::ClassicScript(AK::URL base_url, String filename)
: Script(move(base_url), move(filename))
{
}
ClassicScript::~ClassicScript()
{
}
}