diff --git a/src/me/topchetoeu/jscript/engine/Engine.java b/src/me/topchetoeu/jscript/engine/Engine.java index dd6b1d2..773ec9f 100644 --- a/src/me/topchetoeu/jscript/engine/Engine.java +++ b/src/me/topchetoeu/jscript/engine/Engine.java @@ -69,6 +69,17 @@ public class Engine implements DebugController { private final HashMap> bpts = new HashMap<>(); private final HashMap maps = new HashMap<>(); + public Location mapToCompiled(Location location) { + var map = maps.get(location.filename()); + if (map == null) return location; + return map.toCompiled(location); + } + public Location mapToOriginal(Location location) { + var map = maps.get(location.filename()); + if (map == null) return location; + return map.toOriginal(location); + } + private DebugController debugger; private Thread thread; private PriorityBlockingQueue tasks = new PriorityBlockingQueue<>(); diff --git a/src/me/topchetoeu/jscript/engine/debug/SimpleDebugger.java b/src/me/topchetoeu/jscript/engine/debug/SimpleDebugger.java index c8faa18..5143ad7 100644 --- a/src/me/topchetoeu/jscript/engine/debug/SimpleDebugger.java +++ b/src/me/topchetoeu/jscript/engine/debug/SimpleDebugger.java @@ -67,14 +67,12 @@ public class SimpleDebugger implements Debugger { public final Filename filename; public final String source; public final TreeSet breakpoints; - public final SourceMap map; - public Source(int id, Filename filename, String source, TreeSet breakpoints, SourceMap map) { + public Source(int id, Filename filename, String source, TreeSet breakpoints) { this.id = id; this.filename = filename; this.source = source; this.breakpoints = breakpoints; - this.map = map; } } private static class Breakpoint { @@ -234,21 +232,6 @@ public class SimpleDebugger implements Debugger { return nextId++; } - private Location toCompiled(Location location) { - var id = filenameToId.get(location.filename()); - if (id == null) return location; - var map = idToSource.get(id).map; - if (map == null) return location; - return map.toCompiled(location); - } - private Location toOriginal(Location location) { - var id = filenameToId.get(location.filename()); - if (id == null) return location; - var map = idToSource.get(id).map; - if (map == null) return location; - return map.toOriginal(location); - } - private synchronized void updateFrames(Context ctx) { var frame = ctx.peekFrame(); if (frame == null) return; @@ -858,7 +841,7 @@ public class SimpleDebugger implements Debugger { @Override public void onSource(Filename filename, String source, TreeSet locations, SourceMap map) { int id = nextId(); - var src = new Source(id, filename, source, locations, map); + var src = new Source(id, filename, source, locations); idToSource.put(id, src); filenameToId.put(filename, id); @@ -887,7 +870,7 @@ public class SimpleDebugger implements Debugger { if (!frame.debugData) return false; - if (instruction.location != null) frame.updateLoc(toCompiled(instruction.location)); + if (instruction.location != null) frame.updateLoc(ctx.engine.mapToCompiled(instruction.location)); loc = frame.location; isBreakpointable = loc != null && (instruction.breakpoint.shouldStepIn()); diff --git a/src/me/topchetoeu/jscript/exceptions/EngineException.java b/src/me/topchetoeu/jscript/exceptions/EngineException.java index e63169f..c76502b 100644 --- a/src/me/topchetoeu/jscript/exceptions/EngineException.java +++ b/src/me/topchetoeu/jscript/exceptions/EngineException.java @@ -12,19 +12,44 @@ import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.engine.values.ObjectValue.PlaceholderProto; public class EngineException extends RuntimeException { + public static class StackElement { + public final Location location; + public final String function; + + public String toString(Context ctx) { + var res = ""; + var loc = location; + + if (loc != null && ctx != null && ctx.engine != null) loc = ctx.engine.mapToCompiled(loc); + + if (loc != null) res += "at " + loc.toString() + " "; + if (function != null && !function.equals("")) res += "in " + function + " "; + + return res.trim(); + } + @Override public String toString() { + return toString(null); + } + + public StackElement(Location location, String function) { + if (function != null) function = function.trim(); + if (function.equals("")) function = null; + + this.location = location; + this.function = function; + } + } + public final Object value; public EngineException cause; public Environment env = null; public Engine engine = null; - public final List stackTrace = new ArrayList<>(); + public final List stackTrace = new ArrayList<>(); public EngineException add(String name, Location location) { - var res = ""; - - if (location != null) res += "at " + location.toString() + " "; - if (name != null && !name.equals("")) res += "in " + name + " "; - - this.stackTrace.add(res.trim()); + var el = new StackElement(location, name); + if (el.function == null && el.location == null) return this; + stackTrace.add(el); return this; } public EngineException setCause(EngineException cause) { @@ -46,7 +71,7 @@ public class EngineException extends RuntimeException { ss.append("[Error while stringifying]\n"); } for (var line : stackTrace) { - ss.append(" ").append(line).append('\n'); + ss.append(" ").append(line.toString(ctx)).append("\n"); } if (cause != null) ss.append("Caused by ").append(cause.toString(ctx)).append('\n'); ss.deleteCharAt(ss.length() - 1);