I have the following, almost working piece of code written in 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);
}
[..]
I have the function setupJs() setup the v8 environment and callJs is supposed to be called multiple times (when working, the javascript script increments var a by one each time).
If i put
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);
in setupJs, I can see how the function s called and "1" is printed. But if I leave the function call withing a different function to be called later, i have a Segfault at the line Handle<Value> js_result = jsUpdateFunc->Call(jsGlobal, 0, args);
I've checked and both jsUpdateFunc and jsGlobal are not-null pointers