-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CleanupHook support to Env PR-URL: nodejs/node-addon-api#1014 Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
1 parent
af9274f
commit 53a133a
Showing
8 changed files
with
315 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <stdio.h> | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
#if (NAPI_VERSION > 2) | ||
namespace { | ||
|
||
static void cleanup(void* arg) { | ||
printf("static cleanup(%d)\n", *(int*)(arg)); | ||
} | ||
static void cleanupInt(int* arg) { | ||
printf("static cleanup(%d)\n", *(arg)); | ||
} | ||
|
||
static void cleanupVoid() { | ||
printf("static cleanup()\n"); | ||
} | ||
|
||
static int secret1 = 42; | ||
static int secret2 = 43; | ||
|
||
Value AddHooks(const CallbackInfo& info) { | ||
auto env = info.Env(); | ||
|
||
bool shouldRemove = info[0].As<Boolean>().Value(); | ||
|
||
// hook: void (*)(void *arg), hint: int | ||
auto hook1 = env.AddCleanupHook(cleanup, &secret1); | ||
// test using same hook+arg pair | ||
auto hook1b = env.AddCleanupHook(cleanup, &secret1); | ||
|
||
// hook: void (*)(int *arg), hint: int | ||
auto hook2 = env.AddCleanupHook(cleanupInt, &secret2); | ||
|
||
// hook: void (*)(int *arg), hint: void (default) | ||
auto hook3 = env.AddCleanupHook(cleanupVoid); | ||
// test using the same hook | ||
auto hook3b = env.AddCleanupHook(cleanupVoid); | ||
|
||
// hook: lambda []void (int *arg)->void, hint: int | ||
auto hook4 = env.AddCleanupHook( | ||
[&](int* arg) { printf("lambda cleanup(%d)\n", *arg); }, &secret1); | ||
|
||
// hook: lambda []void (void *)->void, hint: void | ||
auto hook5 = | ||
env.AddCleanupHook([&](void*) { printf("lambda cleanup(void)\n"); }, | ||
static_cast<void*>(nullptr)); | ||
|
||
// hook: lambda []void ()->void, hint: void (default) | ||
auto hook6 = env.AddCleanupHook([&]() { printf("lambda cleanup()\n"); }); | ||
|
||
if (shouldRemove) { | ||
hook1.Remove(env); | ||
hook1b.Remove(env); | ||
hook2.Remove(env); | ||
hook3.Remove(env); | ||
hook3b.Remove(env); | ||
hook4.Remove(env); | ||
hook5.Remove(env); | ||
hook6.Remove(env); | ||
} | ||
|
||
int added = 0; | ||
|
||
added += !hook1.IsEmpty(); | ||
added += !hook1b.IsEmpty(); | ||
added += !hook2.IsEmpty(); | ||
added += !hook3.IsEmpty(); | ||
added += !hook3b.IsEmpty(); | ||
added += !hook4.IsEmpty(); | ||
added += !hook5.IsEmpty(); | ||
added += !hook6.IsEmpty(); | ||
|
||
return Number::New(env, added); | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
Object InitEnvCleanup(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["addHooks"] = Function::New(env, AddHooks); | ||
|
||
return exports; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
if (process.argv[2] === 'runInChildProcess') { | ||
const binding_path = process.argv[3]; | ||
const remove_hooks = process.argv[4] === 'true'; | ||
|
||
const binding = require(binding_path); | ||
const actualAdded = binding.env_cleanup.addHooks(remove_hooks); | ||
const expectedAdded = remove_hooks === true ? 0 : 8; | ||
assert(actualAdded === expectedAdded, 'Incorrect number of hooks added'); | ||
} | ||
else { | ||
module.exports = require('./common').runTestWithBindingPath(test); | ||
} | ||
|
||
function test(bindingPath) { | ||
for (const remove_hooks of [false, true]) { | ||
const { status, output } = require('./napi_child').spawnSync( | ||
process.execPath, | ||
[ | ||
__filename, | ||
'runInChildProcess', | ||
bindingPath, | ||
remove_hooks, | ||
], | ||
{ encoding: 'utf8' } | ||
); | ||
|
||
const stdout = output[1].trim(); | ||
/** | ||
* There is no need to sort the lines, as per Node-API documentation: | ||
* > The hooks will be called in reverse order, i.e. the most recently | ||
* > added one will be called first. | ||
*/ | ||
const lines = stdout.split(/[\r\n]+/); | ||
|
||
assert(status === 0, `Process aborted with status ${status}`); | ||
|
||
if (remove_hooks) { | ||
assert.deepStrictEqual(lines, [''], 'Child process had console output when none expected') | ||
} else { | ||
assert.deepStrictEqual(lines, [ | ||
'lambda cleanup()', | ||
'lambda cleanup(void)', | ||
'lambda cleanup(42)', | ||
'static cleanup()', | ||
'static cleanup()', | ||
'static cleanup(43)', | ||
'static cleanup(42)', | ||
'static cleanup(42)' | ||
], 'Child process console output mismisatch') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters