Promises: Remove additional array for storing deferred objects

There are 2 possible states for the deferred symbol --
1) UNDEFINED -- This is the zero state, no deferred object is attached
   to this symbol. When we want to add a new deferred we directly
   attach it to this symbol.
2) symbol with attached deferred object -- New deferred objects are
   not attached to this symbol, but instead they are directly attached
   to the resolve, reject callback arrays. At this point, the deferred
   symbol's state is stale, and the deferreds should be read from the
   reject, resolve callbacks.

BUG=v8:5046

Review-Url: https://codereview.chromium.org/2018913004
Cr-Commit-Position: refs/heads/master@{#36623}
This commit is contained in:
gsathya 2016-05-31 11:49:09 -07:00 committed by Commit bot
parent 72f7d9a294
commit e3bd4a396b

View File

@ -115,7 +115,18 @@ function PromiseSet(promise, status, value) {
// therefore we can just push the new callback to the existing array. // therefore we can just push the new callback to the existing array.
SET_PRIVATE(promise, promiseFulfillReactionsSymbol, UNDEFINED); SET_PRIVATE(promise, promiseFulfillReactionsSymbol, UNDEFINED);
SET_PRIVATE(promise, promiseRejectReactionsSymbol, UNDEFINED); SET_PRIVATE(promise, promiseRejectReactionsSymbol, UNDEFINED);
// There are 2 possible states for this symbol --
// 1) UNDEFINED -- This is the zero state, no deferred object is
// attached to this symbol. When we want to add a new deferred we
// directly attach it to this symbol.
// 2) symbol with attached deferred object -- New deferred objects
// are not attached to this symbol, but instead they are directly
// attached to the resolve, reject callback arrays. At this point,
// the deferred symbol's state is stale, and the deferreds should be
// read from the reject, resolve callbacks.
SET_PRIVATE(promise, promiseDeferredReactionsSymbol, UNDEFINED); SET_PRIVATE(promise, promiseDeferredReactionsSymbol, UNDEFINED);
return promise; return promise;
} }
@ -162,8 +173,8 @@ function PromiseEnqueue(value, tasks, deferreds, status) {
%DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name }); %DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name });
} }
if (IS_ARRAY(tasks)) { if (IS_ARRAY(tasks)) {
for (var i = 0; i < tasks.length; i += 1) { for (var i = 0; i < tasks.length; i += 2) {
PromiseHandle(value, tasks[i], deferreds[i]); PromiseHandle(value, tasks[i], tasks[i + 1]);
} }
} else { } else {
PromiseHandle(value, tasks, deferreds); PromiseHandle(value, tasks, deferreds);
@ -189,23 +200,20 @@ function PromiseAttachCallbacks(promise, deferred, onResolve, onReject) {
} else if (!IS_ARRAY(maybeResolveCallbacks)) { } else if (!IS_ARRAY(maybeResolveCallbacks)) {
var resolveCallbacks = new InternalArray(); var resolveCallbacks = new InternalArray();
var rejectCallbacks = new InternalArray(); var rejectCallbacks = new InternalArray();
var deferreds = new InternalArray(); var existingDeferred = GET_PRIVATE(promise, promiseDeferredReactionsSymbol);
resolveCallbacks.push(maybeResolveCallbacks); resolveCallbacks.push(
rejectCallbacks.push(GET_PRIVATE(promise, promiseRejectReactionsSymbol)); maybeResolveCallbacks, existingDeferred, onResolve, deferred);
deferreds.push(GET_PRIVATE(promise, promiseDeferredReactionsSymbol)); rejectCallbacks.push(GET_PRIVATE(promise, promiseRejectReactionsSymbol),
existingDeferred,
resolveCallbacks.push(onResolve); onReject,
rejectCallbacks.push(onReject); deferred);
deferreds.push(deferred);
SET_PRIVATE(promise, promiseFulfillReactionsSymbol, resolveCallbacks); SET_PRIVATE(promise, promiseFulfillReactionsSymbol, resolveCallbacks);
SET_PRIVATE(promise, promiseRejectReactionsSymbol, rejectCallbacks); SET_PRIVATE(promise, promiseRejectReactionsSymbol, rejectCallbacks);
SET_PRIVATE(promise, promiseDeferredReactionsSymbol, deferreds);
} else { } else {
maybeResolveCallbacks.push(onResolve); maybeResolveCallbacks.push(onResolve, deferred);
GET_PRIVATE(promise, promiseRejectReactionsSymbol).push(onReject); GET_PRIVATE(promise, promiseRejectReactionsSymbol).push(onReject, deferred);
GET_PRIVATE(promise, promiseDeferredReactionsSymbol).push(deferred);
} }
} }
@ -511,8 +519,8 @@ function PromiseHasUserDefinedRejectHandlerRecursive(promise) {
if (!IS_ARRAY(queue)) { if (!IS_ARRAY(queue)) {
return PromiseHasUserDefinedRejectHandlerCheck(queue, deferreds); return PromiseHasUserDefinedRejectHandlerCheck(queue, deferreds);
} else { } else {
for (var i = 0; i < queue.length; i += 1) { for (var i = 0; i < queue.length; i += 2) {
if (PromiseHasUserDefinedRejectHandlerCheck(queue[i], deferreds[i])) { if (PromiseHasUserDefinedRejectHandlerCheck(queue[i], queue[i + 1])) {
return true; return true;
} }
} }