-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api: add generic finalizer callback
Add `napi_add_finalizer()`, which provides the ability to attach data to an arbitrary object and be notified when that object is garbage- collected so as to have an opportunity to delete the data previously attached. This differs from `napi_wrap()` in that it does not use up the private slot on the object, and is therefore neither removable, nor retrievable after the call to `napi_add_finalizer()`. It is assumed that the data is accessible by other means, yet it must be tied to the lifetime of the object. This is the case for data passed to a dynamically created function which is itself heap-allocated and must therefore be freed along with the function. Fixes: nodejs/abi-stable-node#313 PR-URL: #22244 Backport-PR-URL: #28296 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
1 parent
37e24b1
commit 727cd0f
Showing
5 changed files
with
219 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
// Flags: --expose-gc | ||
|
||
const common = require('../../common'); | ||
const test_general = require(`./build/${common.buildType}/test_general`); | ||
const assert = require('assert'); | ||
|
||
let finalized = {}; | ||
const callback = common.mustCall(2); | ||
|
||
// Add two items to be finalized and ensure the callback is called for each. | ||
test_general.addFinalizerOnly(finalized, callback); | ||
test_general.addFinalizerOnly(finalized, callback); | ||
|
||
// Ensure attached items cannot be retrieved. | ||
common.expectsError(() => test_general.unwrap(finalized), | ||
{ type: Error, message: 'Invalid argument' }); | ||
|
||
// Ensure attached items cannot be removed. | ||
common.expectsError(() => test_general.removeWrap(finalized), | ||
{ type: Error, message: 'Invalid argument' }); | ||
finalized = null; | ||
global.gc(); | ||
|
||
// Add an item to an object that is already wrapped, and ensure that its | ||
// finalizer as well as the wrap finalizer gets called. | ||
let finalizeAndWrap = {}; | ||
test_general.wrap(finalizeAndWrap); | ||
test_general.addFinalizerOnly(finalizeAndWrap, common.mustCall()); | ||
finalizeAndWrap = null; | ||
global.gc(); | ||
assert.strictEqual(test_general.derefItemWasCalled(), true, | ||
'finalize callback was called'); |
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