2013-06-26 32 views
1

我想从后台线程的android应用程序中的JNI调用调用v8。这是导致运行崩溃了约V8投诉:: ObjectTemplate ::新(V8 ::处理v8在Android的后台线程JNI

重现从下面的Java代码

{ 
    final Thread loadJS = new Thread() { 
     @Override 
     public void run() { 
      JSfunc()); 
     } 
    }; 
    loadJS.start(); 
    } 
拨打以下JNI

void JSfunc() { 
     v8::Isolate* currentIsolate = v8::Isolate::GetCurrent(); 
     if(!currentIsolate) { 
      currentIsolate = v8::Isolate::New(); 
     } 
     v8::HandleScope handle_scope(currentIsolate); 
     v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 
    } 

如果直接从UI线程或可运行它的工作原理调用该函数。

任何想法?

+0

如果你崩溃那里,我想我们是正确的假设,在为了我们的利益,您已经遗漏了JSFunc的长JNI头声明的jni代码片段? (不要责怪你...) – Tom

+0

是的,我错过了外部“C”{JNIEXPORT void Java_com_example_app_class_JSfunc(JNIEnv * env,jobject thiz){JSfunc(); }} – navillus

+0

太好了。难... – Tom

回答

0

的问题是,生成隔离代码丢失

currentIsolate->Enter(); 

所以正确的JNI功能

void JSfunc() { 
    v8::Isolate* currentIsolate = v8::Isolate::GetCurrent(); 
    if(!currentIsolate) { 
     currentIsolate = v8::Isolate::New(); 
     currentIsolate->Enter(); 
    } 
    v8::HandleScope handle_scope(currentIsolate); 
    v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 
}