【问题标题】:How to enter a v8::Persistent<v8::Context> in a multithreaded environment?如何在多线程环境中输入 v8::Persistent<v8::Context>?
【发布时间】:2014-09-25 12:39:00
【问题描述】:

我试图设置一个多线程环境,可能会不断地进入/退出多个 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?
  });
}

正如您在代码中的 cmets 中看到的那样,它不起作用。我只想输入一个特定的 Isolate 和一个 Persistent。我该怎么做?

提前致谢!

【问题讨论】:

    标签: v8 embedded-v8


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2019-10-16
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多