Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CleanupHook public #1240

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6233,6 +6233,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