1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 17:05:06 +00:00

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>
This commit is contained in:
Luke Wilde 2022-01-16 13:16:04 +01:00 committed by Linus Groh
parent 232a8432b7
commit 631bbcd00a
20 changed files with 293 additions and 129 deletions

View file

@ -175,7 +175,6 @@ protected:
void print_file_result(const JSFileResult& file_result) const;
String m_common_path;
RefPtr<JS::Script> m_test_script;
};
class TestRunnerGlobalObject final : public JS::GlobalObject {
@ -279,6 +278,14 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
double start_time = get_time_in_ms();
auto interpreter = JS::Interpreter::create<TestRunnerGlobalObject>(*g_vm);
// Since g_vm is reused for each new interpreter, Interpreter::create will end up pushing multiple
// global execution contexts onto the VM's execution context stack. To prevent this, we immediately
// pop the global execution context off the execution context stack and manually handle pushing
// and popping it. Since the global execution context should be the only thing on the stack
// at interpreter creation, let's assert there is only one.
VERIFY(g_vm->execution_context_stack().size() == 1);
auto& global_execution_context = *g_vm->execution_context_stack().take_first();
// FIXME: This is a hack while we're refactoring Interpreter/VM stuff.
JS::VM::InterpreterExecutionScope scope(*interpreter);
@ -323,26 +330,28 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
}
}
if (!m_test_script) {
auto result = parse_script(m_common_path, interpreter->realm());
if (result.is_error()) {
warnln("Unable to parse test-common.js");
warnln("{}", result.error().error.to_string());
warnln("{}", result.error().hint);
cleanup_and_exit();
}
m_test_script = result.release_value();
// FIXME: Since a new interpreter is created every time with a new realm, we no longer cache the test-common.js file as scripts are parsed for the current realm only.
// Find a way to cache this.
auto result = parse_script(m_common_path, interpreter->realm());
if (result.is_error()) {
warnln("Unable to parse test-common.js");
warnln("{}", result.error().error.to_string());
warnln("{}", result.error().hint);
cleanup_and_exit();
}
auto test_script = result.release_value();
if (g_run_bytecode) {
auto executable = JS::Bytecode::Generator::generate(m_test_script->parse_node());
auto executable = JS::Bytecode::Generator::generate(test_script->parse_node());
executable.name = test_path;
if (JS::Bytecode::g_dump_bytecode)
executable.dump();
JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm());
MUST(bytecode_interpreter.run(executable));
} else {
MUST(interpreter->run(interpreter->global_object(), m_test_script->parse_node()));
g_vm->push_execution_context(global_execution_context, interpreter->global_object());
MUST(interpreter->run(*test_script));
g_vm->pop_execution_context();
}
auto file_script = parse_script(test_path, interpreter->realm());
@ -356,7 +365,9 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm());
(void)bytecode_interpreter.run(executable);
} else {
(void)interpreter->run(interpreter->global_object(), file_script.value()->parse_node());
g_vm->push_execution_context(global_execution_context, interpreter->global_object());
(void)interpreter->run(file_script.value());
g_vm->pop_execution_context();
}
if (g_vm->exception())