I'm trying to embed Rhino in my Android app to add scripting
functionality. According to Google, the Rhino shell works fine on
Android, which I've confirmed by building and running it from source
under Donut. So unless I'm comparing apples and orangutangs, my use case
should work too.
First I tried the basic embedding examples from the documentation,
though I called cx.setOptimization(-1) to disable all compilation. No
luck. I then thought that perhaps something was being optimized in the
context before I could turn off optimization, so I created my own
ContextFactory, and set the optimization level in the onContextCreated()
method, but no luck. Here's my current Scala code for creating an
interpreter and evaling an empty string for now:
package info.thewordnerd.spiel.scripting
import org.mozilla.javascript.Context
import org.mozilla.javascript.ContextFactory
import org.mozilla.javascript.Scriptable
class MyContextFactory extends ContextFactory {
override protected def onContextCreated(cx:Context) {
cx.setOptimizationLevel(-1)
}
}
object Scripter {
val js = """
"""
def apply() = {
ContextFactory.initGlobal(new MyContextFactory)
val cx = Context.enter
cx.setOptimizationLevel(-1)
val scope = cx.initStandardObjects
val result = cx.evaluateString(scope, js, "<spiel>", 1, null)
Context.exit
true
}
}
Running this on the emulator gives the following error (line 20 in
scripts.scala is the Context.enter call):
E/AndroidRuntime( 771): Uncaught handler: thread main exiting due to
uncaught exception
E/AndroidRuntime( 771): java.lang.ExceptionInInitializerError
E/AndroidRuntime( 771): at
org.mozilla.javascript.Context.enter(Context.java:411)
E/AndroidRuntime( 771): at
org.mozilla.javascript.Context.enter(Context.java:406)
E/AndroidRuntime( 771): at
org.mozilla.javascript.Context.enter(Context.java:386)
E/AndroidRuntime( 771): at
info.thewordnerd.spiel.scripting.Scripter$.apply(scripting.scala:20)
E/AndroidRuntime( 771): at
info.thewordnerd.spiel.services.Spiel.onServiceConnected(Spiel.scala:28)
E/AndroidRuntime( 771): at
android.accessibilityservice.AccessibilityService$IEventListenerWrapper.executeMessage(AccessibilityService.java:222)
E/AndroidRuntime( 771): at
com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:45)
E/AndroidRuntime( 771): at
android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 771): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 771): at
android.app.ActivityThread.main(ActivityThread.java:4203)
E/AndroidRuntime( 771): at
java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 771): at
java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 771): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
E/AndroidRuntime( 771): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
E/AndroidRuntime( 771): at dalvik.system.NativeStart.main(Native
Method)
E/AndroidRuntime( 771): Caused by: java.lang.IllegalStateException:
Failed to create VMBridge instance
E/AndroidRuntime( 771): at
org.mozilla.javascript.VMBridge.makeInstance(VMBridge.java:69)
E/AndroidRuntime( 771): at
org.mozilla.javascript.VMBridge.<clinit>(VMBridge.java:49)
E/AndroidRuntime( 771): ... 15 more
So what am I missing that lets the shell, compiled on a very similar
toolchain, work just fine? Is there some setup step I'm missing?
Thanks.
Ultimately, the problem seems to boil down to Context.java:411, which reads:
Object helper = VMBridge.instance.getThreadContextHelper();
I've scoured the shell example, trying to figure out what it does
differently from the very simple Context.enter() case, and while it goes
about things more circuitously, my attempts to do things similarly
always hit Context.java:411. It's starting to make me think that, while
I do ultimately have to disable optimization, my initial impression was
off. I think this because I've tried adding debugging log statements to
my factory's makeContext, and have even tried to insert a ContextAction
via contextFactory.call(). But none of this ever triggers.
In short, if I call Context.enter(), or contextFactory.call(), I get the
error detailed below, and none of my attempts to get *anything* running
before this error throws seem to work. I'm not clever enough to delve
into the Dalvik internals, so I've hit my limit as far as knowing what
to try.
Any thoughts?
"Nolan Darilek" <no...@thewordnerd.info> wrote in message
news:mailman.5459.1255642648.42...@lists.mozilla.org...
Here is a working example. I used the rhino jar from the ASE-
reporsitory (placed in libs).
Best regards, Micke
package miki.jsapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import org.mozilla.javascript.*;
public class JSApp extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText("Hello JavaScript!");
setContentView(view);
doit(view,
"var x = 'Kalle\\nLisa';\n" +
"TheView.append(x);"
);
}
void doit(TextView view, String code)
{
// Creates and enters a Context. The Context stores
information
// about the execution environment of a script.
Context cx = Context.enter();
cx.setOptimizationLevel(-1);
try
{
// Initialize the standard objects (Object, Function,
etc.)
// This must be done before scripts can be executed.
Returns
// a scope object that we use in later calls.
Scriptable scope = cx.initStandardObjects();
ScriptableObject.putProperty(scope, "TheView",
Context.javaToJS(view, scope));
// Now evaluate the string we've colected.
Object result = cx.evaluateString(scope, code, "doit:", 1,
null);
// Convert the result to a string and print it.
view.append(Context.toString(result));
}
finally
{
// Exit from the context.
Context.exit();
In any case, thanks for showing me that it could be done and that I
wasn't wasting my time. :)
> _______________________________________________
> dev-tech-js-engine-rhino mailing list
> dev-tech-js-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
>