如何在v8 Javascript中的多个函数中使用相同的上下文? [英] How to use the same context across multiple functions in v8 Javascript?

查看:208
本文介绍了如何在v8 Javascript中的多个函数中使用相同的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是V8的新手,正在尝试在我的C ++和JS代码之间创建一些绑定.问题是我无法跨不同的Javascript函数访问全局Javascript变量.原因是因为每个Javascript函数都称为我不同的C ++函数.我想我的代码的问题是我正在每个C ++函数中创建一个新的本地上下文.

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.

我的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 在第二个函数中未被识别,可能是因为每个函数在不同的上下文中执行.如果是这样,如何使它们在相同的上下文中执行?如果没有,有人可以帮我理解和解决此问题吗?

When the above JS code is executed, 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 each function is being executed in different contexts. If so, how do I make them execute in the same context? If not, can someone please help me understand and fix the issue?

任何帮助将不胜感激:)

Any help will be greatly appreciated :)

推荐答案

我想通了自己.错误是我每次都在所有功能中运行脚本.相反,我应该像编译一样在构造函数中运行一次.

I figured out myself. The mistake was that I was running the script each time in all functions. Rather, I should run just once in the constructor like compiling.

这篇关于如何在v8 Javascript中的多个函数中使用相同的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆