|
| 1 | +#if (NAPI_VERSION > 5) |
| 2 | +#include <stdio.h> |
| 3 | +#include "napi.h" |
| 4 | + |
| 5 | +// An overly elaborate way to get/set a boolean stored in the instance data: |
| 6 | +// 0. A boolean named "verbose" is stored in the instance data. The constructor |
| 7 | +// for JS `VerboseIndicator` instances is also stored in the instance data. |
| 8 | +// 1. Add a property named "verbose" onto exports served by a getter/setter. |
| 9 | +// 2. The getter returns a object of type VerboseIndicator, which itself has a |
| 10 | +// property named "verbose", also served by a getter/setter: |
| 11 | +// * The getter returns a boolean, indicating whether "verbose" is set. |
| 12 | +// * The setter sets "verbose" on the instance data. |
| 13 | +// 3. The setter sets "verbose" on the instance data. |
| 14 | + |
| 15 | +class Addon { |
| 16 | + public: |
| 17 | + class VerboseIndicator : public Napi::ObjectWrap<VerboseIndicator> { |
| 18 | + public: |
| 19 | + VerboseIndicator(const Napi::CallbackInfo& info): |
| 20 | + Napi::ObjectWrap<VerboseIndicator>(info) { |
| 21 | + info.This().As<Napi::Object>()["verbose"] = |
| 22 | + Napi::Boolean::New(info.Env(), |
| 23 | + info.Env().GetInstanceData<Addon>()->verbose); |
| 24 | + } |
| 25 | + |
| 26 | + Napi::Value Getter(const Napi::CallbackInfo& info) { |
| 27 | + return Napi::Boolean::New(info.Env(), |
| 28 | + info.Env().GetInstanceData<Addon>()->verbose); |
| 29 | + } |
| 30 | + |
| 31 | + void Setter(const Napi::CallbackInfo& info, const Napi::Value& val) { |
| 32 | + info.Env().GetInstanceData<Addon>()->verbose = val.As<Napi::Boolean>(); |
| 33 | + } |
| 34 | + |
| 35 | + static Napi::FunctionReference Init(Napi::Env env) { |
| 36 | + return Napi::Persistent(DefineClass(env, "VerboseIndicator", { |
| 37 | + InstanceAccessor< |
| 38 | + &VerboseIndicator::Getter, |
| 39 | + &VerboseIndicator::Setter>("verbose") |
| 40 | + })); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + static Napi::Value Getter(const Napi::CallbackInfo& info) { |
| 45 | + return info.Env().GetInstanceData<Addon>()->VerboseIndicator.New({}); |
| 46 | + } |
| 47 | + |
| 48 | + static void Setter(const Napi::CallbackInfo& info) { |
| 49 | + info.Env().GetInstanceData<Addon>()->verbose = info[0].As<Napi::Boolean>(); |
| 50 | + } |
| 51 | + |
| 52 | + Addon(Napi::Env env): VerboseIndicator(VerboseIndicator::Init(env)) {} |
| 53 | + ~Addon() { |
| 54 | + if (verbose) { |
| 55 | + fprintf(stderr, "addon_data: Addon::~Addon\n"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + static void DeleteAddon(Napi::Env, Addon* addon, uint32_t* hint) { |
| 60 | + delete addon; |
| 61 | + fprintf(stderr, "hint: %d\n", *hint); |
| 62 | + delete hint; |
| 63 | + } |
| 64 | + |
| 65 | + static Napi::Object Init(Napi::Env env, Napi::Value jshint) { |
| 66 | + if (!jshint.IsNumber()) { |
| 67 | + NAPI_THROW(Napi::Error::New(env, "Expected number"), Napi::Object()); |
| 68 | + } |
| 69 | + uint32_t hint = jshint.As<Napi::Number>(); |
| 70 | + if (hint == 0) |
| 71 | + env.SetInstanceData(new Addon(env)); |
| 72 | + else |
| 73 | + env.SetInstanceData<Addon, uint32_t, DeleteAddon>(new Addon(env), |
| 74 | + new uint32_t(hint)); |
| 75 | + Napi::Object result = Napi::Object::New(env); |
| 76 | + result.DefineProperties({ |
| 77 | + Napi::PropertyDescriptor::Accessor<Getter, Setter>("verbose"), |
| 78 | + }); |
| 79 | + |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + private: |
| 84 | + bool verbose = false; |
| 85 | + Napi::FunctionReference VerboseIndicator; |
| 86 | +}; |
| 87 | + |
| 88 | +// We use an addon factory so we can cover both the case where there is an |
| 89 | +// instance data hint and the case where there isn't. |
| 90 | +static Napi::Value AddonFactory(const Napi::CallbackInfo& info) { |
| 91 | + return Addon::Init(info.Env(), info[0]); |
| 92 | +} |
| 93 | + |
| 94 | +Napi::Object InitAddonData(Napi::Env env) { |
| 95 | + return Napi::Function::New(env, AddonFactory); |
| 96 | +} |
| 97 | +#endif // (NAPI_VERSION > 5) |
0 commit comments