4

I'm using javax.script to execute javascript from a java method.

In my java method I invoke different functions defined in javascript. On the javascript side I want to keep a global variable so the output of a call depends on the previous ones.

java method

public void myMethod(){
  ScriptEngineManager factory = new ScriptEngineManager();
  ScriptEngine engine = factory.getEngineByName("JavaScript");

  engine.eval(new java.io.FileReader("myTest.js"));

  Invocable inv = (Invocable) engine;

  Object obj = engine.get("obj");
  inv.invokeMethod(obj, "method1");

  inv.invokeMethod(obj, "method2");
}

myTest.js

var obj=new Object();
var myStatus=1;

obj.method1 = function(){
  myStatus++;
};

obj.method2 = function(){
  for (var i=0; i<myStatus)
    println('Hello world');
}

What is the scope of the variable declared in the script? If I add a global variable to the script using

engine.put("globalVariable", myVariable)

what is the scope of this variable?

Thanks

1 Answer 1

1
engine.put("globalVariable", myVariable)

meas this variable is belonged to the engine, every script the engines runs shares this variable, here's an example:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");   
engine.put("status",0);
engine.eval("status++; println(status);"); //print 1
engine.eval("status++; println(status);"); //print 2

If you want to pass some script scope parameters to you script, you should use bindings

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");

Bindings bindings=engine.createBindings();
bindings.put("status",0);

Bindings bindings2=engine.createBindings();
bindings2.put("status",0);

engine.eval("status++; println(status);",bindings); //print 1
engine.eval("status++; println(status);",bindings2); //print 1

Next, the variable defined in the script, if you don't use bindings, they are all engine scope:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval("var status=0; status++; println(status);"); //print 1
engine.eval("status++; println(status);"); //print 2

If you use bindings, the variable defined in the script is binding scope, it will not pollute the engines scope.

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");

Bindings bindings=engine.createBindings();
//bindings.put("status",0);

Bindings bindings2=engine.createBindings();
//bindings2.put("status",0);

engine.eval("var status=0; status++; println(status);",bindings); //print 1
engine.eval("status++; println(status);",bindings2); // exception, status not defined
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.