Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created January 21, 2011 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/789134 to your computer and use it in GitHub Desktop.
Save cowboyd/789134 to your computer and use it in GitHub Desktop.
trying to do an instance_eval on any old object from java
package spike;
import org.jruby.embed.ScriptingContainer;
public class Spike {
public static void main(String[] args) {
ScriptingContainer container = new ScriptingContainer();
Object someInstance = container.runScriptlet("Object.new");
container.callMethod(someInstance, "instance_eval", "self", "<eval>", 1);
}
}
java.lang.NullPointerException
at org.jruby.runtime.ThreadContext.preExecuteUnder(ThreadContext.java:1304)
at org.jruby.RubyBasicObject.evalUnder(RubyBasicObject.java:1897)
at org.jruby.RubyBasicObject.specificEval(RubyBasicObject.java:1851)
at org.jruby.RubyObject.instance_eval(RubyObject.java:1212)
at org.jruby.RubyObject$i_method_multi$RUBYINVOKER$instance_eval.call(org/jruby/RubyObject$i_method_multi$RUBYINVOKER$instance_eval.gen:65535)
at org.jruby.internal.runtime.methods.JavaMethod$JavaMethodZeroOrOneOrTwoOrThreeBlock.call(JavaMethod.java:488)
at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:178)
at org.jruby.RubyClass.finvoke(RubyClass.java:665)
at org.jruby.javasupport.util.RuntimeHelpers.invoke(RuntimeHelpers.java:552)
at org.jruby.embed.internal.EmbedRubyObjectAdapterImpl.callEachType(EmbedRubyObjectAdapterImpl.java:448)
at org.jruby.embed.internal.EmbedRubyObjectAdapterImpl.call(EmbedRubyObjectAdapterImpl.java:392)
at org.jruby.embed.internal.EmbedRubyObjectAdapterImpl.callMethod(EmbedRubyObjectAdapterImpl.java:332)
at org.jruby.embed.ScriptingContainer.callMethod(ScriptingContainer.java:1325)
at spike.Spike.main(Spike.java:9)
@yokolet
Copy link

yokolet commented Jan 21, 2011

This seems to come from pushing a scope for sharing local variables. As far as I tested, when sharing variables feature was off, the exception wasn't raised. This is a bug of embedding API. Please file this. Workaround is setting sharing variables off like below:

container.setAttribute(AttributeName.SHARING_VARIABLES, false);
container.callMethod(someInstance, "instance_eval", "self", "", 1);

This attribute can be changed in each runScriptlet/callMethod by setting the value just before those methods.

@cowboyd
Copy link
Author

cowboyd commented Jan 22, 2011

Thanks for the explanation and workaround. Where should I file it.?

@yokolet
Copy link

yokolet commented Feb 13, 2011

Sorry for not responding this. I fixed this bug in rev. 34aec51. So, you don't need to set the attribute any more.

Thanks for reporting the bug!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment