From fc5dc94c7eee5ad71619122a5eb6397938b2f8bb Mon Sep 17 00:00:00 2001 From: Kevin Eady <8634912+KevinEady@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:32:11 +0200 Subject: [PATCH] add AddPostFinalizer test --- test/finalizer_order.cc | 45 +++++++++++++++++++++++++++++++++++++++++ test/finalizer_order.js | 36 +++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/test/finalizer_order.cc b/test/finalizer_order.cc index e06a6cfe1..a50132ee4 100644 --- a/test/finalizer_order.cc +++ b/test/finalizer_order.cc @@ -84,11 +84,56 @@ Napi::Value IsExternalGcFinalizerCalled(const Napi::CallbackInfo& info) { return Napi::Boolean::New(info.Env(), externalGcFinalizerCalled); } +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(); +} + } // namespace Napi::Object InitFinalizerOrder(Napi::Env env) { Napi::Object exports = Napi::Object::New(env); Test::Initialize(env, exports); + exports["AddPostFinalizer"] = Napi::Function::New(env, AddPostFinalizer); + exports["AddPostFinalizerWithData"] = + Napi::Function::New(env, AddPostFinalizerWithData); + exports["AddPostFinalizerWithDataAndHint"] = + Napi::Function::New(env, AddPostFinalizerWithDataAndHint); exports["CreateExternalNogc"] = Napi::Function::New(env, CreateExternalNogc); exports["CreateExternalGc"] = Napi::Function::New(env, CreateExternalGc); exports["isExternalNogcFinalizerCalled"] = 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); }