[gjs/wip/ptomato/mozjs52: 33/33] js: Replace error reporter callbacks



commit b8a90b595354c1339b44d8def25ebef3b110bd35
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jun 10 18:13:23 2017 -0700

    js: Replace error reporter callbacks
    
    JS_SetErrorReporter() goes away in SpiderMonkey 52, and is replaced by
    JS::SetWarningReporter(). Errors should now be reported manually after
    each call into JS code. (We don't actually have to change much, since we
    already used the DontReportUncaught option and reported uncaught
    exceptions manually already. This is now the only choice.)

 gjs/jsapi-private.cpp |   10 ++++++----
 gjs/jsapi-util.h      |    5 ++---
 gjs/runtime.cpp       |    2 +-
 modules/console.cpp   |   33 +++++++++++++++++++++++----------
 4 files changed, 32 insertions(+), 18 deletions(-)
---
diff --git a/gjs/jsapi-private.cpp b/gjs/jsapi-private.cpp
index cf16f73..f6584ef 100644
--- a/gjs/jsapi-private.cpp
+++ b/gjs/jsapi-private.cpp
@@ -32,13 +32,14 @@
 #include "jsapi-wrapper.h"
 
 void
-gjs_error_reporter(JSContext     *context,
-                   const char    *message,
-                   JSErrorReport *report)
+gjs_warning_reporter(JSContext     *context,
+                     JSErrorReport *report)
 {
     const char *warning;
     GLogLevelFlags level;
 
+    g_assert(report);
+
     if (gjs_environment_variable_is_set("GJS_ABORT_ON_OOM") &&
         report->flags == JSREPORT_ERROR &&
         report->errorNumber == 137) {
@@ -66,5 +67,6 @@ gjs_error_reporter(JSContext     *context,
         level = G_LOG_LEVEL_WARNING;
     }
 
-    g_log(G_LOG_DOMAIN, level, "JS %s: [%s %d]: %s", warning, report->filename, report->lineno, message);
+    g_log(G_LOG_DOMAIN, level, "JS %s: [%s %d]: %s", warning, report->filename,
+          report->lineno, report->message().c_str());
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 44bc372..4addbe6 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -233,9 +233,8 @@ bool gjs_call_function_value(JSContext                  *context,
                              const JS::HandleValueArray& args,
                              JS::MutableHandleValue      rval);
 
-void        gjs_error_reporter               (JSContext       *context,
-                                              const char      *message,
-                                              JSErrorReport   *report);
+void gjs_warning_reporter(JSContext     *cx,
+                          JSErrorReport *report);
 
 bool        gjs_string_to_utf8               (JSContext       *context,
                                               const JS::Value  string_val,
diff --git a/gjs/runtime.cpp b/gjs/runtime.cpp
index 356d687..0272255 100644
--- a/gjs/runtime.cpp
+++ b/gjs/runtime.cpp
@@ -213,7 +213,7 @@ gjs_create_js_context(GjsContext *js_context)
     // JS_SetGCParameter(cx, JSGC_ALLOCATION_THRESHOLD, 30);
     // JS_SetGCParameter(cx, JSGC_DECOMMIT_THRESHOLD, 32);
     JS_SetLocaleCallbacks(cx, &gjs_locale_callbacks);
-    JS_SetErrorReporter(cx, gjs_error_reporter);
+    JS::SetWarningReporter(cx, gjs_warning_reporter);
 
     return cx;
 }
diff --git a/modules/console.cpp b/modules/console.cpp
index 03e72f8..30375f5 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -58,15 +58,11 @@
 #include "gjs/jsapi-wrapper.h"
 
 static void
-gjs_console_print_error(const char *message, JSErrorReport *report)
+gjs_console_print_error(JSErrorReport *report)
 {
     /* Code modified from SpiderMonkey js/src/jscntxt.cpp, js::PrintError() */
 
-    if (!report) {
-        fprintf(stderr, "%s\n", message);
-        fflush(stderr);
-        return;
-    }
+    g_assert(report);
 
     char *prefix = nullptr;
     if (report->filename)
@@ -85,6 +81,8 @@ gjs_console_print_error(const char *message, JSErrorReport *report)
         g_free(tmp);
     }
 
+    const char *message = report->message().c_str();
+
     /* embedded newlines -- argh! */
     const char *ctmp;
     while ((ctmp = strchr(message, '\n')) != 0) {
@@ -135,9 +133,9 @@ gjs_console_print_error(const char *message, JSErrorReport *report)
 }
 
 static void
-gjs_console_error_reporter(JSContext *cx, const char *message, JSErrorReport *report)
+gjs_console_warning_reporter(JSContext *cx, JSErrorReport *report)
 {
-    gjs_console_print_error(message, report);
+    gjs_console_print_error(report);
 }
 
 /* Based on js::shell::AutoReportException from SpiderMonkey. */
@@ -155,6 +153,8 @@ public:
         JS::RootedValue v_exn(m_cx);
         (void) JS_GetPendingException(m_cx, &v_exn);
 
+        JS_ClearPendingException(m_cx);
+
         JS::RootedObject exn(m_cx, &v_exn.toObject());
         JSErrorReport *report = JS_ErrorFromException(m_cx, exn);
         if (!report) {
@@ -166,7 +166,20 @@ public:
 
         MOZ_ASSERT(!JSREPORT_IS_WARNING(report->flags));
 
-        JS_ReportPendingException(m_cx);
+        gjs_console_print_error(report->message().c_str(), report);
+
+        {
+            JS::AutoSaveExceptionState savedExc(m_cx);
+            JS::RootedObject stack(m_cx, ExceptionStackOrNull(exn));
+            if (stack) {
+                JS::RootedString stack_trace(m_cx);
+                if (!JS::BuildStackString(m_cx, stack, &stack_trace, 2))
+                    fputs("(Unable to print stack trace)\n", stderr);
+            }
+            savedExc.restore();
+        }
+
+        JS_ClearPendingException(m_cx);
     }
 };
 
@@ -249,7 +262,7 @@ gjs_console_interact(JSContext *context,
     int startline;
     FILE *file = stdin;
 
-    JS_SetErrorReporter(JS_GetRuntime(context), gjs_console_error_reporter);
+    JS::SetWarningReporter(context, gjs_console_warning_reporter);
 
         /* It's an interactive filehandle; drop into read-eval-print loop. */
     lineno = 1;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]