Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: allow non-copyable callbacks be finalizer parameters #915

Closed
wants to merge 2 commits into from

Conversation

legendecas
Copy link
Member

Fixes: #301

In real-world use cases, Napi::References are frequently been passed around by lambda captures in finalizers, callbacks. This PR aims to make those conditions easier to handle.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Value createExternal(const CallbackInfo& info) {
FunctionReference ref = Reference<Function>::New(info[0].As<Function>(), 1);
auto ret = External<char>::New(
info.Env(), nullptr, [ref = std::move(ref)](Napi::Env env, char* data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails to compile for me with:

../movable_callbacks.cc:8:61: error: unused parameter ‘env’ [-Werror=unused-parameter]
       info.Env(), nullptr, [ref = std::move(ref)](Napi::Env env, char* data) {
                                                   ~~~~~~~~~~^~~
../movable_callbacks.cc:8:72: error: unused parameter ‘data’ [-Werror=unused-parameter]
       info.Env(), nullptr, [ref = std::move(ref)](Napi::Env env, char* data) {
                                                                  ~~~~~~^~~~

Copy link
Member

@mhdawson mhdawson Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a commit to fix

test/movable_callbacks.cc Outdated Show resolved Hide resolved
Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

mhdawson pushed a commit that referenced this pull request Mar 10, 2021
allow non-copyable callbacks be finalizer parameters

Fixes: #301
PR-URL: #915
Reviewed-By: Michael Dawson <midawson@redhat.com>
@mhdawson
Copy link
Member

Fixed up linter complaints as part of landing.

Landed as 74ab50c

@mhdawson mhdawson closed this Mar 10, 2021
@legendecas legendecas deleted the movable-callbacks branch March 26, 2021 06:28
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
allow non-copyable callbacks be finalizer parameters

Fixes: nodejs/node-addon-api#301
PR-URL: nodejs/node-addon-api#915
Reviewed-By: Michael Dawson <midawson@redhat.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
allow non-copyable callbacks be finalizer parameters

Fixes: nodejs/node-addon-api#301
PR-URL: nodejs/node-addon-api#915
Reviewed-By: Michael Dawson <midawson@redhat.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
allow non-copyable callbacks be finalizer parameters

Fixes: nodejs/node-addon-api#301
PR-URL: nodejs/node-addon-api#915
Reviewed-By: Michael Dawson <midawson@redhat.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
allow non-copyable callbacks be finalizer parameters

Fixes: nodejs/node-addon-api#301
PR-URL: nodejs/node-addon-api#915
Reviewed-By: Michael Dawson <midawson@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Napi::Reference not copyable, but semantically could be copyable
2 participants