From 301c5bfd9e07c9494a1f791008334b43572dc370 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sun, 27 Oct 2019 17:56:15 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node-addon-api/issues/557 --- napi-inl.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index d23fc83f7..91837f7f4 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -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(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(data); - }; - } status = napi_create_external(env, data, finalizer, @@ -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; }