8134930: Defer stack trace walking of NashornException for extracting line number and file name

Wed, 02 Sep 2015 12:26:57 +0200

author
attila
date
Wed, 02 Sep 2015 12:26:57 +0200
changeset 1537
ecb9327a3854
parent 1536
bb0fac71c1da
child 1538
82a41eb20242

8134930: Defer stack trace walking of NashornException for extracting line number and file name
Reviewed-by: hannesw, sundar

src/jdk/nashorn/api/scripting/NashornException.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/api/scripting/NashornException.java	Mon Aug 31 15:18:59 2015 +0200
     1.2 +++ b/src/jdk/nashorn/api/scripting/NashornException.java	Wed Sep 02 12:26:57 2015 +0200
     1.3 @@ -51,6 +51,8 @@
     1.4      private String fileName;
     1.5      // script line number
     1.6      private int line;
     1.7 +    // are the line and fileName unknown?
     1.8 +    private boolean lineAndFileNameUnknown;
     1.9      // script column number
    1.10      private int column;
    1.11      // underlying ECMA error object - lazily initialized
    1.12 @@ -92,27 +94,10 @@
    1.13       */
    1.14      protected NashornException(final String msg, final Throwable cause) {
    1.15          super(msg, cause == null ? null : cause);
    1.16 -        // This is not so pretty - but it gets the job done. Note that the stack
    1.17 -        // trace has been already filled by "fillInStackTrace" call from
    1.18 -        // Throwable
    1.19 -        // constructor and so we don't pay additional cost for it.
    1.20 -
    1.21          // Hard luck - no column number info
    1.22          this.column = -1;
    1.23 -
    1.24 -        // Find the first JavaScript frame by walking and set file, line from it
    1.25 -        // Usually, we should be able to find it in just few frames depth.
    1.26 -        for (final StackTraceElement ste : getStackTrace()) {
    1.27 -            if (ECMAErrors.isScriptFrame(ste)) {
    1.28 -                // Whatever here is compiled from JavaScript code
    1.29 -                this.fileName = ste.getFileName();
    1.30 -                this.line = ste.getLineNumber();
    1.31 -                return;
    1.32 -            }
    1.33 -        }
    1.34 -
    1.35 -        this.fileName = null;
    1.36 -        this.line = 0;
    1.37 +        // We can retrieve the line number and file name from the stack trace if needed
    1.38 +        this.lineAndFileNameUnknown = true;
    1.39      }
    1.40  
    1.41      /**
    1.42 @@ -121,6 +106,7 @@
    1.43       * @return the file name
    1.44       */
    1.45      public final String getFileName() {
    1.46 +        ensureLineAndFileName();
    1.47          return fileName;
    1.48      }
    1.49  
    1.50 @@ -131,6 +117,7 @@
    1.51       */
    1.52      public final void setFileName(final String fileName) {
    1.53          this.fileName = fileName;
    1.54 +        lineAndFileNameUnknown = false;
    1.55      }
    1.56  
    1.57      /**
    1.58 @@ -139,6 +126,7 @@
    1.59       * @return the line number
    1.60       */
    1.61      public final int getLineNumber() {
    1.62 +        ensureLineAndFileName();
    1.63          return line;
    1.64      }
    1.65  
    1.66 @@ -148,6 +136,7 @@
    1.67       * @param line the line number
    1.68       */
    1.69      public final void setLineNumber(final int line) {
    1.70 +        lineAndFileNameUnknown = false;
    1.71          this.line = line;
    1.72      }
    1.73  
    1.74 @@ -274,4 +263,19 @@
    1.75      public void setEcmaError(final Object ecmaError) {
    1.76          this.ecmaError = ecmaError;
    1.77      }
    1.78 +
    1.79 +    private void ensureLineAndFileName() {
    1.80 +        if (lineAndFileNameUnknown) {
    1.81 +            for (final StackTraceElement ste : getStackTrace()) {
    1.82 +                if (ECMAErrors.isScriptFrame(ste)) {
    1.83 +                    // Whatever here is compiled from JavaScript code
    1.84 +                    fileName = ste.getFileName();
    1.85 +                    line = ste.getLineNumber();
    1.86 +                    return;
    1.87 +                }
    1.88 +            }
    1.89 +
    1.90 +            lineAndFileNameUnknown = false;
    1.91 +        }
    1.92 +    }
    1.93  }

mercurial