简体   繁体   中英

N-API pass argument within require statement

I am creating an n-api module, is it possible to pass an argument when the require is called?

js wrapper

const NativeAPI = new (require('./../build/Release/mymodule.node')).Hello(3);
// how do i access this argument 3 inside TestClass::Init()

cpp wrapper

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
    return TestClass::Init(env, exports);
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, InitAll);

cpp class

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;
}

I understand this is not possible, instead create a constructor and pass any arguments to the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM