property_descriptor.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "napi.h"
  2. static napi_value Getter_Core(napi_env env, napi_callback_info info) {
  3. (void) info;
  4. napi_value result;
  5. napi_status status = napi_create_uint32(env, 42, &result);
  6. NAPI_THROW_IF_FAILED(env, status, nullptr);
  7. return result;
  8. }
  9. static napi_value Setter_Core(napi_env env, napi_callback_info info) {
  10. size_t argc = 1;
  11. napi_value argv;
  12. napi_status status =
  13. napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
  14. NAPI_THROW_IF_FAILED(env, status, nullptr);
  15. (void) argv;
  16. return nullptr;
  17. }
  18. static Napi::Value Getter(const Napi::CallbackInfo& info) {
  19. return Napi::Number::New(info.Env(), 42);
  20. }
  21. static void Setter(const Napi::CallbackInfo& info) {
  22. (void) info[0];
  23. }
  24. static Napi::Object Init(Napi::Env env, Napi::Object exports) {
  25. napi_status status;
  26. napi_property_descriptor core_prop = {
  27. "core",
  28. nullptr,
  29. nullptr,
  30. Getter_Core,
  31. Setter_Core,
  32. nullptr,
  33. napi_enumerable,
  34. nullptr
  35. };
  36. status = napi_define_properties(env, exports, 1, &core_prop);
  37. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  38. exports.DefineProperty(
  39. Napi::PropertyDescriptor::Accessor(env,
  40. exports,
  41. "cplusplus",
  42. Getter,
  43. Setter,
  44. napi_enumerable));
  45. exports.DefineProperty(
  46. Napi::PropertyDescriptor::Accessor<Getter, Setter>("templated",
  47. napi_enumerable));
  48. return exports;
  49. }
  50. NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)