[Feature request] [Discussion] Document/legalize Embind support for custom marshalling
#9022 aperta il 18 lug 2019
Metriche repository
- Star
- (27.361 star)
- Metriche merge PR
- (Merge medio 19g 10h) (147 PR mergiate in 30 g)
Descrizione
I have a C++ class which I want to (un)marshall to JS in a custom way. There is an "unofficial" way of doing so which works at the moment: provide specializations for emscripten::internal::{BindingType,TypeId} in each translation unit where you want them (together with emscripten/bind.h), like this:
namespace emscripten::internal {
struct BindingType<SomeWeirdType> {
typedef internal::EM_VAL WireType;
static WireType toWireType(const SomeWeirdType& foo) {
// One can also call JS-side functions here and let them construct the new object instead of crafting it here.
return BindingType<val>::toWireType(val(foo.a * 10 + foo.b));
}
};
struct TypeID<SomeWeirdType> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
struct TypeID<const SomeWeirdType> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
struct TypeID<SomeWeirdType&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
struct TypeID<const SomeWeirdType&> {
static constexpr TYPEID get() {
return LightTypeID<val>::get();
}
};
}
However, this is not documented anywhere. Is it likely for this API to change rapidly? If not, it would be nice to have some documentation and, possibly, legalization of such activities (at the very least, moving it out of internal namespace).
How about establishing a documented custom marshalling API?
Some example usecases are:
std::functionwhich I want to convert to JSfunction(forgetting about ownership and leaks) to provide callbacks to JS easily.- Wrapper class around
HTMLElementDocumentor some other JS api which I want to call from C++ for whatever reason instead of writing JS glue code. Say, it holds a reference toemscripten::valand just redirects calls toval::operator(). Something like Rust'swasm-bindgen'sweb_sys. - Custom
Promise-like class which I want to interop with JS, so I cannot just directly bind properties/methods, hence I have to create a new object on JS side in a specific way (probably like in Rust'swasm_bindgen_futures, but I haven't looked into it).