From c128fc17bbf49cef56be20b0255863c0e0ce905d Mon Sep 17 00:00:00 2001 From: Lucas Woo Date: Fri, 31 Aug 2018 11:57:33 +0800 Subject: [PATCH] =?UTF-8?q?test:=20checks=20on=20napi=20factory=20wrap?= =?UTF-8?q?=E2=80=99s=20finalization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/nodejs/node/issues/22396 --- test/addons-napi/7_factory_wrap/binding.cc | 11 +++++--- test/addons-napi/7_factory_wrap/myobject.cc | 9 +++++++ test/addons-napi/7_factory_wrap/myobject.h | 1 + test/addons-napi/7_factory_wrap/test.js | 29 ++++++++++++++------- test/addons-napi/8_passing_wrapped/test.js | 5 ++-- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/test/addons-napi/7_factory_wrap/binding.cc b/test/addons-napi/7_factory_wrap/binding.cc index d98132457875a1..e937516c894a90 100644 --- a/test/addons-napi/7_factory_wrap/binding.cc +++ b/test/addons-napi/7_factory_wrap/binding.cc @@ -15,9 +15,14 @@ napi_value CreateObject(napi_env env, napi_callback_info info) { napi_value Init(napi_env env, napi_value exports) { NAPI_CALL(env, MyObject::Init(env)); - NAPI_CALL(env, - // NOLINTNEXTLINE (readability/null_usage) - napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); + napi_property_descriptor descriptors[] = { + DECLARE_NAPI_GETTER("finalizeCount", MyObject::GetFinalizeCount), + DECLARE_NAPI_PROPERTY("createObject", CreateObject), + }; + + NAPI_CALL(env, napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); + return exports; } diff --git a/test/addons-napi/7_factory_wrap/myobject.cc b/test/addons-napi/7_factory_wrap/myobject.cc index 4e1d79c1febc17..b7cc9c534f366a 100644 --- a/test/addons-napi/7_factory_wrap/myobject.cc +++ b/test/addons-napi/7_factory_wrap/myobject.cc @@ -1,6 +1,8 @@ #include "myobject.h" #include "../common.h" +static int finalize_count = 0; + MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {} MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); } @@ -8,10 +10,17 @@ MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); } void MyObject::Destructor(napi_env env, void* nativeObject, void* /*finalize_hint*/) { + ++finalize_count; MyObject* obj = static_cast(nativeObject); delete obj; } +napi_value MyObject::GetFinalizeCount(napi_env env, napi_callback_info info) { + napi_value result; + NAPI_CALL(env, napi_create_int32(env, finalize_count, &result)); + return result; +} + napi_ref MyObject::constructor; napi_status MyObject::Init(napi_env env) { diff --git a/test/addons-napi/7_factory_wrap/myobject.h b/test/addons-napi/7_factory_wrap/myobject.h index 28ca94d16e30dd..172fcd3ca49295 100644 --- a/test/addons-napi/7_factory_wrap/myobject.h +++ b/test/addons-napi/7_factory_wrap/myobject.h @@ -7,6 +7,7 @@ class MyObject { public: static napi_status Init(napi_env env); static void Destructor(napi_env env, void* nativeObject, void* finalize_hint); + static napi_value GetFinalizeCount(napi_env env, napi_callback_info info); static napi_status NewInstance(napi_env env, napi_value arg, napi_value* instance); diff --git a/test/addons-napi/7_factory_wrap/test.js b/test/addons-napi/7_factory_wrap/test.js index 20108e14b629bc..8aaf1b0ba91ee7 100644 --- a/test/addons-napi/7_factory_wrap/test.js +++ b/test/addons-napi/7_factory_wrap/test.js @@ -1,14 +1,25 @@ 'use strict'; +// Flags: --expose-gc + const common = require('../../common'); const assert = require('assert'); -const createObject = require(`./build/${common.buildType}/binding`); +const test = require(`./build/${common.buildType}/binding`); -const obj = createObject(10); -assert.strictEqual(obj.plusOne(), 11); -assert.strictEqual(obj.plusOne(), 12); -assert.strictEqual(obj.plusOne(), 13); +assert.strictEqual(test.finalizeCount, 0); +(() => { + const obj = test.createObject(10); + assert.strictEqual(obj.plusOne(), 11); + assert.strictEqual(obj.plusOne(), 12); + assert.strictEqual(obj.plusOne(), 13); +})(); +global.gc(); +assert.strictEqual(test.finalizeCount, 1); -const obj2 = createObject(20); -assert.strictEqual(obj2.plusOne(), 21); -assert.strictEqual(obj2.plusOne(), 22); -assert.strictEqual(obj2.plusOne(), 23); +(() => { + const obj2 = test.createObject(20); + assert.strictEqual(obj2.plusOne(), 21); + assert.strictEqual(obj2.plusOne(), 22); + assert.strictEqual(obj2.plusOne(), 23); +})(); +global.gc(); +assert.strictEqual(test.finalizeCount, 2); diff --git a/test/addons-napi/8_passing_wrapped/test.js b/test/addons-napi/8_passing_wrapped/test.js index 7793133f7750ba..525993c96d3bad 100644 --- a/test/addons-napi/8_passing_wrapped/test.js +++ b/test/addons-napi/8_passing_wrapped/test.js @@ -6,11 +6,12 @@ const assert = require('assert'); const addon = require(`./build/${common.buildType}/binding`); let obj1 = addon.createObject(10); -const obj2 = addon.createObject(20); +let obj2 = addon.createObject(20); const result = addon.add(obj1, obj2); assert.strictEqual(result, 30); // Make sure the native destructor gets called. obj1 = null; +obj2 = null; global.gc(); -assert.strictEqual(addon.finalizeCount(), 1); +assert.strictEqual(addon.finalizeCount(), 2);