aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2013-05-10 23:05:15 -0700
committerChad Austin <chad@imvu.com>2013-05-17 12:58:44 -0700
commitfec256e358077534355250f08ddd47f19ec80703 (patch)
tree839aefa8d0b3e59536ec30934112af3a90d66e27
parent40037cce12465baa90e642c1b3c48dcc7d1ed237 (diff)
Add support for multiple overloaded smart pointer constructors.
-rw-r--r--system/include/emscripten/bind.h9
-rw-r--r--tests/embind/embind.test.js9
-rw-r--r--tests/embind/embind_test.cpp31
3 files changed, 43 insertions, 6 deletions
diff --git a/system/include/emscripten/bind.h b/system/include/emscripten/bind.h
index 3c5a8266..cd465e45 100644
--- a/system/include/emscripten/bind.h
+++ b/system/include/emscripten/bind.h
@@ -894,16 +894,17 @@ namespace emscripten {
policies...);
}
- template<typename... Args, typename... Policies>
- class_& constructor(ClassType* (*factory)(Args...), Policies...) {
+ template<typename... Args, typename ReturnType, typename... Policies>
+ class_& constructor(ReturnType (*factory)(Args...), Policies...) {
using namespace internal;
- typename WithPolicies<Policies...>::template ArgTypeList<AllowedRawPointer<ClassType>, Args...> args;
+ // TODO: allows all raw pointers... policies need a rethink
+ typename WithPolicies<allow_raw_pointers, Policies...>::template ArgTypeList<ReturnType, Args...> args;
_embind_register_class_constructor(
TypeID<ClassType>::get(),
args.count,
args.types,
- reinterpret_cast<GenericFunction>(&Invoker<ClassType*, Args...>::invoke),
+ reinterpret_cast<GenericFunction>(&Invoker<ReturnType, Args...>::invoke),
reinterpret_cast<GenericFunction>(factory));
return *this;
}
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index 23d50fe1..e60e1ab3 100644
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -684,6 +684,15 @@ module({
c.delete();
});
+ test("access multiple smart ptr ctors", function() {
+ var a = new cm.MultipleSmartCtors(10);
+ assert.equal(a.WhichCtorCalled(), 1);
+ var b = new cm.MultipleCtors(20, 20);
+ assert.equal(b.WhichCtorCalled(), 2);
+ a.delete();
+ b.delete();
+ });
+
test("wrong number of constructor arguments throws", function() {
assert.throws(cm.BindingError, function() { new cm.MultipleCtors(); });
assert.throws(cm.BindingError, function() { new cm.MultipleCtors(1,2,3,4); });
diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp
index 72506346..3561b8a1 100644
--- a/tests/embind/embind_test.cpp
+++ b/tests/embind/embind_test.cpp
@@ -1865,7 +1865,7 @@ int overloaded_function(int i, int j) {
class MultipleCtors {
public:
- int value;
+ int value = 0;
MultipleCtors(int i) {
value = 1;
@@ -1888,6 +1888,25 @@ public:
}
};
+class MultipleSmartCtors {
+public:
+ int value = 0;
+
+ MultipleSmartCtors(int i) {
+ value = 1;
+ assert(i == 10);
+ }
+ MultipleSmartCtors(int i, int j) {
+ value = 2;
+ assert(i == 20);
+ assert(j == 20);
+ }
+
+ int WhichCtorCalled() const {
+ return value;
+ }
+};
+
class MultipleOverloads {
public:
MultipleOverloads() {}
@@ -1994,7 +2013,15 @@ EMSCRIPTEN_BINDINGS(overloads) {
.constructor<int>()
.constructor<int, int>()
.constructor<int, int, int>()
- .function("WhichCtorCalled", &MultipleCtors::WhichCtorCalled);
+ .function("WhichCtorCalled", &MultipleCtors::WhichCtorCalled)
+ ;
+
+ class_<MultipleSmartCtors>("MultipleSmartCtors")
+ .smart_ptr<std::shared_ptr<MultipleSmartCtors>>()
+ .constructor(&std::make_shared<MultipleSmartCtors, int>)
+ .constructor(&std::make_shared<MultipleSmartCtors, int, int>)
+ .function("WhichCtorCalled", &MultipleSmartCtors::WhichCtorCalled)
+ ;
class_<MultipleOverloads>("MultipleOverloads")
.constructor<>()