diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-11-11 04:11:56 +0330 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-12 13:01:59 +0000 |
commit | 070d2eaa5116d96d236c08c6f5dd8324d47a8838 (patch) | |
tree | b222714dd6a3bec37cd0b1881933c12b50b28657 | |
parent | 3b0bf05fa54d173c9f2143732326b7c7454063ab (diff) | |
download | serenity-070d2eaa5116d96d236c08c6f5dd8324d47a8838.zip |
LibJS+LibTest+js: Convert BC::Interpreter::run to ThrowCompletionOr<>
Note that this is just a shallow API change.
7 files changed, 19 insertions, 11 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 13227436e3..e89536dbee 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -146,6 +146,12 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable const& e m_manually_entered_frames.take_last(); } + Value exception_value; + if (vm().exception()) { + exception_value = vm().exception()->value(); + vm().clear_exception(); + } + auto return_value = m_return_value.value_or(js_undefined()); m_return_value = {}; @@ -162,6 +168,9 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable const& e vm().finish_execution_generation(); + if (!exception_value.is_empty()) + return { throw_completion(exception_value), move(frame) }; + return { return_value, move(frame) }; } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h index 758f839f19..82d7b6f482 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h @@ -32,14 +32,14 @@ public: Realm& realm() { return m_realm; } VM& vm() { return m_vm; } - Value run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr) + ThrowCompletionOr<Value> run(Bytecode::Executable const& executable, Bytecode::BasicBlock const* entry_point = nullptr) { auto value_and_frame = run_and_return_frame(executable, entry_point); return value_and_frame.value; } struct ValueAndFrame { - Value value; + ThrowCompletionOr<Value> value; OwnPtr<RegisterWindow> frame; }; ValueAndFrame run_and_return_frame(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point); diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 3498569131..3e290b5b1b 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -542,7 +542,7 @@ ThrowCompletionOr<Value> perform_eval(Value x, GlobalObject& caller_realm, Calle executable.name = "eval"sv; if (JS::Bytecode::g_dump_bytecode) executable.dump(); - eval_result = bytecode_interpreter->run(executable); + eval_result = TRY(bytecode_interpreter->run(executable)); } else { auto& ast_interpreter = vm.interpreter(); // FIXME: We need to use evaluate_statements() here because Program::execute() calls global_declaration_instantiation() when it shouldn't diff --git a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp index 22b0b7dba6..d40b39989f 100644 --- a/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp @@ -772,7 +772,7 @@ Completion ECMAScriptFunctionObject::ordinary_call_evaluate_body() return throw_completion(exception->value()); VERIFY(result_and_frame.frame != nullptr); - auto result = result_and_frame.value; + auto result = TRY(result_and_frame.value); // NOTE: Running the bytecode should eventually return a completion. // Until it does, we assume "return" and include the undefined fallback from the call site. diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp index 5c14bbe93b..417bc69e00 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/GeneratorObject.cpp @@ -102,10 +102,7 @@ ThrowCompletionOr<Value> GeneratorObject::next_impl(VM& vm, GlobalObject& global bytecode_interpreter->accumulator() = next_argument.value_or(js_undefined()); } - // Temporarily switch to the captured execution context - vm.push_execution_context(m_execution_context, global_object); - - m_previous_value = bytecode_interpreter->run(*m_generating_function->bytecode_executable(), next_block); + auto next_result = bytecode_interpreter->run(*m_generating_function->bytecode_executable(), next_block); m_frame = move(*bytecode_interpreter->pop_frame()); @@ -113,6 +110,8 @@ ThrowCompletionOr<Value> GeneratorObject::next_impl(VM& vm, GlobalObject& global m_done = TRY(generated_continuation(m_previous_value)) == nullptr; + m_previous_value = TRY(next_result); + result->define_direct_property("value", TRY(generated_value(m_previous_value)), JS::default_attributes); result->define_direct_property("done", Value(m_done), JS::default_attributes); diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h index 3c9deb341b..7993a97841 100644 --- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h +++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h @@ -340,7 +340,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) if (JS::Bytecode::g_dump_bytecode) executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); - bytecode_interpreter.run(executable); + TRY_OR_DISCARD(bytecode_interpreter.run(executable)); } else { interpreter->run(interpreter->global_object(), m_test_script->parse_node()); } @@ -356,7 +356,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path) if (JS::Bytecode::g_dump_bytecode) executable.dump(); JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm()); - bytecode_interpreter.run(executable); + TRY_OR_DISCARD(bytecode_interpreter.run(executable)); } else { interpreter->run(interpreter->global_object(), file_script.value()->parse_node()); } diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp index d94f592264..6c44120bd1 100644 --- a/Userland/Utilities/js.cpp +++ b/Userland/Utilities/js.cpp @@ -837,7 +837,7 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView source, Strin if (s_run_bytecode) { JS::Bytecode::Interpreter bytecode_interpreter(interpreter.global_object(), interpreter.realm()); - bytecode_interpreter.run(executable); + TRY_OR_DISCARD(bytecode_interpreter.run(executable)); } else { return true; } |