我试图建立一个多线程环境,在那里可能会不断进入/退出多个v8::Isolate对象来编译和运行一些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?
});
}但是,正如您在代码中的注释中所看到的,它不起作用。我只想输入一个特定的隔离和一个持久化。我该怎么做?
提前感谢!
发布于 2015-05-21 12:23:41
你的C++在我看来是对的。你的Javascript中会有错误吗?使用TryCatch对象将揭示javascript代码中的任何问题。
https://stackoverflow.com/questions/25094967
复制相似问题