2

JRuby (1.6.7.2) 框架的 JSR 223 javax.script 接口似乎忽略了 ScriptContext 的绑定中的绑定 Java 值。我犯错了吗?这是一个不起作用的简单示例:

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;
    }
}

或者这是一个已知问题?

4

1 回答 1

3

默认情况下,局部变量不会在多次评估中存活。见: http: //kenai.com/projects/jruby/pages/RedBridge

要更改此行为,请设置org.jruby.embed.localvariable.behavior属性:

    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"));
于 2013-03-11T15:36:09.080 回答