Skip to content

Commit

Permalink
src: make CleanupHook public
Browse files Browse the repository at this point in the history
PR-URL: #1240
Reviewed-By: Michael Dawson <midawson@redhat.com
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>

Also fixed a bad #endif position
  • Loading branch information
julianmesa-gitkraken authored and mhdawson committed Dec 2, 2022
1 parent edf630c commit 39267ba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6242,6 +6242,11 @@ Env::CleanupHook<Hook> Env::AddCleanupHook(Hook hook) {
return CleanupHook<Hook>(*this, hook);
}

template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook() {
data = nullptr;
}

template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
Expand Down
14 changes: 6 additions & 8 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,7 @@ using MaybeOrValue = T;
/// corresponds to an Isolate.
class Env {
private:
#if NAPI_VERSION > 2
template <typename Hook, typename Arg = void>
class CleanupHook;
#endif // NAPI_VERSION > 2
napi_env _env;
#if NAPI_VERSION > 5
template <typename T>
static void DefaultFini(Env, T* data);
Expand All @@ -310,6 +307,9 @@ class Env {
MaybeOrValue<Value> RunScript(String script) const;

#if NAPI_VERSION > 2
template <typename Hook, typename Arg = void>
class CleanupHook;

template <typename Hook>
CleanupHook<Hook> AddCleanupHook(Hook hook);

Expand All @@ -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 <typename Hook, typename Arg>
class CleanupHook {
public:
CleanupHook();
CleanupHook(Env env, Hook hook, Arg* arg);
CleanupHook(Env env, Hook hook);
bool Remove(Env env);
Expand All @@ -357,8 +355,8 @@ class Env {
Arg* arg;
} * data;
};
};
#endif // NAPI_VERSION > 2
};

/// A JavaScript value of unknown type.
///
Expand Down
12 changes: 12 additions & 0 deletions test/env_cleanup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ static void cleanupVoid() {
static int secret1 = 42;
static int secret2 = 43;

class TestClass {
public:
Env::CleanupHook<void (*)(void* arg), int> hook;

void removeHook(Env env) { hook.Remove(env); }
};

Value AddHooks(const CallbackInfo& info) {
auto env = info.Env();

Expand Down Expand Up @@ -72,6 +79,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);
}

Expand Down

0 comments on commit 39267ba

Please sign in to comment.