Skip to content

Commit

Permalink
Implement CreateAsyncFromSyncIterator abstract op
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiasBuelens committed Feb 8, 2021
1 parent b40c5c1 commit df038ab
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions reference-implementation/lib/abstract-ops/ecmascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,32 @@ exports.GetMethod = (V, P) => {
return func;
};

exports.CreateAsyncFromSyncIterator = syncIteratorRecord => {
// Instead of re-implementing CreateAsyncFromSyncIterator and %AsyncFromSyncIteratorPrototype%,
// we use yield* inside an async generator function to achieve the same result.

// Wrap the sync iterator inside a sync iterable, so we can use it with yield*.
const syncIterable = {
[Symbol.iterator]: () => syncIteratorRecord.iterator
};
// Create an async generator function and immediately invoke it.
const asyncIterator = (async function* () {
return yield* syncIterable;
}());
// Return as an async iterator record.
const nextMethod = asyncIterator.next;
return { iterator: asyncIterator, nextMethod, done: false };
};

exports.GetIterator = (obj, hint = 'sync', method) => {
assert(hint === 'sync' || hint === 'async');
if (method === undefined) {
if (hint === 'async') {
method = exports.GetMethod(obj, Symbol.asyncIterator);
if (method === undefined) {
const syncMethod = exports.GetMethod(obj, Symbol.iterator);
const syncIterator = exports.GetIterator(obj, 'sync', syncMethod);
return syncIterator; // TODO sync to async iterator
const syncIteratorRecord = exports.GetIterator(obj, 'sync', syncMethod);
return exports.CreateAsyncFromSyncIterator(syncIteratorRecord);
}
} else {
method = exports.GetMethod(obj, Symbol.iterator);
Expand Down

0 comments on commit df038ab

Please sign in to comment.