[gjs: 13/15] jsapi-util: Include column number in SyntaxError output
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 13/15] jsapi-util: Include column number in SyntaxError output
- Date: Sun, 16 Jan 2022 21:28:17 +0000 (UTC)
commit f1b47f2b25157e08766573d506ddafd164340b02
Author: Philip Chimento <philip chimento gmail com>
Date: Sat Jan 15 15:28:41 2022 -0800
jsapi-util: Include column number in SyntaxError output
Previously, logging a SyntaxError would print the file name and line
number, if available. This adds the column number as well.
gjs/jsapi-util.cpp | 15 +++++++++++----
installed-tests/scripts/testWarnings.sh | 2 +-
2 files changed, 12 insertions(+), 5 deletions(-)
---
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 61d41a989..5fd6014e7 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -367,8 +367,8 @@ static JSString* exception_to_string(JSContext* cx, JS::HandleValue exc) {
return JS::ToString(cx, exc);
}
-// Helper function: format the file name and line number where a SyntaxError
-// occurred.
+// Helper function: format the file name, line number, and column number where a
+// SyntaxError occurred.
static std::string format_syntax_error_location(JSContext* cx,
JS::HandleObject exc) {
const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
@@ -381,6 +381,13 @@ static std::string format_syntax_error_location(JSContext* cx,
}
JS_ClearPendingException(cx);
+ int32_t column = 0;
+ if (JS_GetPropertyById(cx, exc, atoms.column_number(), &property)) {
+ if (property.isInt32())
+ column = property.toInt32();
+ }
+ JS_ClearPendingException(cx);
+
JS::UniqueChars utf8_filename;
if (JS_GetPropertyById(cx, exc, atoms.file_name(), &property)) {
if (property.isString()) {
@@ -396,7 +403,7 @@ static std::string format_syntax_error_location(JSContext* cx,
out << utf8_filename.get();
else
out << "<unknown>";
- out << ":" << line;
+ out << ":" << line << ":" << column;
return out.str();
}
@@ -429,7 +436,7 @@ static std::string format_exception_log_message(JSContext* cx,
if (JS_InstanceOf(cx, exc_obj, syntax_error, nullptr)) {
// We log syntax errors differently, because the stack for those
// includes only the referencing module, but we want to print out the
- // filename and line number from the exception.
+ // file name, line number, and column number from the exception.
out << format_syntax_error_location(cx, exc_obj);
return out.str();
}
diff --git a/installed-tests/scripts/testWarnings.sh b/installed-tests/scripts/testWarnings.sh
index 621b19c2b..5dbf1c008 100755
--- a/installed-tests/scripts/testWarnings.sh
+++ b/installed-tests/scripts/testWarnings.sh
@@ -29,7 +29,7 @@ $gjs -c 'imports.gi.GLib.get_home_dir("foobar")' 2>&1 | \
report "passing too many arguments to a GI function should warn"
$gjs -c '**' 2>&1 | \
- grep -q 'SyntaxError.*@ <command line>:1'
+ grep -q 'SyntaxError.*@ <command line>:1:0'
report "file and line number are logged for syntax errors"
echo "1..$total"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]