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

Reviewed-by: hannesw, sundar
This commit is contained in:
Attila Szegedi 2015-09-02 12:26:57 +02:00
parent b803598d6a
commit 106a9bf4b9

View File

@ -51,6 +51,8 @@ public abstract class NashornException extends RuntimeException {
private String fileName;
// script line number
private int line;
// are the line and fileName unknown?
private boolean lineAndFileNameUnknown;
// script column number
private int column;
// underlying ECMA error object - lazily initialized
@ -92,27 +94,10 @@ public abstract class NashornException extends RuntimeException {
*/
protected NashornException(final String msg, final Throwable cause) {
super(msg, cause == null ? null : cause);
// This is not so pretty - but it gets the job done. Note that the stack
// trace has been already filled by "fillInStackTrace" call from
// Throwable
// constructor and so we don't pay additional cost for it.
// Hard luck - no column number info
this.column = -1;
// Find the first JavaScript frame by walking and set file, line from it
// Usually, we should be able to find it in just few frames depth.
for (final StackTraceElement ste : getStackTrace()) {
if (ECMAErrors.isScriptFrame(ste)) {
// Whatever here is compiled from JavaScript code
this.fileName = ste.getFileName();
this.line = ste.getLineNumber();
return;
}
}
this.fileName = null;
this.line = 0;
// We can retrieve the line number and file name from the stack trace if needed
this.lineAndFileNameUnknown = true;
}
/**
@ -121,6 +106,7 @@ public abstract class NashornException extends RuntimeException {
* @return the file name
*/
public final String getFileName() {
ensureLineAndFileName();
return fileName;
}
@ -131,6 +117,7 @@ public abstract class NashornException extends RuntimeException {
*/
public final void setFileName(final String fileName) {
this.fileName = fileName;
lineAndFileNameUnknown = false;
}
/**
@ -139,6 +126,7 @@ public abstract class NashornException extends RuntimeException {
* @return the line number
*/
public final int getLineNumber() {
ensureLineAndFileName();
return line;
}
@ -148,6 +136,7 @@ public abstract class NashornException extends RuntimeException {
* @param line the line number
*/
public final void setLineNumber(final int line) {
lineAndFileNameUnknown = false;
this.line = line;
}
@ -274,4 +263,19 @@ public abstract class NashornException extends RuntimeException {
public void setEcmaError(final Object ecmaError) {
this.ecmaError = ecmaError;
}
private void ensureLineAndFileName() {
if (lineAndFileNameUnknown) {
for (final StackTraceElement ste : getStackTrace()) {
if (ECMAErrors.isScriptFrame(ste)) {
// Whatever here is compiled from JavaScript code
fileName = ste.getFileName();
line = ste.getLineNumber();
return;
}
}
lineAndFileNameUnknown = false;
}
}
}