-
-
Notifications
You must be signed in to change notification settings - Fork 477
src: add iterator for Object #930
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,5 +288,47 @@ Napi::Value Napi::Object::operator[] (uint32_t index) const; | |
|
||
Returns an indexed property or array element as a [`Napi::Value`](value.md). | ||
|
||
## Iterator | ||
|
||
Iterators expose an `std::pair<...>`, where the `first` property is a | ||
[`Napi::Value`](value.md) that holds the currently iterated key and the | ||
`second` property is a [`Napi::Object::PropertyLValue`](propertylvalue.md) that | ||
holds the currently iterated value. Iterators are only available if C++ | ||
exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS` during the build). | ||
|
||
### Constant Iterator | ||
|
||
In constant iterators, the iterated values are immutable. | ||
|
||
```cpp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you should add title |
||
Value Sum(const CallbackInfo& info) { | ||
Object object = info[0].As<Object>(); | ||
int64_t sum = 0; | ||
|
||
for (const auto& e : object) { | ||
sum += static_cast<Value>(e.second).As<Number>().Int64Value(); | ||
} | ||
|
||
return Number::New(info.Env(), sum); | ||
} | ||
``` | ||
|
||
### 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 commentThe reason will be displayed to describe this comment to others. Learn more. Here you should document the interface exposed by |
||
```cpp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you should add title |
||
void Increment(const CallbackInfo& info) { | ||
Env env = info.Env(); | ||
Object object = info[0].As<Object>(); | ||
|
||
for (auto e : object) { | ||
int64_t value = static_cast<Value>(e.second).As<Number>().Int64Value(); | ||
++value; | ||
e.second = Napi::Number::New(env, value); | ||
} | ||
} | ||
``` | ||
|
||
[`Napi::Value`]: ./value.md | ||
[`Napi::Value::From`]: ./value.md#from |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -741,6 +741,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 commentThe reason will be displayed to describe this comment to others. Learn more. This whitespacing is strange. Please close the bracket immediately after There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
|
||
/// Gets or sets an indexed property or array element. | ||
PropertyLValue<Value> operator[](Value index /// Property / element index | ||
) const; | ||
|
||
/// Gets a named property. | ||
MaybeOrValue<Value> operator[]( | ||
const char* utf8name ///< UTF-8 encoded null-terminated property name | ||
|
@@ -928,6 +936,21 @@ namespace Napi { | |
inline void AddFinalizer(Finalizer finalizeCallback, | ||
T* data, | ||
Hint* finalizeHint); | ||
|
||
#ifdef NAPI_CPP_EXCEPTIONS | ||
class const_iterator; | ||
|
||
inline const_iterator begin() const; | ||
|
||
inline const_iterator end() const; | ||
|
||
class iterator; | ||
|
||
inline iterator begin(); | ||
|
||
inline iterator end(); | ||
#endif // NAPI_CPP_EXCEPTIONS | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need these forward declarations? Why not place the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gabrielschulhof The iterator classes require the definition of the |
||
#if NAPI_VERSION >= 8 | ||
/// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into | ||
/// JavaScript. | ||
|
@@ -976,6 +999,55 @@ namespace Napi { | |
uint32_t Length() const; | ||
}; | ||
|
||
#ifdef NAPI_CPP_EXCEPTIONS | ||
class Object::const_iterator { | ||
private: | ||
enum class Type { BEGIN, END }; | ||
|
||
inline const_iterator(const Object* object, const Type type); | ||
|
||
public: | ||
inline const_iterator& operator++(); | ||
|
||
inline bool operator==(const const_iterator& other) const; | ||
|
||
inline bool operator!=(const const_iterator& other) const; | ||
|
||
inline const std::pair<Value, Object::PropertyLValue<Value>> operator*() | ||
const; | ||
|
||
private: | ||
const Napi::Object* _object; | ||
Array _keys; | ||
uint32_t _index; | ||
|
||
friend class Object; | ||
}; | ||
|
||
class Object::iterator { | ||
private: | ||
enum class Type { BEGIN, END }; | ||
|
||
inline iterator(Object* object, const Type type); | ||
|
||
public: | ||
inline iterator& operator++(); | ||
|
||
inline bool operator==(const iterator& other) const; | ||
|
||
inline bool operator!=(const iterator& other) const; | ||
|
||
inline std::pair<Value, Object::PropertyLValue<Value>> operator*(); | ||
|
||
private: | ||
Napi::Object* _object; | ||
Array _keys; | ||
uint32_t _index; | ||
|
||
friend class Object; | ||
}; | ||
#endif // NAPI_CPP_EXCEPTIONS | ||
|
||
/// A JavaScript array buffer value. | ||
class ArrayBuffer : public Object { | ||
public: | ||
|
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
.