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