prepare codebase for source maps

This commit is contained in:
TopchetoEU 2024-12-25 02:53:11 +02:00
parent a1bb5bdba6
commit 277f59e54a
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 38 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import java.util.NavigableSet;
import java.util.Objects; import java.util.Objects;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -50,11 +51,31 @@ public class FunctionMap {
return sourceMap.lastEntry().getValue(); return sourceMap.lastEntry().getValue();
} }
public FunctionMap build(String[] localNames, String[] captureNames) { public FunctionMapBuilder map(Function<Location, Location> mapper) {
return new FunctionMap(sourceMap, breakpoints, localNames, captureNames); var newSourceMaps = new HashMap<Integer, Location>();
var newBreakpoints = new HashMap<Location, BreakpointType>();
for (var key : sourceMap.keySet()) {
newSourceMaps.put(key, mapper.apply(sourceMap.get(key)));
}
for (var key : breakpoints.keySet()) {
newBreakpoints.put(mapper.apply(key), breakpoints.get(key));
}
sourceMap.clear();
sourceMap.putAll(newSourceMaps);
breakpoints.clear();
breakpoints.putAll(newBreakpoints);
return this;
} }
public FunctionMap build() {
return new FunctionMap(sourceMap, breakpoints, new String[0], new String[0]); public FunctionMap build(String[] localNames, String[] capturableNames, String[] captureNames) {
return new FunctionMap(sourceMap, breakpoints, localNames, capturableNames, captureNames);
}
public FunctionMap build(Function<Location, Location> mapper) {
return new FunctionMap(sourceMap, breakpoints, new String[0], new String[0], new String[0]);
} }
private FunctionMapBuilder() { } private FunctionMapBuilder() { }
@ -67,7 +88,7 @@ public class FunctionMap {
private final TreeMap<Integer, Location> pcToLoc = new TreeMap<>(); private final TreeMap<Integer, Location> pcToLoc = new TreeMap<>();
public final String[] localNames, captureNames; public final String[] localNames, capturableNames, captureNames;
public Location toLocation(int pc, boolean approxiamte) { public Location toLocation(int pc, boolean approxiamte) {
if (pcToLoc.size() == 0 || pc < 0 || pc > pcToLoc.lastKey()) return null; if (pcToLoc.size() == 0 || pc < 0 || pc > pcToLoc.lastKey()) return null;
@ -128,7 +149,7 @@ public class FunctionMap {
} }
public FunctionMap clone() { public FunctionMap clone() {
var res = new FunctionMap(new HashMap<>(), new HashMap<>(), localNames, captureNames); var res = new FunctionMap(new HashMap<>(), new HashMap<>(), localNames, capturableNames, captureNames);
res.pcToLoc.putAll(this.pcToLoc); res.pcToLoc.putAll(this.pcToLoc);
res.bps.putAll(bps); res.bps.putAll(bps);
res.bpLocs.putAll(bpLocs); res.bpLocs.putAll(bpLocs);
@ -136,7 +157,7 @@ public class FunctionMap {
return res; return res;
} }
public FunctionMap(Map<Integer, Location> map, Map<Location, BreakpointType> breakpoints, String[] localNames, String[] captureNames) { public FunctionMap(Map<Integer, Location> map, Map<Location, BreakpointType> breakpoints, String[] localNames, String[] capturableNames, String[] captureNames) {
var locToPc = new HashMap<Location, Integer>(); var locToPc = new HashMap<Location, Integer>();
for (var el : map.entrySet()) { for (var el : map.entrySet()) {
@ -154,10 +175,12 @@ public class FunctionMap {
this.localNames = localNames; this.localNames = localNames;
this.captureNames = captureNames; this.captureNames = captureNames;
this.capturableNames = capturableNames;
} }
private FunctionMap() { private FunctionMap() {
localNames = new String[0]; localNames = new String[0];
captureNames = new String[0]; captureNames = new String[0];
capturableNames = new String[0];
} }
public static FunctionMapBuilder builder() { public static FunctionMapBuilder builder() {

View File

@ -3,7 +3,6 @@ package me.topchetoeu.jscript.compilation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Stack;
import java.util.function.IntSupplier; import java.util.function.IntSupplier;
import me.topchetoeu.jscript.common.Instruction; import me.topchetoeu.jscript.common.Instruction;

View File

@ -190,12 +190,19 @@ public final class FunctionScope {
return res; return res;
} }
public String[] localNames() { public String[] localNames() {
var res = new String[this.locals.size() + this.capturables.size()]; var res = new String[this.locals.size()];
var i = 0; var i = 0;
for (var el : this.locals) { for (var el : this.locals) {
res[i++] = el.name; res[i++] = el.name;
} }
return res;
}
public String[] capturableNames() {
var res = new String[this.capturables.size()];
var i = 0;
for (var el : this.capturables) { for (var el : this.capturables) {
res[i++] = el.name; res[i++] = el.name;
} }