Skip to content

Commit

Permalink
test: checks on napi factory wrap’s finalization
Browse files Browse the repository at this point in the history
Fixes: #22396
PR-URL: #22612
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
  • Loading branch information
legendecas authored and Gabriel Schulhof committed Sep 13, 2018
1 parent cf0e881 commit 1cee085
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
11 changes: 8 additions & 3 deletions test/addons-napi/7_factory_wrap/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 9 additions & 0 deletions test/addons-napi/7_factory_wrap/myobject.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#include "myobject.h"
#include "../common.h"

static int finalize_count = 0;

MyObject::MyObject() : env_(nullptr), wrapper_(nullptr) {}

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

void MyObject::Destructor(napi_env env,
void* nativeObject,
void* /*finalize_hint*/) {
++finalize_count;
MyObject* obj = static_cast<MyObject*>(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) {
Expand Down
1 change: 1 addition & 0 deletions test/addons-napi/7_factory_wrap/myobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 20 additions & 9 deletions test/addons-napi/7_factory_wrap/test.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 3 additions & 2 deletions test/addons-napi/8_passing_wrapped/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 1cee085

Please sign in to comment.