3
我是V8的新手,嘗試在我的C++和JS代碼之間創建一些綁定。問題是我無法通過不同的Javascript函數訪問全局Javascript變量。原因是因爲每個Javascript函數都被稱爲我不同的C++函數。我想我的代碼的問題是,我在每個C++函數中創建一個新的本地上下文。如何在v8 Javascript的多個函數中使用相同的上下文?
我的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代碼,我得到的是說名是一個屬性是一個錯誤的未定義。我的理解是變量test在第二個函數中不被識別,可能是因爲每個函數都在不同的上下文中執行。如果是這樣,我如何讓它們在相同的上下文中執行?如果不是,有人可以幫助我理解並解決問題嗎?
任何幫助,將不勝感激:)
嗨!你提供的代碼片段看起來不完整,你能否包括你如何精確地調用js函數? – pinepain
@pinepain我已經更新了代碼。你可以看看現在嗎? –
很高興你知道自己出了什麼問題! – pinepain