首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在多线程环境中输入v8::Persistent<v8::Context>?

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

Stack Overflow用户
提问于 2014-08-02 20:20:10
回答 1查看 1.1K关注 0票数 2

我试图建立一个多线程环境,在那里可能会不断进入/退出多个v8::Isolate对象来编译和运行一些JavaScript代码。我有一个方法可以在特定的隔离/上下文中编译和运行某些javascript代码:

代码语言: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?
  });
}

但是,正如您在代码中的注释中所看到的,它不起作用。我只想输入一个特定的隔离和一个持久化。我该怎么做?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2015-05-21 12:23:41

你的C++在我看来是对的。你的Javascript中会有错误吗?使用TryCatch对象将揭示javascript代码中的任何问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25094967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档