从 Web 工作线程调用C++ v8 函数
calling C++ v8 function from a web worker thread
我在我的javascript代码中创建了一个Web worker thread。我正在尝试使用 node-gyp 和 V8 从线程调用 C++ 函数。但我无法让它工作。
这是 hello.cc 的代码
#include <v8.h>
using namespace v8;
extern std::string myhello();
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("hello"));
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction()
);
}
NODE_MODULE(hello, init)
这是myhello的代码.js
var addon = require('./build/Release/hello');
var thread = require('webworker-threads');
var t = thread.create();
console.log(t.eval("addon.hello()"));
当我运行node myhello.js
时,我得到以下输出
{ id: 0,
eval: [Function: eval],
load: [Function: load],
emit: [Function: emit],
emitSerialized: [Function: emitSerialized],
destroy: [Function: destroy],
on: [Function],
once: [Function],
removeAllListeners: [Function],
dispatchEvents: [Function],
_on: {} }
我希望"你好"打印在控制台上。
感谢任何帮助或指示。
我看到 2 个问题:
- t.eval 返回线程本身(从控制台输出中可以看到)。代码执行的结果将传递给回调(如果提供了回调)
- 您需要在传递给 eval 的代码中要求插件,闭包语义在这里不起作用。但看起来这是不可能的:需求不是在线程上下文中定义的。我认为这是为了防止竞争条件问题而设计的。另请参阅 https://github.com/audreyt/node-webworker-threads/issues/15
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 在 G++ v8.2.0 中使用 std::experimental::可选
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- v8::HandleScope::CreateHandle() # 中的致命错误无法在没有 HandleScope 的情
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在 C++ 中嵌入 V8 会导致与 libcpp 相关的"unrecognized external symbol"错误
- 当我使用 V8 库中的 GetInternalField() 时出现分段错误
- 在 v8 JavaScript 中重复调用C++是否有巨大的开销?
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 与纯 V8 相比,NodeJS 是否有任何性能缺陷或显著开销?
- V8垃圾收集器回调,用于测量GC活动
- 如何在 C++ 中从 V8 调用 Javascript 函数
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 使用导入的函数从嵌入式v8调用webassembly
- v8 源代码中 ArrayMap 函数的回调 fn 参数是什么?
- 如何在 v8 Javascript 中的多个函数中使用相同的上下文?
- 如何使用 v8 本机插件将 C++ 数组交付到 Node.js
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- v8 不再支持 v8::Value::ToNumber 了吗?
- 如何将wchar_t转换为 V8 字符串?