Cool, thanks, actually I figured it out. I'm using Proguard to slim down
my app, and apparently the crazy amount of introspection and reflection
being used was tripping Rhino up. Also, the exception I was getting was
a wrapper for another, which cued me into the fact that classes weren't
being loaded as they should. Including the entire Rhino jar fixed this,
and I now have a scriptable app.
In any case, thanks for showing me that it could be done and that I
wasn't wasting my time. :)
On 11/20/2009 04:36 PM, Mikael Kindborg wrote:
Hi!
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();
}
}
}
On Oct 15, 2:10 am, "Nolan Darilek"<[email protected]> wrote:
Hi, all, hoping someone can give me a few pointers as to what I'm doing
wrong.
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.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino