How to use global_object with multiple v8 :: Context s?

I am reorganizing my V8 with an OpenGL implementation and run into a problem in the execution context.

The concept is this:

  • V8GL::initialize() This method initializes the context and global template. It also uses context for glut because there are several loops that are also subject to JS context. (e.g. glut.mainLoop())

  • V8GL::execute(context, source, url) This method basically executes the string inside the given context. I think it should be for the subsequent use of isolates at intervals / timeouts.

What does not work:

V8GL::initialize()also attaches embedded JavaScript files and executes them. It works great.

Simplified code:

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);

}

Problem:

. , JavaScript. , . main.cpp main() .

int main(...) {

    v8gl::V8GL::initialize(&argc, argv); // argc and argv required for glut.

    v8::HandleScope scope;
    // This doesn't work:
    // v8::Local<v8::Context> context = v8::Context::GetCurrent();

    // You can't pass NULL as second parameter, so what then?
    v8::Context::New(NULL, v8::ObjectTemplate::New(), v8gl::global);

    // In here, all globals are lost. Why?
    v8gl::V8GL::execute(context, source, filepath);

}

:

  • objectTemplate v8::Context::New()?
  • global_object ?
  • ? DetachGlobal() ReattachGlobal() , . .
+5

All Articles