使用节点插件api将C库回调传递给NodeJS EventEmitter
Passing on a C library callback to a NodeJS EventEmitter using node-addon-api
我目前正在编写一个节点模块,该模块使用node-Addon-API绑定Alex Diner的跨平台游戏板代码。
在大多数情况下,这是一项相当容易的任务。唯一有问题的是Gamepad库的回调函数。
我的想法是通过.on(...)
使模块成为EventEmitter来公开这些回调。该节点模块中的Nan模块已经以类似的方式进行了操作。
问题是,每当从我的本机模块触发事件时,我都会得到以下错误:
internal/timers.js:531
timer._onTimeout();
^
TypeError: Cannot read property '_events' of undefined
at emit (events.js:163:23)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7)
我的binding.cc
:的重要部分
#include <napi.h>
#include "gamepad/Gamepad.h" // The Gamepad library written by Alex Diner
// The JS .emit(event, ...args) function from the EventEmitter
Napi::FunctionReference emitFn;
void GamepadSetEmitFn(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1) {
Napi::TypeError::New(env, "Parameter 'emitFn' needs to be defined.")
.ThrowAsJavaScriptException();
return;
}
if (!info[0].IsFunction()) {
Napi::TypeError::New(env, "Parameter 'emitFn' needs to be a function.")
.ThrowAsJavaScriptException();
return;
}
// Sets the emitFn to the specified function
emitFn = Napi::Persistent(info[0].As<Napi::Function>());
}
// The following handle functions are the callbacks for the actual Gamepad module
void HandleDeviceAttach(struct Gamepad_device* device, void* context) {
if (emitFn == nullptr) return;
Napi::Env env = emitFn.Env();
Napi::String eventName = Napi::String::New(env, "attach");
Napi::Number nDeviceID = Napi::Number::New(env, device->deviceID);
emitFn.Call({ eventName, nDeviceID });
}
void HandleDeviceRemove(struct Gamepad_device* device, void* context) {
if (emitFn == nullptr) return;
Napi::Env env = emitFn.Env();
Napi::String eventName = Napi::String::New(env, "remove");
Napi::Number nDeviceID = Napi::Number::New(env, device->deviceID);
emitFn.Call({ eventName, nDeviceID });
}
void HandleButtonDown(struct Gamepad_device* device, unsigned int buttonID, double timestamp, void* context) {
if (emitFn == nullptr) return;
Napi::Env env = emitFn.Env();
Napi::String eventName = Napi::String::New(env, "down");
Napi::Number nDeviceID = Napi::Number::New(env, device->deviceID);
Napi::Number nButtonID = Napi::Number::New(env, buttonID);
Napi::Number nTimestamp = Napi::Number::New(env, timestamp);
emitFn.Call({ eventName, nDeviceID, nButtonID, nTimestamp });
}
void HandleButtonUp(struct Gamepad_device* device, unsigned int buttonID, double timestamp, void* context) {
if (emitFn == nullptr) return;
Napi::Env env = emitFn.Env();
Napi::String eventName = Napi::String::New(env, "up");
Napi::Number nDeviceID = Napi::Number::New(env, device->deviceID);
Napi::Number nButtonID = Napi::Number::New(env, buttonID);
Napi::Number nTimestamp = Napi::Number::New(env, timestamp);
emitFn.Call({ eventName, nDeviceID, nButtonID, nTimestamp });
}
void HandleAxisMovement(struct Gamepad_device* device, unsigned int axisID, float value, float lastValue, double timestamp, void* context) {
if (emitFn == nullptr) return;
Napi::Env env = emitFn.Env();
Napi::String eventName = Napi::String::New(env, "move");
Napi::Number nDeviceID = Napi::Number::New(env, device->deviceID);
Napi::Number nAxisID = Napi::Number::New(env, axisID);
Napi::Number nValue = Napi::Number::New(env, value);
Napi::Number nLastValue = Napi::Number::New(env, lastValue);
Napi::Number nTimestamp = Napi::Number::New(env, timestamp);
emitFn.Call({ eventName, nDeviceID, nAxisID, nValue, nLastValue, nTimestamp });
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
// Applies the specified callback functions above to the respective Gamepad function.
Gamepad_deviceAttachFunc(HandleDeviceAttach, NULL);
Gamepad_deviceRemoveFunc(HandleDeviceRemove, NULL);
Gamepad_buttonDownFunc(HandleButtonDown, NULL);
Gamepad_buttonUpFunc(HandleButtonUp, NULL);
Gamepad_axisMoveFunc(HandleAxisMovement, NULL);
// All functionality exposed to JS
// (including the function implementations omitted from this stackoverflow excerpt)
exports.Set(Napi::String::New(env, "init"), Napi::Function::New(env, GamepadInit));
exports.Set(Napi::String::New(env, "shutdown"), Napi::Function::New(env, GamepadShutdown));
exports.Set(Napi::String::New(env, "detectDevices"), Napi::Function::New(env, GamepadDetectDevices));
exports.Set(Napi::String::New(env, "processEvents"), Napi::Function::New(env, GamepadProcessEvents));
exports.Set(Napi::String::New(env, "numDevices"), Napi::Function::New(env, GamepadNumDevices));
exports.Set(Napi::String::New(env, "deviceAtIndex"), Napi::Function::New(env, GamepadDeviceAtIndex));
exports.Set(Napi::String::New(env, "setEmitFn"), Napi::Function::New(env, GamepadSetEmitFn));
return exports;
}
以及连接C++模块和EventEmitter:的index.js
const gamepad = require('../build/Release/gamepad.node')
const { EventEmitter } = require('events')
// Makes the native module extend 'EventEmitter'
gamepad.__proto__ = EventEmitter.prototype
// Exposes the emit function to the native module
gamepad.setEmitFn(gamepad.emit)
module.exports = gamepad
如果需要更多信息,请毫不犹豫地询问!
提前感谢!:(
好的,所以我能够自己解决这个问题。我不确定为什么这是一个问题,但错误的发生是因为我直接从C++从EventEmitter类调用.emitt(event,…args(方法。
我只是从JavaScript向我的模块添加了一个包装器函数,现在它就像一个魅力:
const gamepad = require('../build/Release/gamepad.node')
const { EventEmitter } = require('events')
// Makes the native module extend 'EventEmitter'
gamepad.__proto__ = EventEmitter.prototype
// Wraps the emit function for the call from the native module
gamepad.applyEmit = (...args) => {
const event = args.shift()
gamepad.emit(event, ...args)
}
// Exposes the applyEmit function to the native module
gamepad.setEmitFn(gamepad.applyEmit)
module.exports = gamepad
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 用于在回调中调用解析器的设计模式
- 如何使用C++对象的成员函数作为 C 样式回调?
- Java从C++回调到C++回调
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- 从不同的 cpp 调用回调函数会导致bad_function_call
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 不带轮询的 SDL2 事件回调
- C++存储带有可变参数的回调
- 如何使用 Node-addon-API 实现 node-nan 回调
- 处理影响跨不同线程共享对象的定时回调的最佳方法是什么?
- 访问类C++ C 样式回调
- 处理类内的回调时,必须调用对非静态成员函数的引用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 在C++中实现回调
- 使用节点插件api将C库回调传递给NodeJS EventEmitter