3

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

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

提前致谢!

4

1 回答 1

0

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

于 2015-05-21T04:23:40.937 回答