[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 6 | var $TypeError = require('es-errors/type');
|
---|
| 7 | var $Promise = GetIntrinsic('%Promise%', true);
|
---|
| 8 |
|
---|
| 9 | var Call = require('./Call');
|
---|
| 10 | var CreateIterResultObject = require('./CreateIterResultObject');
|
---|
| 11 | var Get = require('./Get');
|
---|
| 12 | var GetMethod = require('./GetMethod');
|
---|
| 13 | var IteratorComplete = require('./IteratorComplete');
|
---|
| 14 | var IteratorNext = require('./IteratorNext');
|
---|
| 15 | var IteratorValue = require('./IteratorValue');
|
---|
| 16 | var ObjectCreate = require('./ObjectCreate');
|
---|
| 17 | var PromiseResolve = require('./PromiseResolve');
|
---|
| 18 | var Type = require('./Type');
|
---|
| 19 |
|
---|
| 20 | var isIteratorRecord = require('../helpers/records/iterator-record');
|
---|
| 21 |
|
---|
| 22 | var SLOT = require('internal-slot');
|
---|
| 23 |
|
---|
| 24 | var callBound = require('call-bind/callBound');
|
---|
| 25 |
|
---|
| 26 | var $then = callBound('Promise.prototype.then', true);
|
---|
| 27 |
|
---|
| 28 | var AsyncFromSyncIteratorContinuation = function AsyncFromSyncIteratorContinuation(result) {
|
---|
| 29 | if (Type(result) !== 'Object') {
|
---|
| 30 | throw new $TypeError('Assertion failed: Type(O) is not Object');
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | if (arguments.length > 1) {
|
---|
| 34 | throw new $TypeError('although AsyncFromSyncIteratorContinuation should take a second argument, it is not used in this implementation');
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | if (!$Promise) {
|
---|
| 38 | throw new $SyntaxError('This environment does not support Promises.');
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return new $Promise(function (resolve) {
|
---|
| 42 | var done = IteratorComplete(result); // step 2
|
---|
| 43 | var value = IteratorValue(result); // step 4
|
---|
| 44 | var valueWrapper = PromiseResolve($Promise, value); // step 6
|
---|
| 45 |
|
---|
| 46 | // eslint-disable-next-line no-shadow
|
---|
| 47 | var onFulfilled = function (value) { // steps 8-9
|
---|
| 48 | return CreateIterResultObject(value, done); // step 8.a
|
---|
| 49 | };
|
---|
| 50 | resolve($then(valueWrapper, onFulfilled)); // step 11
|
---|
| 51 | }); // step 12
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | var $AsyncFromSyncIteratorPrototype = GetIntrinsic('%AsyncFromSyncIteratorPrototype%', true) || {
|
---|
| 55 | next: function next(value) {
|
---|
| 56 | if (!$Promise) {
|
---|
| 57 | throw new $SyntaxError('This environment does not support Promises.');
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | var O = this; // step 1
|
---|
| 61 |
|
---|
| 62 | SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
|
---|
| 63 |
|
---|
| 64 | var argsLength = arguments.length;
|
---|
| 65 |
|
---|
| 66 | return new $Promise(function (resolve) { // step 3
|
---|
| 67 | var syncIteratorRecord = SLOT.get(O, '[[SyncIteratorRecord]]'); // step 4
|
---|
| 68 | var result;
|
---|
| 69 | if (argsLength > 0) {
|
---|
| 70 | result = IteratorNext(syncIteratorRecord['[[Iterator]]'], value); // step 5.a
|
---|
| 71 | } else { // step 6
|
---|
| 72 | result = IteratorNext(syncIteratorRecord['[[Iterator]]']);// step 6.a
|
---|
| 73 | }
|
---|
| 74 | resolve(AsyncFromSyncIteratorContinuation(result)); // step 8
|
---|
| 75 | });
|
---|
| 76 | },
|
---|
| 77 | 'return': function () {
|
---|
| 78 | if (!$Promise) {
|
---|
| 79 | throw new $SyntaxError('This environment does not support Promises.');
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | var O = this; // step 1
|
---|
| 83 |
|
---|
| 84 | SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
|
---|
| 85 |
|
---|
| 86 | var valueIsPresent = arguments.length > 0;
|
---|
| 87 | var value = valueIsPresent ? arguments[0] : void undefined;
|
---|
| 88 |
|
---|
| 89 | return new $Promise(function (resolve, reject) { // step 3
|
---|
| 90 | var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
|
---|
| 91 | var iteratorReturn = GetMethod(syncIterator, 'return'); // step 5
|
---|
| 92 |
|
---|
| 93 | if (typeof iteratorReturn === 'undefined') { // step 7
|
---|
| 94 | var iterResult = CreateIterResultObject(value, true); // step 7.a
|
---|
| 95 | Call(resolve, undefined, [iterResult]); // step 7.b
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 | var result;
|
---|
| 99 | if (valueIsPresent) { // step 8
|
---|
| 100 | result = Call(iteratorReturn, syncIterator, [value]); // step 8.a
|
---|
| 101 | } else { // step 9
|
---|
| 102 | result = Call(iteratorReturn, syncIterator); // step 9.a
|
---|
| 103 | }
|
---|
| 104 | if (Type(result) !== 'Object') { // step 11
|
---|
| 105 | Call(reject, undefined, [new $TypeError('Iterator `return` method returned a non-object value.')]); // step 11.a
|
---|
| 106 | return;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | resolve(AsyncFromSyncIteratorContinuation(result)); // step 12
|
---|
| 110 | });
|
---|
| 111 | },
|
---|
| 112 | 'throw': function () {
|
---|
| 113 | if (!$Promise) {
|
---|
| 114 | throw new $SyntaxError('This environment does not support Promises.');
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | var O = this; // step 1
|
---|
| 118 |
|
---|
| 119 | SLOT.assert(O, '[[SyncIteratorRecord]]'); // step 2
|
---|
| 120 |
|
---|
| 121 | var valueIsPresent = arguments.length > 0;
|
---|
| 122 | var value = valueIsPresent ? arguments[0] : void undefined;
|
---|
| 123 |
|
---|
| 124 | return new $Promise(function (resolve, reject) { // step 3
|
---|
| 125 | var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]']; // step 4
|
---|
| 126 |
|
---|
| 127 | var throwMethod = GetMethod(syncIterator, 'throw'); // step 5
|
---|
| 128 |
|
---|
| 129 | if (typeof throwMethod === 'undefined') { // step 7
|
---|
| 130 | Call(reject, undefined, [value]); // step 7.a
|
---|
| 131 | return;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | var result;
|
---|
| 135 | if (valueIsPresent) { // step 8
|
---|
| 136 | result = Call(throwMethod, syncIterator, [value]); // step 8.a
|
---|
| 137 | } else { // step 9
|
---|
| 138 | result = Call(throwMethod, syncIterator); // step 9.a
|
---|
| 139 | }
|
---|
| 140 | if (Type(result) !== 'Object') { // step 11
|
---|
| 141 | Call(reject, undefined, [new $TypeError('Iterator `throw` method returned a non-object value.')]); // step 11.a
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | resolve(AsyncFromSyncIteratorContinuation(result/* , promiseCapability */)); // step 12
|
---|
| 146 | });
|
---|
| 147 | }
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 | // https://262.ecma-international.org/9.0/#sec-createasyncfromsynciterator
|
---|
| 151 |
|
---|
| 152 | module.exports = function CreateAsyncFromSyncIterator(syncIteratorRecord) {
|
---|
| 153 | if (!isIteratorRecord(syncIteratorRecord)) {
|
---|
| 154 | throw new $TypeError('Assertion failed: `syncIteratorRecord` is not an Iterator Record');
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | // var asyncIterator = ObjectCreate(%AsyncFromSyncIteratorPrototype%, « [[SyncIteratorRecord]] »); // step 1
|
---|
| 158 | var asyncIterator = ObjectCreate($AsyncFromSyncIteratorPrototype);
|
---|
| 159 |
|
---|
| 160 | SLOT.set(asyncIterator, '[[SyncIteratorRecord]]', syncIteratorRecord); // step 2
|
---|
| 161 |
|
---|
| 162 | var nextMethod = Get(asyncIterator, 'next'); // step 3
|
---|
| 163 |
|
---|
| 164 | return { // steps 3-4
|
---|
| 165 | '[[Iterator]]': asyncIterator,
|
---|
| 166 | '[[NextMethod]]': nextMethod,
|
---|
| 167 | '[[Done]]': false
|
---|
| 168 | };
|
---|
| 169 | };
|
---|