Skip to content

Commit

Permalink
Make sure all data is cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Aug 23, 2014
1 parent be24ab1 commit 3e3a96a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/progress.js
Expand Up @@ -17,6 +17,14 @@ Promise.prototype._progress = function Promise$_progress(progressValue) {

};

Promise.prototype._clearFirstHandlerData$Base =
Promise.prototype._clearFirstHandlerData;
Promise.prototype._clearFirstHandlerData =
function Promise$_clearFirstHandlerData() {
this._clearFirstHandlerData$Base();
this._progressHandler0 = void 0;
};

Promise.prototype._progressHandlerAt =
function Promise$_progressHandlerAt(index) {
return index === 0
Expand Down
13 changes: 12 additions & 1 deletion src/promise.js
Expand Up @@ -915,15 +915,26 @@ Promise.prototype._queueGC = function Promise$_queueGC() {
};

Promise.prototype._gc = function Promise$gc() {
var len = this._length() * CALLBACK_SIZE;
var len = this._length() * CALLBACK_SIZE - CALLBACK_SIZE;
ASSERT(!(len in this));
for (var i = 0; i < len; i++) {
ASSERT(i in this);
//Delete is cool on array indexes
delete this[i];
}
this._clearFirstHandlerData();
this._setLength(0);
this._unsetGcQueued();
};

Promise.prototype._clearFirstHandlerData =
function Promise$_clearFirstHandlerData() {
this._fulfillmentHandler0 = void 0;
this._rejectionHandler0 = void 0;
this._promise0 = void 0;
this._receiver0 = void 0;
};

Promise.prototype._queueSettleAt = function Promise$_queueSettleAt(index) {
ASSERT(typeof index === "number");
ASSERT(index >= 0);
Expand Down
77 changes: 43 additions & 34 deletions test/mocha/reused_promise.js
Expand Up @@ -9,53 +9,62 @@ var pending = adapter.pending;
var Promise = adapter;



describe("If promise is reused to get at the value many times over the course of application", function() {
var three = Promise.fulfilled(3);

specify("It will not keep references to anything", function(done){
specify("It will not keep references to anything", function(done) {
var three = Promise.fulfilled(3);
var fn = function(){};
var l = 256;
while(l--) {
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
var len;
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);

for (var i = 0; i < 1000; ++i) {
if (!(i in three)) {
break;
}
}
len = i;
assert(len > 0);

setTimeout(function(){
for( var i = 0; i < three._length() - 5; ++i) {
assert( three[i] === void 0 );

setTimeout(function() {
for (var i = 0; i < len; ++i) {
assert((!(i in three)));
}
done();
}, 13);
});

specify("It will be able to reuse the space", function(done) {
var three = Promise.fulfilled(3);
var fn = function(){};
var prom = three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);

var l = 256;
while(l--) {
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
three.then(fn, fn, fn);
}


assert( three._promise0 === prom );
assert( three._fulfillmentHandler0 === fn );
assert( three._rejectionHandler0 === fn );
assert( three._progressHandler0 === fn );
assert( three._receiver0 === void 0 );

three.then(function(){
setTimeout(function(){
assert(three._length() === 0);
done();
}, 13);
});
assert(three._promise0 === prom);
assert(three._fulfillmentHandler0 === fn);
assert(three._rejectionHandler0 === fn);
assert(three._progressHandler0 === fn);
assert(three._receiver0 === void 0);
setTimeout(function() {
assert(three._promise0 === void 0);
assert(three._fulfillmentHandler0 === void 0);
assert(three._rejectionHandler0 === void 0);
assert(three._progressHandler0 === void 0);
assert(three._receiver0 === void 0);
var prom = three.then(fn, fn, fn);
assert(three._promise0 === prom);
assert(three._fulfillmentHandler0 === fn);
assert(three._rejectionHandler0 === fn);
assert(three._progressHandler0 === fn);
assert(three._receiver0 === void 0);
done();
}, 13);
});
});

0 comments on commit 3e3a96a

Please sign in to comment.