Skip to content

Commit

Permalink
Pass stack to printer when running console.trace().
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxisTekfield committed Mar 1, 2022
1 parent 7d7bded commit 178d18c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/org/mozilla/javascript/NativeConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public enum Level {
}

public interface ConsolePrinter extends Serializable {
void print(Context cx, Scriptable scope, Level level, Object[] args);
void print(
Context cx,
Scriptable scope,
Level level,
ScriptStackElement[] stack,
Object[] args);
}

public static void init(Scriptable scope, boolean sealed, ConsolePrinter printer) {
Expand Down Expand Up @@ -140,24 +145,28 @@ public Object execIdCall(
return "Console";

case Id_trace:
printer.print(cx, scope, Level.TRACE, args);
break;
{
ScriptStackElement[] stack =
new EvaluatorException("[object Object]").getScriptStack();
printer.print(cx, scope, Level.TRACE, stack, args);
break;
}

case Id_debug:
printer.print(cx, scope, Level.DEBUG, args);
printer.print(cx, scope, Level.DEBUG, null, args);
break;

case Id_log:
case Id_info:
printer.print(cx, scope, Level.INFO, args);
printer.print(cx, scope, Level.INFO, null, args);
break;

case Id_warn:
printer.print(cx, scope, Level.WARN, args);
printer.print(cx, scope, Level.WARN, null, args);
break;

case Id_error:
printer.print(cx, scope, Level.ERROR, args);
printer.print(cx, scope, Level.ERROR, null, args);
break;

case Id_assert:
Expand Down Expand Up @@ -192,7 +201,7 @@ public Object execIdCall(
}

private void print(Context cx, Scriptable scope, Level level, String msg) {
printer.print(cx, scope, level, new String[] {msg});
printer.print(cx, scope, level, null, new String[] {msg});
}

public static String format(Context cx, Scriptable scope, Object[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
import java.nio.charset.Charset;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeConsole;
import org.mozilla.javascript.ScriptStackElement;
import org.mozilla.javascript.Scriptable;

/** Provide a printer use in console API */
class ShellConsolePrinter implements NativeConsole.ConsolePrinter {
private static final long serialVersionUID = 5869832740127501857L;

@Override
public void print(Context cx, Scriptable scope, NativeConsole.Level level, Object[] args) {
public void print(
Context cx,
Scriptable scope,
NativeConsole.Level level,
ScriptStackElement[] stack,
Object[] args) {
if (args.length == 0) {
return;
}
Expand All @@ -24,6 +30,12 @@ public void print(Context cx, Scriptable scope, NativeConsole.Level level, Objec
ShellConsole console = Main.getGlobal().getConsole(Charset.defaultCharset());
try {
console.println(level + " " + msg);

if (stack != null) {
for (ScriptStackElement element : stack) {
console.println(element.toString());
}
}
} catch (IOException e) {
throw Context.reportRuntimeError(e.getMessage());
}
Expand Down

0 comments on commit 178d18c

Please sign in to comment.