emscripten-core / emscripten-core/emscripten

emscripten binding std::vector as JS Array

Open
#18,431 1 comment 0 reactions 0 assignees View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Hallo,

I'm working on some program which optionally can work with Java-Script objects and native objects as well. For example object Node.
This object has property children which is JS Array. JS Array has property length obviously.
I would like sometimes to use native Node abject as well.
So native code looks like this:

```
class emNode
{
public:
const std::vector& getEmChildren() const { return m_Children; }

private:
std::vector m_Children;
};

namespace emscripten {

emscripten::register_vector("VectorNode");

class_("NativeNode")
.property("children", &emNode::getEmChildren)
;

}

```

But problem is emNode children in the end is not JS Array, but Vector! It doesn't have length property and cannot be iterated in existing JS code.

Question is how to make std::vector binding behave as Array ? So it will be absolutely transparent for JS code to work with native or non-native objects ?

I tried approach in this link:
https://github.com/emscripten-core/emscripten/issues/11070

But it doesn't work for me and throwing lot of errors...

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.