1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 15:04:59 +00:00
serenity/Userland/Libraries/LibWeb/HTML/Scripting/ClassicScript.cpp
Luke Wilde 631bbcd00a LibJS: Refactor interpreter to use Script and Source Text Modules
This also refactors interpreter creation to follow
InitializeHostDefinedRealm, but I couldn't fit it in the title :^)

This allows us to follow the spec much more closely rather than being
completely ad-hoc with just the parse node instead of having all the
surrounding data such as the realm of the parse node.

The interpreter creation refactor creates the global execution context
once and doesn't take it off the stack. This allows LibWeb to take the
global execution context and manually handle it, following the HTML
spec. The HTML spec calls this the "realm execution context" of the
environment settings object.

It also allows us to specify the globalThis type, as it can be
different from the global object type. For example, on the web, Window
global objects use a WindowProxy global this value to enforce the same
origin policy on operations like [[GetOwnProperty]].

Finally, it allows us to directly call Program::execute in perform_eval
and perform_shadow_realm_eval as this moves
global_declaration_instantiation into Interpreter::run
(ScriptEvaluation) as per the spec.

Note that this doesn't evalulate Source Text Modules yet or refactor
the bytecode interpreter, that's work for future us :^)

This patch was originally build by Luke for the environment settings
object change but was also needed for modules. So I (davidot) have
modified it with the new completion changes and setup for that.

Co-authored-by: davidot <davidot@serenityos.org>
2022-01-22 01:21:18 +00:00

99 lines
3.3 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ElapsedTimer.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/HTML/Scripting/ClassicScript.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("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("ClassicScript: Running script {}", filename());
(void)rethrow_errors;
auto timer = Core::ElapsedTimer::start_new();
// 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
// FIXME: Interpreter::run doesn't currently return a JS::Completion.
auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
auto result = 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("ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
if (result.is_error()) {
// FIXME: Propagate error according to the spec.
interpreter->vm().clear_exception();
return {};
}
return result.value();
}
ClassicScript::ClassicScript(AK::URL base_url, String filename)
: Script(move(base_url), move(filename))
{
}
ClassicScript::~ClassicScript()
{
}
}