promises.js 27.8 KB
Newer Older
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// 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.

28
// Flags: --allow-natives-syntax --harmony-tostring
rossberg@chromium.org's avatar
rossberg@chromium.org committed
29

30 31
// Make sure we don't rely on functions patchable by monkeys.
var call = Function.prototype.call.call.bind(Function.prototype.call)
32 33
var getOwnPropertyNames = Object.getOwnPropertyNames;
var defineProperty = Object.defineProperty;
34 35
var numberPrototype = Number.prototype;
var symbolIterator = Symbol.iterator;
36 37 38 39 40 41 42 43


(function() {
  // Test before clearing global (fails otherwise)
  assertEquals("[object Promise]",
      Object.prototype.toString.call(new Promise(function() {})));
})();

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

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)
65 66 67 68 69 70 71 72 73
var whitelist = {
  Promise: true,
  TypeError: true,
  String: true,
  JSON: true,
  Error: true,
  MjsUnitAssertionError: true
};

74 75 76 77 78 79 80 81 82
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])
}


rossberg@chromium.org's avatar
rossberg@chromium.org committed
83 84 85 86 87 88 89 90 91 92 93 94 95
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
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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);
}

rossberg@chromium.org's avatar
rossberg@chromium.org committed
113
function assertAsyncDone(iteration) {
114 115 116 117 118
  var iteration = iteration || 0;
  %EnqueueMicrotask(function() {
    if (asyncAssertsExpected === 0)
      assertAsync(true, "all")
    else if (iteration > 10)  // Shouldn't take more.
119
      assertAsync(false, "all... " + asyncAssertsExpected)
120 121 122
    else
      assertAsyncDone(iteration + 1)
  });
rossberg@chromium.org's avatar
rossberg@chromium.org committed
123 124
}

125 126 127 128 129 130 131 132
(function() {
  assertThrows(function() { Promise(function() {}) }, TypeError)
})();

(function() {
  assertTrue(new Promise(function() {}) instanceof Promise)
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
133 134 135 136 137
(function() {
  assertThrows(function() { new Promise(5) }, TypeError)
})();

(function() {
138 139 140 141 142 143 144 145 146
  assertDoesNotThrow(function() { new Promise(function() { throw 5 }) })
})();

(function() {
  (new Promise(function() { throw 5 })).chain(
    assertUnreachable,
    function(r) { assertAsync(r === 5, "new-throw") }
  )
  assertAsyncRan()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
147 148 149
})();

(function() {
150 151
  Promise.accept(5);
  Promise.accept(5).chain(undefined, assertUnreachable).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
152 153 154 155 156 157 158
    function(x) { assertAsync(x === 5, "resolved/chain-nohandler") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
159
  Promise.reject(5).chain(assertUnreachable, undefined).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
160 161 162 163 164 165 166
    assertUnreachable,
    function(r) { assertAsync(r === 5, "rejected/chain-nohandler") }
  )
  assertAsyncRan()
})();

(function() {
167
  Promise.accept(5).then(undefined, assertUnreachable).chain(
168 169 170 171
    function(x) { assertAsync(x === 5, "resolved/then-nohandler-undefined") },
    assertUnreachable
  )
  assertAsyncRan()
172
  Promise.accept(6).then(null, assertUnreachable).chain(
173
    function(x) { assertAsync(x === 6, "resolved/then-nohandler-null") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
174 175 176 177 178 179
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
180
  Promise.reject(5).then(assertUnreachable, undefined).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
181
    assertUnreachable,
182 183 184 185 186 187
    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") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
188 189 190 191 192
  )
  assertAsyncRan()
})();

(function() {
193 194 195
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
196 197 198 199 200 201 202 203
  p3.chain(
    function(x) { assertAsync(x === p2, "resolved/chain") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
204 205 206
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
207 208 209 210 211 212 213 214
  p3.then(
    function(x) { assertAsync(x === 5, "resolved/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
215
  var p1 = Promise.reject(5)
216 217
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
218 219 220 221 222 223 224 225
  p3.chain(
    function(x) { assertAsync(x === p2, "rejected/chain") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
226
  var p1 = Promise.reject(5)
227 228
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
229 230 231 232 233 234 235
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "rejected/then") }
  )
  assertAsyncRan()
})();

236
/* TODO(caitp): remove tests once PromiseChain is removed, per bug 3237
rossberg@chromium.org's avatar
rossberg@chromium.org committed
237
(function() {
238 239 240
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
241 242 243 244 245
  p3.chain(function(x) { return x }, assertUnreachable).chain(
    function(x) { assertAsync(x === p1, "resolved/chain/chain") },
    assertUnreachable
  )
  assertAsyncRan()
246
})();*/
rossberg@chromium.org's avatar
rossberg@chromium.org committed
247 248

(function() {
249 250 251
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
252 253 254 255 256 257 258 259
  p3.chain(function(x) { return x }, assertUnreachable).then(
    function(x) { assertAsync(x === 5, "resolved/chain/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
260 261 262
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
263 264 265 266 267 268 269 270
  p3.chain(function(x) { return 6 }, assertUnreachable).chain(
    function(x) { assertAsync(x === 6, "resolved/chain/chain2") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
271 272 273
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
274 275 276 277 278 279 280 281
  p3.chain(function(x) { return 6 }, assertUnreachable).then(
    function(x) { assertAsync(x === 6, "resolved/chain/then2") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
282 283 284
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
285 286 287 288 289 290 291 292
  p3.then(function(x) { return x + 1 }, assertUnreachable).chain(
    function(x) { assertAsync(x === 6, "resolved/then/chain") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
293 294 295
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
296 297 298 299 300 301 302 303
  p3.then(function(x) { return x + 1 }, assertUnreachable).then(
    function(x) { assertAsync(x === 6, "resolved/then/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
304 305 306 307
  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(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
308 309 310 311 312 313 314
    function(x) { assertAsync(x === 6, "resolved/then/chain2") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
315 316 317 318
  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(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
319 320 321 322 323 324 325
    function(x) { assertAsync(x === 6, "resolved/then/then2") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
326 327 328
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
329 330 331 332 333 334 335 336
  p3.chain(function(x) { throw 6 }, assertUnreachable).chain(
    assertUnreachable,
    function(x) { assertAsync(x === 6, "resolved/chain-throw/chain") }
  )
  assertAsyncRan()
})();

(function() {
337 338 339
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
340 341 342 343 344 345 346 347
  p3.chain(function(x) { throw 6 }, assertUnreachable).then(
    assertUnreachable,
    function(x) { assertAsync(x === 6, "resolved/chain-throw/then") }
  )
  assertAsyncRan()
})();

(function() {
348 349 350
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
351 352 353 354 355 356 357 358
  p3.then(function(x) { throw 6 }, assertUnreachable).chain(
    assertUnreachable,
    function(x) { assertAsync(x === 6, "resolved/then-throw/chain") }
  )
  assertAsyncRan()
})();

(function() {
359 360 361
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
362 363 364 365 366 367 368 369
  p3.then(function(x) { throw 6 }, assertUnreachable).then(
    assertUnreachable,
    function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
  )
  assertAsyncRan()
})();

(function() {
370
  var p1 = Promise.accept(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
371
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
372
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
373 374 375 376 377 378 379 380
  p3.chain(
    function(x) { assertAsync(x === p2, "resolved/thenable/chain") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
381
  var p1 = Promise.accept(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
382
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
383
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
384 385 386 387 388 389 390 391
  p3.then(
    function(x) { assertAsync(x === 5, "resolved/thenable/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
392
  var p1 = Promise.reject(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
393
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
394
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
395 396 397 398 399 400 401 402
  p3.chain(
    function(x) { assertAsync(x === p2, "rejected/thenable/chain") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
403
  var p1 = Promise.reject(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
404
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
405
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
406 407 408 409 410 411 412 413
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "rejected/thenable/then") }
  )
  assertAsyncRan()
})();

(function() {
414
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
415
  var p1 = deferred.promise
416 417
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
418 419 420 421 422 423 424 425 426
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/resolve") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
427
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
428
  var p1 = deferred.promise
429 430
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
431 432 433 434 435 436 437 438 439
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
440
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
441
  var p1 = deferred.promise
442 443
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
444 445 446 447 448 449 450 451 452
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/reject") },
    assertUnreachable
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
453
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
454
  var p1 = deferred.promise
455 456
  var p2 = Promise.accept(p1)
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
457 458 459 460 461 462 463 464
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
(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()
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
489
(function() {
490
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
491 492
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
493
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
494 495 496 497 498 499 500 501 502
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/resolve/thenable") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
503
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
504 505
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
506
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
507 508 509 510 511 512 513 514 515
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
516
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
517 518
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
519
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
520 521 522 523 524 525 526 527 528
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/reject/thenable") },
    assertUnreachable
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
529
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
530 531
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
532
  var p3 = Promise.accept(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
533 534 535 536 537 538 539 540
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject/thenable") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

541
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
rossberg@chromium.org's avatar
rossberg@chromium.org committed
542
(function() {
543 544
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
545
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
546 547 548 549 550 551 552
  var p3 = deferred.promise
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/resolve2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
553
})(); */
rossberg@chromium.org's avatar
rossberg@chromium.org committed
554 555

(function() {
556 557
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
558
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
559 560 561 562 563 564 565 566 567 568
  var p3 = deferred.promise
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
})();

(function() {
569 570
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
571
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
572 573 574 575 576 577 578 579 580 581
  var p3 = deferred.promise
  p3.chain(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "chain/reject2") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
582 583
  var p1 = Promise.accept(5)
  var p2 = Promise.accept(p1)
584
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
585 586 587 588 589 590 591 592 593
  var p3 = deferred.promise
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject2") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

594
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
rossberg@chromium.org's avatar
rossberg@chromium.org committed
595
(function() {
596
  var p1 = Promise.accept(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
597
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
598
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
599 600 601 602 603 604 605
  var p3 = deferred.promise
  p3.chain(
    function(x) { assertAsync(x === p2, "chain/resolve/thenable2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
606
})(); */
rossberg@chromium.org's avatar
rossberg@chromium.org committed
607 608

(function() {
609
  var p1 = Promise.accept(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
610
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
611
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
612 613 614 615 616 617 618 619 620 621
  var p3 = deferred.promise
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
})();

(function() {
622
  var p1 = Promise.accept(0)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
623 624 625 626 627 628 629 630 631
  var p2 = p1.chain(function(x) { return p2 }, assertUnreachable)
  p2.chain(
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/chain") }
  )
  assertAsyncRan()
})();

(function() {
632
  var p1 = Promise.accept(0)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
633 634 635 636 637 638 639 640
  var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
  p2.chain(
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/then") }
  )
  assertAsyncRan()
})();

641
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
rossberg@chromium.org's avatar
rossberg@chromium.org committed
642
(function() {
643
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
644 645 646 647 648 649 650
  var p = deferred.promise
  deferred.resolve(p)
  p.chain(
    function(x) { assertAsync(x === p, "cyclic/deferred/chain") },
    assertUnreachable
  )
  assertAsyncRan()
651
})();*/
rossberg@chromium.org's avatar
rossberg@chromium.org committed
652

653
/* TODO(caitp): remove tests once PromiseChain is removed, per bug v8:3237
rossberg@chromium.org's avatar
rossberg@chromium.org committed
654
(function() {
655
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
656 657 658 659 660 661 662
  var p = deferred.promise
  deferred.resolve(p)
  p.then(
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") }
  )
  assertAsyncRan()
663
})();*/
rossberg@chromium.org's avatar
rossberg@chromium.org committed
664 665 666 667 668 669 670 671 672

(function() {
  Promise.all([]).chain(
    function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
    assertUnreachable
  )
  assertAsyncRan()
})();

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
(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();
})();


rossberg@chromium.org's avatar
rossberg@chromium.org committed
712
(function() {
713
  var deferred1 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
714
  var p1 = deferred1.promise
715
  var deferred2 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
716
  var p2 = deferred2.promise
717
  var deferred3 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
  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() {
738
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
739
  var p1 = deferred.promise
740
  var p2 = Promise.accept(2)
741
  var p3 = Promise.defer().promise
rossberg@chromium.org's avatar
rossberg@chromium.org committed
742 743 744 745 746 747 748 749
  Promise.all([p1, p2, p3]).chain(
    assertUnreachable,
    assertUnreachable
  )
  deferred.resolve(1)
})();

(function() {
750
  var deferred1 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
751
  var p1 = deferred1.promise
752
  var deferred2 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
753
  var p2 = deferred2.promise
754
  var deferred3 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
755 756 757 758 759 760 761 762 763 764 765
  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()
})();

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

(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();
})();


rossberg@chromium.org's avatar
rossberg@chromium.org committed
812
(function() {
813
  Promise.race([]).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
814 815 816 817 818 819
    assertUnreachable,
    assertUnreachable
  )
})();

(function() {
820 821 822
  var p1 = Promise.accept(1)
  var p2 = Promise.accept(2)
  var p3 = Promise.accept(3)
823 824
  Promise.race([p1, p2, p3]).chain(
    function(x) { assertAsync(x === 1, "resolved/one") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
825 826 827 828 829 830
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
831 832 833
  var p1 = Promise.accept(1)
  var p2 = Promise.accept(2)
  var p3 = Promise.accept(3)
834 835
  Promise.race([0, p1, p2, p3]).chain(
    function(x) { assertAsync(x === 0, "resolved-const/one") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
836 837 838 839 840
    assertUnreachable
  )
  assertAsyncRan()
})();

841 842
(function() {
  var deferred1 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
843
  var p1 = deferred1.promise
844
  var deferred2 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
845
  var p2 = deferred2.promise
846
  var deferred3 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
847
  var p3 = deferred3.promise
848
  Promise.race([p1, p2, p3]).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
849 850 851 852 853 854 855 856 857
    function(x) { assertAsync(x === 3, "one/resolve") },
    assertUnreachable
  )
  deferred3.resolve(3)
  deferred1.resolve(1)
  assertAsyncRan()
})();

(function() {
858
  var deferred = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
859
  var p1 = deferred.promise
860
  var p2 = Promise.accept(2)
861 862
  var p3 = Promise.defer().promise
  Promise.race([p1, p2, p3]).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
863 864 865 866 867 868 869 870
    function(x) { assertAsync(x === 2, "resolved/one") },
    assertUnreachable
  )
  deferred.resolve(1)
  assertAsyncRan()
})();

(function() {
871
  var deferred1 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
872
  var p1 = deferred1.promise
873
  var deferred2 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
874
  var p2 = deferred2.promise
875
  var deferred3 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
876
  var p3 = deferred3.promise
877
  Promise.race([p1, p2, p3]).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
878 879 880 881 882 883 884 885 886
    function(x) { assertAsync(x === 3, "one/resolve/reject") },
    assertUnreachable
  )
  deferred3.resolve(3)
  deferred1.reject(1)
  assertAsyncRan()
})();

(function() {
887
  var deferred1 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
888
  var p1 = deferred1.promise
889
  var deferred2 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
890
  var p2 = deferred2.promise
891
  var deferred3 = Promise.defer()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
892
  var p3 = deferred3.promise
893
  Promise.race([p1, p2, p3]).chain(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
894 895 896 897 898 899 900 901
    assertUnreachable,
    function(x) { assertAsync(x === 3, "one/reject/resolve") }
  )
  deferred3.reject(3)
  deferred1.resolve(1)
  assertAsyncRan()
})();

902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

(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();
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
999 1000 1001 1002
(function() {
  var log
  function MyPromise(resolver) {
    log += "n"
1003 1004 1005 1006 1007 1008 1009 1010
    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
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1011 1012 1013
  }

  MyPromise.__proto__ = Promise
1014
  MyPromise.defer = function() {
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1015
    log += "d"
1016
    return call(this.__proto__.defer, this)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1017 1018 1019 1020
  }

  MyPromise.prototype.__proto__ = Promise.prototype
  MyPromise.prototype.chain = function(resolve, reject) {
1021
    log += "c"
1022
    return call(this.__proto__.__proto__.chain, this, resolve, reject)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1023 1024 1025 1026 1027
  }

  log = ""
  var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
  var p2 = new MyPromise(function(resolve, reject) { reject(2) })
1028 1029 1030
  var d3 = MyPromise.defer()
  assertTrue(d3.promise instanceof Promise, "subclass/instance")
  assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1031 1032 1033
  assertTrue(log === "nx1nr2dn", "subclass/create")

  log = ""
1034 1035
  var p4 = MyPromise.resolve(4)
  var p5 = MyPromise.reject(5)
1036
  assertTrue(p4 instanceof MyPromise, "subclass/instance4")
1037
  assertTrue(p4 instanceof MyPromise, "subclass/instance-my4")
1038
  assertTrue(p5 instanceof MyPromise, "subclass/instance5")
1039
  assertTrue(p5 instanceof MyPromise, "subclass/instance-my5")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1040 1041 1042 1043
  d3.resolve(3)
  assertTrue(log === "nx4nr5x3", "subclass/resolve")

  log = ""
1044 1045 1046 1047
  var d6 = MyPromise.defer()
  d6.promise.chain(function(x) {
    return new Promise(function(resolve) { resolve(x) })
  }).chain(function() {})
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1048
  d6.resolve(6)
1049
  assertTrue(log === "dncncnx6", "subclass/chain")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1050 1051

  log = ""
1052
  Promise.all([11, Promise.accept(12), 13, MyPromise.accept(14), 15, 16])
1053 1054

  assertTrue(log === "nx14", "subclass/all/arg")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1055 1056

  log = ""
1057
  MyPromise.all([21, Promise.accept(22), 23, MyPromise.accept(24), 25, 26])
1058
  assertTrue(log === "nx24nnx21nnnnx23nnnx25nnx26n", "subclass/all/self")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1059 1060
})();

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
(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");
}());
rossberg@chromium.org's avatar
rossberg@chromium.org committed
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
(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);
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
1119
assertAsyncDone()