Skip to content

Commit 39267ba

Browse files
julianmesa-gitkrakenmhdawson
authored andcommitted
src: make CleanupHook public
PR-URL: #1240 Reviewed-By: Michael Dawson <[email protected] Reviewed-By: Chengzhong Wu <[email protected]> Also fixed a bad #endif position
1 parent edf630c commit 39267ba

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

napi-inl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6242,6 +6242,11 @@ Env::CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
62426242
return CleanupHook<Hook>(*this, hook);
62436243
}
62446244

6245+
template <typename Hook, typename Arg>
6246+
Env::CleanupHook<Hook, Arg>::CleanupHook() {
6247+
data = nullptr;
6248+
}
6249+
62456250
template <typename Hook, typename Arg>
62466251
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
62476252
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {

napi.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,7 @@ using MaybeOrValue = T;
283283
/// corresponds to an Isolate.
284284
class Env {
285285
private:
286-
#if NAPI_VERSION > 2
287-
template <typename Hook, typename Arg = void>
288-
class CleanupHook;
289-
#endif // NAPI_VERSION > 2
286+
napi_env _env;
290287
#if NAPI_VERSION > 5
291288
template <typename T>
292289
static void DefaultFini(Env, T* data);
@@ -310,6 +307,9 @@ class Env {
310307
MaybeOrValue<Value> RunScript(String script) const;
311308

312309
#if NAPI_VERSION > 2
310+
template <typename Hook, typename Arg = void>
311+
class CleanupHook;
312+
313313
template <typename Hook>
314314
CleanupHook<Hook> AddCleanupHook(Hook hook);
315315

@@ -335,13 +335,11 @@ class Env {
335335
void SetInstanceData(DataType* data, HintType* hint) const;
336336
#endif // NAPI_VERSION > 5
337337

338-
private:
339-
napi_env _env;
340-
341338
#if NAPI_VERSION > 2
342339
template <typename Hook, typename Arg>
343340
class CleanupHook {
344341
public:
342+
CleanupHook();
345343
CleanupHook(Env env, Hook hook, Arg* arg);
346344
CleanupHook(Env env, Hook hook);
347345
bool Remove(Env env);
@@ -357,8 +355,8 @@ class Env {
357355
Arg* arg;
358356
} * data;
359357
};
360-
};
361358
#endif // NAPI_VERSION > 2
359+
};
362360

363361
/// A JavaScript value of unknown type.
364362
///

test/env_cleanup.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ static void cleanupVoid() {
2020
static int secret1 = 42;
2121
static int secret2 = 43;
2222

23+
class TestClass {
24+
public:
25+
Env::CleanupHook<void (*)(void* arg), int> hook;
26+
27+
void removeHook(Env env) { hook.Remove(env); }
28+
};
29+
2330
Value AddHooks(const CallbackInfo& info) {
2431
auto env = info.Env();
2532

@@ -72,6 +79,11 @@ Value AddHooks(const CallbackInfo& info) {
7279
added += !hook5.IsEmpty();
7380
added += !hook6.IsEmpty();
7481

82+
// Test store a hook in a member class variable
83+
auto myclass = TestClass();
84+
myclass.hook = env.AddCleanupHook(cleanup, &secret1);
85+
myclass.removeHook(env);
86+
7587
return Number::New(env, added);
7688
}
7789

0 commit comments

Comments
 (0)