Skip to content

Commit

Permalink
add hinted option
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Apr 13, 2020
1 parent 131e252 commit f8d6482
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ inline T* Env::GetInstanceData() {
template <typename T> void Env::DefaultFini(Env, T* data) {
delete data;
}

template <typename DataType, typename HintType>
void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
delete data;
}
#endif // NAPI_VERSION > 5

////////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ namespace Napi {
template <typename T> using Finalizer = void (*)(Env, T*);
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
void SetInstanceData(T* data);

template <typename DataType, typename HintType>
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
template <typename DataType,
typename HintType,
FinalizerWithHint<DataType, HintType> fini =
Env::DefaultFiniWithHint<DataType, HintType>>
void SetInstanceData(DataType* data, HintType* hint);
#endif // NAPI_VERSION > 5

private:
Expand Down
22 changes: 18 additions & 4 deletions test/addon_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,22 @@ class Addon {
info.Env().GetInstanceData<Addon>()->verbose = info[0].As<Napi::Boolean>();
}

Addon(Napi::Env env): VerboseIndicator(VerboseIndicator::Init(env)) {}
Addon(Napi::Env env, uint32_t hint = 0) :
hint(hint), VerboseIndicator(VerboseIndicator::Init(env)) {}
~Addon() {
if (verbose) {
fprintf(stderr, "addon_data: Addon::~Addon\n");
if (hint > 0) {
fprintf(stderr, "hint: %u\n", hint);
}
}
}

static Napi::Object Init(Napi::Env env) {
env.SetInstanceData(new Addon(env));
static Napi::Object Init(Napi::Env env, Napi::Value hint) {
if (!hint.IsNumber()) {
NAPI_THROW(Napi::Error::New(env, "Expected number"), Napi::Object());
}
env.SetInstanceData(new Addon(env, hint.As<Napi::Number>()));
Napi::Object result = Napi::Object::New(env);
result.DefineProperties({
Napi::PropertyDescriptor::Accessor<Getter, Setter>("verbose"),
Expand All @@ -68,10 +75,17 @@ class Addon {

private:
bool verbose = false;
uint32_t hint = 0;
Napi::FunctionReference VerboseIndicator;
};

// We use an addon factory so we can cover both the case where there is an
// instance data hint and the case where there isn't.
static Napi::Value AddonFactory(const Napi::CallbackInfo& info) {
return Addon::Init(info.Env(), info[0]);
}

Napi::Object InitAddonData(Napi::Env env) {
return Addon::Init(env);
return Napi::Function::New(env, AddonFactory);
}
#endif // (NAPI_VERSION > 5)

0 comments on commit f8d6482

Please sign in to comment.