调用函数时V8段错误 - 我做错了什么?

时间:2015-03-17 17:11:21

标签: android android-ndk v8 embedded-v8

我有一个非常古老的预制V8版本,可以通过JNI与Android NDK集成,而且一切都很顺利。

我决定将V8升级到更新的代码(4.1.0.22),在Ubuntu上构建我自己的ARM归档文件,并保持自己的架构相同,我重构了代码,因为V8代码库中的更改显然需要这些代码

然而,它崩溃了。

我想要构建它的方式,可能与一些更简单的示例相比,有一个持久的V8实例,我可以初始化一次然后再多次调用。每个调用都提供Javascript代码内容和方法名称(通常为“main”)。

所以,这实际上是我的初始化代码,运行一次:

Persistent<Context> exec_context;

Isolate* isolateRef;

Platform* platformRef;

V8Resources(void (&initialiseTemplate)(Isolate* isolate, Handle<ObjectTemplate>))
{
    // Initialize V8.
    V8::InitializeICU();
    platformRef = platform::CreateDefaultPlatform();
    V8::InitializePlatform(platformRef);
    V8::Initialize();

    isolateRef = Isolate::New();
    {
        // Create a stack-allocated handle scope.
        HandleScope handle_scope(isolateRef);

        //Register for callbacks
        Handle<ObjectTemplate> global_templ = ObjectTemplate::New(isolateRef);
        initialiseTemplate(isolateRef, global_templ);

        Local<Context> local_context = Context::New(isolateRef, NULL, global_templ);
        exec_context.Reset(isolateRef, local_context);

        Context::Scope context_scope(local_context);
    }
}

上面我的V8Resources对象是什么。据我所知,此代码可以正常工作。

然后在'run script'方法中,稍后调用,我有:

Isolate* isolate = v8instance->getIsolate();

Locker v8Locker(isolate);

const char *source_str= //...
const char *function_name_str = //...

HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
Local<Object> global = context->Global();
TryCatch trycatch(isolate);

Handle<String> js_source = String::NewFromUtf8(isolate, source_str);
Handle<Script> js_compiled = Script::Compile(js_source);

if (js_compiled.IsEmpty())
{
    throwNativeException("Error: compiled script is empty!");
    return 0;
}

js_compiled->Run(context);

Handle<String> js_function_name = String::NewFromUtf8(isolate, function_name_str);
Handle<Value> js_function_val = global->Get(js_function_name);

Handle<Function> js_func = Handle<Function>::Cast(js_function_val);

Handle<Value> argm[0];

Handle<Value> js_result;
{
    js_result = js_func->Call(global, 0, argm);

    if (js_result.IsEmpty())
    {
        //...
    }
    else
    {
        //...
    }
}

通过记录确定,这会在js_func->Call处以SIGSEGV爆炸,并且在我有任何有用的范围内,提供此堆栈跟踪:

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 000045d0
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #00  pc 002b24a0  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::TryCatch::TryCatch()+52)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #01  pc 0033cfd8  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::Execution::TryCall(v8::internal::Handle<v8::internal::JSFunction>, v8::internal::Handle<v8::internal::Object>, int, v8::internal::Handle<v8::internal::Object>*, v8::internal::MaybeHandle<v8::internal::Object>*)+64)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #02  pc 0034abfc  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::Factory::NewError(char const*, char const*, v8::internal::Handle<v8::internal::JSArray>)+384)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #03  pc 00349da4  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::Factory::NewError(char const*, char const*, v8::internal::Vector<v8::internal::Handle<v8::internal::Object> >)+372)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #04  pc 00349e90  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::Factory::NewReferenceError(char const*, v8::internal::Vector<v8::internal::Handle<v8::internal::Object> >)+40)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #05  pc 00429d74  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::IC::ReferenceError(char const*, v8::internal::Handle<v8::internal::Name>)+76)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #06  pc 0042ce44  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::LoadIC::Load(v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Name>)+1048)
Stack frame 03-17 15:42:52.168: I/DEBUG(28556):     #07  pc 0042d8a0  /data/app-lib/com.myapp.app-43/libv8jsevaluator.so (v8::internal::LoadIC_Miss(int, v8::internal::Object**, v8::internal::Isolate*)+396)
Stack frame 03-17 15:42:52.178: I/DEBUG(28556):     #08  pc 00000090  <unknown>

我已经玩了很多 - 包括不同的TryCatch模式 - 但无法到达任何地方,我不相信我实际上正在使用V8。

这个描述中有什么明显的错误吗?

0 个答案:

没有答案