You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow napi_add_finalizer to work on primitive types #48311
Finalizers are implemented via FinalizationRegistry in browser, but primitive types can not work with FinalizationRegistry. When should the finalizer associated with primitive value be called?
@toyobayashi V8 has support for weak references to primitive types. @vmoroz implemented support for such weak references in #45715 for napi_create_reference(). I'm proposing we similarly loosen restrictions from objects-only to objects and primitive types in napi_add_finalizer().
@gabrielschulhof , I am curious how it could be implemented for the primitive types:
Currently the finalizers are invoked after V8 invokes them. I had an impression that V8 does it only for object types.
As an alternative we can notify user code when a specific napi_ref is deleted. It will have a different semantic as it will not necessarily mean that the underlying V8 object is deleted.
Do you know if V8 supports calling finalizers for primitive types?
@gabrielschulhof yes I know the implementation detail of #45715 and toyobayashi/emnapi#64 implemented it. I'm not sure if adding finalizer to primitive types could be implemented in emnapi, even if v8 supports, emnapi can't access v8 API, it relies on FinalizationRegistry to call finalizers. However FinalizationRegistry doesn't work on primitive types, so I asked when the finalizer should be called.
Activity
gabrielschulhof commentedon Jun 3, 2023
@vmoroz WDYT?
toyobayashi commentedon Jun 4, 2023
Finalizers are implemented via
FinalizationRegistryin browser, but primitive types can not work withFinalizationRegistry. When should the finalizer associated with primitive value be called?gabrielschulhof commentedon Jun 5, 2023
@toyobayashi V8 has support for weak references to primitive types. @vmoroz implemented support for such weak references in #45715 for
napi_create_reference(). I'm proposing we similarly loosen restrictions from objects-only to objects and primitive types innapi_add_finalizer().vmoroz commentedon Jun 5, 2023
@gabrielschulhof , I am curious how it could be implemented for the primitive types:
napi_refis deleted. It will have a different semantic as it will not necessarily mean that the underlying V8 object is deleted.Do you know if V8 supports calling finalizers for primitive types?
toyobayashi commentedon Jun 6, 2023
@gabrielschulhof yes I know the implementation detail of #45715 and toyobayashi/emnapi#64 implemented it. I'm not sure if adding finalizer to primitive types could be implemented in emnapi, even if v8 supports, emnapi can't access v8 API, it relies on
FinalizationRegistryto call finalizers. HoweverFinalizationRegistrydoesn't work on primitive types, so I asked when the finalizer should be called.gabrielschulhof commentedon Jun 8, 2023
Oh, I see now. References can be created for primitive types, but not weak ones.