1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

LibJS: Convert Interpreter::run() to ThrowCompletionOr<Value>

Instead of making it a void function, checking for an exception, and
then receiving the relevant result via VM::last_value(), we can
consolidate all of this by using completions.

This allows us to remove more uses of VM::exception(), and all uses of
VM::last_value().
This commit is contained in:
Linus Groh 2022-01-08 21:28:27 +01:00
parent f73afbb5ae
commit eb60d16549
13 changed files with 62 additions and 56 deletions

View file

@ -112,21 +112,21 @@ void EvaluateExpressionDialog::handle_evaluation(const String& expression)
auto program = parser.parse_program();
StringBuilder output_html;
auto result = JS::ThrowCompletionOr<JS::Value> { JS::js_undefined() };
if (parser.has_errors()) {
auto error = parser.errors()[0];
auto hint = error.source_location_hint(expression);
if (!hint.is_empty())
output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
result = m_interpreter->vm().throw_completion<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
} else {
m_interpreter->run(m_interpreter->global_object(), *program);
result = m_interpreter->run(m_interpreter->global_object(), *program);
}
if (m_interpreter->exception()) {
auto* exception = m_interpreter->exception();
if (result.is_error()) {
m_interpreter->vm().clear_exception();
output_html.append("Uncaught exception: ");
auto error = exception->value();
auto error = *result.throw_completion().value();
if (error.is_object())
output_html.append(JS::MarkupGenerator::html_from_error(error.as_object()));
else
@ -135,7 +135,7 @@ void EvaluateExpressionDialog::handle_evaluation(const String& expression)
return;
}
set_output(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
set_output(JS::MarkupGenerator::html_from_value(result.value()));
}
void EvaluateExpressionDialog::set_output(StringView html)