-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
node-api: add nested object wrap and napi_ref test #57981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 2 commits into
nodejs:main
from
legendecas:node-api/nested-wrap
Apr 25, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,17 +1,28 @@ | ||
| { | ||
| "targets": [ | ||
| { | ||
| "target_name": "6_object_wrap", | ||
| "target_name": "myobject", | ||
| "sources": [ | ||
| "6_object_wrap.cc" | ||
| "myobject.cc", | ||
| "myobject.h", | ||
| ] | ||
| }, | ||
| { | ||
| "target_name": "6_object_wrap_basic_finalizer", | ||
| "target_name": "myobject_basic_finalizer", | ||
| "defines": [ "NAPI_EXPERIMENTAL" ], | ||
| "sources": [ | ||
| "6_object_wrap.cc" | ||
| "myobject.cc", | ||
| "myobject.h", | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "target_name": "nested_wrap", | ||
| # Test without basic finalizers as it schedules differently. | ||
| "defines": [ "NAPI_VERSION=10" ], | ||
| "sources": [ | ||
| "nested_wrap.cc", | ||
| "nested_wrap.h", | ||
| ], | ||
| }, | ||
| ] | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| #include "nested_wrap.h" | ||
| #include "../common.h" | ||
| #include "../entry_point.h" | ||
|
|
||
| napi_ref NestedWrap::constructor; | ||
| static int finalization_count = 0; | ||
|
|
||
| NestedWrap::NestedWrap() : env_(nullptr), wrapper_(nullptr), nested_(nullptr) {} | ||
|
vmoroz marked this conversation as resolved.
Outdated
|
||
|
|
||
| NestedWrap::~NestedWrap() { | ||
| napi_delete_reference(env_, wrapper_); | ||
|
|
||
| // Delete the nested reference as well. | ||
| napi_delete_reference(env_, nested_); | ||
| } | ||
|
|
||
| void NestedWrap::Destructor(node_api_basic_env env, | ||
| void* nativeObject, | ||
| void* /*finalize_hint*/) { | ||
| // Once this destructor is called, it cancels all pending | ||
| // finalizers for the object by deleting the references. | ||
| NestedWrap* obj = static_cast<NestedWrap*>(nativeObject); | ||
| delete obj; | ||
|
|
||
| finalization_count++; | ||
| } | ||
|
|
||
| void NestedWrap::Init(napi_env env, napi_value exports) { | ||
| napi_value cons; | ||
| NODE_API_CALL_RETURN_VOID( | ||
| env, | ||
| napi_define_class( | ||
| env, "NestedWrap", -1, New, nullptr, 0, nullptr, &cons)); | ||
|
|
||
| NODE_API_CALL_RETURN_VOID(env, | ||
| napi_create_reference(env, cons, 1, &constructor)); | ||
|
|
||
| NODE_API_CALL_RETURN_VOID( | ||
| env, napi_set_named_property(env, exports, "NestedWrap", cons)); | ||
| } | ||
|
|
||
| napi_value NestedWrap::New(napi_env env, napi_callback_info info) { | ||
| napi_value new_target; | ||
| NODE_API_CALL(env, napi_get_new_target(env, info, &new_target)); | ||
| bool is_constructor = (new_target != nullptr); | ||
| NODE_API_BASIC_ASSERT_BASE( | ||
| is_constructor, "Constructor called without new", nullptr); | ||
|
|
||
| napi_value this_val; | ||
| NODE_API_CALL(env, | ||
| napi_get_cb_info(env, info, 0, nullptr, &this_val, nullptr)); | ||
|
|
||
| NestedWrap* obj = new NestedWrap(); | ||
|
|
||
| obj->env_ = env; | ||
| NODE_API_CALL(env, | ||
| napi_wrap(env, | ||
| this_val, | ||
| obj, | ||
| NestedWrap::Destructor, | ||
| nullptr /* finalize_hint */, | ||
| &obj->wrapper_)); | ||
|
|
||
| // Create a second napi_ref to be deleted in the destructor. | ||
| NODE_API_CALL(env, | ||
| napi_add_finalizer(env, | ||
| this_val, | ||
| obj, | ||
| NestedWrap::Destructor, | ||
| nullptr /* finalize_hint */, | ||
| &obj->nested_)); | ||
|
|
||
| return this_val; | ||
| } | ||
|
|
||
| static napi_value GetFinalizerCallCount(napi_env env, napi_callback_info info) { | ||
| napi_value result; | ||
| NODE_API_CALL(env, napi_create_int32(env, finalization_count, &result)); | ||
| return result; | ||
| } | ||
|
|
||
| EXTERN_C_START | ||
| napi_value Init(napi_env env, napi_value exports) { | ||
| NestedWrap::Init(env, exports); | ||
|
|
||
| napi_property_descriptor descriptors[] = { | ||
| DECLARE_NODE_API_PROPERTY("getFinalizerCallCount", GetFinalizerCallCount), | ||
| }; | ||
|
|
||
| NODE_API_CALL( | ||
| env, | ||
| napi_define_properties(env, | ||
| exports, | ||
| sizeof(descriptors) / sizeof(*descriptors), | ||
| descriptors)); | ||
|
|
||
| return exports; | ||
| } | ||
| EXTERN_C_END | ||
This file contains hidden or 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 @@ | ||
| #ifndef JS_NATIVE_API_6_OBJECT_WRAP_NESTED_WRAP_H_ | ||
| #define JS_NATIVE_API_6_OBJECT_WRAP_NESTED_WRAP_H_ | ||
|
|
||
| #include <js_native_api.h> | ||
|
|
||
| /** | ||
| * Test that an napi_ref can be nested inside another ObjectWrap. | ||
| * | ||
| * This test shows a critical case where a finalizer deletes an napi_ref | ||
| * whose finalizer is also scheduled. | ||
| */ | ||
|
|
||
| class NestedWrap { | ||
| public: | ||
| static void Init(napi_env env, napi_value exports); | ||
| static void Destructor(node_api_basic_env env, | ||
| void* nativeObject, | ||
| void* finalize_hint); | ||
|
|
||
| private: | ||
| explicit NestedWrap(); | ||
| ~NestedWrap(); | ||
|
|
||
| static napi_value New(napi_env env, napi_callback_info info); | ||
|
|
||
| static napi_ref constructor; | ||
|
|
||
| napi_env env_; | ||
| napi_ref wrapper_; | ||
| napi_ref nested_; | ||
| }; | ||
|
|
||
| #endif // JS_NATIVE_API_6_OBJECT_WRAP_NESTED_WRAP_H_ |
This file contains hidden or 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,20 @@ | ||
| // Flags: --expose-gc | ||
|
|
||
| 'use strict'; | ||
| const common = require('../../common'); | ||
| const { gcUntil } = require('../../common/gc'); | ||
| const assert = require('assert'); | ||
| const addon = require(`./build/${common.buildType}/nested_wrap`); | ||
|
|
||
| // This test verifies that ObjectWrap and napi_ref can be nested and finalized | ||
| // correctly with a non-basic finalizer. | ||
| (() => { | ||
| let obj = new addon.NestedWrap(); | ||
| obj = null; | ||
| // Silent eslint about unused variables. | ||
| assert.strictEqual(obj, null); | ||
| })(); | ||
|
|
||
| gcUntil('object-wrap-ref', () => { | ||
| return addon.getFinalizerCallCount() === 1; | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.