File tree Expand file tree Collapse file tree 2 files changed +38
-3
lines changed
porting/connecting_cpp_and_javascript Expand file tree Collapse file tree 2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -641,6 +641,38 @@ 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.
656+
657+ :param sizeMethodName: Name of the bound method that returns the number of elements.
658+
659+ :param getMethodName: Name of the bound method that retrieves an element by index.
660+
661+ :returns: |class_-function-returns |
662+
663+ .. code-block :: cpp
664+
665+ class_<MyContainer>("MyContainer")
666+ .function("size", &MyContainer::size)
667+ .function("get", &MyContainer::get)
668+ .iterable<int>("size", "get");
669+
670+ .. code-block :: javascript
671+
672+ const container = new Module.MyContainer ();
673+ for (const item of container) { /* ... */ }
674+ const arr = Array .from (container);
675+
644676
645677 .. cpp :function :: const class_& property () const
646678
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments