Error Stack Column number

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Thu Dec 1 06:44:46 UTC 2016


We do not aim to provide complete compatibility with other JS 
implementations on the non-standard properties such as "stack". stack 
tries to mimic whatever is done for Java code (no column number for 
eg.). But, as you've noted there are enough information on Error objects 
via other properties like lineNumber, columnNumber, fileName. It should 
be possible to write a simple utility function to format stack string as 
desired for specific applications.

Thanks,
-Sundar

On 01/12/16, 3:32 AM, Art Fiedler wrote:
> When making an implementation of console.count([label])  part of the
> Mozilla's developer docs
>      https://developer.mozilla.org/en-US/docs/Web/API/Console/count
> mentions when label is omitted, the function will count the number of times
> count() was called
> on that line... however, if the javascript code has been minified all lines
> of code may be on the
> same line... messing up all console.count() calls...
>
> I was providing that implementation by using new Error().stack and parsing
> the current file:line...
> however I could fix the minify issue if new Error().stack output the column
> also for instance...
> other browser seem to be doing file:line:column in the stack trace.
> Including nodejs see:
>      https://nodejs.org/api/errors.html#errors_error_stack
>
> Microsoft Edge's dev console outputs...
>      new Error().stack:
>      "Error
>         at eval code (eval code:1:1)"
> Firefox since FF30:
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack
>
> Currently nashorn error stacks only include the line number...
>      public static void main(String[] args) throws Exception {
>          NashornScriptEngineFactory factory = new
> NashornScriptEngineFactory();
>          ScriptEngine scriptEngine = factory.getScriptEngine();
>          scriptEngine.put(ScriptEngine.FILENAME, "myfile.js");
>          scriptEngine.eval("print('fileName: ' + new Error().fileName);");
>          scriptEngine.eval("print('lineNumber: ' + new
> Error().lineNumber);");
>          scriptEngine.eval("print('columnNumber: ' + new
> Error().columnNumber);");
>          scriptEngine.eval("print('stack: ' + new Error().stack);");
>      }
>      /*
>      fileName: myfile.js
>      lineNumber: 1
>      columnNumber: -1
>      stack: Error
>          at<program>  (myfile.js:1)
>       */
>
> Would be nice to add the column number to the stacks and error object in
> nashorn.
>
> Thanks,
> Arthur Fiedler


More information about the nashorn-dev mailing list