Search code examples
javascriptandroidjvmrhinodalvik

Android StackOverflow error using Rhino's evaluateString() on Javascript


i'm trying to use Rhino for Android in order to interact with this Chat service on private website. I don't actually need to look at the chat screen with the app, so that's why i thought maybe Rhino is a good approach because it hides all the window/browsery stuff.

1) is this even the right approach? the only reason i'm trying this is because i've heard that WebViews were not safe/the best solution? (please do correct me on this if this is not true at all for my situation!)

2) so i just downloaded the javascript and tried to create a Context and evaluateString on that. i have the right scope and all that. Furthermore, i also made sure to set the optimization level to -1, because Dalvik code won't run JVM bytecode generated from Rhino. however, the error i'm getting is this:

D/dalvikvm(  465): GC_FOR_MALLOC freed 1840 objects / 326872 bytes in 57ms
I/dalvikvm(  465): threadid=1: stack overflow on call to Lorg/mozilla/javascript/TokenStream;.getChar:I
I/dalvikvm(  465):   method requires 36+20+8=64 bytes, fp is 0x4186933c (60 left)
I/dalvikvm(  465):   expanding stack end (0x41869300 to 0x41869000)
I/dalvikvm(  465): Shrank stack (to 0x41869300, curFrame is 0x41869754)
D/dalvikvm(  465): GC_FOR_MALLOC freed 5607 objects / 262320 bytes in 47ms
W/dalvikvm(  465): Exception Lorg/mozilla/javascript/EvaluatorException; thrown during Lcom/test/testing/rhinochat/TestChat;.<clinit>
I/dalvikvm(  465): Rejecting re-init on previously-failed class Lcom/test/testing/rhinochat/TestChat; v=0x0
D/AndroidRuntime(  465): Shutting down VM
W/dalvikvm(  465): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  465): FATAL EXCEPTION: main
E/AndroidRuntime(  465): java.lang.NoClassDefFoundError: com.test.testing.rhinochat.TestChat
E/AndroidRuntime(  465):        at com.test.testing.rhinochat.TestChatRunner.run(TestChatRunner.java:46)
E/AndroidRuntime(  465):        at com.test.testing.rhinochat.StartChat.onCreate(StartChat.java:10)
E/AndroidRuntime(  465):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  465):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  465):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  465):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  465):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  465):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  465):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  465):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  465):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  465):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  465):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  465):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  465):        at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(   59):   Force finishing activity com.test.testing/.rhinochat.StartChat
W/ActivityManager(   59):   Force finishing activity com.test.testing/.RhinoOnAndroidActivity
W/ActivityManager(   59): Activity pause timeout for HistoryRecord{450a1240 com.test.testing/.rhinochat.StartChat}

i've set the max allowed stack space from Eclipse (i think the max long or something ridiculous like that) and still getting this error. tested with various devices and same results.

here's the catch: i run my code on my desktop, and everything compiles and runs perfectly, even with no optimization on compiler. but when i run the SAME EXACT code on Android, it crashes. (i've tried both emulator and multiple hardware devices)

so at this point i'm super stumped. Does anyone have any idea as to why this is happening? is there a way i can make Dalvik behave like JVM when compiling/running this javascript? is this the javascript's fault? and if so, how come i don't get the same error on my desktop with the same javascript?

To make matters worse, i don't even know how to get any hints on what the javascript error is throwing, or a stack trace or anything

ANY help or hints would be severely appreciated.

happy holidays, and thanks for reading the question.


Solution

  • okay, so the problem was that i didn't ask the right question. i shouldn't have freaked out and threw a bunch of information out.

    it turns out if you set the max stack size, it just completely gets ignored. so by setting the stack size to a fairly reasonable amount, it'll work. of course, this requires tweaking with the stack space and figuring out exactly at which point things will work. it turns out using 100kb of stack space was enough for me, given that i had also put it in a separate thread.

    Runnable r = new Runnable(){
        public void run(){
        //... evaluateString() function here ...
        }
    }
    final Thread t = new Thread(new ThreadGroup("A") , r, "my thread name", 100000);
    t.start();