assets/src/org/ruboto/ScriptLoader.java in ruboto-0.15.0 vs assets/src/org/ruboto/ScriptLoader.java in ruboto-0.16.0

- old
+ new

@@ -1,8 +1,9 @@ package org.ruboto; import java.io.IOException; +import java.util.Map; import android.app.ProgressDialog; import android.content.Context; import android.os.Bundle; @@ -10,11 +11,11 @@ /** Return true if we are called from JRuby. */ public static boolean isCalledFromJRuby() { StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); - int maxLookBack = Math.min(9, stackTraceElements.length); + int maxLookBack = Math.min(8, stackTraceElements.length); for(int i = 0; i < maxLookBack ; i++){ if (stackTraceElements[i].getClassName().startsWith("org.jruby.javasupport.JavaMethod")) { return true; } } @@ -50,11 +51,10 @@ // FIXME(uwe): Simplify when we stop support for Ruby 1.8 mode. if (JRubyAdapter.isRubyOneEight()) { JRubyAdapter.put("$java_instance", component); rubyClass = JRubyAdapter.runScriptlet("class << $java_instance; self; end"); } else if (JRubyAdapter.isRubyOneNine()) { - JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true"); rubyClass = JRubyAdapter.runRubyMethod(component, "singleton_class"); } else { throw new RuntimeException("Unknown Ruby version: " + JRubyAdapter.get("RUBY_VERSION")); } // EMXIF @@ -65,11 +65,10 @@ Log.d("Loading script: " + component.getScriptInfo().getScriptName()); if (scriptContainsClass) { Log.d("Script contains class definition"); if (rubyClass == null && hasBackingJavaClass) { Log.d("Script has separate Java class"); - JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true"); rubyClass = JRubyAdapter.runScriptlet("Java::" + component.getClass().getName()); } Log.d("Set class: " + rubyClass); JRubyAdapter.put(component.getScriptInfo().getRubyClassName(), rubyClass); // FIXME(uwe): Collect these threads in a ThreadGroup ? @@ -114,18 +113,42 @@ } } } public static final void callOnCreate(final RubotoComponent component, Object... args) { + persistObjectProxy(component); if (component instanceof android.content.Context) { Log.d("Call onCreate on: " + component.getScriptInfo().getRubyInstance()); // FIXME(uwe): Simplify when we stop support for snake case aliasing interface callback methods. if ((Boolean)JRubyAdapter.runScriptlet(component.getScriptInfo().getRubyClassName() + ".instance_methods(false).any?{|m| m.to_sym == :onCreate}")) { JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "onCreate", args); - } else if ((Boolean)JRubyAdapter.runScriptlet(component.getScriptInfo().getRubyClassName() + ".instance_methods(false).any?{|m| m.to_sym == :on_create}")) { + } else if ((Boolean)JRubyAdapter.runScriptlet(component.getScriptInfo().getRubyClassName() + ".instance_methods(true).any?{|m| m.to_sym == :on_create}")) { JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "on_create", args); + } else { + JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "onCreate", args); } // EMXIF } + } + + public static final void callOnDestroy(final RubotoComponent component) { + String rubyClassName = component.getScriptInfo().getRubyClassName(); + if ((Boolean)JRubyAdapter.runScriptlet(rubyClassName + ".instance_methods(false).any?{|m| m.to_sym == :onDestroy}")) { + JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "onDestroy"); + } else if ((Boolean)JRubyAdapter.runScriptlet(rubyClassName + ".instance_methods(true).any?{|m| m.to_sym == :on_destroy}")) { + JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "on_destroy"); + } else { + JRubyAdapter.runRubyMethod(component.getScriptInfo().getRubyInstance(), "onDestroy"); + } + releaseObjectProxy(component); + } + + private static void persistObjectProxy(RubotoComponent component) { + JRubyAdapter.runScriptlet("Java::" + component.getClass().getName() + ".__persistent__ = true"); + ((Map)JRubyAdapter.get("RUBOTO_JAVA_PROXIES")).put(component.getScriptInfo().getRubyInstance(), component.getScriptInfo().getRubyInstance()); + } + + private static void releaseObjectProxy(RubotoComponent component) { + ((Map)JRubyAdapter.get("RUBOTO_JAVA_PROXIES")).remove(component.getScriptInfo().getRubyInstance()); } }