/** * Stream creation, disposal, and inflight tracking * * Owns the stream lifecycle for script-defined stream closures: * - Stream creation from a ScriptCallable with a stream return type * - Scope-level stream tracking for disposal on scope exit * - Dispose error propagation: halt and control signals re-thrown directly; * other errors wrapped as catchable RILL_R002 halts * * Methods added: * - invokeStreamClosure(closure, args, location) -> Promise * - trackStream(stream) -> void * - disposeStreams(streams) -> Promise * * State: * - streamScopeStack: RillStream[][] — per-instance stack; no cross-instance * contamination * * Cross-module dependencies: * - createCallableContext(callable) — provided by closures.ts * - evaluateBodyExpression(body) — provided by control-flow.ts (on the body evaluator state) * * @internal */ import type { SourceLocation } from '../../../../types.js'; import type { ScriptCallable } from '../../callable.js'; import type { RuntimeContext } from '../../types/runtime.js'; import type { RillValue, RillStream, TypeStructure } from '../../types/structures.js'; import type { EvalState } from '../state.js'; /** * Rendezvous channel for stream closure body ↔ async generator communication. * The body pushes yielded values; the generator pulls them one at a time. * Backpressure: push() blocks until the consumer calls pull(). * * @internal */ export interface StreamChannel { /** * Push a yielded chunk value. The returned promise resolves when this * specific chunk is consumed by a pull (or immediately once the channel * is terminated). Chunks are delivered FIFO, so concurrent pushes (e.g. * from `fan`) are each queued and delivered in turn rather than * overwriting one another. */ push(value: RillValue): Promise; /** Pull the next chunk. Returns done:true when body completes. */ pull(): Promise<{ value: RillValue; done: false; } | { value?: undefined; done: true; }>; /** Signal body completion with a resolution value. */ close(resolution: RillValue): void; /** Signal body failure with an error. */ error(err: unknown): void; /** * Stop the channel without recording a resolution. Any parked push() * promises resolve immediately and any future push() returns at once, so a * body suspended at a yield can run to completion. Used by the resolve path * and by scope-exit disposal to unblock a partially consumed body. */ cancel(): void; } /** * Allows child EvalState (e.g. created by seq via getEvalState(callableCtx)) to locate * the active stream channel by walking the RuntimeContext parent chain. * Populated by invokeStreamClosure for the duration of the stream body execution. */ export declare const activeStreamContexts: WeakMap; /** * Push a stream scope, run `body(s, arg)`, then dispose any unconsumed * streams created inside it. * * `arg` is threaded through to `body` instead of captured in a closure, so * callers on hot paths (e.g. `evaluateBlock`) can pass a plain function * reference without allocating a per-call arrow function. * * Reads/writes the class-field `streamScopeStack` on `s`; never allocates * a fresh local stack (streamScopeStack is owned by StreamClosuresEvaluator). */ export declare function runInStreamScope(s: EvalState, arg: A, body: (s: EvalState, arg: A) => Promise): Promise; /** * Track a stream in the current scope for cleanup on scope exit. * Streams with dispose functions get cleaned up when their scope exits. */ export declare function trackStream(s: EvalState, stream: RillStream): void; /** * Create a RillStream from a stream-typed ScriptCallable. * * Initializes a callable context, marshals arguments, spins up a dedicated * body evaluator with an active stream channel, and returns a lazy * RillStream whose async generator pulls from that channel. * * Error contracts: * - Chunk type mismatch at yield → TYPE_MISMATCH (validated by evaluateYield) * - Resolution type mismatch → TYPE_MISMATCH * - Body RillError preserved with original code; dispose runs before re-throw */ export declare function invokeStreamClosure(s: EvalState, callable: ScriptCallable, args: RillValue[], callLocation?: SourceLocation): Promise;