node-addon-api
node-addon-api copied to clipboard
InstanceMethod does not accept function returning Napi::Object
Hello, First off, I send my sincere regards for all the time and efforts you put into the project.
Preface
I have a simple C++ class as below:
class mmapIPC : public Napi::ObjectWrap<mmapIPC>
{
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
mmapIPC(const Napi::CallbackInfo& info);
static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
private:
//-----------members
shm_metadata shm_hint;
Napi::Buffer<void> memoryBuffer;
//-----------funcs
Napi::Object buffer(const Napi::CallbackInfo& info);
Napi::Value acquireReadLock(const Napi::CallbackInfo& info);
Napi::Value acquireWriteLock(const Napi::CallbackInfo& info);
Napi::Value removeLock(const Napi::CallbackInfo& info);
//-----------statics
static void freeCallback(Napi::Env, void* memoryAddress, shm_metadata* hint);
static int createShmFile( const char *name, std::size_t memLenght);
static void* mapMemory( int shm_fd, std::size_t regionLength, bool lockPagesToRam );
};
Which is based on examples of this repo for object_wrap; Now in my static Napi::Object Init I have the following code to export members:
// This method is used to hook the accessor and method callbacks
Napi::Function functionList = DefineClass(env, "mmapIPC",
{
InstanceMethod<&mmapIPC::buffer>("buffer", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::acquireReadLock>("acquireReadLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::acquireWriteLock>("acquireWriteLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
InstanceMethod<&mmapIPC::removeLock>("removeLock", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
StaticMethod<&mmapIPC::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable))
});
And definition for Napi::Object buffer is (it's some kind of setter/getter accessor at the same?) :
Napi::Object mmapIPC::buffer(const Napi::CallbackInfo& info)
{
return this->memoryBuffer;
}
Questions:
- First of all, when I set the return type for
buffer()function asNapi::ObjecttheInstanceMethodcomplains about it, and when I change it toNapi::Valueeven tho it seems I can usebuf1.buffer().write('Balh Blah', 'ascii')to write into the buffer, callingbuf1.buffer().toString('ascii')prints the whole stack trace of node! I should note that when I tested the code without OOP, exportingbuffer()withNapi::Objectas return type was fine and everything was working smoothly:
exports.Set(Napi::String::New(env, "buffer"),
Napi::Function::New(env, buffer));
- In this code, is it fine to supply
voidas the type? If not, what else?charoru_int8_tperhaps?:
this->memoryBuffer = Napi::Buffer<void>::New(
env,
externalData,
this->shm_hint.mmapLength,
freeCallback,
&this->shm_hint);
- In the example for
object_wrapthere's thisstatic Napi::Value CreateNewItem(const Napi::CallbackInfo& info)method which retrieves the stored constructor and I quote: "to create a new instance of the JS class the constructor represents". What does this method do? Couldn't I just create another instance using the required constructor in my JS code? It says in the comments forstatic Napi::Object Init(Napi::Env env, Napi::Object exports)that: "allow this add-on to support multiple instances of itself running on multiple worker threads, as well as multiple instances of itself running in different contexts on the same thread"! So is this method necessary?
P.S: I'm very truly sorry if this turned out to be a long question; I just wanted to ask it all once and get it over with...