【发布时间】:2015-10-16 11:02:42
【问题描述】:
我有以下用 c++ 编写的几乎可以工作的代码:
[..]
Handle<Object> jsGlobal;
Handle<Function> jsUpdateFunc;
void setupJs () {
V8::Initialize();
Isolate* isolate = v8::Isolate::New();
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
Local<String> source = String::NewFromUtf8(isolate, "var a = 0; function test() { a++; return a.toString(); }");
Local<Script> script = Script::Compile(source);
script->Run();
jsGlobal = context->Global();
Handle<Value> value = jsGlobal->Get(String::NewFromUtf8(isolate, "test"));
jsUpdateFunc = Handle<Function>::Cast(value);
}
void callJs() {
Handle<Value> args[0];
Handle<Value> js_result = jsUpdateFunc->Call(jsGlobal, 0, args);
js_result->ToString();
String::Utf8Value utf8(js_result);
printf("%s\n", *utf8);
}
[..]
我有 setupJs() 函数设置 v8 环境,并且 callJs 应该被多次调用(工作时,javascript 脚本每次将 var a 递增一)。
如果我放
Handle<Value> args[0];
Handle<Value> js_result = jsUpdateFunc->Call(jsGlobal, 0, args);
js_result->ToString();
String::Utf8Value utf8(js_result);
printf("%s\n", *utf8);
在 setupJs 中,我可以看到函数是如何被调用的,并且“1”是如何打印的。但是,如果我将函数调用留给稍后调用的不同函数,我会在 Handle<Value> js_result = jsUpdateFunc->Call(jsGlobal, 0, args); 行出现 Segfault
我检查过,jsUpdateFunc 和 jsGlobal 都是非空指针
【问题讨论】:
标签: c++ scope v8 embedded-v8 libv8