aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChad Austin <chad@imvu.com>2014-05-09 13:20:58 -0700
committerBruce Mitchener <bruce.mitchener@gmail.com>2014-05-21 22:59:16 +0700
commit09b00f5cd39dc986bc5797c5eb9920af04b4ac59 (patch)
tree3ef7f74426e0ce45b8211498b5145f26ac79521f
parente25a0f0af05a12705bacf35e4d94bcdc9cd334e9 (diff)
Prevent some common mistakes when calling parent constructors and destructors
-rw-r--r--src/embind/embind.js8
-rw-r--r--tests/embind/embind.test.js29
2 files changed, 37 insertions, 0 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js
index 27dfa928..6f905d55 100644
--- a/src/embind/embind.js
+++ b/src/embind/embind.js
@@ -1823,6 +1823,10 @@ function __embind_create_inheriting_constructor(constructorName, wrapperType, pr
// It's a little nasty that we're modifying the wrapper prototype here.
wrapperPrototype.__construct = function __construct() {
+ if (this === wrapperPrototype) {
+ throwBindingError("Pass correct 'this' to __construct");
+ }
+
var inner = baseConstructor.__$implement.apply(
undefined,
[this].concat(arraySlice.call(arguments)));
@@ -1835,6 +1839,10 @@ function __embind_create_inheriting_constructor(constructorName, wrapperType, pr
};
wrapperPrototype.__destruct = function __destruct() {
+ if (this === wrapperPrototype) {
+ throwBindingError("Pass correct 'this' to __destruct");
+ }
+
unregisterInheritedInstance(registeredClass, this.$$.ptr);
};
diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js
index bc0a6154..067b3f60 100644
--- a/tests/embind/embind.test.js
+++ b/tests/embind/embind.test.js
@@ -1800,6 +1800,35 @@ module({
rv.delete();
});
+ test("can instantiate two wrappers with constructors", function() {
+ var parent = cm.HeldAbstractClass;
+ var C = parent.extend("C", {
+ __construct: function() {
+ this.__parent.__construct.call(this);
+ },
+ method: function() {
+ }
+ });
+ var a = new C;
+ var b = new C;
+ a.delete();
+ b.delete();
+ });
+
+ test("incorrectly calling parent is an error", function() {
+ var parent = cm.HeldAbstractClass;
+ var C = parent.extend("C", {
+ __construct: function() {
+ this.__parent.__construct();
+ },
+ method: function() {
+ }
+ });
+ assert.throws(cm.BindingError, function() {
+ new C;
+ });
+ });
+
test("deleteLater() works for JavaScript implementations", function() {
var parent = cm.HeldAbstractClass;
var C = parent.extend("C", {