mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:57:35 +00:00
LibJS+Everywhere: Convert JS::Error to String
This includes an Error::create overload to create an Error from a UTF-8 StringView. If creating a String from that view fails, the factory will return an OOM InternalError instead. VM::throw_completion can also make use of this overload via its perfect forwarding.
This commit is contained in:
parent
153b793638
commit
88814acbd3
36 changed files with 198 additions and 151 deletions
|
@ -107,7 +107,7 @@ static bool s_opt_bytecode = false;
|
|||
static bool s_as_module = false;
|
||||
static bool s_print_last_result = false;
|
||||
|
||||
static bool parse_and_run(JS::Interpreter& interpreter, StringView source, StringView source_name)
|
||||
static ErrorOr<bool> parse_and_run(JS::Interpreter& interpreter, StringView source, StringView source_name)
|
||||
{
|
||||
enum class ReturnEarly {
|
||||
No,
|
||||
|
@ -116,14 +116,14 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView source, Strin
|
|||
|
||||
JS::ThrowCompletionOr<JS::Value> result { JS::js_undefined() };
|
||||
|
||||
auto run_script_or_module = [&](auto& script_or_module) {
|
||||
auto run_script_or_module = [&](auto& script_or_module) -> ErrorOr<ReturnEarly> {
|
||||
if (s_dump_ast)
|
||||
script_or_module->parse_node().dump(0);
|
||||
|
||||
if (JS::Bytecode::g_dump_bytecode || s_run_bytecode) {
|
||||
auto executable_result = JS::Bytecode::Generator::generate(script_or_module->parse_node());
|
||||
if (executable_result.is_error()) {
|
||||
result = g_vm->throw_completion<JS::InternalError>(executable_result.error().to_deprecated_string());
|
||||
result = g_vm->throw_completion<JS::InternalError>(TRY(executable_result.error().to_string()));
|
||||
return ReturnEarly::No;
|
||||
}
|
||||
|
||||
|
@ -161,10 +161,12 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView source, Strin
|
|||
auto hint = error.source_location_hint(source);
|
||||
if (!hint.is_empty())
|
||||
displayln("{}", hint);
|
||||
displayln("{}", error.to_deprecated_string());
|
||||
result = interpreter.vm().throw_completion<JS::SyntaxError>(error.to_deprecated_string());
|
||||
|
||||
auto error_string = TRY(error.to_string());
|
||||
displayln("{}", error_string);
|
||||
result = interpreter.vm().throw_completion<JS::SyntaxError>(move(error_string));
|
||||
} else {
|
||||
auto return_early = run_script_or_module(script_or_error.value());
|
||||
auto return_early = TRY(run_script_or_module(script_or_error.value()));
|
||||
if (return_early == ReturnEarly::Yes)
|
||||
return true;
|
||||
}
|
||||
|
@ -175,10 +177,12 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView source, Strin
|
|||
auto hint = error.source_location_hint(source);
|
||||
if (!hint.is_empty())
|
||||
displayln("{}", hint);
|
||||
displayln(error.to_deprecated_string());
|
||||
result = interpreter.vm().throw_completion<JS::SyntaxError>(error.to_deprecated_string());
|
||||
|
||||
auto error_string = TRY(error.to_string());
|
||||
displayln("{}", error_string);
|
||||
result = interpreter.vm().throw_completion<JS::SyntaxError>(move(error_string));
|
||||
} else {
|
||||
auto return_early = run_script_or_module(module_or_error.value());
|
||||
auto return_early = TRY(run_script_or_module(module_or_error.value()));
|
||||
if (return_early == ReturnEarly::Yes)
|
||||
return true;
|
||||
}
|
||||
|
@ -269,7 +273,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::print)
|
|||
{
|
||||
auto result = ::print(vm.argument(0));
|
||||
if (result.is_error())
|
||||
return g_vm->throw_completion<JS::InternalError>(DeprecatedString::formatted("Failed to print value: {}", result.error()));
|
||||
return g_vm->throw_completion<JS::InternalError>(TRY_OR_THROW_OOM(*g_vm, String::formatted("Failed to print value: {}", result.error())));
|
||||
|
||||
displayln();
|
||||
|
||||
|
@ -388,5 +392,10 @@ extern "C" int initialize_repl(char const* time_zone)
|
|||
|
||||
extern "C" bool execute(char const* source)
|
||||
{
|
||||
return parse_and_run(*g_interpreter, { source, strlen(source) }, "REPL"sv);
|
||||
if (auto result = parse_and_run(*g_interpreter, { source, strlen(source) }, "REPL"sv); result.is_error()) {
|
||||
displayln("{}", result.error());
|
||||
return false;
|
||||
} else {
|
||||
return result.value();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue