| |
| |
| |
| |
|
|
| #include <stdio.h> |
| #include <emscripten.h> |
| #include <emscripten/bind.h> |
| #include <emscripten/val.h> |
| #include <type_traits> |
| #include <string> |
|
|
| using namespace emscripten; |
|
|
| |
| struct IntWrapper { |
| int get() const { |
| return value; |
| } |
| static IntWrapper create(int v) { |
| return IntWrapper(v); |
| } |
|
|
| private: |
| explicit IntWrapper(int v) : value(v) {} |
| int value; |
| }; |
|
|
| |
| template<typename T> struct IsIntWrapper : std::false_type {}; |
| template<> struct IsIntWrapper<IntWrapper> : std::true_type {}; |
|
|
| |
| |
| |
| using IntWrapperIntermediate = int; |
|
|
| |
| namespace emscripten { |
| namespace internal { |
| template<typename T> |
| struct TypeID<T, typename std::enable_if<IsIntWrapper<T>::value, void>::type> { |
| static constexpr TYPEID get() { |
| return TypeID<IntWrapperIntermediate>::get(); |
| } |
| }; |
|
|
| template<typename T> |
| struct BindingType<T, typename std::enable_if<IsIntWrapper<T>::value, void>::type> { |
| typedef typename BindingType<IntWrapperIntermediate>::WireType WireType; |
|
|
| constexpr static WireType toWireType(const T& v, rvp::default_tag) { |
| return BindingType<IntWrapperIntermediate>::toWireType(v.get(), rvp::default_tag{}); |
| } |
| constexpr static T fromWireType(WireType v) { |
| return T::create(BindingType<IntWrapperIntermediate>::fromWireType(v)); |
| } |
| }; |
| } |
| } |
|
|
| template<typename T> |
| void test() { |
| IntWrapper x = IntWrapper::create(10); |
| val js_func = val::module_property("js_func"); |
| IntWrapper y = js_func(val(std::forward<T>(x))).as<IntWrapper>(); |
| printf("C++ got %d\n", y.get()); |
| } |
|
|
| int main(int argc, char **argv) { |
| test<IntWrapper>(); |
| test<IntWrapper&>(); |
| test<const IntWrapper>(); |
| test<const IntWrapper&>(); |
| return 0; |
| } |
|
|