feat: use mappings in stack traces

This commit is contained in:
TopchetoEU 2023-12-18 22:30:14 +02:00
parent 42f443572a
commit 76c3d377af
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 47 additions and 28 deletions

View File

@ -69,6 +69,17 @@ public class Engine implements DebugController {
private final HashMap<Filename, TreeSet<Location>> bpts = new HashMap<>();
private final HashMap<Filename, SourceMap> 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<Task> tasks = new PriorityBlockingQueue<>();

View File

@ -67,14 +67,12 @@ public class SimpleDebugger implements Debugger {
public final Filename filename;
public final String source;
public final TreeSet<Location> breakpoints;
public final SourceMap map;
public Source(int id, Filename filename, String source, TreeSet<Location> breakpoints, SourceMap map) {
public Source(int id, Filename filename, String source, TreeSet<Location> 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<Location> 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());

View File

@ -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<String> stackTrace = new ArrayList<>();
public final List<StackElement> 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);