Skip to content

Commit

Permalink
Bug 1847849 - Part 1: Use JSErrorBase::column instead of JSErrorRepor…
Browse files Browse the repository at this point in the history
…t::tokenOffset in JSErrorToXPCException. r=mccr8,evilpie

Differential Revision: https://phabricator.services.mozilla.com/D198508
  • Loading branch information
arai-a committed Jan 17, 2024
1 parent c539ebd commit e118de5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion js/xpconnect/src/XPCConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,8 @@ static nsresult JSErrorToXPCException(JSContext* cx, const char* toStringResult,
bestMessage, NS_ConvertUTF8toUTF16(report->filename.c_str()),
linebuf ? nsDependentString(linebuf, report->linebufLength())
: EmptyString(),
report->lineno, report->tokenOffset(), flags, "XPConnect JavaScript"_ns,
report->lineno, report->column.oneOriginValue(), flags,
"XPConnect JavaScript"_ns,
nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(cx));
}

Expand Down
58 changes: 58 additions & 0 deletions js/xpconnect/tests/unit/test_error_to_exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

add_task(async function() {
// Throwing an error inside a JS callback in xpconnect should preserve
// the columnNumber.

const tests = [
// Parser error.
{
throwError() {
eval("a b");
},
messagePattern: /unexpected token/,
lineNumber: 1,
columnNumber: 3,
},
// Runtime error.
{
throwError() { // line = 21
not_found();
},
messagePattern: /is not defined/,
lineNumber: 22,
columnNumber: 9,
},
];

for (const test of tests) {
const { promise, resolve } = Promise.withResolvers();
const listener = {
observe(msg) {
if (msg instanceof Ci.nsIScriptError) {
resolve(msg);
}
}
};

try {
Services.console.registerListener(listener);

try {
const obs = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
obs.addObserver(test.throwError, "test-obs", false);
obs.notifyObservers(null, "test-obs");
} catch {}

const msg = await promise;
Assert.stringMatches(msg.errorMessage, test.messagePattern);
Assert.equal(msg.lineNumber, test.lineNumber);
Assert.equal(msg.columnNumber, test.columnNumber);
} finally {
Services.console.unregisterListener(listener);
}
}
});
2 changes: 2 additions & 0 deletions js/xpconnect/tests/unit/xpcshell.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ support-files = [

["test_envChain_subscript_in_JSM.js"]

["test_error_to_exception.js"]

["test_eventSource.js"]

["test_exportFunction.js"]
Expand Down

0 comments on commit e118de5

Please sign in to comment.