Skip to content

Commit d484d03

Browse files
author
Gabriel Schulhof
committed
src: attach data with napi_add_finalizer
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
1 parent bcc1d58 commit d484d03

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

napi-inl.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ static inline napi_status AttachData(napi_env env,
2727
FreeType* data,
2828
napi_finalize finalizer = nullptr,
2929
void* hint = nullptr) {
30+
napi_status status;
31+
if (finalizer == nullptr) {
32+
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
33+
delete static_cast<FreeType*>(data);
34+
};
35+
}
36+
#if (NAPI_VERSION < 5)
3037
napi_value symbol, external;
31-
napi_status status = napi_create_symbol(env, nullptr, &symbol);
38+
status = napi_create_symbol(env, nullptr, &symbol);
3239
if (status == napi_ok) {
33-
if (finalizer == nullptr) {
34-
finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
35-
delete static_cast<FreeType*>(data);
36-
};
37-
}
3840
status = napi_create_external(env,
3941
data,
4042
finalizer,
@@ -54,6 +56,9 @@ static inline napi_status AttachData(napi_env env,
5456
status = napi_define_properties(env, obj, 1, &desc);
5557
}
5658
}
59+
#else // NAPI_VERSION >= 5
60+
status = napi_add_finalizer(env, obj, data, finalizer, hint, nullptr);
61+
#endif
5762
return status;
5863
}
5964

0 commit comments

Comments
 (0)