GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Finalizer for napi_create_function() · Issue #313 · nodejs/abi-stable-node

Finalizer for napi_create_function() #313

New issue
Copy link
New issue
Copy link
Closed
Closed
Finalizer for napi_create_function()#313
Copy link
Assignees
gabrielschulhof

Description

@gabrielschulhof
gabrielschulhof
opened on Apr 28, 2018
Issue body actions

When using napi_create_object() and napi_create_function(), you cannot
specify a finalizer callback, which probably should be allowed since it
can be useful to have.

The omission of a napi_finalize for napi_create_function() is of particular concern IMO.

Re: nodejs/node#14256

Activity

  1. gabrielschulhof commented on Apr 30, 2018

    @gabrielschulhof
    gabrielschulhof
    on Apr 30, 2018
    CollaboratorAuthor
    More actions

    ... because it prevents one from being able to associate dynamic data with a function.

    OTOH, how often to we dynamically associate a native function with a JS function? Most often we do so from Init() and then never again.

  2. gabrielschulhof
    self-assigned this
    on May 10, 2018
  3. gabrielschulhof commented on May 16, 2018

    @gabrielschulhof
    gabrielschulhof
    on May 16, 2018
    CollaboratorAuthor
    More actions

    @nodejs/n-api I think the reason we don't have napi_finalize on napi_create_function() and napi_define_class() is that native-backed JS functions live forever – at least on V8. Similarly, IIRC, the v8::FunctionTemplate also lives forever.

    IMO this also means we should discourage function factories. Of course, we still may want to add finalizers for the sake of other engines.

  4. gabrielschulhof commented on May 16, 2018

    @gabrielschulhof
    gabrielschulhof
    on May 16, 2018
    CollaboratorAuthor
    More actions

    If we were to implement this, we couldn't really test it on V8.

  5. mhdawson commented on May 16, 2018

    @mhdawson
    mhdawson
    on May 16, 2018
    Member
    More actions

    Unless the team from ChakraCore thinks this is important, I'd probably focus on other priorities as opposed to adding a new method with the additional signature.

  6. gabrielschulhof commented on May 16, 2018

    @gabrielschulhof
    gabrielschulhof
    on May 16, 2018
    CollaboratorAuthor
    More actions

    @kfarnung @digitalinfinity what do you think?

  7. digitalinfinity commented on May 17, 2018

    @digitalinfinity
    digitalinfinity
    on May 17, 2018
    Contributor
    More actions

    I agree with the sentiment that it could be useful, but I'm also of the view that we should generally wait for module authors to say that something is impossible for them to implement in N-API without a new API. Or in other words, I'm in favor of kicking this can down the road until someone convinces us otherwise.

    Reacted by Gabriel Schulhof
  8. gabrielschulhof commented on Jun 21, 2018

    @gabrielschulhof
    gabrielschulhof
    on Jun 21, 2018
    CollaboratorAuthor
    More actions

    If we sacrifice the ability to wrap a function, we can already accomplish this:

    void delete_data(napi_env env, void* data, void* hint) {
      free(data);
    }
    
    NativeData* data = malloc(*data);
    napi_value js_function;
    
    assert(napi_create_function(env,
                                "someMethod",
                                NAPI_AUTO_LENGTH,
                                data,
                                &js_function) == napi_ok);
    assert(napi_wrap(env, js_function, data, delete_data, NULL, NULL) == napi_ok);
  9. gabrielschulhof commented on Aug 10, 2018

    @gabrielschulhof
    gabrielschulhof
    on Aug 10, 2018
    CollaboratorAuthor
    More actions

    In the API working group meeting of 2018-08-09 we agreed to add an API for adding finalizers so that data associated with dynamically created functions can itself be freed when the function is finalized while at the same time advising potential bug reporters to rely on napi_wrap() until the new API becomes supported on all supported versions of Node.js.

    Relying on napi_wrap(), although not ideal, should not be a big sacrifice because, although it sacrifices the ability to wrap a function, it is usually not functions that are wrapped, but rather objects which are either plain or constructed using functions.

  10. gabrielschulhof
    added a commit that references this issue on Sep 3, 2018

    n-api: add generic finalizer callback

    b373ccf
  11. gabrielschulhof
    closed this as completedin cf0e881on Sep 13, 2018
  12. targos
    added 3 commits that reference this issue on Sep 14, 2018

    n-api: add generic finalizer callback

    8153c10

    n-api: add generic finalizer callback

    e0bc988

    n-api: add generic finalizer callback

    bb2bbc8
  13. gabrielschulhof
    added a commit that references this issue on Sep 2, 2019

    n-api: add generic finalizer callback

    d3596aa
  14. BethGriggs
    added a commit that references this issue on Sep 19, 2019

    n-api: add generic finalizer callback

    727cd0f
  15. sjarmak
    added a commit that references this issue on Jul 27, 2026

    n-api: add generic finalizer callback

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

Metadata

Metadata

Assignees

  • @gabrielschulhof
    gabrielschulhof

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.