Skip to content

Commit

Permalink
add AddPostFinalizer test
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinEady committed Jun 21, 2024
1 parent 7c949dd commit 321fe1f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
50 changes: 50 additions & 0 deletions test/finalizer_order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ Napi::Value IsExternalGcFinalizerCalled(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(info.Env(), externalGcFinalizerCalled);
}

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
Napi::Value AddPostFinalizer(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/) { callback.Call({}); });

return env.Undefined();
}

Napi::Value AddPostFinalizerWithData(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/, Napi::Reference<Napi::Value>* data) {
callback.Call({data->Value()});
delete data;
},
new Napi::Reference<Napi::Value>(Napi::Persistent(info[1])));
return env.Undefined();
}

Napi::Value AddPostFinalizerWithDataAndHint(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/,
Napi::Reference<Napi::Value>* data,
Napi::Reference<Napi::Value>* hint) {
callback.Call({data->Value(), hint->Value()});
delete data;
delete hint;
},
new Napi::Reference<Napi::Value>(Napi::Persistent(info[1])),
new Napi::Reference<Napi::Value>(Napi::Persistent(info[2])));
return env.Undefined();
}
#endif

} // namespace

Napi::Object InitFinalizerOrder(Napi::Env env) {
Expand All @@ -95,5 +137,13 @@ Napi::Object InitFinalizerOrder(Napi::Env env) {
Napi::Function::New(env, IsExternalNogcFinalizerCalled);
exports["isExternalGcFinalizerCalled"] =
Napi::Function::New(env, IsExternalGcFinalizerCalled);

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
exports["AddPostFinalizer"] = Napi::Function::New(env, AddPostFinalizer);
exports["AddPostFinalizerWithData"] =
Napi::Function::New(env, AddPostFinalizerWithData);
exports["AddPostFinalizerWithDataAndHint"] =
Napi::Function::New(env, AddPostFinalizerWithDataAndHint);
#endif
return exports;
}
36 changes: 34 additions & 2 deletions test/finalizer_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-unused-vars */

const assert = require('assert');
const common = require('./common');
const testUtil = require('./testUtil');

module.exports = require('./common').runTest(test);
Expand All @@ -12,7 +13,7 @@ function test (binding) {

let isCallbackCalled = false;

return testUtil.runGCTests([
const tests = [
'Finalizer Order - ObjectWrap',
() => {
let test = new binding.finalizer_order.Test(() => { isCallbackCalled = true; });
Expand Down Expand Up @@ -62,5 +63,36 @@ function test (binding) {
() => {
assert.strictEqual(binding.finalizer_order.isExternalGcFinalizerCalled(), true, 'Expected External gc finalizer to be called [after ticking]');
}
]);
];

if (binding.isExperimental) {
tests.push(...[
'AddPostFinalizer',
() => {
binding.finalizer_order.AddPostFinalizer(common.mustCall());
},

'AddPostFinalizerWithData',
() => {
const data = {};
const callback = (callbackData) => {
assert.strictEqual(callbackData, data);
};
binding.finalizer_order.AddPostFinalizerWithData(common.mustCall(callback), data);
},

'AddPostFinalizerWithDataAndHint',
() => {
const data = {};
const hint = {};
const callback = (callbackData, callbackHint) => {
assert.strictEqual(callbackData, data);
assert.strictEqual(callbackHint, hint);
};
binding.finalizer_order.AddPostFinalizerWithDataAndHint(common.mustCall(callback), data, hint);
}
]);
}

return testUtil.runGCTests(tests);
}

0 comments on commit 321fe1f

Please sign in to comment.