MCPcopy Create free account
hub / github.com/nodejs/node-addon-api / AddHooks

Function AddHooks

test/env_cleanup.cc:30–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28};
29
30Value AddHooks(const CallbackInfo& info) {
31 auto env = info.Env();
32
33 bool shouldRemove = info[0].As<Boolean>().Value();
34
35 // hook: void (*)(void *arg), hint: int
36 auto hook1 = env.AddCleanupHook(cleanup, &secret1);
37 // test using same hook+arg pair
38 auto hook1b = env.AddCleanupHook(cleanup, &secret1);
39
40 // hook: void (*)(int *arg), hint: int
41 auto hook2 = env.AddCleanupHook(cleanupInt, &secret2);
42
43 // hook: void (*)(int *arg), hint: void (default)
44 auto hook3 = env.AddCleanupHook(cleanupVoid);
45 // test using the same hook
46 auto hook3b = env.AddCleanupHook(cleanupVoid);
47
48 // hook: lambda []void (int *arg)->void, hint: int
49 auto hook4 = env.AddCleanupHook(
50 [&](int* arg) { printf("lambda cleanup(%d)\n", *arg); }, &secret1);
51
52 // hook: lambda []void (void *)->void, hint: void
53 auto hook5 =
54 env.AddCleanupHook([&](void*) { printf("lambda cleanup(void)\n"); },
55 static_cast<void*>(nullptr));
56
57 // hook: lambda []void ()->void, hint: void (default)
58 auto hook6 = env.AddCleanupHook([&]() { printf("lambda cleanup()\n"); });
59
60 if (shouldRemove) {
61 hook1.Remove(env);
62 hook1b.Remove(env);
63 hook2.Remove(env);
64 hook3.Remove(env);
65 hook3b.Remove(env);
66 hook4.Remove(env);
67 hook5.Remove(env);
68 hook6.Remove(env);
69 }
70
71 int added = 0;
72
73 added += !hook1.IsEmpty();
74 added += !hook1b.IsEmpty();
75 added += !hook2.IsEmpty();
76 added += !hook3.IsEmpty();
77 added += !hook3b.IsEmpty();
78 added += !hook4.IsEmpty();
79 added += !hook5.IsEmpty();
80 added += !hook6.IsEmpty();
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
87 return Number::New(env, added);

Callers

nothing calls this directly

Calls 7

EnvMethod · 0.80
ValueMethod · 0.80
AddCleanupHookMethod · 0.80
RemoveMethod · 0.80
IsEmptyMethod · 0.80
removeHookMethod · 0.80
TestClassClass · 0.70

Tested by

no test coverage detected