You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the example below the second execution uses global variables from the first one.
Is it possible to store global variables in the scriptcontext instead in the default-context of the engine?
We are executing a lot of scripts in our application an need to ensure that they have a "clean" context.
Our corrent workaround is to reset global variables after script-execution but this has the side-effect that variables will be gone if another script-execution is triggered inside the script.
this works:
call script
do something
reset global variables
--> next execution will have a clean context.
this doesn't work:
call script
do something that triggers another script
"another script is executed"
"another script" clears global variables
return to main script - global variables are gone. :-(
For example we put a variable $tool into each context to access some helper-functions.
In the second example $tool is nil in the main-script after the "sub-script" is done.
Our expected behavior would be that each script-exection has it's only context including the global variables - so each script would have it's one instance of tool without affecting eachother.
System.setProperty(
PropertyName.LOCALCONTEXT_SCOPE.toString(),
LocalContextScope.THREADSAFE.toString().toLowerCase());
System.setProperty(
PropertyName.LOCALVARIABLE_BEHAVIOR.toString(),
LocalVariableBehavior.GLOBAL.toString().toLowerCase());
finalScriptEngine_e = newJRubyEngineFactory().getScriptEngine();
finalStringWriter_s = newStringWriter();
finalPrintWriter_p = newPrintWriter(_s);
_p.println("p $foo");
_p.println("p $msg");
_p.println("$foo = 'bar'");
_p.println("p $foo");
finalSimpleBindings_ctx = newSimpleBindings();
finalMap<String, Object> _params = newHashMap<String, Object>();
_params.put("msg", "Hello World");
if (_params != null)
for (Entry<String, Object> _pe : _params.entrySet())
_ctx.put(_pe.getKey(), _pe.getValue());
// first p shows nil_e.eval(_s.toString(), _ctx);
finalSimpleBindings_ctx2 = newSimpleBindings();
if (_params != null)
for (Entry<String, Object> _pe : _params.entrySet())
_ctx2.put(_pe.getKey(), _pe.getValue());
// first p shows bar instead of nil_e.eval(_s.toString(), _ctx2);
The text was updated successfully, but these errors were encountered:
probably resolved by now, but for the record: sounds like you wanted to have multiple engines started.
probably the meaningful work-around would have been to use ScriptingContainer directly instead.
other possible 'solution', if you really want a single runtime would be to execute against an engine from multiple threads and set scope to CONCURRENT.
In the example below the second execution uses global variables from the first one.
Is it possible to store global variables in the scriptcontext instead in the default-context of the engine?
We are executing a lot of scripts in our application an need to ensure that they have a "clean" context.
Our corrent workaround is to reset global variables after script-execution but this has the side-effect that variables will be gone if another script-execution is triggered inside the script.
this works:
--> next execution will have a clean context.
this doesn't work:
For example we put a variable $tool into each context to access some helper-functions.
In the second example $tool is nil in the main-script after the "sub-script" is done.
Our expected behavior would be that each script-exection has it's only context including the global variables - so each script would have it's one instance of tool without affecting eachother.
The text was updated successfully, but these errors were encountered: