C++ 如何使用多个v8::Context正确重用全局_对象?

C++ 如何使用多个v8::Context正确重用全局_对象?,c++,v8,embedded-v8,C++,V8,Embedded V8,我正在用OpenGL实现重构我的V8,但在执行上下文中遇到了一个问题 概念如下: V8GL::initialize() 此方法初始化上下文和全局模板。它还使用glut的上下文,因为运行的几个循环也暴露于JS上下文。(例如,glut.mainLoop()) V8GL::执行(上下文、源、url) 此方法主要在给定上下文中执行字符串。我认为以后使用间隔/超时时必须采用这种方式 什么不起作用: V8GL::initialize(...) { v8::Persistent<v8::Co

我正在用OpenGL实现重构我的V8,但在执行上下文中遇到了一个问题

概念如下:

  • V8GL::initialize()
    此方法初始化上下文和全局模板。它还使用glut的上下文,因为运行的几个循环也暴露于JS上下文。(例如,
    glut.mainLoop()

  • V8GL::执行(上下文、源、url)
    此方法主要在给定上下文中执行字符串。我认为以后使用间隔/超时时必须采用这种方式

什么不起作用:

V8GL::initialize(...) {
    v8::Persistent<v8::Context> context = v8::Context::New(NULL, globalTemplate);
    v8::Context::Scope context_scope(context);

    // cached for glut mainLoop etc.
    GlutFactory::context_ = v8::Persistent<v8::Context>::New(context);

    execute(context, v8::String::New(...script content...), v8::String::New(...script url path to show for exception stacktraces...);


    // after all the stuff was dispatched to the global context, I want to cache the global object for later usage.
    v8gl::global = v8::Persistent<v8::Object>::New(context->Global());
    context->DetachGlobal();
    context.Dispose();

}

V8GL::execute(context, source, filename) {

    v8::HandleScope scope;
    // Previously, I had v8::Context::Scope(context) in here.
    v8::Local<v8::Script> script = v8::Script::Compile(source, filename);

}
V8GL::initialize()
还附加内置JavaScript文件并执行它们。那很好用

简化代码:

V8GL::initialize(...) {
    v8::Persistent<v8::Context> context = v8::Context::New(NULL, globalTemplate);
    v8::Context::Scope context_scope(context);

    // cached for glut mainLoop etc.
    GlutFactory::context_ = v8::Persistent<v8::Context>::New(context);

    execute(context, v8::String::New(...script content...), v8::String::New(...script url path to show for exception stacktraces...);


    // after all the stuff was dispatched to the global context, I want to cache the global object for later usage.
    v8gl::global = v8::Persistent<v8::Object>::New(context->Global());
    context->DetachGlobal();
    context.Dispose();

}

V8GL::execute(context, source, filename) {

    v8::HandleScope scope;
    // Previously, I had v8::Context::Scope(context) in here.
    v8::Local<v8::Script> script = v8::Script::Compile(source, filename);

}
问题:

V8GL::initialize(...) {
    v8::Persistent<v8::Context> context = v8::Context::New(NULL, globalTemplate);
    v8::Context::Scope context_scope(context);

    // cached for glut mainLoop etc.
    GlutFactory::context_ = v8::Persistent<v8::Context>::New(context);

    execute(context, v8::String::New(...script content...), v8::String::New(...script url path to show for exception stacktraces...);


    // after all the stuff was dispatched to the global context, I want to cache the global object for later usage.
    v8gl::global = v8::Persistent<v8::Object>::New(context->Global());
    context->DetachGlobal();
    context.Dispose();

}

V8GL::execute(context, source, filename) {

    v8::HandleScope scope;
    // Previously, I had v8::Context::Scope(context) in here.
    v8::Local<v8::Script> script = v8::Script::Compile(source, filename);

}
  • 我是否必须缓存global的objectTemplate以便与
    v8::Context::New()
    一起使用
  • 全局_对象必须是持久的还是局部的
  • 我如何重复使用它?
    DetachGlobal()
    resattachglobal()
    这两个东西只适用于一个上下文,而不适用于多个上下文。我的案例需要多个上下文