我是V8的新手,并试图在我的C ++和JS代码之间创建一些绑定。问题是我无法跨不同的Javascript函数访问全局Javascript变量。原因是因为每个Javascript函数都被称为我不同的C ++函数。我想我的代码的问题是我在每个C ++函数中创建一个新的本地上下文。
我的JS代码:
var test = [];
function f1()
{
var obj = {};
obj.name = "Testing";
test.push(obj);
}
function f2()
{
var value = test[0].name;
}
我的C ++代码:
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
result = script->Run(con).ToLocalChecked();
v8::Local<v8::Object> global = con->Global();
v8::Local<v8::Value> function_value1 = global->Get(v8::String::NewFromUtf8(isolate, "f1"));
v8::Local<v8::Function> function1 = v8::Local<v8::Function>::Cast(function_value1);
v8::Local<v8::Value> js_result1;
js_result1 = function1->Call(con, global, 0, nullptr).ToLocalChecked();
}
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
result = script->Run(con).ToLocalChecked();
v8::Local<v8::Object> global = con->Global();
v8::Local<v8::Value> function_value2 = global->Get(v8::String::NewFromUtf8(isolate, "f2"));
v8::Local<v8::Function> function2 = v8::Local<v8::Function>::Cast(function_value2);
v8::Local<v8::Value> js_result2;
js_result2 = function2->Call(con, global, 0, nullptr).ToLocalChecked();
}
};
void main(int argcs, char* argvs[])
{
V8::InitializeICUDefaultLocation(argvs[0]);
V8::InitializeExternalStartupData(argvs[0]);
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
Test test(jsFile);
test.function1();
test.function2();
}
执行上述JS代码时,出现错误,指出 name 是未定义的属性。我的理解是,在第二个函数中无法识别变量 test ,因为每个函数都在不同的上下文中执行。如果是这样,我如何让它们在相同的上下文中执行?如果没有,有人可以帮我理解并解决问题吗?
任何帮助将不胜感激:)
答案 0 :(得分:1)
我想通了自己。错误是我每次都在所有功能中运行脚本。相反,我应该在构造函数中运行一次,就像编译一样。