Skip to content

New API to store per-addon-instance data #378

@gabrielschulhof

Description

@gabrielschulhof

Picking up from nodejs/node#28464 (comment):

@addaleax:

@gabrielschulhof It’s not safe to run JS at that point, but otherwise I think this should work. And sure, it’s possible to do this, it’s just not very ergonomical at this point (imo).

You're right. It's not ergonomical. WDYT about the API below?

NAPI_EXTERN napi_status napi_set_module_data(napi_env env,
                                             void* data,
                                             napi_finalizer finalize_cb,
                                             void* finalize_hint);

NAPI_EXTERN napi_status napi_get_module_data(napi_env env, void** data);

We are already using can_call_into_js() to determine whether it's safe to call into JS, so I think we can offer napi_env in the cleanup hook, and thus we can render it as a napi_finalize.

This API would have the added advantage that bindings need not call napi_get_cb_info() to retrieve their void* data, because napi_get_cb_info() is fairly heavy.

Also, if we switch to a per-module napi_env, that's actually what we should've had all along, but any ObjectTemplates we may wish to store in the future (I hope there will be none) should be stored on the node::Environment and not on the per-addon napi_env.

Metadata

Metadata

Assignees

No one assigned

    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