diff --git a/src/me/topchetoeu/jscript/Main.java b/src/me/topchetoeu/jscript/Main.java index 9729f58..2143316 100644 --- a/src/me/topchetoeu/jscript/Main.java +++ b/src/me/topchetoeu/jscript/Main.java @@ -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), diff --git a/src/me/topchetoeu/jscript/engine/Environment.java b/src/me/topchetoeu/jscript/engine/Environment.java index 0046289..7fb7714 100644 --- a/src/me/topchetoeu/jscript/engine/Environment.java +++ b/src/me/topchetoeu/jscript/engine/Environment.java @@ -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) -> { diff --git a/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java b/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java index 284336f..5b3e701 100644 --- a/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java +++ b/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java @@ -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); } } } diff --git a/src/me/topchetoeu/jscript/exceptions/EngineException.java b/src/me/topchetoeu/jscript/exceptions/EngineException.java index c76502b..cb82bb1 100644 --- a/src/me/topchetoeu/jscript/exceptions/EngineException.java +++ b/src/me/topchetoeu/jscript/exceptions/EngineException.java @@ -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 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)); diff --git a/src/me/topchetoeu/jscript/interop/OverloadFunction.java b/src/me/topchetoeu/jscript/interop/OverloadFunction.java index 9edd2a5..ac6bcfd 100644 --- a/src/me/topchetoeu/jscript/interop/OverloadFunction.java +++ b/src/me/topchetoeu/jscript/interop/OverloadFunction.java @@ -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); } }