src: use Global for storing resource in Node-API callback scope#59828
src: use Global for storing resource in Node-API callback scope#59828addaleax wants to merge 2 commits intonodejs:mainfrom
Global for storing resource in Node-API callback scope#59828Conversation
|
Review requested:
|
There was a problem hiding this comment.
The resource object used in the napi_callback_scope is already saved with a Global though:
Line 636 in 234c26c
It is a weak ref if user provided the object, and replace the ref-ed value with a plain object (strong-ref) if the user provided object is GC-ed. We can enforce the requirement that napi_async_context must be destroyed with napi_async_destroy so that this global can be a strong one.
|
@legendecas Yeah, I was confused by this quite a bit and maybe you can help clarify things here. Why are we initially only storing the reference as a weak one, rather than a strong one? I didn't want to touch that logic since whoever put it there must have done so for a good reason (i.e. that this is an example of Chesterton's fence), but I couldn't think of this as anything other than it being a bug.
And how else would the |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59828 +/- ##
=======================================
Coverage 88.28% 88.28%
=======================================
Files 702 702
Lines 206995 206987 -8
Branches 39833 39831 -2
=======================================
- Hits 182740 182738 -2
+ Misses 16265 16233 -32
- Partials 7990 8016 +26
🚀 New features to boost your workflow:
|
|
However, after the resource object been exposed as Back to this PR, |
9a20f3d to
85eef92
Compare
|
@legendecas Okay, that makes some sense, but yeah, callers have always been required to have matching calls of |
This is a follow-up to 234c26c. The Node-API interface does not allow us to enforce that values are stored in a specific location, e.g. on the stack or not; however, V8 requires `Local<>` handles to be stored on the stack. To circumvent this restriction, we add the ability to the async handle stack to store either `Local<>*` pointers or `Global<>*` pointers, with Node-API making use of the latter.
There already is an existing requirement to have matching calls of `napi_async_init()` and `napi_async_destroy()`, so expecting users of this API to manually hold onto the resource for the duration of the `napi_async_context`'s lifetime is unnecessary. Weak callbacks are generally useful for when a corresponding C++ object should be cleaned up when a JS object is gargbage-collected, but that is not the case here.
2e83988 to
24cde9f
Compare
|
Landed in 3e79dba...0ec1d18 |
This is a follow-up to 234c26c. The Node-API interface does not allow us to enforce that values are stored in a specific location, e.g. on the stack or not; however, V8 requires `Local<>` handles to be stored on the stack. To circumvent this restriction, we add the ability to the async handle stack to store either `Local<>*` pointers or `Global<>*` pointers, with Node-API making use of the latter. PR-URL: #59828 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
There already is an existing requirement to have matching calls of `napi_async_init()` and `napi_async_destroy()`, so expecting users of this API to manually hold onto the resource for the duration of the `napi_async_context`'s lifetime is unnecessary. Weak callbacks are generally useful for when a corresponding C++ object should be cleaned up when a JS object is gargbage-collected, but that is not the case here. PR-URL: #59828 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
|
Looks like it depends on #59705 |
| private: | ||
| void* reserved_; | ||
| v8::Local<v8::Object> resource_storage_; | ||
| void* resource_storage_global_; |
There was a problem hiding this comment.
What is this field for? I'm getting a -Wunused-private-field warning with Clang 21.
src: use
Globalfor storing resource in Node-API callback scopeThis is a follow-up to 234c26c. The Node-API interface does
not allow us to enforce that values are stored in a specific location,
e.g. on the stack or not; however, V8 requires
Local<>handlesto be stored on the stack.
To circumvent this restriction, we add the ability to the async handle
stack to store either
Local<>*pointers orGlobal<>*pointers, withNode-API making use of the latter.
src: always use strong reference to
napi_async_contextresourceThere already is an existing requirement to have matching calls of
napi_async_init()andnapi_async_destroy(), so expecting usersof this API to manually hold onto the resource for the duration of
the
napi_async_context's lifetime is unnecessary.Weak callbacks are generally useful for when a corresponding C++
object should be cleaned up when a JS object is gargbage-collected,
but that is not the case here.