On Sat, Aug 26, 2017 at 9:08 PM, Kavin Mani <[email protected]> wrote: > Hi, > > I am new to V8 and trying to create some bindings between my C++ and JS > code. The issue is that I am not able to access a global Javascript variable > across different Javascript functions. The reason is because each Javascript > function is called my different C++ function. I guess the problem with my > code is that I am creating a new local context within each C++ function. > > My JS Code: > > var test = []; > > function f1() > { > var obj = {}; > obj.name = "Testing"; > test.push(obj); > } > > function f2() > { > var value = test[0].name; > } > > My C++ code: > > class Test { > > v8::Persistent<v8::Script> compiledScript; > v8::Local<v8::Value> result; > v8::Isolate* isolate; > v8::Persistent<v8::Context> context; > > Test(filePath) { > > // Create and allocate isolate > > v8::Locker isolateLocker(isolate); > v8::Isolate::Scope isolate_scope(isolate); > v8::HandleScope handle_scope(isolate); > > // Create some bindings > > v8::Local<v8::Context> con = v8::Context::New(isolate, > nullptr, binding_template); > con->SetAlignedPointerInEmbedderData(0, &binder); > > context.Reset(isolate, con); > v8::Context::Scope context_scope(con); > > std::string source_file = LoadFile(filePath); > v8::Local<v8::String> sourceScript = > v8::String::NewFromUtf8(isolate, source_file.c_str(), > v8::NewStringType::kNormal).ToLocalChecked(); > v8::Local<v8::Script> script = v8::Script::Compile(con, > sourceScript).ToLocalChecked(); > compiledScript.Reset(isolate, script); > } > > void function1() > { > v8::Locker isolateLocker(isolate); > v8::Isolate::Scope isolate_scope(isolate); > v8::HandleScope handle_scope(isolate); > > v8::Local<v8::Context> con= > v8::Local<v8::Context>::New(isolate, context); > v8::Local<v8::Script> script = > v8::Local<v8::Script>::New(isolate, compiledScript); > > v8::Context::Scope context_scope(con); > > // Code to call the Javascript function f1 > > } > > void function2() > { > v8::Locker isolateLocker(isolate); > v8::Isolate::Scope isolate_scope(isolate); > v8::HandleScope handle_scope(isolate); > > v8::Local<v8::Context> con = > v8::Local<v8::Context>::New(isolate, context); > v8::Local<v8::Script> script = > v8::Local<v8::Script>::New(isolate, compiledScript); > > v8::Context::Scope context_scope(con); > > // Code to call the Javascript function f2 > > } > > When the above JS code compiles, I get an error that says name is a property > of undefined. My understanding is that variable test is not recognized in > second function probably because they are executing in different contexts. > Can someone please help me understand and fix the issue? > > Any help will be greatly appreciated :) > > Thanks, > Kavin
You create the Context once in the constructor so that looks okay. I suspect the issue is that you call Script::Compile() but you never seem to call script->Run(). -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
