Mozilla's Rhino engine includes a step-through debugger. It is relatively easy to embed this in your own Java application.
Versions: Rhino 1.7R4 on Java 7.
Running the debugger
// test.js
for(var x = 0; x < 5; x++) {
java.lang.System.out.println("Hello, World!");
}
Stepping through the above code can be done via this command:
java -cp js.jar org.mozilla.javascript.tools.debugger.Main test.js
This will launch the Swing GUI.
Embedding the debugger
It is relatively easy to do something similar using an embedded engine:
import java.io.*;
import org.mozilla.javascript.*;
import org.mozilla.javascript.tools.debugger.Main;
public class DbgGui {
public static void main(String[] args) throws IOException {
String file = "test.js";
ContextFactory factory = new ContextFactory();
Main dbg = new Main(file);
dbg.attachTo(factory);
Context context = factory.enterContext();
ScriptableObject scope = context.initStandardObjects();
// redirect std I/O
System.setIn(dbg.getIn());
System.setOut(dbg.getOut());
System.setErr(dbg.getErr());
dbg.setBreakOnEnter(true);
dbg.setScope(scope);
dbg.setSize(640, 400);
dbg.setVisible(true);
dbg.setExitAction(new ExitOnClose());
try (Reader reader = new FileReader(file)) {
context.evaluateReader(scope, reader, file, 1, null);
}
}
private static class ExitOnClose implements Runnable {
@Override
public void run() {
System.exit(0);
}
}
}

Sweet! _Running the debugger_ helped me bc the official Rhino documentation on their debugger didn't mention js.jar to load up org.mozilla.javascript.tools.debugger.Main ... Kind of important!
ReplyDeleteNote: I installed rhino on my Mac with Homebrew so the location is /usr/local/Cellar/rhino/1.7.6/libexec/js.jar (I used `find / -name js.jar` on the terminal) to find it.
Good luck to all!
For a while I thought Nashorn https://en.wikipedia.org/wiki/Nashorn_(JavaScript_engine) would stall Rhino but commits are still happening in 2016: https://github.com/mozilla/rhino
Delete