From a176445a1783b4c6bfcd7dc64cc6b22fc489d385 Mon Sep 17 00:00:00 2001 From: Julian Mesa Date: Wed, 23 Nov 2022 18:41:43 +0100 Subject: [PATCH 1/3] Make CleanupHook public Also fixed a bad #endif position --- napi-inl.h | 5 +++++ napi.h | 14 ++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 3b79809c8..0dbfb49f8 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -6233,6 +6233,11 @@ Env::CleanupHook Env::AddCleanupHook(Hook hook) { return CleanupHook(*this, hook); } +template +Env::CleanupHook::CleanupHook() { + data = nullptr; +} + template Env::CleanupHook::CleanupHook(Napi::Env env, Hook hook) : wrapper(Env::CleanupHook::Wrapper) { diff --git a/napi.h b/napi.h index 58a0c523b..0dfc856c7 100644 --- a/napi.h +++ b/napi.h @@ -283,10 +283,7 @@ using MaybeOrValue = T; /// corresponds to an Isolate. class Env { private: -#if NAPI_VERSION > 2 - template - class CleanupHook; -#endif // NAPI_VERSION > 2 + napi_env _env; #if NAPI_VERSION > 5 template static void DefaultFini(Env, T* data); @@ -310,6 +307,9 @@ class Env { MaybeOrValue RunScript(String script) const; #if NAPI_VERSION > 2 + template + class CleanupHook; + template CleanupHook AddCleanupHook(Hook hook); @@ -335,13 +335,11 @@ class Env { void SetInstanceData(DataType* data, HintType* hint) const; #endif // NAPI_VERSION > 5 - private: - napi_env _env; - #if NAPI_VERSION > 2 template class CleanupHook { public: + CleanupHook(); CleanupHook(Env env, Hook hook, Arg* arg); CleanupHook(Env env, Hook hook); bool Remove(Env env); @@ -357,8 +355,8 @@ class Env { Arg* arg; } * data; }; -}; #endif // NAPI_VERSION > 2 +}; /// A JavaScript value of unknown type. /// From 52a79f257a2b1050967823065dbf38c3d2f2fbf9 Mon Sep 17 00:00:00 2001 From: Julian Mesa Date: Wed, 30 Nov 2022 18:35:51 +0100 Subject: [PATCH 2/3] Added CleanupHook test storing it as a class member --- test/env_cleanup.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/env_cleanup.cc b/test/env_cleanup.cc index 44be0d5f7..b10460049 100644 --- a/test/env_cleanup.cc +++ b/test/env_cleanup.cc @@ -20,6 +20,15 @@ static void cleanupVoid() { static int secret1 = 42; static int secret2 = 43; +class TestClass { + public: + Env::CleanupHook hook; + + void removeHook(Env env) { + hook.Remove(env); + } +}; + Value AddHooks(const CallbackInfo& info) { auto env = info.Env(); @@ -72,6 +81,11 @@ Value AddHooks(const CallbackInfo& info) { added += !hook5.IsEmpty(); added += !hook6.IsEmpty(); + // Test store a hook in a member class variable + auto myclass = TestClass(); + myclass.hook = env.AddCleanupHook(cleanup, &secret1); + myclass.removeHook(env); + return Number::New(env, added); } From 5a030e3a35d23db434dab2aefe47b375a6fdb7e2 Mon Sep 17 00:00:00 2001 From: Julian Mesa Date: Thu, 1 Dec 2022 17:33:50 +0100 Subject: [PATCH 3/3] Fix lint --- test/env_cleanup.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/env_cleanup.cc b/test/env_cleanup.cc index b10460049..a0ef62b2c 100644 --- a/test/env_cleanup.cc +++ b/test/env_cleanup.cc @@ -21,12 +21,10 @@ static int secret1 = 42; static int secret2 = 43; class TestClass { - public: - Env::CleanupHook hook; + public: + Env::CleanupHook hook; - void removeHook(Env env) { - hook.Remove(env); - } + void removeHook(Env env) { hook.Remove(env); } }; Value AddHooks(const CallbackInfo& info) {