Problems with SyntaxError construction

A. Sundararajan sundararajan.athijegannathan at oracle.com
Fri Feb 20 08:09:40 UTC 2015


* SyntaxError constructor accepting extra arguments apart from message 
is not as per ECMAScript standard. That one is Firefox/Mozilla specific 
extension. We try to *add* properties, functions for extension - try not 
to modify/extend standard defined functions, constructors.

* User can fileName, lineNumber and columnNumber of constructed 
SyntaxError instance.

* On the java side, the NashornException and ScriptException's file, 
line, column use the actual throw point from the script source. While 
this can read the values from SyntaxError or any Error for that matter, 
there is a problem. fileName, lineNumber, columnNumber could be set to 
any value in script. These have to be converted to String, int, int 
respectively. Conversion means calling [[DefaultValue]] with String, 
Number hint - which is calling out to script code. This process can 
itself throw exception!! All this may destroy info. on where exactly the 
underlying exception was thrown.

* There is clear workaround. Script code can always read SyntaxError 
instance's fileName, lineNumber etc. - which can be set to anything post 
construction. Java code can get underling ECMA error instance via 
NashornException.getEcmaError - which would return a ScriptObjectMirror. 
Java code can call ScriptObjectMirror.getMember("fileName") etc. to 
retrieve properties set by user's script.

Hope this helps,
-Sundar

On Wednesday 18 February 2015 08:29 PM, Tim Fox wrote:
> Hi all,
>
> I've started a new thread for this, so as not to clutter the previous 
> thread on exceptions.
>
> I've come across a possible issue with creating SyntaxError objects 
> (and potentially other exceptions although I haven't tested them).
>
> https://gist.github.com/purplefox/dfe30a38098c1708e24b
>
> In the above gist I create a SyntaxError specifying message, fileName, 
> and lineNumber.
>
> However fileName and lineNumber appear to be ignored, and instead the 
> SyntaxError fileName and lineNumber are set to the fileName of the 
> file where it was created and the lineNumber of the creation.
>
> I'm not sure if I'm doing something wrong, but according to 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError 
> I should be able to create SyntaxError objects like this.
>
> There appears to be a further issue. If I then throw the syntax error 
> from the JS script, and catch it in Java like this:
>
> try {
>       engine.eval(script);
>     } catch (ScriptException e) {
>       System.out.println("message:" + e.getMessage());
>       System.out.println("fileName:" + e.getFileName());
>       System.out.println("lineNumber:" + e.getLineNumber());
> }
>
> Then the message, fileName and lineNumber always correspond to place 
> where the object was created, not to the subsequently set values.
>
> In other words, I can't figure out a way of creating a SyntaxError 
> object in JS with my own values of fileName and lineNumber and have 
> those values available in Java when caught.
>
> Any ideas from the experts? :)
>
>



More information about the nashorn-dev mailing list