diff options
author | Andy Friesen <andy@imvu.com> | 2013-01-31 10:10:49 -0800 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-04-12 14:23:47 +0300 |
commit | 3b6ea45565ae17f32d77933b505a6ccfe7a8967f (patch) | |
tree | 19c81bafb8888a9c7f43cb5deb2a7fade6b3d724 | |
parent | 5b3d12c90558f0472c374349c5a4cdce474832a4 (diff) |
emscripten::internal::optional is now copyable.
Fix JSInterface::JSInterface copy constructor to take a const&
-rwxr-xr-x | system/include/emscripten/bind.h | 31 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 21 | ||||
-rw-r--r-- | tests/embind/embind_test.js | 5 |
3 files changed, 49 insertions, 8 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h index 70a5b60c..99b29e9c 100755 --- a/system/include/emscripten/bind.h +++ b/system/include/emscripten/bind.h @@ -817,13 +817,22 @@ namespace emscripten { } } - optional(const optional&) = delete; + optional(const optional& rhs) + : initialized(false) + { + *this = rhs; + } T& operator*() { assert(initialized); return *get(); } + const T& operator*() const { + assert(initialized); + return *get(); + } + explicit operator bool() const { return initialized; } @@ -837,12 +846,14 @@ namespace emscripten { return *this; } - optional& operator=(optional& o) { + optional& operator=(const optional& o) { if (initialized) { get()->~T(); } - new(get()) T(*o); - initialized = true; + if (o.initialized) { + new(get()) T(*o); + } + initialized = o.initialized; } private: @@ -850,6 +861,10 @@ namespace emscripten { return reinterpret_cast<T*>(&data); } + T const* get() const { + return reinterpret_cast<T const*>(&data); + } + bool initialized; typename std::aligned_storage<sizeof(T)>::type data; }; @@ -865,9 +880,9 @@ namespace emscripten { initialize(handle); } - JSInterface(JSInterface& obj) { - jsobj = obj.jsobj; - } + JSInterface(const JSInterface& obj) + : jsobj(obj.jsobj) + {} template<typename ReturnType, typename... Args> ReturnType call(const char* name, Args... args) { @@ -875,7 +890,7 @@ namespace emscripten { return Caller<ReturnType, Args...>::call(*jsobj, name, args...); } - static std::shared_ptr<JSInterface> cloneToSharedPtr(JSInterface& i) { + static std::shared_ptr<JSInterface> cloneToSharedPtr(const JSInterface& i) { return std::make_shared<JSInterface>(i); } diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index dc052d1a..485cdfcd 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -233,6 +233,24 @@ void emval_test_call_function(val v, int i, float f, TupleVector tv, StructVecto v(i, f, tv, sv);
}
+void optional_test_copy() {
+ using emscripten::internal::optional;
+
+ optional<int> foo = 22;
+ optional<int> bar(foo);
+
+ return bool(bar);
+}
+
+void optional_test_copy2() {
+ using emscripten::internal::optional;
+
+ optional<int> foo;
+ optional<int> bar(foo);
+
+ return bool(bar);
+}
+
EMSCRIPTEN_BINDINGS(([]() {
function("mallinfo", &emval_test_mallinfo);
@@ -337,4 +355,7 @@ EMSCRIPTEN_BINDINGS(([]() { function("emval_test_call_method3", &emval_test_call_method3);
function("emval_test_call_function", &emval_test_call_function);
+
+ function('optional_test_copy', &optional_test_copy);
+ function('optional_test_copy2', &optional_test_copy2);
}));
diff --git a/tests/embind/embind_test.js b/tests/embind/embind_test.js index 8c61553b..9ba6a48f 100644 --- a/tests/embind/embind_test.js +++ b/tests/embind/embind_test.js @@ -390,4 +390,9 @@ module({ assert.deepEqual(['called'], calls); }, }); + + test('emscripten::internal::optional', function () { + assert.true(cm.optional_test_copy()); + assert.false(cm.optional_test_copy2()); + }); }); |