Skip to content

Commit

Permalink
src: attach data with napi_add_finalizer
Browse files Browse the repository at this point in the history
Use `napi_add_finalizer()` to attach data when building against N-API 5
instead of the symbol + external approach that leaves a trace on the
target object.

Fixes: #557
PR-URL: #577
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Gabriel Schulhof committed Nov 1, 2019
1 parent 9e955a8 commit bdfd141
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ static inline napi_status AttachData(napi_env env,
FreeType* data,
napi_finalize finalizer = nullptr,
void* hint = nullptr) {
napi_status status;
if (finalizer == nullptr) {
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
delete static_cast<FreeType*>(data);
};
}
#if (NAPI_VERSION < 5)
napi_value symbol, external;
napi_status status = napi_create_symbol(env, nullptr, &symbol);
status = napi_create_symbol(env, nullptr, &symbol);
if (status == napi_ok) {
if (finalizer == nullptr) {
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
delete static_cast<FreeType*>(data);
};
}
status = napi_create_external(env,
data,
finalizer,
Expand All @@ -54,6 +56,9 @@ static inline napi_status AttachData(napi_env env,
status = napi_define_properties(env, obj, 1, &desc);
}
}
#else // NAPI_VERSION >= 5
status = napi_add_finalizer(env, obj, data, finalizer, hint, nullptr);
#endif
return status;
}

Expand Down

0 comments on commit bdfd141

Please sign in to comment.