feat: make environment hidable from stack trace

This commit is contained in:
TopchetoEU 2023-12-18 22:38:26 +02:00
parent 76c3d377af
commit 380a5c720a
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
5 changed files with 21 additions and 14 deletions

View File

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

View File

@ -35,6 +35,7 @@ public class Environment implements PermissionsProvider {
private static int nextId = 0; private static int nextId = 0;
@Native public boolean stackVisible = true;
@Native public int id = ++nextId; @Native public int id = ++nextId;
@Native public FunctionValue compile = new NativeFunction("compile", (ctx, thisArg, args) -> { @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); returnValue = Runners.exec(ctx, instr, this);
} }
catch (EngineException e) { 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 static class StackElement {
public final Location location; public final Location location;
public final String function; 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 res = "";
var loc = location; var loc = location;
@ -27,14 +31,13 @@ public class EngineException extends RuntimeException {
return res.trim(); 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 != null) function = function.trim();
if (function.equals("")) function = null; 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.location = location;
this.function = function; this.function = function;
} }
@ -46,9 +49,10 @@ public class EngineException extends RuntimeException {
public Engine engine = null; public Engine engine = null;
public final List<StackElement> stackTrace = new ArrayList<>(); public final List<StackElement> stackTrace = new ArrayList<>();
public EngineException add(String name, Location location) { public EngineException add(Context ctx, String name, Location location) {
var el = new StackElement(location, name); var el = new StackElement(ctx, location, name);
if (el.function == null && el.location == null) return this; if (el.function == null && el.location == null) return this;
setCtx(ctx.environment(), ctx.engine);
stackTrace.add(el); stackTrace.add(el);
return this; return this;
} }
@ -71,7 +75,7 @@ public class EngineException extends RuntimeException {
ss.append("[Error while stringifying]\n"); ss.append("[Error while stringifying]\n");
} }
for (var line : stackTrace) { 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'); if (cause != null) ss.append("Caused by ").append(cause.toString(ctx)).append('\n');
ss.deleteCharAt(ss.length() - 1); ss.deleteCharAt(ss.length() - 1);
@ -99,7 +103,7 @@ public class EngineException extends RuntimeException {
return new EngineException(err(null, msg, PlaceholderProto.ERROR)); return new EngineException(err(null, msg, PlaceholderProto.ERROR));
} }
public static EngineException ofSyntax(SyntaxException e) { 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) { public static EngineException ofSyntax(String msg) {
return new EngineException(err(null, msg, PlaceholderProto.SYNTAX_ERROR)); return new EngineException(err(null, msg, PlaceholderProto.SYNTAX_ERROR));

View File

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