Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Object InitTypedThreadSafeFunction(Env env);
Object InitTypedArray(Env env);
Object InitObjectWrap(Env env);
Object InitObjectWrapConstructorException(Env env);
Object InitObjectWrapRef(Env env);
Object InitObjectWrapRemoveWrap(Env env);
Object InitObjectWrapMultipleInheritance(Env env);
Object InitObjectReference(Env env);
Expand Down Expand Up @@ -131,6 +132,7 @@ Object Init(Env env, Object exports) {
exports.Set("objectwrap", InitObjectWrap(env));
exports.Set("objectwrapConstructorException",
InitObjectWrapConstructorException(env));
exports.Set("objectwrap_ref", InitObjectWrapRef(env));
exports.Set("objectwrap_removewrap", InitObjectWrapRemoveWrap(env));
exports.Set("objectwrap_multiple_inheritance", InitObjectWrapMultipleInheritance(env));
exports.Set("objectreference", InitObjectReference(env));
Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'typedarray.cc',
'objectwrap.cc',
'objectwrap_constructor_exception.cc',
'objectwrap_ref.cc',
'objectwrap-removewrap.cc',
'objectwrap_multiple_inheritance.cc',
'objectreference.cc',
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ let testModules = [
'typedarray-bigint',
'objectwrap',
'objectwrap_constructor_exception',
'objectwrap_ref',
'objectwrap-removewrap',
'objectwrap_multiple_inheritance',
'objectwrap_worker_thread',
Expand Down
34 changes: 34 additions & 0 deletions test/objectwrap_ref.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <napi.h>

class ObjectWrapRef : public Napi::ObjectWrap<ObjectWrapRef> {
public:
ObjectWrapRef(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<ObjectWrapRef>(info) {}

Napi::Value DoRef(const Napi::CallbackInfo& info) {
auto count = this->Ref();
return Napi::Number::New(info.Env(), count);
}

Napi::Value DoUnref(const Napi::CallbackInfo& info) {
auto count = this->Unref();
return Napi::Number::New(info.Env(), count);
}

static void Initialize(Napi::Env env, Napi::Object exports) {
const char* name = "ObjectWrapRef";
exports.Set(
name,
DefineClass(
env,
name,
{InstanceMethod("ref", &ObjectWrapRef::DoRef, napi_default),
InstanceMethod("unref", &ObjectWrapRef::DoUnref, napi_default)}));
}
};

Napi::Object InitObjectWrapRef(Napi::Env env) {
Napi::Object exports = Napi::Object::New(env);
ObjectWrapRef::Initialize(env, exports);
return exports;
}
21 changes: 21 additions & 0 deletions test/objectwrap_ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
const testUtil = require('./testUtil');

function test(binding) {
return testUtil.runGCTests([
'objectwrap ref',
() => {
const { ObjectWrapRef } = binding.objectwrap_ref;
const objectWrapRef = new ObjectWrapRef();
assert.equal(objectWrapRef.ref(), 1);
// assert.equal(objectWrapRef.unref(), 0);
},
// Do on gc before returning.
() => {}
]);
}

module.exports = test(require(`./build/${buildType}/binding.node`))
.then(() => test(require(`./build/${buildType}/binding_noexcept.node`)));