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

Define iterator for Napi::Array for using with ranged for-loop or STL algorithms #830

Closed
aminya opened this issue Nov 3, 2020 · 7 comments
Labels

Comments

@aminya
Copy link

aminya commented Nov 3, 2020

Currently, the only way to loop over the Napi::Array is to maintain the index manually and loop over using traditional for loops:

    auto napiArray = Napi::Array::New(info.Env());
	for (uint32_t i = 0, len = cppVector.size(); i < len; i++) {
        napiArray[i] = Napi::Number::New(info.Env(), cppVector[i]);
    }

However, I'd like to be able to use ranged for loop or STL algorithms:

    auto napiArray = Napi::Array::New(info.Env());
    std::transform(cppVector.begin(), cppVector.end(), napiArray.begin(), 
	[&info](auto a) { return Napi::Number::New(info.Env(), a); });

or other similar examples

@RaisinTen
Copy link
Contributor

@aminya perhaps you mean an iterator for Napi::Object because Napi::Array inherits from Napi::Object. What should the iterator invalidation rules be?

@aminya
Copy link
Author

aminya commented Nov 18, 2020

We shouldn't necessarily use the same iterator for these two types. I expect the Array to work the same as std::vector. But Napi::Object is more similar to std::map.

@github-actions
Copy link
Contributor

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

@github-actions github-actions bot added the stale label Feb 17, 2021
@aminya
Copy link
Author

aminya commented Feb 17, 2021

Unstale

@mhdawson
Copy link
Member

We discussed in the Node-API team meeting today and it seems reasonable but it might be a more complicated due to sparse arrays etc. @aminya would you be able to submit a PR?

RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Mar 12, 2021
@RaisinTen
Copy link
Contributor

PR to add an iterator for Napi::Object: #930

RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Mar 13, 2021
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Mar 13, 2021
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Apr 8, 2021
@aminya
Copy link
Author

aminya commented Apr 13, 2021

This isn't fixed yet, though.

@KevinEady KevinEady reopened this Apr 13, 2021
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Jun 16, 2021
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Jul 11, 2021
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Jul 18, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Aug 28, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Aug 28, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Sep 11, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Sep 11, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Sep 11, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
RaisinTen added a commit to RaisinTen/node-addon-api that referenced this issue Sep 14, 2021
Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
mhdawson pushed a commit that referenced this issue Sep 28, 2021
Refs: #830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this issue Aug 24, 2022
Refs: nodejs/node-addon-api#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this issue Aug 26, 2022
Refs: nodejs/node-addon-api#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this issue Sep 19, 2022
Refs: nodejs/node-addon-api#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this issue Aug 11, 2023
Refs: nodejs/node-addon-api#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants