emscripten-core/emscripten

[Feature request] [Discussion] Document/legalize Embind support for custom marshalling

Open

#9,022 建立於 2019年7月18日

在 GitHub 查看
 (8 留言) (4 反應) (0 負責人)C++ (3,519 fork)batch import
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:

  1. std::function which I want to convert to JS function (forgetting about ownership and leaks) to provide callbacks to JS easily.
  2. Wrapper class around HTMLElementDocument or 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 to emscripten::val and just redirects calls to val::operator(). Something like Rust's wasm-bindgen's web_sys.
  3. 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's wasm_bindgen_futures, but I haven't looked into it).

貢獻者指南