CreateAsyncFromSyncIterator.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $SyntaxError = require('es-errors/syntax');
  4. var $TypeError = require('es-errors/type');
  5. var $Promise = GetIntrinsic('%Promise%', true);
  6. var AsyncFromSyncIteratorContinuation = require('./AsyncFromSyncIteratorContinuation');
  7. var Call = require('./Call');
  8. var CreateIterResultObject = require('./CreateIterResultObject');
  9. var Get = require('./Get');
  10. var GetMethod = require('./GetMethod');
  11. var IteratorNext = require('./IteratorNext');
  12. var OrdinaryObjectCreate = require('./OrdinaryObjectCreate');
  13. var Type = require('./Type');
  14. var isIteratorRecord = require('../helpers/records/iterator-record');
  15. var SLOT = require('internal-slot');
  16. var $AsyncFromSyncIteratorPrototype = GetIntrinsic('%AsyncFromSyncIteratorPrototype%', true) || {
  17. next: function next(value) {
  18. if (!$Promise) {
  19. throw new $SyntaxError('This environment does not support Promises.');
  20. }
  21. var O = this; // step 1
  22. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  23. var argsLength = arguments.length;
  24. return new $Promise(function (resolve) { // step 3
  25. var syncIteratorRecord = SLOT.get(O, '[[SyncIteratorRecord]]'); // step 4
  26. var result;
  27. if (argsLength > 0) {
  28. result = IteratorNext(syncIteratorRecord['[[Iterator]]'], value); // step 5.a
  29. } else { // step 6
  30. result = IteratorNext(syncIteratorRecord['[[Iterator]]']);// step 6.a
  31. }
  32. resolve(AsyncFromSyncIteratorContinuation(result)); // step 8
  33. });
  34. },
  35. 'return': function () {
  36. if (!$Promise) {
  37. throw new $SyntaxError('This environment does not support Promises.');
  38. }
  39. var O = this; // step 1
  40. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  41. var valueIsPresent = arguments.length > 0;
  42. var value = valueIsPresent ? arguments[0] : void undefined;
  43. return new $Promise(function (resolve, reject) { // step 3
  44. var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
  45. var iteratorReturn = GetMethod(syncIterator, 'return'); // step 5
  46. if (typeof iteratorReturn === 'undefined') { // step 7
  47. var iterResult = CreateIterResultObject(value, true); // step 7.a
  48. Call(resolve, undefined, [iterResult]); // step 7.b
  49. return;
  50. }
  51. var result;
  52. if (valueIsPresent) { // step 8
  53. result = Call(iteratorReturn, syncIterator, [value]); // step 8.a
  54. } else { // step 9
  55. result = Call(iteratorReturn, syncIterator); // step 9.a
  56. }
  57. if (Type(result) !== 'Object') { // step 11
  58. Call(reject, undefined, [new $TypeError('Iterator `return` method returned a non-object value.')]); // step 11.a
  59. return;
  60. }
  61. resolve(AsyncFromSyncIteratorContinuation(result)); // step 12
  62. });
  63. },
  64. 'throw': function () {
  65. if (!$Promise) {
  66. throw new $SyntaxError('This environment does not support Promises.');
  67. }
  68. var O = this; // step 1
  69. SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
  70. var valueIsPresent = arguments.length > 0;
  71. var value = valueIsPresent ? arguments[0] : void undefined;
  72. return new $Promise(function (resolve, reject) { // step 3
  73. var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
  74. var throwMethod = GetMethod(syncIterator, 'throw'); // step 5
  75. if (typeof throwMethod === 'undefined') { // step 7
  76. Call(reject, undefined, [value]); // step 7.a
  77. return;
  78. }
  79. var result;
  80. if (valueIsPresent) { // step 8
  81. result = Call(throwMethod, syncIterator, [value]); // step 8.a
  82. } else { // step 9
  83. result = Call(throwMethod, syncIterator); // step 9.a
  84. }
  85. if (Type(result) !== 'Object') { // step 11
  86. Call(reject, undefined, [new $TypeError('Iterator `throw` method returned a non-object value.')]); // step 11.a
  87. return;
  88. }
  89. resolve(AsyncFromSyncIteratorContinuation(result/* , promiseCapability */)); // step 12
  90. });
  91. }
  92. };
  93. // https://262.ecma-international.org/11.0/#sec-createasyncfromsynciterator
  94. module.exports = function CreateAsyncFromSyncIterator(syncIteratorRecord) {
  95. if (!isIteratorRecord(syncIteratorRecord)) {
  96. throw new $TypeError('Assertion failed: `syncIteratorRecord` must be an Iterator Record');
  97. }
  98. // var asyncIterator = OrdinaryObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »); // step 1
  99. var asyncIterator = OrdinaryObjectCreate($AsyncFromSyncIteratorPrototype);
  100. SLOT.set(asyncIterator, '[[SyncIteratorRecord]]', syncIteratorRecord); // step 2
  101. var nextMethod = Get(asyncIterator, 'next'); // step 3
  102. return { // steps 3-4
  103. '[[Iterator]]': asyncIterator,
  104. '[[NextMethod]]': nextMethod,
  105. '[[Done]]': false
  106. };
  107. };