123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 'use strict';
- var GetIntrinsic = require('get-intrinsic');
- var $TypeError = require('es-errors/type');
- var AsyncFromSyncIteratorContinuation = require('./AsyncFromSyncIteratorContinuation');
- var Call = require('./Call');
- var CreateIterResultObject = require('./CreateIterResultObject');
- var Get = require('./Get');
- var GetMethod = require('./GetMethod');
- var IteratorNext = require('./IteratorNext');
- var ObjectCreate = require('./ObjectCreate');
- var Type = require('./Type');
- var isIteratorRecord = require('../helpers/records/iterator-record');
- var SLOT = require('internal-slot');
- var $AsyncFromSyncIteratorPrototype = GetIntrinsic('%AsyncFromSyncIteratorPrototype%', true) || {
- next: function next(value) {
- var O = this;
- SLOT.assert(O, '[[SyncIteratorRecord]]');
- var argsLength = arguments.length;
- return new Promise(function (resolve) {
- var syncIteratorRecord = SLOT.get(O, '[[SyncIteratorRecord]]');
- var result;
- if (argsLength > 0) {
- result = IteratorNext(syncIteratorRecord['[[Iterator]]'], value);
- } else {
- result = IteratorNext(syncIteratorRecord['[[Iterator]]']);
- }
- resolve(AsyncFromSyncIteratorContinuation(result));
- });
- },
- 'return': function () {
- var O = this;
- SLOT.assert(O, '[[SyncIteratorRecord]]');
- var valueIsPresent = arguments.length > 0;
- var value = valueIsPresent ? arguments[0] : void undefined;
- return new Promise(function (resolve, reject) {
- var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]'];
- var iteratorReturn = GetMethod(syncIterator, 'return');
- if (typeof iteratorReturn === 'undefined') {
- var iterResult = CreateIterResultObject(value, true);
- Call(resolve, undefined, [iterResult]);
- return;
- }
- var result;
- if (valueIsPresent) {
- result = Call(iteratorReturn, syncIterator, [value]);
- } else {
- result = Call(iteratorReturn, syncIterator);
- }
- if (Type(result) !== 'Object') {
- Call(reject, undefined, [new $TypeError('Iterator `return` method returned a non-object value.')]);
- return;
- }
- resolve(AsyncFromSyncIteratorContinuation(result));
- });
- },
- 'throw': function () {
- var O = this;
- SLOT.assert(O, '[[SyncIteratorRecord]]');
- var valueIsPresent = arguments.length > 0;
- var value = valueIsPresent ? arguments[0] : void undefined;
- return new Promise(function (resolve, reject) {
- var syncIterator = SLOT.get(O, '[[SyncIteratorRecord]]')['[[Iterator]]'];
- var throwMethod = GetMethod(syncIterator, 'throw');
- if (typeof throwMethod === 'undefined') {
- Call(reject, undefined, [value]);
- return;
- }
- var result;
- if (valueIsPresent) {
- result = Call(throwMethod, syncIterator, [value]);
- } else {
- result = Call(throwMethod, syncIterator);
- }
- if (Type(result) !== 'Object') {
- Call(reject, undefined, [new $TypeError('Iterator `throw` method returned a non-object value.')]);
- return;
- }
- resolve(AsyncFromSyncIteratorContinuation(result));
- });
- }
- };
- module.exports = function CreateAsyncFromSyncIterator(syncIteratorRecord) {
- if (!isIteratorRecord(syncIteratorRecord)) {
- throw new $TypeError('Assertion failed: `syncIteratorRecord` must be an Iterator Record');
- }
-
- var asyncIterator = ObjectCreate($AsyncFromSyncIteratorPrototype);
- SLOT.set(asyncIterator, '[[SyncIteratorRecord]]', syncIteratorRecord);
- var nextMethod = Get(asyncIterator, 'next');
- return {
- '[[Iterator]]': asyncIterator,
- '[[NextMethod]]': nextMethod,
- '[[Done]]': false
- };
- };
|