Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the properties parameter of the DefineClass(...) function, the last item is not registered. #1623

Open
crskycode opened this issue Dec 6, 2024 · 2 comments

Comments

@crskycode
Copy link

I created an object using the following code

void Device::Initialize(Napi::Env env, Napi::Object exports)
{
  std::vector<Napi::ClassPropertyDescriptor<Device>> properties =
  {
    InstanceMethod("connect", &Device::Connect),
    InstanceMethod("disconnect", &Device::Disconnect),
    InstanceMethod("beep", &Device::Beep),
    InstanceMethod("beep1", &Device::Beep1), // undefined
  };

  Napi::Function constructorFunction = DefineClass(env, "MyDevice", properties);

  Napi::FunctionReference *constructor = new Napi::FunctionReference();
  *constructor = Napi::Persistent(constructorFunction);
  env.SetInstanceData(constructor);

  exports.Set("MyDevice", constructorFunction);
}

I can't see beep1 in JavaScript environment. Is this correct?

@legendecas
Copy link
Member

legendecas commented Dec 6, 2024

Would you mind sharing a minimum reproducible setup, and sharing your environment information like Node.js version and platforms? Thank you!

@crskycode
Copy link
Author

crskycode commented Dec 9, 2024

Would you mind sharing a minimum reproducible setup, and sharing your environment information like Node.js version and platforms? Thank you!

Node v20.12.0 on Windows

All code here

#include <napi.h>

class Device : public Napi::ObjectWrap<Device>
{
public:
  static void Initialize(Napi::Env env, Napi::Object exports);

  Device(const Napi::CallbackInfo& info);
  ~Device();

private:
  Napi::Value Connect(const Napi::CallbackInfo& info);
  Napi::Value Disconnect(const Napi::CallbackInfo& info);
  Napi::Value Beep(const Napi::CallbackInfo& info);
  Napi::Value Beep1(const Napi::CallbackInfo& info);
};

void Device::Initialize(Napi::Env env, Napi::Object exports)
{
  std::vector<Napi::ClassPropertyDescriptor<Device>> properties =
  {
    InstanceMethod("connect", &Device::Connect),
    InstanceMethod("disconnect", &Device::Disconnect),
    InstanceMethod("beep", &Device::Beep),
    InstanceMethod("beep1", &Device::Beep1), // undefined
  };

  Napi::Function constructorFunction = DefineClass(env, "MyDevice", properties);

  Napi::FunctionReference *constructor = new Napi::FunctionReference();
  *constructor = Napi::Persistent(constructorFunction);
  env.SetInstanceData(constructor);

  exports.Set("MyDevice", constructorFunction);
}

Device::Device(const Napi::CallbackInfo &info)
    : Napi::ObjectWrap<Device>(info)
{
}

Device::~Device()
{
}

Napi::Value Device::Connect(const Napi::CallbackInfo &info)
{
  return info.Env().Undefined();
}

Napi::Value Device::Disconnect(const Napi::CallbackInfo &info)
{
  return info.Env().Undefined();
}

Napi::Value Device::Beep(const Napi::CallbackInfo &info)
{
  return info.Env().Undefined();
}

Napi::Value Device::Beep1(const Napi::CallbackInfo &info)
{
  return info.Env().Undefined();
}

Napi::Object Initialize(Napi::Env env, Napi::Object exports)
{
  // Add Device module exports
  Device::Initialize(env, exports);
  return exports;
}

NODE_API_MODULE(mydevice, Initialize)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Need Triage
Development

No branches or pull requests

2 participants