Add support for source mappings #10

Merged
TopchetoEU merged 22 commits from TopchetoEU/mapping into master 2023-12-18 20:42:38 +00:00
5 changed files with 21 additions and 14 deletions
Showing only changes of commit 380a5c720a - Show all commits

View File

@ -129,8 +129,10 @@ public class Main {
private static void initTypescript() {
try {
var tsEnv = Internals.apply(new Environment(null, null, null));
tsEnv.stackVisible = false;
tsEnv.global.define(null, "module", false, new ObjectValue());
var bsEnv = Internals.apply(new Environment(null, null, null));
bsEnv.stackVisible = false;
engine.pushMsg(
false, new Context(engine, tsEnv),

View File

@ -35,6 +35,7 @@ public class Environment implements PermissionsProvider {
private static int nextId = 0;
@Native public boolean stackVisible = true;
@Native public int id = ++nextId;
@Native public FunctionValue compile = new NativeFunction("compile", (ctx, thisArg, args) -> {

View File

@ -208,7 +208,7 @@ public class CodeFrame {
returnValue = Runners.exec(ctx, instr, this);
}
catch (EngineException e) {
error = e.add(function.name, prevLoc).setCtx(function.environment, ctx.engine);
error = e.add(ctx, function.name, prevLoc);
}
}
}

View File

@ -15,8 +15,12 @@ public class EngineException extends RuntimeException {
public static class StackElement {
public final Location location;
public final String function;
public final Context ctx;
public String toString(Context ctx) {
public boolean visible() {
return ctx == null || ctx.environment() == null || ctx.environment().stackVisible;
}
public String toString() {
var res = "";
var loc = location;
@ -27,14 +31,13 @@ public class EngineException extends RuntimeException {
return res.trim();
}
@Override public String toString() {
return toString(null);
}
public StackElement(Location location, String function) {
public StackElement(Context ctx, Location location, String function) {
if (function != null) function = function.trim();
if (function.equals("")) function = null;
if (ctx == null) this.ctx = null;
else this.ctx = new Context(ctx.engine).pushEnv(ctx.environment());
this.location = location;
this.function = function;
}
@ -46,9 +49,10 @@ public class EngineException extends RuntimeException {
public Engine engine = null;
public final List<StackElement> stackTrace = new ArrayList<>();
public EngineException add(String name, Location location) {
var el = new StackElement(location, name);
public EngineException add(Context ctx, String name, Location location) {
var el = new StackElement(ctx, location, name);
if (el.function == null && el.location == null) return this;
setCtx(ctx.environment(), ctx.engine);
stackTrace.add(el);
return this;
}
@ -71,7 +75,7 @@ public class EngineException extends RuntimeException {
ss.append("[Error while stringifying]\n");
}
for (var line : stackTrace) {
ss.append(" ").append(line.toString(ctx)).append("\n");
if (line.visible()) ss.append(" ").append(line.toString()).append("\n");
}
if (cause != null) ss.append("Caused by ").append(cause.toString(ctx)).append('\n');
ss.deleteCharAt(ss.length() - 1);
@ -99,7 +103,7 @@ public class EngineException extends RuntimeException {
return new EngineException(err(null, msg, PlaceholderProto.ERROR));
}
public static EngineException ofSyntax(SyntaxException e) {
return new EngineException(err(null, e.msg, PlaceholderProto.SYNTAX_ERROR)).add(null, e.loc);
return new EngineException(err(null, e.msg, PlaceholderProto.SYNTAX_ERROR)).add(null, null, e.loc);
}
public static EngineException ofSyntax(String msg) {
return new EngineException(err(null, msg, PlaceholderProto.SYNTAX_ERROR));

View File

@ -83,11 +83,11 @@ public class OverloadFunction extends FunctionValue {
catch (InvocationTargetException e) {
var loc = Location.INTERNAL;
if (e.getTargetException() instanceof EngineException) {
throw ((EngineException)e.getTargetException()).add(name, loc);
throw ((EngineException)e.getTargetException()).add(ctx, name, loc);
}
else if (e.getTargetException() instanceof NullPointerException) {
e.printStackTrace();
throw EngineException.ofType("Unexpected value of 'undefined'.").add(name, loc);
throw EngineException.ofType("Unexpected value of 'undefined'.").add(ctx, name, loc);
}
else if (e.getTargetException() instanceof InterruptException || e.getTargetException() instanceof InterruptedException) {
throw new InterruptException();
@ -100,11 +100,11 @@ public class OverloadFunction extends FunctionValue {
err.defineProperty(ctx, "message", target.getMessage());
err.defineProperty(ctx, "name", NativeWrapperProvider.getName(targetClass));
throw new EngineException(err).add(name, loc);
throw new EngineException(err).add(ctx, name, loc);
}
}
catch (ReflectiveOperationException e) {
throw EngineException.ofError(e.getMessage()).add(name, Location.INTERNAL);
throw EngineException.ofError(e.getMessage()).add(ctx, name, Location.INTERNAL);
}
}