如何在多线程环境中输入v8 :: Persistent <v8 :: context>?</v8 :: context>

时间:2014-08-02 12:20:10

标签: v8 embedded-v8

我尝试设置多线程环境,可以不断输入/退出多个v8 :: Isolate对象来编译和运行一些JavaScript代码。我有一个方法,应该在特定的隔离/上下文中编译和运行som javascript代码:

void myclass::run(const std::string & code, v8::Persistsent<v8::Context> & con)
{
  boost::asio::io_service::strand & strand = this->_strand;
  // strand guarantee at most one handler will be executed concurrently
  strand.post([&]() {
    v8::Isolate::Scope isolate_scope(this->get_isolate());
    v8::HandleScope handle_scope(this->get_isolate());

    // This code below will compile but undefined is allways returned.
    // Because v8::Persistent<v8::Context> does not have any
    // "Enter/Exit" methods so is my attempt to create a Local handle instead.
    // However, as I said earlier, it does not work. Any idea how to fix it?

    v8::Local<v8::Context> context = v8::Local<v8::Context>::New(this->get_isolate(), con);
    v8::Context::Scope context_scope(context); // auto enter/leave. 


    // Compile the source code.
    v8::Local<v8::String> source = v8::String::NewFromUtf8(_isolate, code.c_str());

    v8::Local<v8::Script> script = v8::Script::Compile(source);

    v8::Local<v8::Value> result = script->Run();
    // result is allways undefined, what am I doing wrong?
  });
}

正如您在代码中的注释中所看到的那样,它不起作用。我只是想与Persistent一起输入一个specfic Isolate。我该怎么做?

提前致谢!

1 个答案:

答案 0 :(得分:0)

你的C ++对我来说很合适。您的Javascript中可能有错误吗?使用TryCatch对象将揭示您的javascript代码中的任何问题。