Skip to content

Commit

Permalink
src: add iterator for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
RaisinTen committed Mar 12, 2021
1 parent 74ab50c commit aae87a7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
31 changes: 31 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,37 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback,
}
}

Object::iterator::iterator(Object *object, uint32_t index) {
_object = object;
_index = index;
_keys = object->GetPropertyNames();
}

Object::iterator Napi::Object::begin() {
iterator it(this, 0);
return it;
}

Object::iterator Napi::Object::end() {
iterator it(this, GetPropertyNames().Length());
return it;
}

Object::iterator Object::iterator::operator ++() {
++_index;
return *this;
}

bool Object::iterator::operator !=(iterator other) {
return _index != other._index;
}

Value Object::iterator::operator *() {
Value key = _keys[_index].Value(); // Don't know how to convert this yet
Object object = *_object;
return object[key];
}

////////////////////////////////////////////////////////////////////////////////
// External class
////////////////////////////////////////////////////////////////////////////////
Expand Down
24 changes: 24 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ namespace Napi {
inline void AddFinalizer(Finalizer finalizeCallback,
T* data,
Hint* finalizeHint);

class iterator;

iterator begin();

iterator end();
};

template <typename T>
Expand Down Expand Up @@ -778,6 +784,24 @@ namespace Napi {
uint32_t Length() const;
};

class Object::iterator {
public:
iterator(Object *object, uint32_t index);

iterator operator ++();

bool operator !=(iterator other);

Value operator *();

private:
Napi::Object *_object;
uint32_t _index;
Array _keys;

friend class Object;
};

/// A JavaScript array buffer value.
class ArrayBuffer : public Object {
public:
Expand Down

0 comments on commit aae87a7

Please sign in to comment.