Commit 0decd7ed authored by Philip Chimento's avatar Philip Chimento 🚮

console: Refactor AutoReportException for non-object exceptions

Previously, in the interactive interpreter, throwing a non-object
exception (for example, `throw 'foo';`) would crash when trying to print
the exception value in the console.

This changes AutoReportException so that exceptions are handled better
when they are not objects.
parent e2425144
......@@ -52,6 +52,8 @@
#include <glib.h>
#include <glib/gprintf.h>
#include <string>
#include "console.h"
#include "gjs/context.h"
#include "gjs/context-private.h"
......@@ -142,6 +144,20 @@ gjs_console_warning_reporter(JSContext *cx, JSErrorReport *report)
class AutoReportException {
JSContext *m_cx;
JSErrorReport* error_from_exception_value(JS::HandleValue v_exn) const {
if (!v_exn.isObject())
return nullptr;
JS::RootedObject exn(m_cx, &v_exn.toObject());
return JS_ErrorFromException(m_cx, exn);
JSObject* stack_from_exception_value(JS::HandleValue v_exn) const {
if (!v_exn.isObject())
return nullptr;
JS::RootedObject exn(m_cx, &v_exn.toObject());
return ExceptionStackOrNull(exn);
explicit AutoReportException(JSContext *cx) : m_cx(cx) {}
......@@ -153,22 +169,17 @@ public:
JS::RootedValue v_exn(m_cx);
(void) JS_GetPendingException(m_cx, &v_exn);
JS::RootedObject exn(m_cx, &v_exn.toObject());
JSErrorReport *report = JS_ErrorFromException(m_cx, exn);
JSErrorReport* report = error_from_exception_value(v_exn);
if (report) {
} else {
JS::RootedString message(m_cx, JS::ToString(m_cx, v_exn));
if (!message) {
g_printerr("(could not convert thrown exception to string)\n");
} else {
GjsAutoJSChar message_utf8 = JS_EncodeStringToUTF8(m_cx, message);
g_printerr("%s\n", message_utf8.get());
GjsAutoChar string = gjs_value_debug_string(m_cx, v_exn);
g_printerr("error: %s\n", string.get());
JS::RootedObject stack(m_cx, ExceptionStackOrNull(exn));
JS::RootedObject stack(m_cx, stack_from_exception_value(v_exn));
if (stack) {
GjsAutoChar stack_str = gjs_format_stack_trace(m_cx, stack);
if (!stack_str)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment