Compare commits

..

3 Commits

Author SHA1 Message Date
fd8b0b7cbe fix: source map decoding 2025-05-22 14:20:29 +03:00
a4c09b6cd6 various debugging-related fixes 2025-05-22 14:19:53 +03:00
892408d9dd feat: some much needed REPL improvements 2025-05-22 14:18:49 +03:00
8 changed files with 424 additions and 377 deletions

View File

@@ -38,8 +38,10 @@ public class CompoundNode extends Node {
for (var i = 0; i < statements.size(); i++) { for (var i = 0; i < statements.size(); i++) {
var stm = statements.get(i); var stm = statements.get(i);
if (i != statements.size() - 1) stm.compile(target, false, BreakpointType.STEP_OVER); if (i != statements.size() - 1) stm.compileStatement(target, false, BreakpointType.STEP_OVER);
else stm.compile(target, polluted = pollute, BreakpointType.STEP_OVER); else stm.compileStatement(target, polluted = pollute, BreakpointType.STEP_OVER);
target.setDebug(type);
} }
if (!polluted && pollute) { if (!polluted && pollute) {

View File

@@ -9,9 +9,14 @@ public abstract class Node {
public void resolve(CompileResult target) {} public void resolve(CompileResult target) {}
public void compile(CompileResult target, boolean pollute, BreakpointType type) { public void compile(CompileResult target, boolean pollute, BreakpointType type) {
compileStatement(target, pollute, type);
}
public void compileStatement(CompileResult target, boolean pollute, BreakpointType type) {
int start = target.size(); int start = target.size();
compile(target, pollute); compile(target, pollute);
if (target.size() != start) target.setLocationAndDebug(start, loc(), type); if (target.size() != start) {
target.setLocationAndDebug(start, loc(), type);
}
} }
public void compile(CompileResult target, boolean pollute) { public void compile(CompileResult target, boolean pollute) {
compile(target, pollute, BreakpointType.NONE); compile(target, pollute, BreakpointType.NONE);

View File

@@ -814,7 +814,6 @@ public class SimpleDebugger implements Debugger {
var cond = msg.params.string("condition", "").trim(); var cond = msg.params.string("condition", "").trim();
if (cond.equals("")) cond = null; if (cond.equals("")) cond = null;
if (cond != null) cond = "(" + cond + ")";
Pattern regex; Pattern regex;

View File

@@ -1,10 +1,11 @@
const map: number[] = []; function decodeBase64(val: number) {
let j = 0; if (val >= 65 && val <= 90) return val - 65;
else if (val >= 97 && val <= 122) return val - 97 + 26;
for (let i = 65; i <= 90; i++) map[i] = j++; else if (val >= 48 && val <= 57) return val - 48 + 52;
for (let i = 97; i <= 122; i++) map[i] = j++; else if (val == 43) return 62;
map[43] = j++; else if (val == 47) return 63;
map[47] = j++; else throw "Invalid Base64 char";
}
export function decodeVLQ(val: string): number[][][] { export function decodeVLQ(val: string): number[][][] {
const lines: number[][][] = []; const lines: number[][][] = [];
@@ -30,14 +31,14 @@ export function decodeVLQ(val: string): number[][][] {
for (let i = 0; i < el.length;) { for (let i = 0; i < el.length;) {
let sign = 1; let sign = 1;
let curr = map[el.charCodeAt(i++)]; let curr = decodeBase64(el.charCodeAt(i++));
let cont = (curr & 0x20) === 0x20; let cont = (curr & 0x20) === 0x20;
if ((curr & 1) === 1) sign = -1; if ((curr & 1) === 1) sign = -1;
let res = (curr & 0b11110) >> 1; let res = (curr & 0b11110) >> 1;
let n = 4; let n = 4;
for (; i < el.length && cont;) { for (; i < el.length && cont;) {
curr = map[el.charCodeAt(i++)]; curr = decodeBase64(el.charCodeAt(i++));
cont = (curr & 0x20) == 0x20; cont = (curr & 0x20) == 0x20;
res |= (curr & 0b11111) << n; res |= (curr & 0b11111) << n;
n += 5; n += 5;
@@ -134,7 +135,6 @@ export class VLQSourceMap {
let originalRow = 0; let originalRow = 0;
let originalCol = 0; let originalCol = 0;
let originalFile = 0; let originalFile = 0;
const lastCols = new Set<number>();
for (let compiledRow = 0; compiledRow < mapping.length; compiledRow++) { for (let compiledRow = 0; compiledRow < mapping.length; compiledRow++) {
const line: [start: number, dst: Location][] = file[compiledRow] = []; const line: [start: number, dst: Location][] = file[compiledRow] = [];
@@ -149,10 +149,7 @@ export class VLQSourceMap {
originalRow += rawSeg.length > 2 ? rawSeg[2] : 0; originalRow += rawSeg.length > 2 ? rawSeg[2] : 0;
originalCol += rawSeg.length > 3 ? rawSeg[3] : 0; originalCol += rawSeg.length > 3 ? rawSeg[3] : 0;
if (!lastCols.has(compiledCol)) { line[line.length] = [compiledCol, [filenames[originalFile], originalRow, originalCol]];
line[line.length] = [compiledCol, [filenames[originalFile], originalRow, originalCol]];
}
lastCols.add(compiledCol);
} }
line.sort((a, b) => a[0] - b[0]); line.sort((a, b) => a[0] - b[0]);

View File

@@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@@ -41,55 +42,61 @@ public class SimpleRepl {
static Key<InputStream> STDIN = new Key<>(); static Key<InputStream> STDIN = new Key<>();
static int j = 0; static int j = 0;
static String[] args; static String[] files;
static boolean inspect = false;
static int inspectPort = 9229;
private static void reader() { private static void reader() {
try { try {
server = new DebugServer(); if (inspect) {
debugTask = server.start(new InetSocketAddress("127.0.0.1", 9229), true); server = new DebugServer();
server.targets.put("default", (socket, req) -> new SimpleDebugger(socket) debugTask = server.start(new InetSocketAddress("127.0.0.1", inspectPort), true);
.attach((SimpleDebugHandler)DebugHandler.get(environment)) server.targets.put("default", (socket, req) -> new SimpleDebugger(socket)
); .attach((SimpleDebugHandler)DebugHandler.get(environment))
);
System.out.println("Debug server started at localhost:" + inspectPort);
}
System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author())); System.out.println(String.format("Running %s v%s by %s", Metadata.name(), Metadata.version(), Metadata.author()));
for (var arg : args) { if (files.length > 0) {
var file = new File(arg); for (var arg : files) {
var raw = Reading.streamToString(new FileInputStream(file)); var file = new File(arg);
var raw = Reading.streamToString(new FileInputStream(file));
try {
try { try {
var res = engine.pushMsg( try {
false, environment, var res = engine.pushMsg(
Filename.fromFile(file), raw, null false, environment,
).get(); Filename.fromFile(file), raw, null
).get();
System.err.println(res.toReadable(environment)); System.err.println(res.toReadable(environment));
}
catch (ExecutionException e) { throw e.getCause(); }
} }
catch (ExecutionException e) { throw e.getCause(); } catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); }
} }
catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); }
} }
else {
for (var i = 0; ; i++) {
var raw = Reading.readline();
for (var i = 0; ; i++) { if (raw == null) break;
var raw = Reading.readline();
if (raw == null) break;
try {
try { try {
var res = engine.pushMsg( try {
false, environment, var res = engine.pushMsg(
new Filename(Metadata.name(), "repl/" + i + ".js"), raw, false, environment,
Value.UNDEFINED new Filename(Metadata.name(), "repl/" + i + ".js"), raw,
).get(); Value.UNDEFINED
System.err.println(res.toReadable(environment)); ).get();
System.err.println(res.toReadable(environment));
}
catch (ExecutionException e) { throw e.getCause(); }
} }
catch (ExecutionException e) { throw e.getCause(); } catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); }
} }
catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); }
} }
} }
catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); } catch (EngineException | SyntaxException e) { System.err.println(Value.errorToReadable(environment, e, null)); }
@@ -104,11 +111,30 @@ public class SimpleRepl {
} }
} }
private static Environment createESEnv() { private static Environment createESEnv(String compiler) {
var env = StdLib.apply(null); var env = StdLib.apply(null);
env.add(EventLoop.KEY, engine); env.add(EventLoop.KEY, engine);
env.add(DebugHandler.KEY, new SimpleDebugHandler()); env.add(DebugHandler.KEY, new SimpleDebugHandler());
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler));
switch (compiler) {
case "typescript":
case "ts":
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::typescriptCompiler));
break;
case "coffeescript":
case "cs":
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler, Compilers::coffeescriptCompiler));
break;
case "babel":
case "es6":
case "esnext":
env.add(Compiler.KEY, Compilers.chainTranspilers(Compilers.jsCompiler(), env, Compilers::babelCompiler));
break;
default:
case "js":
env.add(Compiler.KEY, Compilers.jsCompiler());
break;
}
var glob = Value.global(env); var glob = Value.global(env);
@@ -137,10 +163,29 @@ public class SimpleRepl {
} }
public static void main(String args[]) throws InterruptedException { public static void main(String args[]) throws InterruptedException {
SimpleRepl.args = args; var compiler = "js";
var files = new ArrayList<String>();
for (String arg : args) {
if (arg.startsWith("--lang=")) {
compiler = arg.substring(7);
}
else if (arg.equals("--inspect")) {
inspect = true;
}
else if (arg.startsWith("--inspect=")) {
inspect = true;
inspectPort = Integer.parseInt(arg.substring(10));
}
else {
files.add(arg);
}
}
SimpleRepl.files = files.toArray(new String[0]);
var reader = new Thread(SimpleRepl::reader); var reader = new Thread(SimpleRepl::reader);
environment = createESEnv(); environment = createESEnv(compiler);
initEngine(); initEngine();
@@ -150,6 +195,6 @@ public class SimpleRepl {
reader.join(); reader.join();
engineTask.interrupt(); engineTask.interrupt();
debugTask.interrupt(); if (debugTask != null) debugTask.interrupt();
} }
} }

View File

@@ -93,7 +93,7 @@ public interface DebugHandler {
} }
public default FunctionMap getMapOrEmpty(Environment env, FunctionValue func) { public default FunctionMap getMapOrEmpty(Environment env, FunctionValue func) {
if (func instanceof CodeFunction codeFunc) return getMapOrEmpty(env, codeFunc.body); if (func instanceof CodeFunction codeFunc) return getMapOrEmpty(env, codeFunc.body);
else return null; else return FunctionMap.EMPTY;
} }
public static DebugHandler get(Environment exts) { public static DebugHandler get(Environment exts) {

View File

@@ -195,7 +195,6 @@ public abstract class ArrayLikeValue extends ObjectValue {
res.add(" " + line); res.add(" " + line);
} }
} }
res.set(res.size() - 1, res.getLast().substring(0, res.getLast().length() - 1));
res.add("]"); res.add("]");
return res; return res;