summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs
diff options
context:
space:
mode:
authorMatt Cotter <matt.cotter@mongodb.com>2016-09-12 12:05:56 -0400
committerMatt Cotter <matt.cotter@mongodb.com>2016-09-15 15:00:13 -0400
commit7b6cf36cb72558ed199f4ecde945ac3723229c3a (patch)
tree4ee89e4ca9241233ac4fa55e744909d63e899226 /src/mongo/scripting/mozjs
parent2c44906471c2bab952bb14f662fcb24c8c644872 (diff)
downloadmongo-7b6cf36cb72558ed199f4ecde945ac3723229c3a.tar.gz
SERVER-23135 properly print context on js load
This patch ensures that we print the correct filename and line number for syntax errors encountered in included js files via `load()`.
Diffstat (limited to 'src/mongo/scripting/mozjs')
-rw-r--r--src/mongo/scripting/mozjs/implscope.cpp18
-rw-r--r--src/mongo/scripting/mozjs/internedstring.defs4
2 files changed, 16 insertions, 6 deletions
diff --git a/src/mongo/scripting/mozjs/implscope.cpp b/src/mongo/scripting/mozjs/implscope.cpp
index 32562846c94..00cbe8b541c 100644
--- a/src/mongo/scripting/mozjs/implscope.cpp
+++ b/src/mongo/scripting/mozjs/implscope.cpp
@@ -687,7 +687,7 @@ bool MozJSImplScope::exec(StringData code,
JS::CompileOptions co(_context);
setCompileOptions(&co);
- co.setFile(name.c_str());
+ co.setFileAndLine(name.c_str(), 1);
JS::RootedScript script(_context);
bool success = JS::Compile(_context, co, code.rawData(), code.size(), &script);
@@ -847,12 +847,18 @@ bool MozJSImplScope::_checkErrorState(bool success, bool reportError, bool asser
if (JS_GetPendingException(_context, &excn) && excn.isObject()) {
str::stream ss;
- JS::RootedValue stack(_context);
+ auto stackStr = ObjectWrapper(_context, excn).getString(InternedString::stack);
+ auto fnameStr = ObjectWrapper(_context, excn).getString(InternedString::fileName);
+ auto lineNum = ObjectWrapper(_context, excn).getNumberInt(InternedString::lineNumber);
+ auto colNum = ObjectWrapper(_context, excn).getNumberInt(InternedString::columnNumber);
- ObjectWrapper(_context, excn).getValue("stack", &stack);
-
- ss << ValueWriter(_context, excn).toString() << " :\n"
- << ValueWriter(_context, stack).toString();
+ if (fnameStr != "") {
+ ss << "[" << fnameStr << ":" << lineNum << ":" << colNum << "] ";
+ }
+ ss << ValueWriter(_context, excn).toString();
+ if (stackStr != "") {
+ ss << "\nStack trace:\n" << stackStr << "----------\n";
+ }
_status = Status(ErrorCodes::JSInterpreterFailure, ss);
} else {
_status = Status(ErrorCodes::UnknownError, "Unknown Failure from JSInterpreter");
diff --git a/src/mongo/scripting/mozjs/internedstring.defs b/src/mongo/scripting/mozjs/internedstring.defs
index fe2394edb27..2dbcb1bb44a 100644
--- a/src/mongo/scripting/mozjs/internedstring.defs
+++ b/src/mongo/scripting/mozjs/internedstring.defs
@@ -61,3 +61,7 @@ MONGO_MOZJS_INTERNED_STRING(database, "database")
MONGO_MOZJS_INTERNED_STRING(isValid, "isValid")
MONGO_MOZJS_INTERNED_STRING(setName, "setName")
MONGO_MOZJS_INTERNED_STRING(servers, "servers")
+MONGO_MOZJS_INTERNED_STRING(stack, "stack")
+MONGO_MOZJS_INTERNED_STRING(fileName, "fileName")
+MONGO_MOZJS_INTERNED_STRING(lineNumber, "lineNumber")
+MONGO_MOZJS_INTERNED_STRING(columnNumber, "columnNumber")