Error binding C++ class member function as a method of an object with node-addon-api
Daniel-Boll opened this issue · comments
I'm trying to bind a C++ class member function as a method of a JavaScript object using node-addon-api in a C++ addon for Node.js, but I'm encountering an error.
I have the following code:
class ObjectMakerClass {
private:
int value = 0;
public:
ObjectMakerClass() { value = 1; }
ObjectMakerClass(int value) { this->value = value; }
Napi::Value getValue(const Napi::CallbackInfo &info) {
return Napi::Value::From(info.Env(), value);
}
static Napi::Value getObject(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
// Create a new object with a property named "value" and a value of 1
Napi::Object obj = Napi::Object::New(env);
obj.Set("value", 1);
// Add another property to be a function named "getValue" and bind it to the member function
obj.Set("getValue", Napi::Function::New(env, &ObjectMakerClass::getValue));
// Return the object
return obj;
}
};
Napi::Object object_maker(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
// Get the value from the parameters
if (info.Length() < 1) {
auto objectMaker = new ObjectMakerClass();
return objectMaker->getObject(info);
}
int value = info[0].As<Napi::Number>().Int32Value();
auto objectMaker = new ObjectMakerClass(value);
// Return the object
return objectMaker->getObject(info);
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "objectMaker"), Napi::Function::New(env, object_maker));
return exports;
}
NODE_API_MODULE(addon, Init)
The issue I'm facing is that when I try to bind the getValue
member function as a method of the JavaScript object using the &ObjectMakerClass::getValue
syntax, I'm getting the following error:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘cb (...)’, e.g. ‘(... ->* cb) (...)’
I've tried different approaches but couldn't resolve the error. Can anyone provide guidance on how to bind a C++ class member function as a method of a JavaScript object using node-addon-api? Any help would be greatly appreciated!
Best Regards,
Daniel Boll. 🎴