Skip to content

Commit 47e519e

Browse files
committed
Add some documentation
1 parent 983270e commit 47e519e

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

site/source/docs/api_reference/bind.h.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,41 @@ Classes
641641
:param typename... Policies: |policies-argument|
642642
:returns: |class_-function-returns|
643643

644+
.. cpp:function:: const class_& iterable() const
645+
646+
.. code-block:: cpp
647+
648+
// prototype
649+
template<typename ElementType>
650+
EMSCRIPTEN_ALWAYS_INLINE const class_& iterable(const char* sizeMethodName, const char* getMethodName) const
651+
652+
Makes a bound class iterable in JavaScript by installing ``Symbol.iterator``.
653+
This enables use with ``for...of`` loops, ``Array.from()``, and spread syntax.
654+
655+
:tparam ElementType: The type of elements yielded by the iterator. Used for
656+
TypeScript definition generation.
657+
658+
:param sizeMethodName: Name of the bound method that returns the number of elements.
659+
Must return ``size_t``.
660+
661+
:param getMethodName: Name of the bound method that retrieves an element by index.
662+
Must accept a ``size_t`` index and return ``ElementType``.
663+
664+
:returns: |class_-function-returns|
665+
666+
.. code-block:: cpp
667+
668+
class_<MyContainer>("MyContainer")
669+
.function("size", &MyContainer::size)
670+
.function("get", &MyContainer::get)
671+
.iterable<int>("size", "get");
672+
673+
.. code-block:: javascript
674+
675+
const container = new Module.MyContainer();
676+
for (const item of container) { /* ... */ }
677+
const arr = Array.from(container);
678+
644679
645680
.. cpp:function:: const class_& property() const
646681

site/source/docs/porting/connecting_cpp_and_javascript/embind.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,9 +1157,12 @@ The following JavaScript can be used to interact with the above C++.
11571157
// push value into vector
11581158
retVector.push_back(12);
11591159
1160-
// retrieve value from the vector
1161-
for (var i = 0; i < retVector.size(); i++) {
1162-
console.log("Vector Value: ", retVector.get(i));
1160+
// retrieve a value from the vector
1161+
console.log("Vector Value at index 0: ", retVector.get(0));
1162+
1163+
// iterate over vector
1164+
for (var value of retVector) {
1165+
console.log("Vector Value: ", value);
11631166
}
11641167
11651168
// expand vector size

0 commit comments

Comments
 (0)