emscripten-core/emscripten
在 GitHub 查看[Feature request] [Discussion] Document/legalize Embind support for custom marshalling
Open
#9,022 建立於 2019年7月18日
embindhelp wanted
倉庫指標
- Star
- (27,361 star)
- PR 合併指標
- (平均合併 19天 10小時) (30 天內合併 147 個 PR)
描述
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).