diff options
author | Jukka Jylanki <jjylanki@imvu.com> | 2013-04-02 12:26:32 +0300 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-04-12 14:27:10 +0300 |
commit | e8afd54698387cb38dafdea9317c764440063909 (patch) | |
tree | 9b759a55a253f3d11d065674755c5a0c7fd55620 /tests | |
parent | def5681c4f191c5e273765624b200619a50cf805 (diff) |
Add support for binding multiple class constructors with embind. The ctors must have different number of arguments each.
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/embind/embind.test.js | 20 | ||||
-rw-r--r-- | tests/embind/embind_test.cpp | 31 |
2 files changed, 49 insertions, 2 deletions
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 94d935f2..46b7646d 100755 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -589,8 +589,24 @@ module({ assert.throws(TypeError, function() { cm.long_to_string(2147483648); }); assert.throws(TypeError, function() { cm.unsigned_long_to_string(-1); }); assert.throws(TypeError, function() { cm.unsigned_long_to_string(4294967296); }); + }); - }); + test("access multiple class ctors", function() { + var a = new cm.MultipleCtors(10); + assert.equal(a.WhichCtorCalled(), 1); + var b = new cm.MultipleCtors(20, 20); + assert.equal(b.WhichCtorCalled(), 2); + var c = new cm.MultipleCtors(30, 30, 30); + assert.equal(c.WhichCtorCalled(), 3); + a.delete(); + b.delete(); + c.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); }); + }); /* test("can get templated member classes then call its member functions", function() { @@ -1424,7 +1440,7 @@ module({ test("unbound constructor argument", function() { assertMessage( function() { - new cm.HasConstructorUsingUnboundArgument; + new cm.HasConstructorUsingUnboundArgument(1); }, 'Cannot construct HasConstructorUsingUnboundArgument due to unbound types: UnboundClass'); }); diff --git a/tests/embind/embind_test.cpp b/tests/embind/embind_test.cpp index 470309aa..f23b9152 100644 --- a/tests/embind/embind_test.cpp +++ b/tests/embind/embind_test.cpp @@ -1314,6 +1314,31 @@ std::string unsigned_long_to_string(unsigned long val) { return str;
}
+class MultipleCtors {
+public:
+ int value;
+
+ MultipleCtors(int i) {
+ value = 1;
+ assert(i == 10);
+ }
+ MultipleCtors(int i, int j) {
+ value = 2;
+ assert(i == 20);
+ assert(j == 20);
+ }
+ MultipleCtors(int i, int j, int k) {
+ value = 3;
+ assert(i == 30);
+ assert(j == 30);
+ assert(k == 30);
+ }
+
+ int WhichCtorCalled() const {
+ return value;
+ }
+};
+
EMSCRIPTEN_BINDINGS(tests) {
register_js_interface();
@@ -1759,6 +1784,12 @@ EMSCRIPTEN_BINDINGS(tests) { function("unsigned_int_to_string", &unsigned_int_to_string);
function("long_to_string", &long_to_string);
function("unsigned_long_to_string", &unsigned_long_to_string);
+
+ class_<MultipleCtors>("MultipleCtors")
+ .constructor<int>()
+ .constructor<int, int>()
+ .constructor<int, int, int>()
+ .function("WhichCtorCalled", &MultipleCtors::WhichCtorCalled);
}
// tests for out-of-order registration
|