/** * @file The machinery every iterator helper is built on. * Five things here are not obvious and are all observable. * A helper's result is not a generator. It is an object whose [[Prototype]] * is `%IteratorHelperPrototype%`, carrying `next`, `return`, and a * `Symbol.toStringTag` of `'Iterator Helper'`, and test262 reads all three. * The brand check is a WeakMap rather than a property, because * `next.call({})` has to throw a TypeError and a property any object could * carry would not. * A DONE result's `value` is never read. `{ done: true, get value() { throw } * }` is a legal result, and reading it eagerly turns a clean finish into an * exception. * The source's `next` is read once but its callability is checked at CALL * time. `map.call({ next: 0 }, fn)` builds a helper without complaint and * throws only when that helper is stepped. * Closing comes in two flavors, which is why there are two functions rather * than a flag. When an error is already pending, a throwing `return` is * swallowed so the original error wins. On a clean early exit, it propagates. */ /** * A step function's outcome, in the shape `next` returns. */ export interface StepResult { done: boolean; value: T | undefined; } /** * One helper's mutable state. */ export interface HelperState { /** * Close the underlying source, letting its errors escape. */ close: () => void; /** * True once the helper has finished or been closed. */ done: boolean; /** * True while `step` is on the stack, to reject a re-entrant `next`. */ running: boolean; /** * Produce the next result, or a done result when exhausted. */ step: () => StepResult; } /** * A source iterator reduced to what a helper uses. */ export interface IteratorRecord { /** * Close it, letting a throwing `return` escape. */ close: () => void; /** * Close it, swallowing a throwing `return`. */ closeQuietly: () => void; /** * The iterator object itself. `Iterator.from` needs it: an iterator that * already inherits from `%IteratorPrototype%` is returned unwrapped, and * that test is against THIS object rather than whatever was passed in. */ iterator: object; /** * Step it once. */ next: () => StepResult; /** * The `next` property as read at construction, exposed so a caller that * forwards raw results does not have to read it a SECOND time - test262 * counts the property gets. */ nextMethod: unknown; } /** * Throw unless `receiver` is an object, which every helper checks FIRST - * before coercing an argument and before reading `next`. */ export declare function assertObjectReceiver(receiver: unknown): object; /** * Call `iterator.return` if it has one, letting a throw from that call escape. * For a clean early exit: a limit reached, a match found, a caller's `return`. */ export declare function closeIterator(iterator: object): void; /** * Call `iterator.return` if it has one, swallowing a throw from that call. * For when an error is already pending, which must be the one the caller sees. */ export declare function closeIteratorQuietly(iterator: object): void; /** * Run `coerce`, closing `receiver` and rethrowing if it throws. Used for the * `take`/`drop` limit, whose coercion may run user code. */ export declare function closeOnThrow(receiver: object, coerce: () => T): T; /** * Throw a TypeError after closing `receiver`, which is what argument * validation does: a bad callback still owes the source its `return` call. */ export declare function closeThenThrow(receiver: object, message: string): never; /** * A done result, allocated fresh each time because a caller may mutate it. */ export declare function doneResult(): StepResult; /** * The shared `%IteratorHelperPrototype%`. */ export declare const iteratorHelperPrototype: object; /** * `%IteratorPrototype%`. */ export declare function iteratorPrototypeOf(): object; /** * Wrap an iterator object, reading `next` a single time. * * `next` is NOT checked for callability here: the spec's GetIteratorDirect only * reads the property, and a non-callable one throws when the helper is stepped. */ export declare function iteratorRecordOf(iterator: object): IteratorRecord; /** * Build a helper object over `state`. */ export declare function makeIteratorHelper(state: HelperState): IteratorObject; /** * Read a helper's state, throwing when the receiver is not a helper. */ export declare function stateOf(receiver: unknown): HelperState;