Skip to content

Commit

Permalink
Go back to the pre-Java-9 stack trace API
Browse files Browse the repository at this point in the history
This is compatible with Android and also seems to be faster
  • Loading branch information
gbrail committed Dec 19, 2024
1 parent 5da136c commit b40416f
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions rhino/src/main/java/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.function.Consumer;
Expand Down Expand Up @@ -2642,24 +2641,20 @@ static String getSourcePositionFromStack(int[] linep) {
}

/** Returns the current filename in the java stack. */
@SuppressWarnings("AndroidJdkLibsChecker")
// Android uses interpreter, so we should not get here.
static String getSourcePositionFromJavaStack(int[] linep) {
Optional<StackWalker.StackFrame> frame =
StackWalker.getInstance()
.walk(stream -> stream.filter(Context::frameMatches).findFirst());
return frame.map(
f -> {
linep[0] = f.getLineNumber();
return f.getFileName();
})
.orElse(null);
}

@SuppressWarnings("AndroidJdkLibsChecker")
private static boolean frameMatches(StackWalker.StackFrame frame) {
return (frame.getFileName() == null || !frame.getFileName().endsWith(".java"))
&& frame.getLineNumber() > 0;
StackTraceElement[] stack = new Throwable().getStackTrace();
for (StackTraceElement e : stack) {
if (frameMatches(e)) {
linep[0] = e.getLineNumber();
return e.getFileName();
}
}
return null;
}

private static boolean frameMatches(StackTraceElement e) {
return (e.getFileName() == null || !e.getFileName().endsWith(".java"))
&& e.getLineNumber() > 0;
}

RegExpProxy getRegExpProxy() {
Expand Down

0 comments on commit b40416f

Please sign in to comment.