From 8ad890cfa69f105549d42fe43e2c016e231ddc9a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 2 Apr 2020 09:54:15 +0200 Subject: [PATCH] js: Handle exceptions thrown during REPL execution We now print thrown exceptions and clear the interpreter state so it can continue running instead of refusing to do anything after an exception has been thrown. Fixes #1572. --- Userland/js.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Userland/js.cpp b/Userland/js.cpp index bfe9309a15..86467cd9d8 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -132,6 +133,12 @@ static void print_date(const JS::Object& date, HashTable&) printf("\033[34;1mDate %s\033[0m", static_cast(date).string().characters()); } +static void print_error(const JS::Object& object, HashTable&) +{ + auto& error = static_cast(object); + printf("\033[34;1m[%s]\033[0m: %s", error.name().characters(), error.message().characters()); +} + void print_value(JS::Value value, HashTable& seen_objects) { if (value.is_object()) { @@ -153,6 +160,8 @@ void print_value(JS::Value value, HashTable& seen_objects) return print_function(object, seen_objects); if (object.is_date()) return print_date(object, seen_objects); + if (object.is_error()) + return print_error(object, seen_objects); return print_object(object, seen_objects); } @@ -268,7 +277,13 @@ void repl(JS::Interpreter& interpreter) program->dump(0); auto result = interpreter.run(*program); - print(result); + if (interpreter.exception()) { + printf("Exception caught: "); + print(interpreter.exception()->value()); + interpreter.clear_exception(); + } else { + print(result); + } } }