mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-25 06:24:27 +08:00
map, filter, take, drop, flatMap, toArray, forEach, reduce, some, every and find compiled only as one chain from arr.values() or arr.entries() that ended in a consuming method. Every other form was refused, and in JavaScript the refusal was thrown at run time. IteratorObject now maps to the generator type. A consuming method over a chain of stages fuses into one loop over the source: a generator driven like for-of, a Map or Set walked live, or an array by index. A stage on its own builds a helper object: a generator over its source that supports next(), for...of, spreads, Array.from and yield*. A for...of whose head is a chain of stages without flatMap runs the stages inside the loop. The source is closed where Node closes it: when take reaches its count, when some, every or find has its answer, when a callback or the loop body throws, when the loop exits early, and before the RangeError of an invalid take or drop budget. It is not closed when it is exhausted or its own next() throws. return() on a helper object before its first next() closes the source through a new runtime hook, scr_gen_set_unstarted_return. When closing an iterator throws while another error propagates, the first error reaches the caller, as in Node. This applies to for...of over a generator too, where the error from the close used to replace the body's error. The try statement takes a dropFinallyErrors flag, whose cleanup handler calls the new scr_exc_keep_first. throw() lowers only on values typed as generators. Iterator helpers and the built-in iterators have no throw method, and a value typed as an iterator may hold one. Callbacks in JavaScript bind their unannotated parameters to the element type, and flatMap callbacks may return generators. A generator whose record yields share checked storage with its return channel has each value converted to the declared element type as it is pulled.