*/
public class JRubyEngine extends BSFEngineImpl {
private Ruby runtime;
public Object apply(String file, int line, int col, Object funcBody, Vector paramNames, Vector args) {
ThreadContext context = runtime.getCurrentContext();
try {
// add a new method conext
String[] names = new String[paramNames.size()];
paramNames.toArray(names);
context.preBsfApply(names);
// FIXME: This is broken. We are assigning BSF globals as local vars in the top-level
// scope. This may be ok, but we are overwriting $~ and $_. Leaving for now.
DynamicScope scope = context.getCurrentScope();
// set global variables
for (int i = 0, size = args.size(); i < size; i++) {
scope.setValue(i, JavaEmbedUtils.javaToRuby(runtime, args.get(i)), 0);
}
// See eval todo about why this is commented out
//runtime.setPosition(file, line);
Node node = runtime.parseEval(file, funcBody.toString(), null, 0);
IRubyObject result = node.interpret(runtime, context, runtime.getTopSelf(), Block.NULL_BLOCK);
return JavaEmbedUtils.rubyToJava(runtime, result, Object.class);
} catch (StackOverflowError sfe) {
throw runtime.newSystemStackError("stack level too deep");
} finally {
context.postBsfApply();
}
}