diff --git a/test/finalizer_order.cc b/test/finalizer_order.cc index e06a6cfe1..81f176f2b 100644 --- a/test/finalizer_order.cc +++ b/test/finalizer_order.cc @@ -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::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::Env /*env*/, Napi::Reference* data) { + callback.Call({data->Value()}); + delete data; + }, + new Napi::Reference(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::Env /*env*/, + Napi::Reference* data, + Napi::Reference* hint) { + callback.Call({data->Value(), hint->Value()}); + delete data; + delete hint; + }, + new Napi::Reference(Napi::Persistent(info[1])), + new Napi::Reference(Napi::Persistent(info[2]))); + return env.Undefined(); +} +#endif + } // namespace Napi::Object InitFinalizerOrder(Napi::Env env) { @@ -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; } diff --git a/test/finalizer_order.js b/test/finalizer_order.js index 91b66bf36..a8e622e6c 100644 --- a/test/finalizer_order.js +++ b/test/finalizer_order.js @@ -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); @@ -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; }); @@ -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); }