diff options
author | Chad Austin <chad@imvu.com> | 2013-05-08 18:43:08 -0700 |
---|---|---|
committer | Chad Austin <chad@imvu.com> | 2013-05-17 12:58:01 -0700 |
commit | 0ef792b006a82a0071fa70f83661dbf0ef04b18d (patch) | |
tree | da0fa19a17b771dff0aed05c5826d6bb42cdb984 | |
parent | 61a7dea5d49d8b190cad04e0f7b34a94704ece01 (diff) |
Implement an autorelease pool in embind.
-rw-r--r-- | src/embind/embind.js | 51 | ||||
-rw-r--r-- | tests/embind/embind.test.js | 91 |
2 files changed, 139 insertions, 3 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js index 5751bf52..ff402885 100644 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -1114,9 +1114,13 @@ ClassHandle.prototype.isAliasOf = function(other) { return leftClass === rightClass && left === right; }; +function throwInstanceAlreadyDeleted(obj) { + throwBindingError(getInstanceTypeName(obj) + ' instance already deleted'); +} + ClassHandle.prototype.clone = function() { if (!this.$$.ptr) { - throwBindingError(getInstanceTypeName(this) + ' instance already deleted'); + throwInstanceAlreadyDeleted(this); } var clone = Object.create(Object.getPrototypeOf(this), { @@ -1138,9 +1142,12 @@ function runDestructor(handle) { } } -ClassHandle.prototype['delete'] = function() { +ClassHandle.prototype['delete'] = function ClassHandle_delete() { if (!this.$$.ptr) { - throwBindingError(getInstanceTypeName(this) + ' instance already deleted'); + throwInstanceAlreadyDeleted(this); + } + if (this.$$.deleteScheduled) { + throwBindingError('Object already scheduled for deletion'); } this.$$.count.value -= 1; @@ -1150,6 +1157,44 @@ ClassHandle.prototype['delete'] = function() { this.$$.smartPtr = undefined; this.$$.ptr = undefined; }; + +var deletionQueue = []; + +ClassHandle.prototype['isDeleted'] = function isDeleted() { + return !this.$$.ptr; +}; + +ClassHandle.prototype['deleteLater'] = function deleteLater() { + if (!this.$$.ptr) { + throwInstanceAlreadyDeleted(this); + } + if (this.$$.deleteScheduled) { + throwBindingError('Object already scheduled for deletion'); + } + deletionQueue.push(this); + if (deletionQueue.length === 1 && delayFunction) { + delayFunction(flushPendingDeletes); + } + this.$$.deleteScheduled = true; + return this; +}; + +function flushPendingDeletes() { + while (deletionQueue.length) { + var obj = deletionQueue.pop(); + obj.$$.deleteScheduled = false; + obj['delete'](); + } +} +Module['flushPendingDeletes'] = flushPendingDeletes; + +var delayFunction; +Module['setDelayFunction'] = function setDelayFunction(fn) { + delayFunction = fn; + if (deletionQueue.length && delayFunction) { + delayFunction(flushPendingDeletes); + } +}; function RegisteredClass( name, diff --git a/tests/embind/embind.test.js b/tests/embind/embind.test.js index 0c4dc174..8d6dd044 100644 --- a/tests/embind/embind.test.js +++ b/tests/embind/embind.test.js @@ -5,6 +5,8 @@ module({ var CheckForLeaks = fixture("check for leaks", function() { this.setUp(function() { + cm.setDelayFunction(undefined); + if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well! cm._mallocDebug(2); assert.equal(0, cm.count_emval_handles()); @@ -12,6 +14,7 @@ module({ } }); this.tearDown(function() { + cm.flushPendingDeletes(); if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well! cm._mallocAssertAllMemoryFree(); assert.equal(0, cm.count_emval_handles()); @@ -1772,6 +1775,94 @@ module({ assert.deepEqual([1000, 100, 10, 1], [].slice.call(views[2])); }); }); + + BaseFixture.extend("delete pool", function() { + test("can delete objects later", function() { + var v = new cm.ValHolder({}); + v.deleteLater(); + assert.deepEqual({}, v.getVal()); + cm.flushPendingDeletes(); + assert.throws(cm.BindingError, function() { + v.getVal(); + }); + }); + + test("calling deleteLater twice is an error", function() { + var v = new cm.ValHolder({}); + v.deleteLater(); + assert.throws(cm.BindingError, function() { + v.deleteLater(); + }); + }); + + test("deleteLater returns the object", function() { + var v = (new cm.ValHolder({})).deleteLater(); + assert.deepEqual({}, v.getVal()); + }); + + test("deleteLater throws if object is already deleted", function() { + var v = new cm.ValHolder({}); + v.delete(); + assert.throws(cm.BindingError, function() { + v.deleteLater(); + }); + }); + + test("delete throws if object is already scheduled for deletion", function() { + var v = new cm.ValHolder({}); + v.deleteLater(); + assert.throws(cm.BindingError, function() { + v.delete(); + }); + }); + + test("deleteLater invokes delay function", function() { + var runLater; + cm.setDelayFunction(function(fn) { + runLater = fn; + }); + + var v = new cm.ValHolder({}); + assert.false(runLater); + v.deleteLater(); + assert.true(runLater); + assert.false(v.isDeleted()); + runLater(); + assert.true(v.isDeleted()); + }); + + test("deleteLater twice invokes delay function once", function() { + var count = 0; + var runLater; + cm.setDelayFunction(function(fn) { + ++count; + runLater = fn; + }); + + (new cm.ValHolder({})).deleteLater(); + (new cm.ValHolder({})).deleteLater(); + assert.equal(1, count); + runLater(); + (new cm.ValHolder({})).deleteLater(); + assert.equal(2, count); + }); + + test('The delay function is immediately invoked if the deletion queue is not empty', function() { + (new cm.ValHolder({})).deleteLater(); + var count = 0; + cm.setDelayFunction(function(fn) { + ++count; + }); + assert.equal(1, count); + }); + + // The idea is that an interactive application would + // periodically flush the deleteLater queue by calling + // + // setDelayFunction(function(fn) { + // setTimeout(fn, 0); + // }); + }); }); /* global run_all_tests */ |