CreateAsyncFromSyncIterator.js 4.0 KB

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