emscripten-core / emscripten-core/emscripten

Exposing nested public symbols in Module

Open
#15,474 8 comments 1 reaction 0 assignees View on GitHub
embind
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Let's say I am using embind to define some bindings, and I have two different classes by the same name which I would like to expose:
```
namespace some_namespace1 {
class Logger {
public:
void log() {}
};
}
namespace some_namespace2 {
class Logger {
public:
void log() {}
};
}

EMSCRIPTEN_BINDINGS(MyBindings) {
emscripten::class_<::some_namespace1::Logger>("Logger")
.function("log", ::some_namespace1::Logger::log)
;
emscripten::class_<::some_namespace2::Logger>("Logger")
.function("log", ::some_namespace2::Logger::log)
;
}
```

If we do this, we get a runtime error, `Cannot register public name 'Logger' twice`, from [here](https://github.com/emscripten-core/emscripten/blob/0c5046e64a80038bece746e3fde832d3ec1e9401/src/embind/embind.js#L1983), when registering the class, because the `.Logger` property is already defined on the Module, and it is not a function overload with a different number of arguments.

This is solvable by adding a prefix when binding each class, like `emscripten::class_<::some_namespace1::Logger>("some_namespace1_Logger")`.

But I wonder if we could instead support nesting exposed symbols into sub-objects at the Module level. So you could do something like:

```
emscripten::class_<::some_namespace1::Logger>("some_namespace1.Logger")
.function("log", ::some_namespace1::Logger::log)
;
emscripten::class_<::some_namespace2::Logger>("some_namespace2.Logger")
.function("log", ::some_namespace2::Logger::log)
;
```

We'd need to accept the dot character in the legal name check [here](https://github.com/emscripten-core/emscripten/blob/0c5046e64a80038bece746e3fde832d3ec1e9401/src/embind/embind.js#L2489).

We'd also need some logic [here](https://github.com/emscripten-core/emscripten/blob/0c5046e64a80038bece746e3fde832d3ec1e9401/src/embind/embind.js#L145-L148) to recursively apply this to the Module, by something like these [suggestions](https://stackoverflow.com/questions/20424226/easy-way-to-set-javascript-object-multilevel-property).

---

I wonder if this is something we'd want to support? Are there places where this feature would make things very difficult?

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.