Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to store global variables only in current context? #2257

AlBundy33 opened this issue Dec 1, 2014 · 1 comment


Copy link

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.


    final ScriptEngine _e = new JRubyEngineFactory().getScriptEngine();

    final StringWriter _s = new StringWriter();
    final PrintWriter _p = new PrintWriter(_s);

    _p.println("p $foo");
    _p.println("p $msg");
    _p.println("$foo = 'bar'");
    _p.println("p $foo");

    final SimpleBindings _ctx = new SimpleBindings();
    final Map<String, Object> _params = new HashMap<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);

    final SimpleBindings _ctx2 = new SimpleBindings();
    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);
@kares kares added this to the Non-Release milestone Jun 23, 2017
Copy link

kares commented Jun 23, 2017

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.

@kares kares closed this as completed Jun 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

2 participants