matg
matg

Reputation: 111

JRuby JSR223 Interface ignores binidngs

It seems that the JSR 223 javax.script interface of the JRuby (1.6.7.2) framework ignores bound Java values in the Bindings of a ScriptContext. Do I make a mistake? Here is a simple example that doesn't work:

private void run() throws ScriptException {
    ScriptEngine engine = new JRubyEngineFactory().getScriptEngine();
    LittleClass l = new LittleClass();
    engine.put("l", l);
    engine.eval("l.x;");
}

public class LittleClass {
    public int x;
    public void add() {
        x = x + 1;
    }
}

Or is this a known problem?

Upvotes: 2

Views: 317

Answers (1)

fgb
fgb

Reputation: 18569

By default, local variables don't survive across multiple evaluations. See: http://kenai.com/projects/jruby/pages/RedBridge

To change this behaviour, set the org.jruby.embed.localvariable.behavior property:

    System.setProperty("org.jruby.embed.localvariable.behavior", "persistent");

    ScriptEngine engine = new JRubyEngineFactory().getScriptEngine();
    LittleClass l = new LittleClass();
    engine.put("l", l);
    engine.eval("l.add");
    System.out.println(engine.eval("l.x"));

Upvotes: 3

Related Questions