diff options
author | Alon Zakai <alonzakai@gmail.com> | 2014-06-18 16:58:39 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2014-06-18 16:58:39 -0700 |
commit | 00e891dc31bb23d77cceeeec52be7dd64176bbab (patch) | |
tree | 17bfc2400076d2e2fcd669a18de1ecaed0aa299b | |
parent | d54f3a7b86e2b266cc86341b6e06096dc7f04d5e (diff) | |
parent | 22d2a0fcd2729d9c0c8d53e59471432ddf206b6a (diff) |
Merge pull request #2432 from dlardi/incoming
Added embind support for std::unique_ptr
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | system/include/emscripten/bind.h | 4 | ||||
-rw-r--r-- | system/include/emscripten/wire.h | 5 | ||||
-rw-r--r-- | tests/embind/embind.test.js | 12 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 35 |
5 files changed, 54 insertions, 3 deletions
@@ -145,3 +145,4 @@ a license to everyone to use it as detailed in LICENSE.) * Ningxin Hu <ningxin.hu@intel.com> (copyright owned by Intel) * Nicolas Guillemot <nlguillemot@gmail.com> * Sathyanarayanan Gunasekaran <gsathya.ceg@gmail.com> (copyright owned by Mozilla Foundation) +* Nikolay Vorobyov <nik.vorobyov@gmail.com> diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index 7bc28ff6..67e7c6a3 100644 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -422,8 +422,8 @@ namespace emscripten { namespace internal { template<typename ClassType, typename... Args> - ClassType* operator_new(Args... args) { - return new ClassType(args...); + ClassType* operator_new(Args&&... args) { + return new ClassType(std::forward<Args>(args)...); } template<typename WrapperType, typename ClassType, typename... Args> diff --git a/system/include/emscripten/wire.h b/system/include/emscripten/wire.h index c20e2f55..1a9432d6 100644 --- a/system/include/emscripten/wire.h +++ b/system/include/emscripten/wire.h @@ -293,7 +293,6 @@ namespace emscripten { } }; - // Is this necessary? template<typename T> struct GenericBindingType<std::unique_ptr<T>> { typedef typename BindingType<T>::WireType WireType; @@ -301,6 +300,10 @@ namespace emscripten { static WireType toWireType(std::unique_ptr<T> p) { return BindingType<T>::toWireType(*p); } + + static std::unique_ptr<T> fromWireType(WireType wt) { + return std::unique_ptr<T>(new T(std::move(BindingType<T>::fromWireType(wt)))); + } }; template<typename Enum> diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 432202ff..f1de1a12 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -604,6 +604,18 @@ module({ c.delete(); }); + test("can pass unique_ptr", function() { + var p = cm.embind_test_return_unique_ptr(42); + var m = cm.embind_test_accept_unique_ptr(p); + assert.equal(42, m); + }); + + test("can pass unique_ptr to constructor", function() { + var c = new cm.embind_test_construct_class_with_unique_ptr(42); + assert.equal(42, c.getValue()); + c.delete(); + }); + test("can get member classes then call its member functions", function() { var p = new cm.ParentClass(); var c = p.getBigClass(); diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index 30267994..511f179b 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -856,6 +856,32 @@ void emval_test_call_function(val v, int i, float f, TupleVector tv, StructVecto v(i, f, tv, sv); } +class UniquePtrToConstructor { +public: + UniquePtrToConstructor(std::unique_ptr<int> p) + : value(*p) + {} + + int getValue() const { + return value; + } + +private: + int value; +}; + +std::unique_ptr<int> embind_test_return_unique_ptr(int v) { + return std::unique_ptr<int>(new int(v)); +} + +UniquePtrToConstructor* embind_test_construct_class_with_unique_ptr(int v) { + return new UniquePtrToConstructor(embind_test_return_unique_ptr(v)); +} + +int embind_test_accept_unique_ptr(std::unique_ptr<int> p) { + return *p.get(); +} + std::unique_ptr<ValHolder> emval_test_return_unique_ptr() { return std::unique_ptr<ValHolder>(new ValHolder(val::object())); } @@ -1818,6 +1844,15 @@ EMSCRIPTEN_BINDINGS(tests) { function("embind_test_accept_small_class_instance", &embind_test_accept_small_class_instance); function("embind_test_accept_big_class_instance", &embind_test_accept_big_class_instance); + class_<UniquePtrToConstructor>("UniquePtrToConstructor") + .constructor<std::unique_ptr<int>>() + .function("getValue", &UniquePtrToConstructor::getValue) + ; + + function("embind_test_construct_class_with_unique_ptr", embind_test_construct_class_with_unique_ptr, allow_raw_pointer<ret_val>()); + function("embind_test_return_unique_ptr", embind_test_return_unique_ptr); + function("embind_test_accept_unique_ptr", embind_test_accept_unique_ptr); + function("embind_test_return_raw_base_ptr", embind_test_return_raw_base_ptr, allow_raw_pointer<ret_val>()); function("embind_test_return_raw_derived_ptr_as_base", embind_test_return_raw_derived_ptr_as_base, allow_raw_pointer<ret_val>()); function("embind_test_return_raw_sibling_derived_ptr_as_base", embind_test_return_raw_sibling_derived_ptr_as_base, allow_raw_pointer<ret_val>()); |