[gjs/mozjs78: 22/23] Adapt to new JS::PrintError API. - Remove code copied from upstream.
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/mozjs78: 22/23] Adapt to new JS::PrintError API. - Remove code copied from upstream.
- Date: Sun, 5 Jul 2020 03:41:20 +0000 (UTC)
commit b68dbf1983c056c60262e17bde40a140047e2ab4
Author: Evan Welsh <noreply evanwelsh com>
Date: Sat Jul 4 22:30:15 2020 -0500
Adapt to new JS::PrintError API.
- Remove code copied from upstream.
modules/console.cpp | 132 +++-------------------------------------------------
1 file changed, 6 insertions(+), 126 deletions(-)
---
diff --git a/modules/console.cpp b/modules/console.cpp
index 7f8b9477..03dcae71 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -55,6 +55,7 @@
#include <js/CompilationAndEvaluation.h>
#include <js/CompileOptions.h>
#include <js/ErrorReport.h>
+#include <js/Exception.h>
#include <js/RootingAPI.h>
#include <js/SourceText.h>
#include <js/TypeDecls.h>
@@ -73,128 +74,6 @@ namespace mozilla {
union Utf8Unit;
}
-enum class PrintErrorKind { Error, Warning, StrictWarning, Note };
-
-template <typename T>
-static void print_single_error(T* report, PrintErrorKind kind);
-static void print_error_line(const char* prefix, JSErrorReport* report);
-static void print_error_line(const char*, JSErrorNotes::Note*) {}
-
-static void
-gjs_console_print_error(JSErrorReport *report)
-{
- // Code modified from SpiderMonkey js/src/vm/JSContext.cpp, js::PrintError()
-
- g_assert(report);
-
- 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 void print_single_error(T* report, PrintErrorKind kind) {
- JS::UniqueChars prefix;
- if (report->filename)
- prefix.reset(g_strdup_printf("%s:", report->filename));
-
- if (report->lineno) {
- prefix.reset(g_strdup_printf("%s%u:%u ", prefix ? prefix.get() : "",
- report->lineno, report->column));
- }
-
- 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();
-
- /* embedded newlines -- argh! */
- const char *ctmp;
- while ((ctmp = strchr(message, '\n')) != 0) {
- ctmp++;
- if (prefix)
- 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.get(), stderr);
- fputs(message, stderr);
-
- print_error_line(prefix.get(), report);
- fputc('\n', stderr);
-
- fflush(stderr);
-}
-
-static void print_error_line(const char* prefix, JSErrorReport* report) {
- if (const char16_t* linebuf = report->linebuf()) {
- size_t n = report->linebufLength();
-
- fputs(":\n", stderr);
- if (prefix)
- fputs(prefix, stderr);
-
- for (size_t i = 0; i < n; i++)
- fputc(static_cast<char>(linebuf[i]), stderr);
-
- // linebuf usually ends with a newline. If not, add one here.
- if (n == 0 || linebuf[n - 1] != '\n')
- fputc('\n', stderr);
-
- if (prefix)
- fputs(prefix, stderr);
-
- n = report->tokenOffset();
- for (size_t i = 0, j = 0; i < n; i++) {
- if (linebuf[i] == '\t') {
- for (size_t k = (j + 8) & ~7; j < k; j++)
- fputc('.', stderr);
- continue;
- }
- fputc('.', stderr);
- j++;
- }
- fputc('^', stderr);
- }
-}
-
-static void gjs_console_warning_reporter(JSContext*, JSErrorReport* report) {
- gjs_console_print_error(report);
-}
-
/* Based on js::shell::AutoReportException from SpiderMonkey. */
class AutoReportException {
JSContext *m_cx;
@@ -226,8 +105,7 @@ public:
JSErrorReport* report = error_from_exception_value(v_exn);
if (report) {
- g_assert(!JSREPORT_IS_WARNING(report->flags));
- gjs_console_print_error(report);
+ JS::PrintError(m_cx, stderr, report, false);
} else {
GjsAutoChar display_str = gjs_value_debug_string(m_cx, v_exn);
g_printerr("error: %s\n", display_str.get());
@@ -325,9 +203,11 @@ gjs_console_interact(JSContext *context,
int lineno;
int startline;
- JS::SetWarningReporter(context, gjs_console_warning_reporter);
+ JS::SetWarningReporter(context, [](JSContext* cx, JSErrorReport* report) {
+ JS::PrintError(cx, stderr, report, true);
+ });
- /* It's an interactive filehandle; drop into read-eval-print loop. */
+ /* It's an interactive filehandle; drop into read-eval-print loop. */
lineno = 1;
do {
/*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]