Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd6b2ab

Browse filesBrowse files
legendecastargos
authored andcommitted
test: checks on napi factory wrap’s finalization
Fixes: #22396 PR-URL: #22612 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
1 parent bb2bbc8 commit bd6b2ab
Copy full SHA for bd6b2ab

File tree

Expand file treeCollapse file tree

5 files changed

+41
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+41
-14
lines changed
Open diff view settings
Collapse file

‎test/addons-napi/7_factory_wrap/binding.cc‎

Copy file name to clipboardExpand all lines: test/addons-napi/7_factory_wrap/binding.cc
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ napi_value CreateObject(napi_env env, napi_callback_info info) {
1515
napi_value Init(napi_env env, napi_value exports) {
1616
NAPI_CALL(env, MyObject::Init(env));
1717

18-
NAPI_CALL(env,
19-
// NOLINTNEXTLINE (readability/null_usage)
20-
napi_create_function(env, "exports", -1, CreateObject, NULL, &exports));
18+
napi_property_descriptor descriptors[] = {
19+
DECLARE_NAPI_GETTER("finalizeCount", MyObject::GetFinalizeCount),
20+
DECLARE_NAPI_PROPERTY("createObject", CreateObject),
21+
};
22+
23+
NAPI_CALL(env, napi_define_properties(
24+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
25+
2126
return exports;
2227
}
2328

Collapse file

‎test/addons-napi/7_factory_wrap/myobject.cc‎

Copy file name to clipboardExpand all lines: test/addons-napi/7_factory_wrap/myobject.cc
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
#include "myobject.h"
22
#include "../common.h"
33

4+
static int finalize_count = 0;
5+
46
MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {}
57

68
MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); }
79

810
void MyObject::Destructor(napi_env env,
911
void* nativeObject,
1012
void* /*finalize_hint*/) {
13+
++finalize_count;
1114
MyObject* obj = static_cast<MyObject*>(nativeObject);
1215
delete obj;
1316
}
1417

18+
napi_value MyObject::GetFinalizeCount(napi_env env, napi_callback_info info) {
19+
napi_value result;
20+
NAPI_CALL(env, napi_create_int32(env, finalize_count, &result));
21+
return result;
22+
}
23+
1524
napi_ref MyObject::constructor;
1625

1726
napi_status MyObject::Init(napi_env env) {
Collapse file

‎test/addons-napi/7_factory_wrap/myobject.h‎

Copy file name to clipboardExpand all lines: test/addons-napi/7_factory_wrap/myobject.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class MyObject {
77
public:
88
static napi_status Init(napi_env env);
99
static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);
10+
static napi_value GetFinalizeCount(napi_env env, napi_callback_info info);
1011
static napi_status NewInstance(napi_env env,
1112
napi_value arg,
1213
napi_value* instance);
Collapse file
+20-9Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
'use strict';
2+
// Flags: --expose-gc
3+
24
const common = require('../../common');
35
const assert = require('assert');
4-
const createObject = require(`./build/${common.buildType}/binding`);
6+
const test = require(`./build/${common.buildType}/binding`);
57

6-
const obj = createObject(10);
7-
assert.strictEqual(obj.plusOne(), 11);
8-
assert.strictEqual(obj.plusOne(), 12);
9-
assert.strictEqual(obj.plusOne(), 13);
8+
assert.strictEqual(test.finalizeCount, 0);
9+
(() => {
10+
const obj = test.createObject(10);
11+
assert.strictEqual(obj.plusOne(), 11);
12+
assert.strictEqual(obj.plusOne(), 12);
13+
assert.strictEqual(obj.plusOne(), 13);
14+
})();
15+
global.gc();
16+
assert.strictEqual(test.finalizeCount, 1);
1017

11-
const obj2 = createObject(20);
12-
assert.strictEqual(obj2.plusOne(), 21);
13-
assert.strictEqual(obj2.plusOne(), 22);
14-
assert.strictEqual(obj2.plusOne(), 23);
18+
(() => {
19+
const obj2 = test.createObject(20);
20+
assert.strictEqual(obj2.plusOne(), 21);
21+
assert.strictEqual(obj2.plusOne(), 22);
22+
assert.strictEqual(obj2.plusOne(), 23);
23+
})();
24+
global.gc();
25+
assert.strictEqual(test.finalizeCount, 2);
Collapse file

‎test/addons-napi/8_passing_wrapped/test.js‎

Copy file name to clipboardExpand all lines: test/addons-napi/8_passing_wrapped/test.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const assert = require('assert');
66
const addon = require(`./build/${common.buildType}/binding`);
77

88
let obj1 = addon.createObject(10);
9-
const obj2 = addon.createObject(20);
9+
let obj2 = addon.createObject(20);
1010
const result = addon.add(obj1, obj2);
1111
assert.strictEqual(result, 30);
1212

1313
// Make sure the native destructor gets called.
1414
obj1 = null;
15+
obj2 = null;
1516
global.gc();
16-
assert.strictEqual(addon.finalizeCount(), 1);
17+
assert.strictEqual(addon.finalizeCount(), 2);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.