/** * The counters that make a runaway script stop. * * The engine owns the loop, so every one of these is a number it increments * rather than something it hopes about. A platform running many customers' * scripts in one process needs exactly that: exceeding a budget is a diagnostic * on the script that did it, never a hang and never a frozen tab. * * **Two kinds of budget, and the difference matters.** * * The *loop budget* is part of the language. `compiled-program.md` 8.5 requires * two engines to fail at the same iteration of the same loop on the same bar, * so it counts `TICK` executions and nothing else, it comes from the program's * own `limits()` line, and it is identical on every engine. * * The *host budgets* are not part of the language and cannot be, because a * wall clock measures the machine rather than the program. They exist so that * one script cannot take a process down, and a host that sets one accepts that * a program refused here may run elsewhere. That is why the clock is off unless * a host asks for it and why the step ceiling is derived from the program * rather than chosen: see `stepBound`. * * The clock is read once every `clockEvery` steps rather than every step. A * clock read costs more than an instruction, and reading it per instruction * would make the wall clock the thing the wall clock measures. */ import type { Span } from '../span/index.js'; import type { CompiledProgram } from './types.js'; export interface EngineLimits { /** * Instructions one bar may execute, or null to derive it from the program. * * A verified program's bar has a bound that can be computed at load, so the * derived value is the honest ceiling and a host rarely needs to set one. */ readonly steps: number | null; /** Milliseconds one bar may take, or null for no clock at all. */ readonly ms: number | null; /** Elements one array may hold, `language.md` 14.1, OS5002. */ readonly arrayElements: number; /** Code points one string may hold, OS5008. */ readonly stringLength: number; /** * Drawing objects one script may hold at once, OS5010. * * The host's, like the string ceiling: the language fixes no number, and a * host that sets one accepts that a script refused here draws on another. * What the language does fix is that reaching it is reported and that the * oldest object is never dropped to make room, which is `stdlib.md` 14.4. */ readonly drawingObjects: number; /** Call frames, OS5005 at load. */ readonly frames: number; /** The most a program's own `limits(loops = ...)` may ask for, OS5003. */ readonly loops: number | null; /** The most a program's own `limits(history = ...)` may ask for, OS5003. */ readonly history: number | null; /** Instructions in the whole program, OS5009 at load, or null for no ceiling. */ readonly instructions: number | null; /** State regions, OS5004 at load, or null for no ceiling. */ readonly states: number | null; /** * Higher timeframe and other instrument reads one file may make, OS5006. * * The host's, because each read is a series it fetches and keeps in step with * the chart. Reported at load rather than silently capped: dropping the reads * past a ceiling is a plot that quietly turns absent, and nothing on the chart * would say which one went. */ readonly requests: number | null; /** Steps between two readings of the clock. */ readonly clockEvery: number; } /** * What an engine allows when a host says nothing. * * The ceilings that have a catalogue code are set; the clock is off, because a * default wall clock would make the same script pass on a fast machine and fail * on a slow one with nobody having asked for that trade. * * **`arrayElements` is the one number here the language fixes.** `language.md` * 14.1 states it, so it is not a host's choice and a host lowering it refuses a * script that a conforming engine runs. `scripts/check-limits.mjs` compares it * against that sentence on every build, which is the only reason this file may * write the number at all. Every other ceiling below is the host's, because the * specification leaves it open. */ export declare const DEFAULT_LIMITS: EngineLimits; export declare function limitsWith(given: Partial | undefined): EngineLimits; /** * The most instructions one bar of this program can possibly execute. * * Verification proves three things that together bound a bar: the target of * every backward jump is a `TICK` (3.5 check 6), so no cycle runs without * charging the loop budget; recursion is an error, so the call graph is acyclic * and each call site's body runs at most once per acyclic segment; and the * instruction lists are finite. So a segment between two `TICK` executions * costs at most the program's whole instruction count once, and there are at * most `limits.loops` such segments plus the one that ends at `HALT`. * * The number is generous by design. It is not a performance budget: it is the * proof that a bar terminates, turned into a counter so that a program which * somehow exceeds its own static bound stops instead of running forever. */ export declare function stepBound(program: CompiledProgram): number; /** A reading of the clock, injected so that a test is not at the mercy of one. */ export type Clock = () => number; /** * The per-bar counters. * * One object per engine, reset at step 3 of every execution of a bar, so a long * dataset is never itself a reason to fail. */ export declare class Budget { private loops; private steps; private started; private nextClockAt; private bar; private readonly limits; private readonly stepCeiling; private readonly loopCeiling; private readonly clock; constructor(limits: EngineLimits, stepCeiling: number, loopCeiling: number, clock: Clock | undefined); /** Step 3: the counters start again for this execution of this bar. */ begin(bar: number): void; /** * One instruction executed. * * Returns whether a budget is spent, rather than raising, so the dispatch * loop does the check inline and the cost is one comparison on the path every * instruction takes. */ step(): boolean; /** Which budget the last `step` ran out of, and the diagnostic for it. */ overrun(span: Span, loopLine: number | undefined): never; /** Step 5.5: one iteration of loop `l` charged to the per-bar budget. */ tick(span: Span, line: number): void; private spentLoops; /** * The drawing object ceiling, OS5010, charged before the object is built. * * `held` is what the script holds now, so the object about to be created is * number `held + 1` and the refusal names that number. Checked before * building rather than after, because an engine that allocates first and * complains second has already spent the memory it is refusing to spend. */ checkDrawing(span: Span, held: number): void; /** The array ceiling, OS5002. */ checkArray(span: Span, name: string, size: number): void; /** The string ceiling, OS5008, counted in code points as 3.1 requires. */ checkString(span: Span, text: string): string; /** * The same ceiling against a length that has not been built yet. * * `str.repeat` is where a string ceiling is actually reached, and building * the string first to measure it is how an engine runs out of memory instead * of reporting that it would have. */ checkLength(span: Span, length: number): void; } //# sourceMappingURL=budget.d.ts.map