node-addon-api icon indicating copy to clipboard operation
node-addon-api copied to clipboard

InstanceMethod does not accept function returning Napi::Object

Open th3r0b0t opened this issue 1 year ago • 0 comments

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:

  1. First of all, when I set the return type for buffer() function as Napi::Object the InstanceMethod complains about it, and when I change it to Napi::Value even tho it seems I can use buf1.buffer().write('Balh Blah', 'ascii') to write into the buffer, calling buf1.buffer().toString('ascii') prints the whole stack trace of node! I should note that when I tested the code without OOP, exporting buffer() with Napi::Object as return type was fine and everything was working smoothly:
exports.Set(Napi::String::New(env, "buffer"),
                Napi::Function::New(env, buffer));
  1. In this code, is it fine to supply void as the type? If not, what else? char or u_int8_t perhaps?:
this->memoryBuffer =  Napi::Buffer<void>::New(
                        env,
                        externalData,
                        this->shm_hint.mmapLength,
                        freeCallback,
                        &this->shm_hint);
  1. In the example for object_wrap there's this static 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 for static 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...

th3r0b0t avatar Jun 22 '24 17:06 th3r0b0t