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: add iterator for Object #930

Closed
wants to merge 1 commit into from

Conversation

RaisinTen
Copy link
Contributor

@RaisinTen RaisinTen commented Mar 12, 2021

Refs: #830
Signed-off-by: Darshan Sen darshan.sen@postman.com

@RaisinTen
Copy link
Contributor Author

I made this very naive implementation of something that looks like an iterator for Napi::Object. Could we please discuss about how to implement this here? If I'm not wrong, in the C++ STL, RB trees are used to keep track of the sequence of iteration but I'm not sure what to use here other than what I have done below.

@RaisinTen RaisinTen marked this pull request as draft March 12, 2021 16:58
@devsnek
Copy link
Member

devsnek commented Mar 12, 2021

Can this work with c++ exceptions disabled?

@RaisinTen
Copy link
Contributor Author

Can this work with c++ exceptions disabled?

Does all of Napi::* work when c++ exceptions are disabled?

@devsnek
Copy link
Member

devsnek commented Mar 12, 2021

yes, using NAPI_DISABLE_CPP_EXCEPTIONS

@RaisinTen
Copy link
Contributor Author

@devnek No error checking is implemented yet. I will keep in mind that it works without c++ exceptions.

@RaisinTen RaisinTen marked this pull request as ready for review March 13, 2021 15:25
@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch 2 times, most recently from 490a387 to c046a43 Compare March 13, 2021 15:31
@gabrielschulhof
Copy link
Contributor

@RaisinTen could you please add a test which preferably also includes a case where an exception is thrown during iteration?

@RaisinTen
Copy link
Contributor Author

@gabrielschulhof Do you mean a test case where the object _object is pointing to gets freed while iterating? Also, is there a way of getting a pointer to the underlying object keys array of the object that is being iterated? The current implementation keeps a copy of the object keys array when std::begin() is called on the object.

@aminya
Copy link

aminya commented Apr 9, 2021

@gabrielschulhof Do you mean a test case where the object _object is pointing to gets freed while iterating? Also, is there a way of getting a pointer to the underlying object keys array of the object that is being iterated? The current implementation keeps a copy of the object keys array when std::begin() is called on the object.

I think a for ranged loop over a Napi::Array and maybe calling one of the STL functions like std::transform would be suitable test cases.

@RaisinTen
Copy link
Contributor Author

Since most of the tests check that the behaviour from the C++ side is coherent with the JS side, I wrote a test to check that the computed sum of a Napi::Array using the iterator is the same from both the JS side and the C++ side. Since I used the for ranged loop already, I didn't use std::accumulate on this.

Still not very sure, what I should do regarding exceptions though. Should we throw std::out_of_range when anything looks fishy inside the iterator?

napi-inl.h Outdated Show resolved Hide resolved
int64_t sum = 0;

for (const auto& e : array) {
sum += e.second.As<Number>().Int64Value();
Copy link
Contributor Author

@RaisinTen RaisinTen Apr 10, 2021

Choose a reason for hiding this comment

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

Perhaps, I should change this test to iterate over an object because I'm planning on adding a separate iterator for Napi::Array, where the Napi::Array::operator*() would return the Value& at the currently iterated index.

Copy link

Choose a reason for hiding this comment

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

Having a special iterator for arrays would be nice.

@RaisinTen
Copy link
Contributor Author

cc @gabrielschulhof, @mhdawson Could this please get another review?

@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from 8b0da48 to ca0a067 Compare June 16, 2021 15:44
@@ -598,6 +598,14 @@ namespace Napi {
uint32_t index /// Property / element index
);

/// Gets or sets an indexed property or array element.
PropertyLValue<Value> operator[](Value index /// Property / element index
Copy link
Contributor

Choose a reason for hiding this comment

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

This whitespacing is strange. Please close the bracket immediately after index! Please use // for comments!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gabrielschulhof I agree, it does look strange. It was done by the linter though. I used /// for the comments to make it consistent with the rest of the comments nearby. Perhaps we can change all comments to use // in a separate PR?


inline iterator end();
#endif // NAPI_CPP_EXCEPTIONS

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need these forward declarations? Why not place the const_iterator and iterator class definitions in directly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gabrielschulhof The iterator classes require the definition of the Napi::Array class to be completed but it is not completed here yet. That's why I decided to move the actual class definitions after the definition of Napi::Array.

@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from dc30134 to 0a40317 Compare July 11, 2021 04:52
Copy link
Contributor

@KevinEady KevinEady left a comment

Choose a reason for hiding this comment

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

I'm not a big fan of the changes made in order to see if the binding has C++ exceptions enabled. Is it possible to change the logic?

test/common/index.js Outdated Show resolved Hide resolved
test/object/object.js Outdated Show resolved Hide resolved
test/object/object.js Outdated Show resolved Hide resolved
@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from 0a40317 to 176657b Compare July 18, 2021 06:16
@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from 176657b to 98c26e0 Compare August 28, 2021 14:16
@RaisinTen
Copy link
Contributor Author

Been a while since the last review. Can this please get another review as I have addressed all the comments and rebased?
cc @gabrielschulhof @KevinEady @legendecas @mhdawson

@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from 98c26e0 to ebdb98a Compare August 28, 2021 14:28
@mhdawson
Copy link
Member

mhdawson commented Sep 8, 2021

@RaisinTen I looked through it and the main thing I think it needs is additions to the documentation. @gabrielschulhof @KevinEady @legendecas anything else you want to point out?

@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch 3 times, most recently from 2775157 to 3835c91 Compare September 11, 2021 07:40
@RaisinTen
Copy link
Contributor Author

@mhdawson added docs, PTAL :)

doc/object.md 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 with a suggestion

Refs: nodejs#830
Signed-off-by: Darshan Sen <darshan.sen@postman.com>
@RaisinTen RaisinTen force-pushed the src/add-iterator-for-Object branch from 3835c91 to ed6c419 Compare September 14, 2021 14:09
@devnek
Copy link

devnek commented Sep 26, 2021

@devnek No error checking is implemented yet. I will keep in mind that it works without c++ exceptions.

Please don't tag @devnek

@mhdawson
Copy link
Member

@NickNaso I think you wanted to do a pass through before this landed.

@NickNaso
Copy link
Member

@NickNaso I think you wanted to do a pass through before this landed.

Yes, I'm taking a look right now.

Copy link
Member

@NickNaso NickNaso left a comment

Choose a reason for hiding this comment

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

@RaisinTen just some notes about documentation.

### Constant Iterator

In constant iterators, the iterated values are immutable.

Copy link
Member

Choose a reason for hiding this comment

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

Here you should document the interface exposed by Napi::Object::const_iterator.


In constant iterators, the iterated values are immutable.

```cpp
Copy link
Member

Choose a reason for hiding this comment

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

Here you should add title Example

### Non Constant Iterator

In non constant iterators, the iterated values are mutable.

Copy link
Member

Choose a reason for hiding this comment

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

Here you should document the interface exposed by Napi::Object::terator


In non constant iterators, the iterated values are mutable.

```cpp
Copy link
Member

Choose a reason for hiding this comment

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

Here you should add title Example.

@RaisinTen
Copy link
Contributor Author

@NickNaso thank you very much for the review comments. Before I address the comments, I would like to know whether I should add the changes in a separate PR because the changes in this PR seems to be landed already in bd8f6e6.
cc @mhdawson for some clarification.

@mhdawson
Copy link
Member

mhdawson commented Oct 4, 2021

@RaisinTen thanks for noticing that. I think I landed it accidentally as part of landing another PR. I think making those changes in a new PR is the best way to go.

@mhdawson
Copy link
Member

mhdawson commented Oct 8, 2021

Closing since landed and we can fix the suggestions in a follow up.

@mhdawson mhdawson closed this Oct 8, 2021
@RaisinTen RaisinTen deleted the src/add-iterator-for-Object branch October 8, 2021 16:05
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.

9 participants