我正在创建一个 n-api 模块,是否可以在调用 require 时传递参数?
js包装器
const NativeAPI = new (require('./../build/Release/mymodule.node')).Hello(3);
// how do i access this argument 3 inside TestClass::Init()
cpp 包装器
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
return TestClass::Init(env, exports);
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll);
cpp类
Napi::FunctionReference TestClass::constructor;
Napi::Object TestClass::Init(Napi::Env env, Napi::Object exports) {
// access 3 here
Napi::HandleScope scope(env);
Napi::Function func = DefineClass(env, "Hello", {
InstanceMethod("create", &TestClass::create),
InstanceMethod("delete", &TestClass::del)
});
constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
exports.Set("Hello", func);
return exports;
}