B4J Question Debugging Nashorn scripts

Bruce Axtens

Active Member
Licensed User
Longtime User
Is there a way of debugging Nashorn scripts? I'm using evalFile to evaluate js code. Is there any way of stepping thru the code as it executes?
 

Bruce Axtens

Active Member
Licensed User
Longtime User
Hmm ... interesting thought. So B4J code can be executed in a scripting context as well as in a compiled context? If so, how does one do that?
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
The BOLE project I posted up in the "Sharing your code" section last year. I wanted to implement the menu-ing of the app in js scripts.

B4X:
Sub menuHandler_Action
    Dim mi As MenuItem = Sender
    Dim script As String = mi.Tag
   
    If File.IsDirectory(script,"") = False Then
        If File.Exists(script,"") = True Then
            Private oNas As jNashorn
            Private oJFX As JFX
           
            oNas.enginePut("MessageBox",mb)
            oNas.enginePut("MenuItem",mi)
            oNas.enginePut("MainForm",MainForm)
            oNas.enginePut("MainText",taMainText)
            oNas.enginePut("JFX",oJFX)
            oNas.enginePut("gbDirty",gbDirty)
            oNas.enginePut("gsVersion",gsVersion)
            oNas.enginePut("scriptPath",script)
            oNas.enginePut("gsPath",gsPath)
            oNas.enginePut("gsFile",gsFile)
            oNas.enginePut("joMainForm",AsJavaObject(MainForm.RootPane))
            oNas.enginePut("tfStatus",tfStatus)
            oNas.enginePut("tfCursor",tfCursor)
            oNas.enginePut("DirApp",File.DirApp)
            oNas.enginePut("DirAssets",File.DirAssets)
            oNas.enginePut("DirTemp",File.DirTemp)
            Try
                oNas.evalFile(script)
               
                If (Null <> oNas.engineGet("gsPath")) Then
                    gsPath = oNas.engineGet("gsPath")
                End If
                If (Null <> oNas.engineGet("gsFile")) Then
                    gsPath = oNas.engineGet("gsFile")
                End If
            Catch
                mb.show(LastException.Message,gsVersion)
            End Try
            oNas = Null
        End If
    End If
End Sub
And then 1-File\2-Open.js
B4X:
var Stage = Java.type("javafx.stage.Stage");
var FileChooser = Java.type("javafx.stage.FileChooser");
var File = Java.type("java.io.File");
var FileReader = Java.type("java.io.FileReader");
var BufferedReader = Java.type("java.io.BufferedReader");

var st = new Stage;
var fc = new FileChooser;
fc.setTitle(gsVersion + " File Open");
var f = new File(DirApp);
var sParent = f.getParent();
fc.setInitialDirectory(f); // should default to last file edited's parent or ??

var gsPath = fc.showOpenDialog(st);
var fl = new File(gsPath);
var fr = new FileReader(fl);
var br = new BufferedReader(fr);
var blk = [];
if (fl.canRead()) {
    var lin;
    while ( (lin = br.readLine()) !== null) {
        blk.push(lin);
    }
}
MainText.text = blk.join("\r\n") + "\r\n";
tfStatus.text = gsPath;
f = new File(gsPath);
gsPath = f.getParent();
gsFile = f.getName();
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could try jjs ( its in the java directory) call it from an Admin command prompt window and it gives you an interactive console and you can enter each line of your script and see what happens.
As an aside, if you jShell it from your program, you can have a GUI from inside a non-UI java program.
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
Good suggestions, both. Thanks.
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
Erel,

Could i use jCompiler? Compile the menu items on program launch and recompile if they change?
 
Upvote 0

Bruce Axtens

Active Member
Licensed User
Longtime User
I'm good at over-complicating things. Ask my wife and my employer.

But the issue was not just wanting to create menu items at runtime. I can see how to do that and it is quite simple. The issue was to farm out as much of the code in those menu items to scripts as possible. Okay, more a proof-of-concept than anything else. But a *fully* scriptable text-editor was where I was heading.
 
Upvote 0
Top