From 631bbcd00a32773aa22ece270ba00628c1240d4c Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sun, 16 Jan 2022 13:16:04 +0100 Subject: 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 --- Meta/Lagom/Fuzzers/FuzzJs.cpp | 18 ++++++++---------- Meta/Lagom/Fuzzers/FuzzilliJs.cpp | 8 +++----- 2 files changed, 11 insertions(+), 15 deletions(-) (limited to 'Meta/Lagom') diff --git a/Meta/Lagom/Fuzzers/FuzzJs.cpp b/Meta/Lagom/Fuzzers/FuzzJs.cpp index 19dcb8a404..b10b03a54a 100644 --- a/Meta/Lagom/Fuzzers/FuzzJs.cpp +++ b/Meta/Lagom/Fuzzers/FuzzJs.cpp @@ -1,27 +1,25 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2022, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include -#include -#include #include +#include #include #include extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { auto js = StringView(static_cast(data), size); - auto lexer = JS::Lexer(js); - auto parser = JS::Parser(lexer); - auto program = parser.parse_program(); - if (!parser.has_errors()) { - auto vm = JS::VM::create(); - auto interpreter = JS::Interpreter::create(*vm); - (void)interpreter->run(interpreter->global_object(), *program); - } + auto vm = JS::VM::create(); + auto interpreter = JS::Interpreter::create(*vm); + auto parse_result = JS::Script::parse(js, interpreter->realm()); + if (!parse_result.is_error()) + (void)interpreter->run(parse_result.value()); + return 0; } diff --git a/Meta/Lagom/Fuzzers/FuzzilliJs.cpp b/Meta/Lagom/Fuzzers/FuzzilliJs.cpp index 530e570601..e5190b310c 100644 --- a/Meta/Lagom/Fuzzers/FuzzilliJs.cpp +++ b/Meta/Lagom/Fuzzers/FuzzilliJs.cpp @@ -207,13 +207,11 @@ int main(int, char**) auto js = StringView(static_cast(data_buffer.data()), script_size); - auto lexer = JS::Lexer(js); - auto parser = JS::Parser(lexer); - auto program = parser.parse_program(); - if (parser.has_errors()) { + auto parse_result = JS::Script::parse(js, interpreter->realm()); + if (parse_result.is_error()) { result = 1; } else { - auto completion = interpreter->run(interpreter->global_object(), *program); + auto completion = interpreter->run(parse_result.value()); if (completion.is_error()) { result = 1; vm->clear_exception(); -- cgit v1.2.3