resolve-before-loop-exit-from-same.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (C) 2015 André Bargull. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. es6id: 25.4.4.1.1
  5. description: >
  6. Cannot tamper remainingElementsCount when Promise.all resolve element function is called twice in a row.
  7. info: |
  8. Runtime Semantics: PerformPromiseAll( iteratorRecord, constructor, resultCapability)
  9. ...
  10. 4. Let remainingElementsCount be a new Record { [[value]]: 1 }.
  11. ...
  12. 6.d ...
  13. ii. Set remainingElementsCount.[[value]] to remainingElementsCount.[[value]] − 1.
  14. iii. If remainingElementsCount.[[value]] is 0,
  15. 1. Let valuesArray be CreateArrayFromList(values).
  16. 2. Let resolveResult be Call(resultCapability.[[Resolve]], undefined, «valuesArray»).
  17. 3. ReturnIfAbrupt(resolveResult).
  18. ...
  19. 25.4.4.1.2 Promise.all Resolve Element Functions
  20. 1. Let alreadyCalled be the value of F's [[AlreadyCalled]] internal slot.
  21. 2. If alreadyCalled.[[value]] is true, return undefined.
  22. 3. Set alreadyCalled.[[value]] to true.
  23. ...
  24. ---*/
  25. var callCount = 0;
  26. function Constructor(executor) {
  27. function resolve(values) {
  28. callCount += 1;
  29. assert(Array.isArray(values), "values is array");
  30. assert.sameValue(values.length, 3, "values length");
  31. assert.sameValue(values[0], "p1-fulfill", "values[0]");
  32. assert.sameValue(values[1], "p2-fulfill", "values[1]");
  33. assert.sameValue(values[2], "p3-fulfill", "values[2]");
  34. }
  35. executor(resolve, Test262Error.thrower);
  36. }
  37. Constructor.resolve = function(v) {
  38. return v;
  39. };
  40. var p1OnFulfilled;
  41. var p1 = {
  42. then: function(onFulfilled, onRejected) {
  43. p1OnFulfilled = onFulfilled;
  44. }
  45. };
  46. var p2 = {
  47. then: function(onFulfilled, onRejected) {
  48. onFulfilled("p2-fulfill");
  49. onFulfilled("p2-fulfill-unexpected");
  50. }
  51. };
  52. var p3 = {
  53. then: function(onFulfilled, onRejected) {
  54. onFulfilled("p3-fulfill");
  55. }
  56. };
  57. assert.sameValue(callCount, 0, "callCount before call to all()");
  58. Promise.all.call(Constructor, [p1, p2, p3]);
  59. assert.sameValue(callCount, 0, "callCount after call to all()");
  60. p1OnFulfilled("p1-fulfill");
  61. assert.sameValue(callCount, 1, "callCount after resolving p1");