Search code examples
javanashorn

Java nashorn - Get all bindings from JavaScript


I created some bindings and passed them to the engine and global scopes

Bindings bindings = new SimpleBindings();
bindings.put...
scriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
Bindings bindings1 = new SimpleBindings();
bindings1.put...
scriptEngine.setBindings(bindings1, ScriptContext.GLOBAL_SCOPE);

Now on the js side i would like to print all avalaible bindings in a specific scope.

How can i do that?


Solution

  • You could add a binding to an object which holds reference to the engine, and create methods for parsing key list from it. Something like this:

    package nashor;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    
    import javax.script.*;
    
    public class NashorMain {
    
        public static void main (String[] args) throws ScriptException  {
    
            ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");    
            Bindings b = nashorn.createBindings();
            b.put("global", "GLOBAL");
            nashorn.setBindings(b, ScriptContext.GLOBAL_SCOPE);
    
            b = nashorn.createBindings();
            b.put("info", new NashorInfo(nashorn));
            b.put("engineVar", "engine");
            nashorn.setBindings(b, ScriptContext.ENGINE_SCOPE);
    
            nashorn.eval("newEngineVar = 'engine'");
            nashorn.eval("print('Engine vars:'); for each (var key in info.getEngineScopeKeys()) print (key)");
            nashorn.eval("print();print('Global vars:'); for each (var key in info.getGlobalScopeKeys()) print (key)");
        }
    
        public static class NashorInfo {
    
            private ScriptEngine scriptEngine;
    
            public NashorInfo (ScriptEngine scriptEngine) {
                this.scriptEngine = scriptEngine;
            }
    
            public String[] getEngineScopeKeys() {
                return scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).keySet().toArray(new String[]{});
            }
    
            public String[] getGlobalScopeKeys() {
                return scriptEngine.getBindings(ScriptContext.GLOBAL_SCOPE).keySet().toArray(new String[]{});
            }
        }
    
    
    }
    

    The output of the program above is

    Engine vars:
    info
    engineVar
    newEngineVar
    key
    
    Global vars:
    global