File tree Expand file tree Collapse file tree 3 files changed +23
-8
lines changed Expand file tree Collapse file tree 3 files changed +23
-8
lines changed Original file line number Diff line number Diff line change @@ -6242,6 +6242,11 @@ Env::CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
6242
6242
return CleanupHook<Hook>(*this , hook);
6243
6243
}
6244
6244
6245
+ template <typename Hook, typename Arg>
6246
+ Env::CleanupHook<Hook, Arg>::CleanupHook() {
6247
+ data = nullptr ;
6248
+ }
6249
+
6245
6250
template <typename Hook, typename Arg>
6246
6251
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
6247
6252
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
Original file line number Diff line number Diff line change @@ -283,10 +283,7 @@ using MaybeOrValue = T;
283
283
// / corresponds to an Isolate.
284
284
class Env {
285
285
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;
290
287
#if NAPI_VERSION > 5
291
288
template <typename T>
292
289
static void DefaultFini (Env, T* data);
@@ -310,6 +307,9 @@ class Env {
310
307
MaybeOrValue<Value> RunScript (String script) const ;
311
308
312
309
#if NAPI_VERSION > 2
310
+ template <typename Hook, typename Arg = void >
311
+ class CleanupHook ;
312
+
313
313
template <typename Hook>
314
314
CleanupHook<Hook> AddCleanupHook (Hook hook);
315
315
@@ -335,13 +335,11 @@ class Env {
335
335
void SetInstanceData (DataType* data, HintType* hint) const ;
336
336
#endif // NAPI_VERSION > 5
337
337
338
- private:
339
- napi_env _env;
340
-
341
338
#if NAPI_VERSION > 2
342
339
template <typename Hook, typename Arg>
343
340
class CleanupHook {
344
341
public:
342
+ CleanupHook ();
345
343
CleanupHook (Env env, Hook hook, Arg* arg);
346
344
CleanupHook (Env env, Hook hook);
347
345
bool Remove (Env env);
@@ -357,8 +355,8 @@ class Env {
357
355
Arg* arg;
358
356
} * data;
359
357
};
360
- };
361
358
#endif // NAPI_VERSION > 2
359
+ };
362
360
363
361
// / A JavaScript value of unknown type.
364
362
// /
Original file line number Diff line number Diff line change @@ -20,6 +20,13 @@ static void cleanupVoid() {
20
20
static int secret1 = 42 ;
21
21
static int secret2 = 43 ;
22
22
23
+ class TestClass {
24
+ public:
25
+ Env::CleanupHook<void (*)(void * arg), int > hook;
26
+
27
+ void removeHook (Env env) { hook.Remove (env); }
28
+ };
29
+
23
30
Value AddHooks (const CallbackInfo& info) {
24
31
auto env = info.Env ();
25
32
@@ -72,6 +79,11 @@ Value AddHooks(const CallbackInfo& info) {
72
79
added += !hook5.IsEmpty ();
73
80
added += !hook6.IsEmpty ();
74
81
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
+
75
87
return Number::New (env, added);
76
88
}
77
89
You can’t perform that action at this time.
0 commit comments