-
Notifications
You must be signed in to change notification settings - Fork 469
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
Conversation
I made this very naive implementation of something that looks like an iterator for |
Can this work with c++ exceptions disabled? |
Does all of |
yes, using NAPI_DISABLE_CPP_EXCEPTIONS |
@devnek No error checking is implemented yet. I will keep in mind that it works without c++ exceptions. |
490a387
to
c046a43
Compare
@RaisinTen could you please add a test which preferably also includes a case where an exception is thrown during iteration? |
@gabrielschulhof Do you mean a test case where the object |
c046a43
to
02186ee
Compare
I think a |
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 Still not very sure, what I should do regarding exceptions though. Should we throw |
test/object/object.cc
Outdated
int64_t sum = 0; | ||
|
||
for (const auto& e : array) { | ||
sum += e.second.As<Number>().Int64Value(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
cc @gabrielschulhof, @mhdawson Could this please get another review? |
8b0da48
to
ca0a067
Compare
@@ -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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
dc30134
to
0a40317
Compare
There was a problem hiding this 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?
0a40317
to
176657b
Compare
176657b
to
98c26e0
Compare
Been a while since the last review. Can this please get another review as I have addressed all the comments and rebased? |
98c26e0
to
ebdb98a
Compare
@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? |
2775157
to
3835c91
Compare
@mhdawson added docs, PTAL :) |
There was a problem hiding this 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>
3835c91
to
ed6c419
Compare
@NickNaso I think you wanted to do a pass through before this landed. |
Yes, I'm taking a look right now. |
There was a problem hiding this 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. | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. | ||
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 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. |
Closing since landed and we can fix the suggestions in a follow up. |
Refs: #830
Signed-off-by: Darshan Sen darshan.sen@postman.com