Skip to content

StdLib Generators Iterators

Roger Johansson edited this page Jan 14, 2026 · 1 revision

Generators, Iterators, and Async Functions

Function types for iteration and asynchronous programming.


Summary

Type Prototype Methods Implemented
Generator 3 0 (stubs)
GeneratorFunction 1 0 (stub)
AsyncGenerator 3 3
AsyncGeneratorFunction 1 0 (stub)
AsyncFunction 1 1
Iterator 15 15
AsyncIterator ~15 0

Generator

Objects returned by generator functions.

Implementation Status: 0% (Stubs Only)

Prototype Methods

Method Status Description
next(value) Not Implemented Throws NotImplementedException
return(value) Not Implemented Throws NotImplementedException
throw(exception) Not Implemented Throws NotImplementedException

Note: Generator functions work via the IR execution system. The prototype methods are stubs because generators are handled internally by the execution engine, not through prototype method dispatch.


GeneratorFunction

Constructor for generator functions.

Implementation Status: 0% (Stub)

Method Status Description
GeneratorFunction(...args, body) Not Implemented Throws NotImplementedException

Note: Cannot dynamically create generators from strings.


AsyncGenerator

Objects returned by async generator functions.

Implementation Status: 100% Complete

Prototype Methods

Method Status Description
next(value) Implemented Returns Promise for next value
return(value) Implemented Returns Promise, completes generator
throw(exception) Implemented Returns Promise, throws into generator

AsyncGeneratorFunction

Constructor for async generator functions.

Implementation Status: 0% (Stub)

Method Status Description
AsyncGeneratorFunction(...args, body) Not Implemented Throws NotImplementedException

AsyncFunction

Constructor for async functions.

Implementation Status: 100% Complete

Method Status Description
AsyncFunction(...args, body) Implemented Creates async function from strings
const asyncFn = new AsyncFunction('x', 'return await Promise.resolve(x * 2)');
await asyncFn(21);  // 42

Iterator

Base prototype for all iterators with Iterator Helpers (TC39 proposal).

Implementation Status: 100% Complete

Prototype Methods

Method Status Description
[Symbol.iterator]() Implemented Returns this
map(fn) Implemented Lazy mapped iterator
filter(fn) Implemented Lazy filtered iterator
take(n) Implemented First n values
drop(n) Implemented Skip first n values
flatMap(fn) Implemented Map and flatten
reduce(fn, init) Implemented Reduce to single value
toArray() Implemented Collect to array
forEach(fn) Implemented Execute for each value
some(fn) Implemented Any match predicate
every(fn) Implemented All match predicate
find(fn) Implemented First matching value
[Symbol.dispose]() Implemented Close iterator

Static Methods

Method Status Description
Iterator.from(iterable) Implemented Create iterator from iterable
Iterator.concat(...iterables) Implemented Concatenate iterables

Example

const result = [1, 2, 3, 4, 5]
    .values()
    .filter(x => x % 2 === 0)
    .map(x => x * 2)
    .toArray();
// [4, 8]

AsyncIterator

Base prototype for async iterators.

Implementation Status: 0% (Empty)

Expected Methods (Not Implemented)

Method Status Description
[Symbol.asyncIterator]() Not Implemented Should return this
map(fn) Not Implemented Async mapped iterator
filter(fn) Not Implemented Async filtered iterator
take(n) Not Implemented First n values
drop(n) Not Implemented Skip first n
flatMap(fn) Not Implemented Async map and flatten
reduce(fn, init) Not Implemented Async reduce
toArray() Not Implemented Collect to array
forEach(fn) Not Implemented Async execute for each
some(fn) Not Implemented Async any match
every(fn) Not Implemented Async all match
find(fn) Not Implemented Async find first
[Symbol.asyncDispose]() Not Implemented Close async iterator

How Generators Work

Generators are executed via the IR system, not prototype methods:

flowchart LR
    subgraph Creation
        FN["function*"] --> IR["IR Plan"]
        IR --> Invoker["SyncGeneratorInvoker"]
    end
    
    subgraph Execution
        Call[".next()"] --> Runner["ExecutionPlanRunner"]
        Runner --> Yield["YieldInstruction"]
        Yield --> Pause["Pause + Return"]
    end
    
    Creation --> Execution
Loading
  1. Generator functions are compiled to IR
  2. SyncGeneratorInvoker manages execution state
  3. YieldInstruction pauses and returns { value, done }
  4. StoreResumeValue handles .next(value), .return(), .throw()

Files

File Purpose
StdLib/Generator/GeneratorPrototype.cs Generator prototype (stubs)
StdLib/GeneratorFunction/GeneratorFunctionConstructor.cs Constructor (stub)
StdLib/AsyncGenerator/AsyncGeneratorPrototype.cs AsyncGenerator prototype
StdLib/AsyncGeneratorFunction/AsyncGeneratorFunctionConstructor.cs Constructor (stub)
StdLib/AsyncFunction/AsyncFunctionConstructor.cs AsyncFunction constructor
StdLib/Iterator/IteratorPrototype.cs Iterator helpers
StdLib/Iterator/IteratorConstructor.cs Iterator.from/concat
StdLib/AsyncIterator/AsyncIteratorPrototype.cs AsyncIterator (empty)

See Also

Clone this wiki locally