title: Storing a Javascript function from a Java instance variable
body: <p>I'm using Rhino to embed JS code in a Java (android) app.  I need a certain function written in Java to return a Java function to be called later by Javascript.  In order to maintain compatibility with the iOS implementation, this needs to be stored and saved in a JS function variable.  I can't figure out how to return a Java object that's treated as a JS function from Javascript.  I thought the answer to this would be to make a NativeJavaMethod,  but when I try to call one in Javascript it always says the "this" pointer is wrong.  The code I used was:</p>
  
  <pre><code>NativeJavaMethod("", runnable.getClass().getMethod("run"), scope);
  </code></pre>
  
  <p>Where runnable is a Runnable and scope is the top level scope.  There has to be a way to do this, what am I doing wrong?  I'm guessing its the scope, but I don't know what it wants there.</p>
  
  <p>Exact message:   <code>org.mozilla.javascript.EvaluatorException: Java method "run" was invoked with [object Object] as "this" value that can not be converted to Java type java.lang.Runnable. (#1)</code></p>
  
  <p>(I have also even tried writing the runnable into a JS variable then storing runnable.run in a variable and grabbing that from Java to return.  Same problem.)</p>
  
  <p>Edit:</p>
  
  <p>The NativeJavaMethod is returned from a Java function to the caller (which is written in JavaScript), stored in a variable, then later executed. So in JS</p>
  
  <pre><code>var x=myobject.makeFunction();  //makeFunction is in Java, is properly called and returns the NativeJavaMethod
  x();  //This throws the above exception
  </code></pre>