将类实例函数绑定到 v8::FunctionTemplate
Bind class instance function to v8::FunctionTemplate
我对 C++ 和 v8 一般来说相当陌生,我想构建一个原生节点.js插件,但现在我被困在非常简单的 IMO 上,但我无法弄清楚问题是什么,错误消息
C:PathToProjectFile.cpp(50): error C2664: 'v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate *,v8::FunctionCallback,v8::Local<v8::Value>,v8::Local<v8::Signature>,int,v8::ConstructorBehavior,v8::SideEffectType)': cannot convert argument 2 from 'v8::Local<v8::Value> (__cdecl *)(const v8::FunctionCallbackInfo<v8::Value> &)' to 'v8::FunctionCallback' [C:PathToProjectbuildnode_gui.vcxproj]
没有那么有帮助。
我有以下代码,
v8::Local <v8::Object> Window::GetFunctions() {
v8::Local <v8::Object> DrawFunctions = v8::Object::New(isolate);
v8::Local <v8::FunctionTemplate> bgfnc = v8::FunctionTemplate::New(isolate, &Window::BackgroundCB);
DrawFunctions->Set(v8::String::NewFromUtf8(isolate, "background"), bgfnc);
return DrawFunctions;
}
void Window::Background(const v8::FunctionCallbackInfo <v8::Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
renderer->Background(args[0]->NumberValue(), args[1]->NumberValue(), args[2]->NumberValue());
}
v8::Handle <v8::Value> BackgroundCB(const v8::FunctionCallbackInfo <v8::Value> &args) {
return ((Window*)v8::External::Cast(*(args.Data())->Value())->Background());
}
我想创建一个包含函数列表的对象,函数的回调将是 Window
类的成员函数。我知道这之前已经在这里被问过,它曾经使用非成员函数工作过,但除此之外就没有了。
谢谢
旁注:我已经广泛寻找适合初学者的 v8 文档,nodesource 的文档没有解释参数的含义,或者很少给出如何使用函数/类的完整示例,如果有人知道一些更好的文档,那就太好了,谢谢。
感谢社区的亲切和及时的帮助,我能够快速轻松地解决这个问题。事实证明,在编写 NodeJS 插件时,使用 NodeJS 自己的 N-API 是个好主意,因为文档更简单、更清晰,最重要的是,存在。
相关文章:
- 示例外壳应用程序显示的 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::FunctionTemplate
- 在V8 JavaScript引擎中,如何添加functionTemplate作为使用C API的另一个functi
- 如何用c++ lambda函数创建v8::FunctionTemplate
- v8扩展for不能将FunctionTemplate设置为target