2013-11-27 17:21:40 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2015-12-02 20:35:20 +00:00
|
|
|
// Flags: --allow-natives-syntax --harmony-tostring
|
2013-11-27 17:21:40 +00:00
|
|
|
|
2014-07-07 09:47:43 +00:00
|
|
|
// Make sure we don't rely on functions patchable by monkeys.
|
|
|
|
var call = Function.prototype.call.call.bind(Function.prototype.call)
|
2014-10-21 17:21:32 +00:00
|
|
|
var getOwnPropertyNames = Object.getOwnPropertyNames;
|
|
|
|
var defineProperty = Object.defineProperty;
|
2015-02-23 18:10:07 +00:00
|
|
|
var numberPrototype = Number.prototype;
|
|
|
|
var symbolIterator = Symbol.iterator;
|
2014-10-21 17:21:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
// Test before clearing global (fails otherwise)
|
|
|
|
assertEquals("[object Promise]",
|
|
|
|
Object.prototype.toString.call(new Promise(function() {})));
|
|
|
|
})();
|
|
|
|
|
2014-07-07 09:47:43 +00:00
|
|
|
|
|
|
|
function clear(o) {
|
|
|
|
if (o === null || (typeof o !== 'object' && typeof o !== 'function')) return
|
|
|
|
clear(o.__proto__)
|
|
|
|
var properties = getOwnPropertyNames(o)
|
|
|
|
for (var i in properties) {
|
|
|
|
clearProp(o, properties[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearProp(o, name) {
|
|
|
|
var poisoned = {caller: 0, callee: 0, arguments: 0}
|
|
|
|
try {
|
|
|
|
var x = o[name]
|
|
|
|
o[name] = undefined
|
|
|
|
clear(x)
|
|
|
|
} catch(e) {} // assertTrue(name in poisoned) }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find intrinsics and null them out.
|
|
|
|
var globals = Object.getOwnPropertyNames(this)
|
2015-09-24 21:46:35 +00:00
|
|
|
var whitelist = {
|
|
|
|
Promise: true,
|
|
|
|
TypeError: true,
|
|
|
|
String: true,
|
|
|
|
JSON: true,
|
|
|
|
Error: true,
|
|
|
|
MjsUnitAssertionError: true
|
|
|
|
};
|
|
|
|
|
2014-07-07 09:47:43 +00:00
|
|
|
for (var i in globals) {
|
|
|
|
var name = globals[i]
|
|
|
|
if (name in whitelist || name[0] === name[0].toLowerCase()) delete globals[i]
|
|
|
|
}
|
|
|
|
for (var i in globals) {
|
|
|
|
if (globals[i]) clearProp(this, globals[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
var asyncAssertsExpected = 0;
|
|
|
|
|
|
|
|
function assertAsyncRan() { ++asyncAssertsExpected }
|
|
|
|
|
|
|
|
function assertAsync(b, s) {
|
|
|
|
if (b) {
|
|
|
|
print(s, "succeeded")
|
|
|
|
} else {
|
|
|
|
%AbortJS(s + " FAILED!") // Simply throwing here will have no effect.
|
|
|
|
}
|
|
|
|
--asyncAssertsExpected
|
|
|
|
}
|
|
|
|
|
[es6] refactor Promise resolution
Several changes are included here:
1. Each resolution callback references shared data indicating whether
it has already been resolved or not, as described in 25.4.1.3
http://tc39.github.io/ecma262/#sec-createresolvingfunctions.
Previously this was handled exclusively by the Promise's status,
which does not work correctly with the current chaining behaviour.
2. During fulfillment, When a Promise is resolved with a thenable, the
spec chains the promises together by invoking the thenable's `then`
function with the original Promise's resolve and reject methods (per
section 25.4.2.2, or
http://tc39.github.io/ecma262/#sec-promiseresolvethenablejob, on the
next tick, regardless of whether or not there are pending tasks.
3. Adds a spec compliance fix to ensure that the Promise constructor
is only loaded once when `then()` is called, solving v8:4539 as well.
This involves refactoring PromiseChain to accept a constructor
argument. PromiseChain/PromiseDeferred will hopefully be removed soon,
simplifying the process.
BUG=v8:4162, v8:4539, v8:3237
LOG=N
R=rossberg@chromium.org, littledan@chromium.org, adamk@chromium.org
Review URL: https://codereview.chromium.org/1394463003
Cr-Commit-Position: refs/heads/master@{#32046}
2015-11-17 14:23:46 +00:00
|
|
|
function assertLater(f, name) {
|
|
|
|
assertFalse(f()); // should not be true synchronously
|
|
|
|
++asyncAssertsExpected;
|
|
|
|
var iterations = 0;
|
|
|
|
function runAssertion() {
|
|
|
|
if (f()) {
|
|
|
|
print(name, "succeeded");
|
|
|
|
--asyncAssertsExpected;
|
|
|
|
} else if (iterations++ < 10) {
|
|
|
|
%EnqueueMicrotask(runAssertion);
|
|
|
|
} else {
|
|
|
|
%AbortJS(name + " FAILED!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
%EnqueueMicrotask(runAssertion);
|
|
|
|
}
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
function assertAsyncDone(iteration) {
|
2015-07-21 17:16:57 +00:00
|
|
|
var iteration = iteration || 0;
|
|
|
|
%EnqueueMicrotask(function() {
|
|
|
|
if (asyncAssertsExpected === 0)
|
|
|
|
assertAsync(true, "all")
|
|
|
|
else if (iteration > 10) // Shouldn't take more.
|
[es6] refactor Promise resolution
Several changes are included here:
1. Each resolution callback references shared data indicating whether
it has already been resolved or not, as described in 25.4.1.3
http://tc39.github.io/ecma262/#sec-createresolvingfunctions.
Previously this was handled exclusively by the Promise's status,
which does not work correctly with the current chaining behaviour.
2. During fulfillment, When a Promise is resolved with a thenable, the
spec chains the promises together by invoking the thenable's `then`
function with the original Promise's resolve and reject methods (per
section 25.4.2.2, or
http://tc39.github.io/ecma262/#sec-promiseresolvethenablejob, on the
next tick, regardless of whether or not there are pending tasks.
3. Adds a spec compliance fix to ensure that the Promise constructor
is only loaded once when `then()` is called, solving v8:4539 as well.
This involves refactoring PromiseChain to accept a constructor
argument. PromiseChain/PromiseDeferred will hopefully be removed soon,
simplifying the process.
BUG=v8:4162, v8:4539, v8:3237
LOG=N
R=rossberg@chromium.org, littledan@chromium.org, adamk@chromium.org
Review URL: https://codereview.chromium.org/1394463003
Cr-Commit-Position: refs/heads/master@{#32046}
2015-11-17 14:23:46 +00:00
|
|
|
assertAsync(false, "all... " + asyncAssertsExpected)
|
2015-07-21 17:16:57 +00:00
|
|
|
else
|
|
|
|
assertAsyncDone(iteration + 1)
|
|
|
|
});
|
2013-11-27 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:00:56 +00:00
|
|
|
(function() {
|
|
|
|
assertThrows(function() { Promise(function() {}) }, TypeError)
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
assertTrue(new Promise(function() {}) instanceof Promise)
|
|
|
|
})();
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
|
|
|
assertThrows(function() { new Promise(5) }, TypeError)
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
assertDoesNotThrow(function() { new Promise(function() { throw 5 }) })
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
(new Promise(function() { throw 5 })).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r === 5, "new-throw") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
2013-11-27 17:21:40 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
Promise.accept(5);
|
|
|
|
Promise.accept(5).chain(undefined, assertUnreachable).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 5, "resolved/chain-nohandler") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.reject(5).chain(assertUnreachable, undefined).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r === 5, "rejected/chain-nohandler") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
Promise.accept(5).then(undefined, assertUnreachable).chain(
|
2014-03-18 14:55:43 +00:00
|
|
|
function(x) { assertAsync(x === 5, "resolved/then-nohandler-undefined") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
2014-03-18 15:03:35 +00:00
|
|
|
Promise.accept(6).then(null, assertUnreachable).chain(
|
2014-03-18 14:55:43 +00:00
|
|
|
function(x) { assertAsync(x === 6, "resolved/then-nohandler-null") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.reject(5).then(assertUnreachable, undefined).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable,
|
2014-03-18 14:55:43 +00:00
|
|
|
function(r) { assertAsync(r === 5, "rejected/then-nohandler-undefined") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
Promise.reject(6).then(assertUnreachable, null).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r === 6, "rejected/then-nohandler-null") }
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "resolved/chain") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "resolved/then") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var p1 = Promise.reject(5)
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "rejected/chain") },
|
|
|
|
assertUnreachable
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var p1 = Promise.reject(5)
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "rejected/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
/* TODO(caitp): remove tests once PromiseChain is removed, per bug 3237
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { return x }, assertUnreachable).chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p1, "resolved/chain/chain") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
2015-12-05 08:51:06 +00:00
|
|
|
})();*/
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { return x }, assertUnreachable).then(
|
|
|
|
function(x) { assertAsync(x === 5, "resolved/chain/then") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { return 6 }, assertUnreachable).chain(
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/chain/chain2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { return 6 }, assertUnreachable).then(
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/chain/then2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(function(x) { return x + 1 }, assertUnreachable).chain(
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/then/chain") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(function(x) { return x + 1 }, assertUnreachable).then(
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/then/then") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
|
|
|
p3.then(function(x){ return Promise.accept(x+1) }, assertUnreachable).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 6, "resolved/then/chain2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
|
|
|
p3.then(function(x) { return Promise.accept(x+1) }, assertUnreachable).then(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 6, "resolved/then/then2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { throw 6 }, assertUnreachable).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(function(x) { throw 6 }, assertUnreachable).then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/chain-throw/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(function(x) { throw 6 }, assertUnreachable).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/then-throw/chain") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(function(x) { throw 6 }, assertUnreachable).then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "resolved/thenable/chain") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "resolved/thenable/then") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var p1 = Promise.reject(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "rejected/thenable/chain") },
|
|
|
|
assertUnreachable
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var p1 = Promise.reject(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "rejected/thenable/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/resolve") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2015-12-05 08:51:06 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "then/resolve") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/reject") },
|
|
|
|
assertUnreachable
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(p1)
|
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "then/reject") }
|
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2014-05-06 14:48:34 +00:00
|
|
|
(function() {
|
|
|
|
var deferred = Promise.defer()
|
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = p1.then(1, 2)
|
|
|
|
p2.then(
|
|
|
|
function(x) { assertAsync(x === 5, "then/resolve-non-function") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var deferred = Promise.defer()
|
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = p1.then(1, 2)
|
|
|
|
p2.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "then/reject-non-function") }
|
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/resolve/thenable") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "then/resolve/thenable") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/reject/thenable") },
|
|
|
|
assertUnreachable
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-03-18 15:03:35 +00:00
|
|
|
var p3 = Promise.accept(p2)
|
2013-11-27 17:21:40 +00:00
|
|
|
p3.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "then/reject/thenable") }
|
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/resolve2") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(p2)
|
|
|
|
assertAsyncRan()
|
2015-12-05 08:51:06 +00:00
|
|
|
})(); */
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "then/resolve2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(p2)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "chain/reject2") }
|
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
|
|
|
var p2 = Promise.accept(p1)
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 5, "then/reject2") }
|
|
|
|
)
|
|
|
|
deferred.reject(5)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p2, "chain/resolve/thenable2") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(p2)
|
|
|
|
assertAsyncRan()
|
2015-12-05 08:51:06 +00:00
|
|
|
})(); */
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(5)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred.promise
|
|
|
|
p3.then(
|
|
|
|
function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(p2)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(0)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = p1.chain(function(x) { return p2 }, assertUnreachable)
|
|
|
|
p2.chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r instanceof TypeError, "cyclic/chain") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(0)
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
|
|
|
|
p2.chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r instanceof TypeError, "cyclic/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p = deferred.promise
|
|
|
|
deferred.resolve(p)
|
|
|
|
p.chain(
|
2015-12-05 08:51:06 +00:00
|
|
|
function(x) { assertAsync(x === p, "cyclic/deferred/chain") },
|
|
|
|
assertUnreachable
|
2013-11-27 17:21:40 +00:00
|
|
|
)
|
|
|
|
assertAsyncRan()
|
2015-12-05 08:51:06 +00:00
|
|
|
})();*/
|
2013-11-27 17:21:40 +00:00
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p = deferred.promise
|
|
|
|
deferred.resolve(p)
|
|
|
|
p.then(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") }
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
2015-12-05 08:51:06 +00:00
|
|
|
})();*/
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
Promise.all([]).chain(
|
|
|
|
function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-02-23 18:10:07 +00:00
|
|
|
(function() {
|
|
|
|
function testPromiseAllNonIterable(value) {
|
|
|
|
Promise.all(value).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) {
|
|
|
|
assertAsync(r instanceof TypeError, 'all/non iterable');
|
|
|
|
});
|
|
|
|
assertAsyncRan();
|
|
|
|
}
|
|
|
|
testPromiseAllNonIterable(null);
|
|
|
|
testPromiseAllNonIterable(undefined);
|
|
|
|
testPromiseAllNonIterable({});
|
|
|
|
testPromiseAllNonIterable(42);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var deferred = Promise.defer();
|
|
|
|
var p = deferred.promise;
|
|
|
|
function* f() {
|
|
|
|
yield 1;
|
|
|
|
yield p;
|
|
|
|
yield 3;
|
|
|
|
}
|
|
|
|
Promise.all(f()).chain(
|
|
|
|
function(x) {
|
|
|
|
assertAsync(x.length === 3, "all/resolve/iterable");
|
|
|
|
assertAsync(x[0] === 1, "all/resolve/iterable/0");
|
|
|
|
assertAsync(x[1] === 2, "all/resolve/iterable/1");
|
|
|
|
assertAsync(x[2] === 3, "all/resolve/iterable/2");
|
|
|
|
},
|
|
|
|
assertUnreachable);
|
|
|
|
deferred.resolve(2);
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred1 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred1.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred2 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = deferred2.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred3 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred3.promise
|
|
|
|
Promise.all([p1, p2, p3]).chain(
|
|
|
|
function(x) {
|
|
|
|
assertAsync(x.length === 3, "all/resolve")
|
|
|
|
assertAsync(x[0] === 1, "all/resolve/0")
|
|
|
|
assertAsync(x[1] === 2, "all/resolve/1")
|
|
|
|
assertAsync(x[2] === 3, "all/resolve/2")
|
|
|
|
},
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred1.resolve(1)
|
|
|
|
deferred3.resolve(3)
|
|
|
|
deferred2.resolve(2)
|
|
|
|
assertAsyncRan()
|
|
|
|
assertAsyncRan()
|
|
|
|
assertAsyncRan()
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(2)
|
2014-01-09 13:00:56 +00:00
|
|
|
var p3 = Promise.defer().promise
|
2013-11-27 17:21:40 +00:00
|
|
|
Promise.all([p1, p2, p3]).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(1)
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred1 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred1.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred2 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = deferred2.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred3 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred3.promise
|
|
|
|
Promise.all([p1, p2, p3]).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 2, "all/reject") }
|
|
|
|
)
|
|
|
|
deferred1.resolve(1)
|
|
|
|
deferred3.resolve(3)
|
|
|
|
deferred2.reject(2)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-12-05 08:51:06 +00:00
|
|
|
|
2015-02-23 18:10:07 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
var getCalls = 0;
|
|
|
|
var funcCalls = 0;
|
|
|
|
var nextCalls = 0;
|
|
|
|
defineProperty(numberPrototype, symbolIterator, {
|
|
|
|
get: function() {
|
|
|
|
assertEquals('number', typeof this);
|
|
|
|
getCalls++;
|
|
|
|
return function() {
|
|
|
|
assertEquals('number', typeof this);
|
|
|
|
funcCalls++;
|
|
|
|
var n = this;
|
|
|
|
var i = 0
|
|
|
|
return {
|
|
|
|
next() {
|
|
|
|
nextCalls++;
|
|
|
|
return {value: i++, done: i > n};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all(3).chain(
|
|
|
|
function(x) {
|
|
|
|
assertAsync(x.length === 3, "all/iterable/number/length");
|
|
|
|
assertAsync(x[0] === 0, "all/iterable/number/0");
|
|
|
|
assertAsync(x[1] === 1, "all/iterable/number/1");
|
|
|
|
assertAsync(x[2] === 2, "all/iterable/number/2");
|
|
|
|
},
|
|
|
|
assertUnreachable);
|
|
|
|
delete numberPrototype[symbolIterator];
|
|
|
|
|
|
|
|
assertEquals(getCalls, 1);
|
|
|
|
assertEquals(funcCalls, 1);
|
|
|
|
assertEquals(nextCalls, 3 + 1); // + 1 for {done: true}
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
assertAsyncRan();
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([]).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable,
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(1)
|
|
|
|
var p2 = Promise.accept(2)
|
|
|
|
var p3 = Promise.accept(3)
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([p1, p2, p3]).chain(
|
|
|
|
function(x) { assertAsync(x === 1, "resolved/one") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-03-18 15:03:35 +00:00
|
|
|
var p1 = Promise.accept(1)
|
|
|
|
var p2 = Promise.accept(2)
|
|
|
|
var p3 = Promise.accept(3)
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([0, p1, p2, p3]).chain(
|
|
|
|
function(x) { assertAsync(x === 0, "resolved-const/one") },
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2014-01-09 13:00:56 +00:00
|
|
|
(function() {
|
|
|
|
var deferred1 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred1.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred2 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = deferred2.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred3 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred3.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([p1, p2, p3]).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 3, "one/resolve") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred3.resolve(3)
|
|
|
|
deferred1.resolve(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred.promise
|
2014-03-18 15:03:35 +00:00
|
|
|
var p2 = Promise.accept(2)
|
2014-01-09 13:00:56 +00:00
|
|
|
var p3 = Promise.defer().promise
|
|
|
|
Promise.race([p1, p2, p3]).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 2, "resolved/one") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred.resolve(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred1 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred1.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred2 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = deferred2.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred3 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred3.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([p1, p2, p3]).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
function(x) { assertAsync(x === 3, "one/resolve/reject") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred3.resolve(3)
|
|
|
|
deferred1.reject(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred1 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p1 = deferred1.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred2 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p2 = deferred2.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
var deferred3 = Promise.defer()
|
2013-11-27 17:21:40 +00:00
|
|
|
var p3 = deferred3.promise
|
2014-01-09 13:00:56 +00:00
|
|
|
Promise.race([p1, p2, p3]).chain(
|
2013-11-27 17:21:40 +00:00
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 3, "one/reject/resolve") }
|
|
|
|
)
|
|
|
|
deferred3.reject(3)
|
|
|
|
deferred1.resolve(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
2015-02-23 18:10:07 +00:00
|
|
|
|
|
|
|
(function() {
|
|
|
|
function testPromiseRaceNonIterable(value) {
|
|
|
|
Promise.race(value).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(r) {
|
|
|
|
assertAsync(r instanceof TypeError, 'race/non iterable');
|
|
|
|
});
|
|
|
|
assertAsyncRan();
|
|
|
|
}
|
|
|
|
testPromiseRaceNonIterable(null);
|
|
|
|
testPromiseRaceNonIterable(undefined);
|
|
|
|
testPromiseRaceNonIterable({});
|
|
|
|
testPromiseRaceNonIterable(42);
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var deferred1 = Promise.defer()
|
|
|
|
var p1 = deferred1.promise
|
|
|
|
var deferred2 = Promise.defer()
|
|
|
|
var p2 = deferred2.promise
|
|
|
|
var deferred3 = Promise.defer()
|
|
|
|
var p3 = deferred3.promise
|
|
|
|
function* f() {
|
|
|
|
yield p1;
|
|
|
|
yield p2;
|
|
|
|
yield p3;
|
|
|
|
}
|
|
|
|
Promise.race(f()).chain(
|
|
|
|
function(x) { assertAsync(x === 3, "race/iterable/resolve/reject") },
|
|
|
|
assertUnreachable
|
|
|
|
)
|
|
|
|
deferred3.resolve(3)
|
|
|
|
deferred1.reject(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var deferred1 = Promise.defer()
|
|
|
|
var p1 = deferred1.promise
|
|
|
|
var deferred2 = Promise.defer()
|
|
|
|
var p2 = deferred2.promise
|
|
|
|
var deferred3 = Promise.defer()
|
|
|
|
var p3 = deferred3.promise
|
|
|
|
function* f() {
|
|
|
|
yield p1;
|
|
|
|
yield p2;
|
|
|
|
yield p3;
|
|
|
|
}
|
|
|
|
Promise.race(f()).chain(
|
|
|
|
assertUnreachable,
|
|
|
|
function(x) { assertAsync(x === 3, "race/iterable/reject/resolve") }
|
|
|
|
)
|
|
|
|
deferred3.reject(3)
|
|
|
|
deferred1.resolve(1)
|
|
|
|
assertAsyncRan()
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
var getCalls = 0;
|
|
|
|
var funcCalls = 0;
|
|
|
|
var nextCalls = 0;
|
|
|
|
defineProperty(numberPrototype, symbolIterator, {
|
|
|
|
get: function() {
|
|
|
|
assertEquals('number', typeof this);
|
|
|
|
getCalls++;
|
|
|
|
return function() {
|
|
|
|
assertEquals('number', typeof this);
|
|
|
|
funcCalls++;
|
|
|
|
var n = this;
|
|
|
|
var i = 0
|
|
|
|
return {
|
|
|
|
next() {
|
|
|
|
nextCalls++;
|
|
|
|
return {value: i++, done: i > n};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.race(3).chain(
|
|
|
|
function(x) {
|
|
|
|
assertAsync(x === 0, "race/iterable/number");
|
|
|
|
},
|
|
|
|
assertUnreachable);
|
|
|
|
delete numberPrototype[symbolIterator];
|
|
|
|
|
|
|
|
assertEquals(getCalls, 1);
|
|
|
|
assertEquals(funcCalls, 1);
|
|
|
|
assertEquals(nextCalls, 3 + 1); // + 1 for {done: true}
|
|
|
|
assertAsyncRan();
|
|
|
|
})();
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
(function() {
|
|
|
|
var log
|
|
|
|
function MyPromise(resolver) {
|
|
|
|
log += "n"
|
2014-01-09 13:00:56 +00:00
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
resolver(
|
|
|
|
function(x) { log += "x" + x; resolve(x) },
|
|
|
|
function(r) { log += "r" + r; reject(r) }
|
|
|
|
)
|
|
|
|
})
|
|
|
|
promise.__proto__ = MyPromise.prototype
|
|
|
|
return promise
|
2013-11-27 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MyPromise.__proto__ = Promise
|
2014-01-09 13:00:56 +00:00
|
|
|
MyPromise.defer = function() {
|
2013-11-27 17:21:40 +00:00
|
|
|
log += "d"
|
2014-07-07 09:47:43 +00:00
|
|
|
return call(this.__proto__.defer, this)
|
2013-11-27 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MyPromise.prototype.__proto__ = Promise.prototype
|
|
|
|
MyPromise.prototype.chain = function(resolve, reject) {
|
2014-01-09 13:00:56 +00:00
|
|
|
log += "c"
|
2014-07-07 09:47:43 +00:00
|
|
|
return call(this.__proto__.__proto__.chain, this, resolve, reject)
|
2013-11-27 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log = ""
|
|
|
|
var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
|
|
|
|
var p2 = new MyPromise(function(resolve, reject) { reject(2) })
|
2014-01-09 13:00:56 +00:00
|
|
|
var d3 = MyPromise.defer()
|
|
|
|
assertTrue(d3.promise instanceof Promise, "subclass/instance")
|
|
|
|
assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
|
2013-11-27 17:21:40 +00:00
|
|
|
assertTrue(log === "nx1nr2dn", "subclass/create")
|
|
|
|
|
|
|
|
log = ""
|
2014-01-09 13:00:56 +00:00
|
|
|
var p4 = MyPromise.resolve(4)
|
|
|
|
var p5 = MyPromise.reject(5)
|
2015-10-05 22:21:06 +00:00
|
|
|
assertTrue(p4 instanceof MyPromise, "subclass/instance4")
|
2014-01-09 13:00:56 +00:00
|
|
|
assertTrue(p4 instanceof MyPromise, "subclass/instance-my4")
|
2015-10-05 22:21:06 +00:00
|
|
|
assertTrue(p5 instanceof MyPromise, "subclass/instance5")
|
2014-01-09 13:00:56 +00:00
|
|
|
assertTrue(p5 instanceof MyPromise, "subclass/instance-my5")
|
2013-11-27 17:21:40 +00:00
|
|
|
d3.resolve(3)
|
|
|
|
assertTrue(log === "nx4nr5x3", "subclass/resolve")
|
|
|
|
|
|
|
|
log = ""
|
2014-01-09 13:00:56 +00:00
|
|
|
var d6 = MyPromise.defer()
|
|
|
|
d6.promise.chain(function(x) {
|
|
|
|
return new Promise(function(resolve) { resolve(x) })
|
|
|
|
}).chain(function() {})
|
2013-11-27 17:21:40 +00:00
|
|
|
d6.resolve(6)
|
2014-01-09 13:00:56 +00:00
|
|
|
assertTrue(log === "dncncnx6", "subclass/chain")
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
log = ""
|
2014-03-18 15:03:35 +00:00
|
|
|
Promise.all([11, Promise.accept(12), 13, MyPromise.accept(14), 15, 16])
|
2015-10-05 22:21:06 +00:00
|
|
|
|
|
|
|
assertTrue(log === "nx14", "subclass/all/arg")
|
2013-11-27 17:21:40 +00:00
|
|
|
|
|
|
|
log = ""
|
2014-03-18 15:03:35 +00:00
|
|
|
MyPromise.all([21, Promise.accept(22), 23, MyPromise.accept(24), 25, 26])
|
2015-10-05 22:21:06 +00:00
|
|
|
assertTrue(log === "nx24nnx21nnnnx23nnnx25nnx26n", "subclass/all/self")
|
2013-11-27 17:21:40 +00:00
|
|
|
})();
|
|
|
|
|
2015-10-05 22:21:06 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
class Pact extends Promise { }
|
|
|
|
class Vow extends Pact { }
|
|
|
|
class Oath extends Vow { }
|
|
|
|
|
|
|
|
Oath.constructor = Vow;
|
|
|
|
|
|
|
|
assertTrue(Pact.resolve(Pact.resolve()).constructor === Pact,
|
|
|
|
"subclass/resolve/own");
|
|
|
|
|
|
|
|
assertTrue(Pact.resolve(Promise.resolve()).constructor === Pact,
|
|
|
|
"subclass/resolve/ancestor");
|
|
|
|
|
|
|
|
assertTrue(Pact.resolve(Vow.resolve()).constructor === Pact,
|
|
|
|
"subclass/resolve/descendant"); var vow = Vow.resolve();
|
|
|
|
|
|
|
|
vow.constructor = Oath;
|
|
|
|
assertTrue(Oath.resolve(vow) === vow,
|
|
|
|
"subclass/resolve/descendant with transplanted own constructor");
|
|
|
|
}());
|
2013-11-27 17:21:40 +00:00
|
|
|
|
[es6] refactor Promise resolution
Several changes are included here:
1. Each resolution callback references shared data indicating whether
it has already been resolved or not, as described in 25.4.1.3
http://tc39.github.io/ecma262/#sec-createresolvingfunctions.
Previously this was handled exclusively by the Promise's status,
which does not work correctly with the current chaining behaviour.
2. During fulfillment, When a Promise is resolved with a thenable, the
spec chains the promises together by invoking the thenable's `then`
function with the original Promise's resolve and reject methods (per
section 25.4.2.2, or
http://tc39.github.io/ecma262/#sec-promiseresolvethenablejob, on the
next tick, regardless of whether or not there are pending tasks.
3. Adds a spec compliance fix to ensure that the Promise constructor
is only loaded once when `then()` is called, solving v8:4539 as well.
This involves refactoring PromiseChain to accept a constructor
argument. PromiseChain/PromiseDeferred will hopefully be removed soon,
simplifying the process.
BUG=v8:4162, v8:4539, v8:3237
LOG=N
R=rossberg@chromium.org, littledan@chromium.org, adamk@chromium.org
Review URL: https://codereview.chromium.org/1394463003
Cr-Commit-Position: refs/heads/master@{#32046}
2015-11-17 14:23:46 +00:00
|
|
|
(function() {
|
|
|
|
var thenCalled = false;
|
|
|
|
|
|
|
|
var resolve;
|
|
|
|
var promise = new Promise(function(res) { resolve = res; });
|
|
|
|
resolve({ then() { thenCalled = true; throw new Error(); } });
|
|
|
|
assertLater(function() { return thenCalled; }, "resolve-with-thenable");
|
|
|
|
});
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var calledWith;
|
|
|
|
|
|
|
|
var resolve;
|
|
|
|
var p1 = (new Promise(function(res) { resolve = res; }));
|
|
|
|
var p2 = p1.then(function(v) {
|
|
|
|
return {
|
|
|
|
then(resolve, reject) { resolve({ then() { calledWith = v }}); }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve({ then(resolve) { resolve(2); } });
|
|
|
|
assertLater(function() { return calledWith === 2; },
|
|
|
|
"resolve-with-thenable2");
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
var p = Promise.resolve();
|
|
|
|
var callCount = 0;
|
|
|
|
defineProperty(p, "constructor", {
|
|
|
|
get: function() { ++callCount; return Promise; }
|
|
|
|
});
|
|
|
|
p.then();
|
|
|
|
assertEquals(1, callCount);
|
|
|
|
})();
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
assertAsyncDone()
|