-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows one to tie the life cycle of one JavaScript object to another. In effect, this allows for JavaScript-style closures. Fixes: nodejs/node-addon-api#508 PR_URL: nodejs/node-addon-api#551 Reviewed-By: Kevin Eady <[email protected]>
- Loading branch information
1 parent
11a1202
commit 83d41e0
Showing
9 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
static int dummy; | ||
|
||
Value AddFinalizer(const CallbackInfo& info) { | ||
ObjectReference* ref = new ObjectReference; | ||
*ref = Persistent(Object::New(info.Env())); | ||
info[0] | ||
.As<Object>() | ||
.AddFinalizer([](Napi::Env /*env*/, ObjectReference* ref) { | ||
ref->Set("finalizerCalled", true); | ||
delete ref; | ||
}, ref); | ||
return ref->Value(); | ||
} | ||
|
||
Value AddFinalizerWithHint(const CallbackInfo& info) { | ||
ObjectReference* ref = new ObjectReference; | ||
*ref = Persistent(Object::New(info.Env())); | ||
info[0] | ||
.As<Object>() | ||
.AddFinalizer([](Napi::Env /*env*/, ObjectReference* ref, int* dummy_p) { | ||
ref->Set("finalizerCalledWithCorrectHint", dummy_p == &dummy); | ||
delete ref; | ||
}, ref, &dummy); | ||
return ref->Value(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
test(require(`../build/${buildType}/binding.node`)); | ||
test(require(`../build/${buildType}/binding_noexcept.node`)); | ||
|
||
function createWeakRef(binding, bindingToTest) { | ||
return binding.object[bindingToTest]({}); | ||
} | ||
|
||
function test(binding) { | ||
const obj1 = createWeakRef(binding, 'addFinalizer'); | ||
global.gc(); | ||
assert.deepStrictEqual(obj1, { finalizerCalled: true }); | ||
|
||
const obj2 = createWeakRef(binding, 'addFinalizerWithHint'); | ||
global.gc(); | ||
assert.deepStrictEqual(obj2, { finalizerCalledWithCorrectHint: true }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters