Sitelet https://github.com/nodejs/node/issues/48311
Skip to content

Allow napi_add_finalizer to work on primitive types #48311

Description

@gabrielschulhof

What is the problem this feature will solve?

We may want to have feature parity with napi_create_reference, since, internally, both create a reference.

@nodejs/node-api WDYT?

What is the feature you are proposing to solve the problem?

Open up napi_add_finalizer to primitive types.

What alternatives have you considered?

No response

Activity

  1. added
    node-apiIssues and PRs related to Node-API.
    and removed
    feature requestIssues requesting new Node.js features.
    on Jun 3, 2023
  2. gabrielschulhof commented on Jun 3, 2023

    @gabrielschulhof
    ContributorAuthor

    @vmoroz WDYT?

  3. toyobayashi commented on Jun 4, 2023

    @toyobayashi
    Contributor

    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?

  4. gabrielschulhof commented on Jun 5, 2023

    @gabrielschulhof
    ContributorAuthor

    @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().

  5. vmoroz commented on Jun 5, 2023

    @vmoroz
    Member

    @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?

  6. toyobayashi commented on Jun 6, 2023

    @toyobayashi
    Contributor

    @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.

  7. gabrielschulhof commented on Jun 8, 2023

    @gabrielschulhof
    ContributorAuthor

    Oh, I see now. References can be created for primitive types, but not weak ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.node-apiIssues and PRs related to Node-API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions