[gjs: 4/16] console: Include js::PrintError() improvements from upstream



commit af61a09c614495f420d89616c1ae69c91ae56087
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Sep 5 23:40:45 2018 -0400

    console: Include js::PrintError() improvements from upstream
    
    This function is mostly copied from SpiderMonkey; include the new
    improvements from the SpiderMonkey 60 codebase.

 modules/console.cpp | 87 ++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 66 insertions(+), 21 deletions(-)
---
diff --git a/modules/console.cpp b/modules/console.cpp
index 07f136da..fe7c5888 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -38,8 +38,9 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
-#include "config.h"
+#include <config.h>
 
+#include <mozilla/Unused.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -57,47 +58,94 @@
 #include "gjs/context-private.h"
 #include "gjs/jsapi-wrapper.h"
 
+enum class PrintErrorKind { Error, Warning, StrictWarning, Note };
+
+template <typename T>
+static bool print_single_error(T* report, PrintErrorKind kind);
+static void print_error_line(const char* prefix, JSErrorNotes::Note* note) {}
+static void print_error_line(const char* prefix, JSErrorReport* report);
+
 static void
 gjs_console_print_error(JSErrorReport *report)
 {
-    /* Code modified from SpiderMonkey js/src/jscntxt.cpp, js::PrintError() */
+    // Code modified from SpiderMonkey js/src/vm/JSContext.cpp, js::PrintError()
 
     g_assert(report);
 
-    char *prefix = nullptr;
+    PrintErrorKind kind = PrintErrorKind::Error;
+    if (JSREPORT_IS_WARNING(report->flags)) {
+        if (JSREPORT_IS_STRICT(report->flags))
+            kind = PrintErrorKind::StrictWarning;
+        else
+            kind = PrintErrorKind::Warning;
+    }
+    print_single_error(report, kind);
+
+    if (report->notes) {
+        for (auto&& note : *report->notes)
+            print_single_error(note.get(), PrintErrorKind::Note);
+    }
+
+    return;
+}
+
+template <typename T>
+static bool print_single_error(T* report, PrintErrorKind kind) {
+    JS::UniqueChars prefix;
     if (report->filename)
-        prefix = g_strdup_printf("%s:", report->filename);
+        prefix.reset(g_strdup_printf("%s:", report->filename));
+
     if (report->lineno) {
-        char *tmp = prefix;
-        prefix = g_strdup_printf("%s%u:%u ", tmp ? tmp : "", report->lineno,
-                                 report->column);
-        g_free(tmp);
+        prefix.reset(g_strdup_printf("%s%u:%u ", prefix ? prefix.get() : "",
+                                     report->lineno, report->column));
     }
-    if (JSREPORT_IS_WARNING(report->flags)) {
-        char *tmp = prefix;
-        prefix = g_strdup_printf("%s%swarning: ",
-                                 tmp ? tmp : "",
-                                 JSREPORT_IS_STRICT(report->flags) ? "strict " : "");
-        g_free(tmp);
+
+    if (kind != PrintErrorKind::Error) {
+        const char* kindPrefix = nullptr;
+        switch (kind) {
+            case PrintErrorKind::Warning:
+                kindPrefix = "warning";
+                break;
+            case PrintErrorKind::StrictWarning:
+                kindPrefix = "strict warning";
+                break;
+            case PrintErrorKind::Note:
+                kindPrefix = "note";
+                break;
+            case PrintErrorKind::Error:
+            default:
+                g_assert_not_reached();
+        }
+
+        prefix.reset(
+            g_strdup_printf("%s%s: ", prefix ? prefix.get() : "", kindPrefix));
     }
 
-    const char *message = report->message().c_str();
+    const char* message = report->message().c_str();
 
     /* embedded newlines -- argh! */
     const char *ctmp;
     while ((ctmp = strchr(message, '\n')) != 0) {
         ctmp++;
         if (prefix)
-            fputs(prefix, stderr);
-        fwrite(message, 1, ctmp - message, stderr);
+            fputs(prefix.get(), stderr);
+        mozilla::Unused << fwrite(message, 1, ctmp - message, stderr);
         message = ctmp;
     }
 
     /* If there were no filename or lineno, the prefix might be empty */
     if (prefix)
-        fputs(prefix, stderr);
+        fputs(prefix.get(), stderr);
     fputs(message, stderr);
 
+    print_error_line(prefix.get(), report);
+    fputc('\n', stderr);
+
+    fflush(stderr);
+    return true;
+}
+
+static void print_error_line(const char* prefix, JSErrorReport* report) {
     if (const char16_t* linebuf = report->linebuf()) {
         size_t n = report->linebufLength();
 
@@ -127,9 +175,6 @@ gjs_console_print_error(JSErrorReport *report)
         }
         fputc('^', stderr);
     }
-    fputc('\n', stderr);
-    fflush(stderr);
-    g_free(prefix);
 }
 
 static void


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