| 14 | // 3. The setter sets "verbose" on the instance data. |
| 15 | |
| 16 | class Addon { |
| 17 | public: |
| 18 | class VerboseIndicator : public Napi::ObjectWrap<VerboseIndicator> { |
| 19 | public: |
| 20 | VerboseIndicator(const Napi::CallbackInfo& info) |
| 21 | : Napi::ObjectWrap<VerboseIndicator>(info) { |
| 22 | info.This().As<Napi::Object>()["verbose"] = Napi::Boolean::New( |
| 23 | info.Env(), 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( |
| 37 | env, |
| 38 | "VerboseIndicator", |
| 39 | {InstanceAccessor<&VerboseIndicator::Getter, |
| 40 | &VerboseIndicator::Setter>("verbose")})); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | static Napi::Value Getter(const Napi::CallbackInfo& info) { |
| 45 | return MaybeUnwrap( |
| 46 | info.Env().GetInstanceData<Addon>()->VerboseIndicator.New({})); |
| 47 | } |
| 48 | |
| 49 | static void Setter(const Napi::CallbackInfo& info) { |
| 50 | info.Env().GetInstanceData<Addon>()->verbose = info[0].As<Napi::Boolean>(); |
| 51 | } |
| 52 | |
| 53 | Addon(Napi::Env env) : VerboseIndicator(VerboseIndicator::Init(env)) {} |
| 54 | ~Addon() { |
| 55 | if (verbose) { |
| 56 | fprintf(stderr, "addon_data: Addon::~Addon\n"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void DeleteAddon(Napi::Env, Addon* addon, uint32_t* hint) { |
| 61 | delete addon; |
| 62 | fprintf(stderr, "hint: %u\n", *hint); |
| 63 | delete hint; |
| 64 | } |
| 65 | |
| 66 | static Napi::Object Init(Napi::Env env, Napi::Value jshint) { |
| 67 | if (!jshint.IsNumber()) { |
| 68 | NAPI_THROW(Napi::Error::New(env, "Expected number"), Napi::Object()); |
| 69 | } |
| 70 | uint32_t hint = jshint.As<Napi::Number>(); |
| 71 | if (hint == 0) |
| 72 | env.SetInstanceData(new Addon(env)); |
| 73 | else |
nothing calls this directly
no outgoing calls
no test coverage detected