@opencode/codemode 2.1.4 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -4
- package/dist/codemode.d.ts +9 -7
- package/dist/codemode.js +1 -1
- package/dist/data.d.ts +5 -4
- package/dist/data.js +7 -4
- package/dist/extension.d.ts +17 -4
- package/dist/extension.js +8 -0
- package/dist/interpreter/callback.d.ts +10 -10
- package/dist/interpreter/callback.js +43 -31
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/errors.js +12 -15
- package/dist/interpreter/execute.js +59 -29
- package/dist/interpreter/extensions.d.ts +2 -2
- package/dist/interpreter/extensions.js +46 -18
- package/dist/interpreter/generators.js +162 -19
- package/dist/interpreter/globals.js +12 -9
- package/dist/interpreter/interpreter.d.ts +20 -13
- package/dist/interpreter/interpreter.js +1329 -1147
- package/dist/interpreter/intrinsics.js +1 -2
- package/dist/interpreter/model.d.ts +4 -7
- package/dist/interpreter/model.js +1 -11
- package/dist/interpreter/native.d.ts +7 -8
- package/dist/interpreter/native.js +4 -10
- package/dist/interpreter/objects.d.ts +47 -21
- package/dist/interpreter/objects.js +33 -12
- package/dist/interpreter/promises.d.ts +15 -53
- package/dist/interpreter/promises.js +226 -401
- package/dist/interpreter/runner.d.ts +188 -0
- package/dist/interpreter/runner.js +492 -0
- package/dist/stdlib/array.d.ts +3 -3
- package/dist/stdlib/array.js +106 -102
- package/dist/stdlib/bytes.d.ts +3 -3
- package/dist/stdlib/bytes.js +46 -41
- package/dist/stdlib/collections.d.ts +5 -5
- package/dist/stdlib/collections.js +160 -165
- package/dist/stdlib/date.d.ts +1 -1
- package/dist/stdlib/date.js +28 -20
- package/dist/stdlib/headers.d.ts +1 -1
- package/dist/stdlib/headers.js +14 -19
- package/dist/stdlib/iterator.d.ts +1 -1
- package/dist/stdlib/iterator.js +140 -122
- package/dist/stdlib/json.js +9 -12
- package/dist/stdlib/math.js +10 -10
- package/dist/stdlib/number.d.ts +2 -2
- package/dist/stdlib/object.d.ts +1 -1
- package/dist/stdlib/object.js +21 -22
- package/dist/stdlib/regexp.d.ts +1 -1
- package/dist/stdlib/regexp.js +2 -3
- package/dist/stdlib/string.d.ts +1 -1
- package/dist/stdlib/string.js +18 -20
- package/dist/stdlib/url.d.ts +5 -5
- package/dist/stdlib/url.js +32 -33
- package/dist/stdlib/value.d.ts +2 -2
- package/dist/stdlib/web.d.ts +2 -2
- package/package.json +2 -1
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { Cause, type Context, Effect } from "effect";
|
|
2
|
+
import type { Diagnostic } from "../codemode.js";
|
|
3
|
+
import type { Interpreter } from "./interpreter.js";
|
|
4
|
+
import { type AstNode, type GeneratorRequestKind } from "./model.js";
|
|
5
|
+
import { type Callable, PromiseObj, type Value } from "./objects.js";
|
|
6
|
+
/**
|
|
7
|
+
* Interpreter work: a generator that yields signals to the driver and returns its result. Work inside one activation
|
|
8
|
+
* composes with `yield*`; a program call is a `Call`, so program recursion lives on the explicit stack below and never
|
|
9
|
+
* grows the host stack.
|
|
10
|
+
*/
|
|
11
|
+
export type Eval<A> = Generator<Signal, A, unknown>;
|
|
12
|
+
export type Completion = {
|
|
13
|
+
readonly ok: true;
|
|
14
|
+
readonly value: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
readonly ok: false;
|
|
17
|
+
readonly error: unknown;
|
|
18
|
+
};
|
|
19
|
+
export declare const normal: (value: unknown) => Completion;
|
|
20
|
+
export declare const abrupt: (error: unknown) => Completion;
|
|
21
|
+
/** Run `body` as a new activation; a boundary marks where a suspended body splits off the stack. */
|
|
22
|
+
export declare class Call {
|
|
23
|
+
readonly body: Eval<unknown>;
|
|
24
|
+
readonly boundary?: Boundary | undefined;
|
|
25
|
+
constructor(body: Eval<unknown>, boundary?: Boundary | undefined);
|
|
26
|
+
}
|
|
27
|
+
/** Push saved activations back on the stack and continue the top one with `input`. */
|
|
28
|
+
export declare class Enter {
|
|
29
|
+
readonly entries: Array<Entry>;
|
|
30
|
+
readonly input: Completion;
|
|
31
|
+
constructor(entries: Array<Entry>, input: Completion);
|
|
32
|
+
}
|
|
33
|
+
/** Suspend the innermost async body until `value` settles; a stall while it waits is reported at `node`. */
|
|
34
|
+
export declare class Await {
|
|
35
|
+
readonly value: Value;
|
|
36
|
+
readonly node?: AstNode | undefined;
|
|
37
|
+
constructor(value: Value, node?: AstNode | undefined);
|
|
38
|
+
}
|
|
39
|
+
/** Suspend the innermost generator body, producing `value`; it continues with the next request. */
|
|
40
|
+
export declare class Yield {
|
|
41
|
+
readonly value: Value;
|
|
42
|
+
constructor(value: Value);
|
|
43
|
+
}
|
|
44
|
+
/** Block the current job on host work: no other job runs until it ends, as with a synchronous host call. */
|
|
45
|
+
export declare class Host {
|
|
46
|
+
readonly effect: Effect.Effect<unknown, unknown>;
|
|
47
|
+
constructor(effect: Effect.Effect<unknown, unknown>);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
51
|
+
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
52
|
+
* program's body interrupts the execution.
|
|
53
|
+
*/
|
|
54
|
+
export declare class Interruption {
|
|
55
|
+
readonly cause: Cause.Cause<unknown>;
|
|
56
|
+
constructor(cause: Cause.Cause<unknown>);
|
|
57
|
+
}
|
|
58
|
+
/** Let the host run; the current job continues before any other job. */
|
|
59
|
+
export declare const Slice: unique symbol;
|
|
60
|
+
export type Signal = Call | Enter | Await | Yield | Host | typeof Slice;
|
|
61
|
+
/** What a generator body continues with: the `next`, `return`, or `throw` call that resumed it. */
|
|
62
|
+
export type GeneratorRequest = {
|
|
63
|
+
readonly kind: GeneratorRequestKind;
|
|
64
|
+
readonly value: Value;
|
|
65
|
+
};
|
|
66
|
+
/** Where the stack splits when the body above suspends, and what the activation below receives. */
|
|
67
|
+
export interface Boundary {
|
|
68
|
+
/** An `await` above suspends here: an async function, an async generator, or the program. */
|
|
69
|
+
readonly async: boolean;
|
|
70
|
+
/** A `yield` above suspends here. */
|
|
71
|
+
readonly generator: boolean;
|
|
72
|
+
/** The body above returned or threw. */
|
|
73
|
+
finish(completion: Completion): Completion;
|
|
74
|
+
/** The body above suspended at an `await`; its activations are saved in the continuation. */
|
|
75
|
+
suspended(): Completion;
|
|
76
|
+
/** The body above yielded; `split` takes its activations off the stack when it suspends. */
|
|
77
|
+
yielded(value: Value, split: () => Array<Entry>): Completion;
|
|
78
|
+
}
|
|
79
|
+
/** The boundary of an async body: the body's completion settles it, and the caller receives `result` at once. */
|
|
80
|
+
export declare class AsyncBoundary implements Boundary {
|
|
81
|
+
private readonly settle;
|
|
82
|
+
readonly result: Value;
|
|
83
|
+
readonly async = true;
|
|
84
|
+
readonly generator = false;
|
|
85
|
+
constructor(settle: (completion: Completion) => void, result: Value);
|
|
86
|
+
finish(completion: Completion): Completion;
|
|
87
|
+
suspended(): Completion;
|
|
88
|
+
yielded(): Completion;
|
|
89
|
+
}
|
|
90
|
+
export type Entry = {
|
|
91
|
+
readonly body: Eval<unknown>;
|
|
92
|
+
readonly boundary: Boundary | undefined;
|
|
93
|
+
};
|
|
94
|
+
/** Paused or running program code: activations on an explicit stack, innermost last. */
|
|
95
|
+
export declare class Task {
|
|
96
|
+
readonly stack: Array<Entry>;
|
|
97
|
+
constructor(stack: Array<Entry>);
|
|
98
|
+
}
|
|
99
|
+
/** A settled promise's reaction, run as a job: it either finishes at once or starts a task. */
|
|
100
|
+
export type Reaction = (settled: Completion) => Start | void;
|
|
101
|
+
export type Start = {
|
|
102
|
+
readonly task: Task;
|
|
103
|
+
readonly input: Completion;
|
|
104
|
+
};
|
|
105
|
+
/** The functions that settle one promise, at most once between them. */
|
|
106
|
+
export type Resolving = {
|
|
107
|
+
readonly resolve: (resolution: Value) => void;
|
|
108
|
+
readonly reject: (reason: unknown) => void;
|
|
109
|
+
};
|
|
110
|
+
type Job = () => Start | void;
|
|
111
|
+
/** Most activations one task may hold, as the call-stack limit. */
|
|
112
|
+
export declare const MAX_CALL_DEPTH = 10000;
|
|
113
|
+
export declare const isEval: <A>(value: A | Eval<A>) => value is Eval<A>;
|
|
114
|
+
/**
|
|
115
|
+
* One execution's job queue. Program code runs only inside a job, one job at a time and each to completion, as in a
|
|
116
|
+
* JavaScript engine: promise reactions and resumed awaits queue in order, host work runs concurrently and settles its
|
|
117
|
+
* promise as a later job, and the program is stuck exactly when no job is queued and no host work is running.
|
|
118
|
+
*/
|
|
119
|
+
export declare class Runner<R> {
|
|
120
|
+
private readonly ctx;
|
|
121
|
+
/** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
|
|
122
|
+
private readonly services;
|
|
123
|
+
/** The intrinsic `Promise.prototype.then`, so adopting a native promise skips exposing its rejection first. */
|
|
124
|
+
intrinsicThen: Callable | undefined;
|
|
125
|
+
private readonly queue;
|
|
126
|
+
private head;
|
|
127
|
+
private current;
|
|
128
|
+
private fuel;
|
|
129
|
+
private sliceStart;
|
|
130
|
+
private pending;
|
|
131
|
+
private nextID;
|
|
132
|
+
/** Rejections nothing handled yet, described when they happened so reporting them at the end is cheap. */
|
|
133
|
+
private readonly unhandled;
|
|
134
|
+
/** Bodies suspended at an `await` and the promise each waits on, so a stall can point at the one that is stuck. */
|
|
135
|
+
private readonly parked;
|
|
136
|
+
private readonly fibers;
|
|
137
|
+
private readonly inbox;
|
|
138
|
+
private wake;
|
|
139
|
+
private stopping;
|
|
140
|
+
constructor(ctx: Interpreter<R>,
|
|
141
|
+
/** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
|
|
142
|
+
services: Context.Context<R>);
|
|
143
|
+
/** Activations on the running task's stack. */
|
|
144
|
+
get depth(): number;
|
|
145
|
+
/**
|
|
146
|
+
* Runs the program as the first job until its body completes, then returns how it ended. Host work it started may
|
|
147
|
+
* still be running; `interrupt` stops it.
|
|
148
|
+
*/
|
|
149
|
+
run(main: Eval<unknown>): Effect.Effect<Completion, unknown, R>;
|
|
150
|
+
/** Stops host work still running, waiting for its cleanup. */
|
|
151
|
+
interrupt(): Effect.Effect<void>;
|
|
152
|
+
/**
|
|
153
|
+
* Rejections no handler observed, in the order their promises were created. Host work that finished before the
|
|
154
|
+
* program did settles first, so its failures count even though no later job ran.
|
|
155
|
+
*/
|
|
156
|
+
diagnostics(): Array<Diagnostic>;
|
|
157
|
+
/** Counts one interpreter step; true when the slice is over and the caller should yield `Slice`. */
|
|
158
|
+
tick(): boolean;
|
|
159
|
+
/** A blocking host call, with the caller's services provided so the runner can run it as is. */
|
|
160
|
+
blocking<A, E>(effect: Effect.Effect<A, E, R>): Host;
|
|
161
|
+
enqueue(job: Job): void;
|
|
162
|
+
/**
|
|
163
|
+
* Starts host work now; its completion settles through `settle` in a later job, in completion order. A defect is
|
|
164
|
+
* located at `site`, the call that started the work.
|
|
165
|
+
*/
|
|
166
|
+
host(effect: Effect.Effect<unknown, unknown, R>, settle: (completion: Completion) => void, site?: AstNode): void;
|
|
167
|
+
newPromise(): PromiseObj;
|
|
168
|
+
/**
|
|
169
|
+
* CreateResolvingFunctions, for host callers; `reject` takes the failure as thrown. A failure without a location is
|
|
170
|
+
* located at `site`, the call that created the promise.
|
|
171
|
+
*/
|
|
172
|
+
resolving(promise: PromiseObj, site?: AstNode): Resolving;
|
|
173
|
+
/** PerformPromiseThen with one reaction for either outcome; marks the promise handled. */
|
|
174
|
+
then(promise: PromiseObj, reaction: Reaction): void;
|
|
175
|
+
/** Removes a reaction an internal consumer no longer needs, so a long-pending promise does not keep it. */
|
|
176
|
+
detach(promise: PromiseObj, reaction: Reaction): void;
|
|
177
|
+
/** PromiseResolve(%Promise%, value). */
|
|
178
|
+
promiseResolve(value: Value): PromiseObj;
|
|
179
|
+
private settle;
|
|
180
|
+
private resolveWith;
|
|
181
|
+
private thenableJob;
|
|
182
|
+
private drive;
|
|
183
|
+
private stuckAwait;
|
|
184
|
+
private dequeue;
|
|
185
|
+
private get yieldToHost();
|
|
186
|
+
private get hostSettled();
|
|
187
|
+
}
|
|
188
|
+
export {};
|
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
import { Cause, Effect, Exit, Fiber } from "effect";
|
|
2
|
+
import { locate, normalizeError } from "./errors.js";
|
|
3
|
+
import { MAX_PENDING_PROMISES } from "./limits.js";
|
|
4
|
+
import { PendingThrow, rangeError, Throw, typeError } from "./model.js";
|
|
5
|
+
import { fn } from "./native.js";
|
|
6
|
+
import { get, Obj, PromiseObj } from "./objects.js";
|
|
7
|
+
import { typeofValue } from "./references.js";
|
|
8
|
+
export const normal = (value) => ({ ok: true, value });
|
|
9
|
+
export const abrupt = (error) => ({ ok: false, error });
|
|
10
|
+
/** Run `body` as a new activation; a boundary marks where a suspended body splits off the stack. */
|
|
11
|
+
export class Call {
|
|
12
|
+
body;
|
|
13
|
+
boundary;
|
|
14
|
+
constructor(body, boundary) {
|
|
15
|
+
this.body = body;
|
|
16
|
+
this.boundary = boundary;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Push saved activations back on the stack and continue the top one with `input`. */
|
|
20
|
+
export class Enter {
|
|
21
|
+
entries;
|
|
22
|
+
input;
|
|
23
|
+
constructor(entries, input) {
|
|
24
|
+
this.entries = entries;
|
|
25
|
+
this.input = input;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Suspend the innermost async body until `value` settles; a stall while it waits is reported at `node`. */
|
|
29
|
+
export class Await {
|
|
30
|
+
value;
|
|
31
|
+
node;
|
|
32
|
+
constructor(value, node) {
|
|
33
|
+
this.value = value;
|
|
34
|
+
this.node = node;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Suspend the innermost generator body, producing `value`; it continues with the next request. */
|
|
38
|
+
export class Yield {
|
|
39
|
+
value;
|
|
40
|
+
constructor(value) {
|
|
41
|
+
this.value = value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Block the current job on host work: no other job runs until it ends, as with a synchronous host call. */
|
|
45
|
+
export class Host {
|
|
46
|
+
effect;
|
|
47
|
+
constructor(effect) {
|
|
48
|
+
this.effect = effect;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Host work that was interrupted on its own. It settles its promise like a rejection, but program code cannot catch
|
|
53
|
+
* it: handlers, `catch`, and `finally` are skipped, an unobserved one is not reported, and one that reaches the
|
|
54
|
+
* program's body interrupts the execution.
|
|
55
|
+
*/
|
|
56
|
+
export class Interruption {
|
|
57
|
+
cause;
|
|
58
|
+
constructor(cause) {
|
|
59
|
+
this.cause = cause;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Let the host run; the current job continues before any other job. */
|
|
63
|
+
export const Slice = Symbol("codemode.slice");
|
|
64
|
+
/** The boundary of an async body: the body's completion settles it, and the caller receives `result` at once. */
|
|
65
|
+
export class AsyncBoundary {
|
|
66
|
+
settle;
|
|
67
|
+
result;
|
|
68
|
+
async = true;
|
|
69
|
+
generator = false;
|
|
70
|
+
constructor(settle, result) {
|
|
71
|
+
this.settle = settle;
|
|
72
|
+
this.result = result;
|
|
73
|
+
}
|
|
74
|
+
finish(completion) {
|
|
75
|
+
this.settle(completion);
|
|
76
|
+
return normal(this.result);
|
|
77
|
+
}
|
|
78
|
+
suspended() {
|
|
79
|
+
return normal(this.result);
|
|
80
|
+
}
|
|
81
|
+
yielded() {
|
|
82
|
+
return abrupt(typeError("yield is only valid inside a generator."));
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Paused or running program code: activations on an explicit stack, innermost last. */
|
|
86
|
+
export class Task {
|
|
87
|
+
stack;
|
|
88
|
+
constructor(stack) {
|
|
89
|
+
this.stack = stack;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** Most activations one task may hold, as the call-stack limit. */
|
|
93
|
+
export const MAX_CALL_DEPTH = 10_000;
|
|
94
|
+
/** Interpreter steps between clock checks, and how long one slice may run before the host gets a turn. */
|
|
95
|
+
const FUEL = 64;
|
|
96
|
+
const SLICE_MS = 1;
|
|
97
|
+
// Program values never have a `next` method of their own: an object's properties live in its property map.
|
|
98
|
+
export const isEval = (value) => typeof value === "object" && value !== null && typeof value.next === "function";
|
|
99
|
+
/**
|
|
100
|
+
* One execution's job queue. Program code runs only inside a job, one job at a time and each to completion, as in a
|
|
101
|
+
* JavaScript engine: promise reactions and resumed awaits queue in order, host work runs concurrently and settles its
|
|
102
|
+
* promise as a later job, and the program is stuck exactly when no job is queued and no host work is running.
|
|
103
|
+
*/
|
|
104
|
+
export class Runner {
|
|
105
|
+
ctx;
|
|
106
|
+
services;
|
|
107
|
+
/** The intrinsic `Promise.prototype.then`, so adopting a native promise skips exposing its rejection first. */
|
|
108
|
+
intrinsicThen;
|
|
109
|
+
queue = [];
|
|
110
|
+
head = 0;
|
|
111
|
+
current;
|
|
112
|
+
fuel = FUEL;
|
|
113
|
+
sliceStart = 0;
|
|
114
|
+
pending = 0;
|
|
115
|
+
nextID = 0;
|
|
116
|
+
/** Rejections nothing handled yet, described when they happened so reporting them at the end is cheap. */
|
|
117
|
+
unhandled = new Map();
|
|
118
|
+
/** Bodies suspended at an `await` and the promise each waits on, so a stall can point at the one that is stuck. */
|
|
119
|
+
parked = new Map();
|
|
120
|
+
fibers = new Set();
|
|
121
|
+
inbox = [];
|
|
122
|
+
wake;
|
|
123
|
+
stopping = false;
|
|
124
|
+
constructor(ctx,
|
|
125
|
+
/** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
|
|
126
|
+
services) {
|
|
127
|
+
this.ctx = ctx;
|
|
128
|
+
this.services = services;
|
|
129
|
+
}
|
|
130
|
+
/** Activations on the running task's stack. */
|
|
131
|
+
get depth() {
|
|
132
|
+
return this.current?.stack.length ?? 0;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Runs the program as the first job until its body completes, then returns how it ended. Host work it started may
|
|
136
|
+
* still be running; `interrupt` stops it.
|
|
137
|
+
*/
|
|
138
|
+
run(main) {
|
|
139
|
+
const self = this;
|
|
140
|
+
return Effect.gen(function* () {
|
|
141
|
+
let outcome;
|
|
142
|
+
let start = {
|
|
143
|
+
task: new Task([
|
|
144
|
+
{
|
|
145
|
+
body: main,
|
|
146
|
+
boundary: new AsyncBoundary((completion) => {
|
|
147
|
+
outcome = completion;
|
|
148
|
+
}, undefined),
|
|
149
|
+
},
|
|
150
|
+
]),
|
|
151
|
+
input: normal(undefined),
|
|
152
|
+
};
|
|
153
|
+
self.sliceStart = performance.now();
|
|
154
|
+
while (outcome === undefined) {
|
|
155
|
+
if (start === undefined) {
|
|
156
|
+
const job = self.dequeue();
|
|
157
|
+
if (job !== undefined) {
|
|
158
|
+
start = job() ?? undefined;
|
|
159
|
+
if (start === undefined && self.tick())
|
|
160
|
+
yield* self.yieldToHost;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const settle = self.inbox.shift();
|
|
164
|
+
if (settle !== undefined) {
|
|
165
|
+
settle();
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (self.fibers.size > 0) {
|
|
169
|
+
yield* self.hostSettled;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
return abrupt(new PendingThrow("Error", "The program is waiting on a promise that can never settle: nothing else is running, so nothing can resolve it. Code Mode has no timers or sleep, so don't wait or poll inside a program.", self.stuckAwait()));
|
|
173
|
+
}
|
|
174
|
+
const pause = self.drive(start.task, start.input);
|
|
175
|
+
if (pause === undefined) {
|
|
176
|
+
start = undefined;
|
|
177
|
+
if (self.tick())
|
|
178
|
+
yield* self.yieldToHost;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (pause.kind === "slice") {
|
|
182
|
+
start = { task: start.task, input: pause.input };
|
|
183
|
+
yield* self.yieldToHost;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const exit = yield* Effect.exit(pause.effect);
|
|
187
|
+
if (Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause))
|
|
188
|
+
return yield* Effect.failCause(exit.cause);
|
|
189
|
+
start = {
|
|
190
|
+
task: start.task,
|
|
191
|
+
input: Exit.isSuccess(exit) ? normal(exit.value) : abrupt(Cause.squash(exit.cause)),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
if (!outcome.ok && outcome.error instanceof Interruption)
|
|
195
|
+
return yield* Effect.failCause(outcome.error.cause);
|
|
196
|
+
return outcome;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/** Stops host work still running, waiting for its cleanup. */
|
|
200
|
+
interrupt() {
|
|
201
|
+
const self = this;
|
|
202
|
+
return Effect.gen(function* () {
|
|
203
|
+
self.stopping = true;
|
|
204
|
+
while (self.fibers.size > 0)
|
|
205
|
+
yield* Fiber.interruptAll([...self.fibers]);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Rejections no handler observed, in the order their promises were created. Host work that finished before the
|
|
210
|
+
* program did settles first, so its failures count even though no later job ran.
|
|
211
|
+
*/
|
|
212
|
+
diagnostics() {
|
|
213
|
+
for (let settle = this.inbox.shift(); settle !== undefined; settle = this.inbox.shift())
|
|
214
|
+
settle();
|
|
215
|
+
return [...this.unhandled].sort(([left], [right]) => left.id - right.id).map(([, diagnostic]) => diagnostic);
|
|
216
|
+
}
|
|
217
|
+
/** Counts one interpreter step; true when the slice is over and the caller should yield `Slice`. */
|
|
218
|
+
tick() {
|
|
219
|
+
if (--this.fuel > 0)
|
|
220
|
+
return false;
|
|
221
|
+
this.fuel = FUEL;
|
|
222
|
+
return performance.now() - this.sliceStart >= SLICE_MS;
|
|
223
|
+
}
|
|
224
|
+
/** A blocking host call, with the caller's services provided so the runner can run it as is. */
|
|
225
|
+
blocking(effect) {
|
|
226
|
+
return new Host(Effect.provideContext(effect, this.services));
|
|
227
|
+
}
|
|
228
|
+
enqueue(job) {
|
|
229
|
+
this.queue.push(job);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Starts host work now; its completion settles through `settle` in a later job, in completion order. A defect is
|
|
233
|
+
* located at `site`, the call that started the work.
|
|
234
|
+
*/
|
|
235
|
+
host(effect, settle, site) {
|
|
236
|
+
// Starts synchronously, so admission and hooks happen at the call site.
|
|
237
|
+
const fiber = Effect.runForkWith(this.services)(effect);
|
|
238
|
+
this.fibers.add(fiber);
|
|
239
|
+
fiber.addObserver((exit) => {
|
|
240
|
+
this.fibers.delete(fiber);
|
|
241
|
+
// Interrupted by the program ending: nothing is left to settle.
|
|
242
|
+
if (this.stopping)
|
|
243
|
+
return;
|
|
244
|
+
this.inbox.push(() => settle(Exit.isSuccess(exit)
|
|
245
|
+
? normal(exit.value)
|
|
246
|
+
: abrupt(Cause.hasInterruptsOnly(exit.cause)
|
|
247
|
+
? new Interruption(exit.cause)
|
|
248
|
+
: Cause.hasDies(exit.cause)
|
|
249
|
+
? locate(Cause.squash(exit.cause), site)
|
|
250
|
+
: Cause.squash(exit.cause))));
|
|
251
|
+
this.wake?.();
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// --- Promise abstract operations ---
|
|
255
|
+
newPromise() {
|
|
256
|
+
if (this.pending >= MAX_PENDING_PROMISES) {
|
|
257
|
+
throw rangeError(`Too many pending promises (limit ${MAX_PENDING_PROMISES}); await promises before creating more.`);
|
|
258
|
+
}
|
|
259
|
+
this.pending++;
|
|
260
|
+
return new PromiseObj(this.ctx.builtins.Promise, this.nextID++);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* CreateResolvingFunctions, for host callers; `reject` takes the failure as thrown. A failure without a location is
|
|
264
|
+
* located at `site`, the call that created the promise.
|
|
265
|
+
*/
|
|
266
|
+
resolving(promise, site) {
|
|
267
|
+
let done = false;
|
|
268
|
+
return {
|
|
269
|
+
resolve: (resolution) => {
|
|
270
|
+
if (done)
|
|
271
|
+
return;
|
|
272
|
+
done = true;
|
|
273
|
+
this.resolveWith(promise, resolution, site);
|
|
274
|
+
},
|
|
275
|
+
reject: (reason) => {
|
|
276
|
+
if (done)
|
|
277
|
+
return;
|
|
278
|
+
done = true;
|
|
279
|
+
this.settle(promise, abrupt(locate(reason, site)));
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/** PerformPromiseThen with one reaction for either outcome; marks the promise handled. */
|
|
284
|
+
then(promise, reaction) {
|
|
285
|
+
if (promise.state === "pending")
|
|
286
|
+
promise.reactions.push(reaction);
|
|
287
|
+
else {
|
|
288
|
+
if (promise.state === "rejected")
|
|
289
|
+
this.unhandled.delete(promise);
|
|
290
|
+
const settled = promise.state === "fulfilled" ? normal(promise.result) : abrupt(promise.result);
|
|
291
|
+
this.enqueue(() => reaction(settled));
|
|
292
|
+
}
|
|
293
|
+
promise.handled = true;
|
|
294
|
+
}
|
|
295
|
+
/** Removes a reaction an internal consumer no longer needs, so a long-pending promise does not keep it. */
|
|
296
|
+
detach(promise, reaction) {
|
|
297
|
+
const index = promise.reactions.indexOf(reaction);
|
|
298
|
+
if (index >= 0)
|
|
299
|
+
promise.reactions.splice(index, 1);
|
|
300
|
+
}
|
|
301
|
+
/** PromiseResolve(%Promise%, value). */
|
|
302
|
+
promiseResolve(value) {
|
|
303
|
+
if (value instanceof PromiseObj)
|
|
304
|
+
return value;
|
|
305
|
+
const promise = this.newPromise();
|
|
306
|
+
this.resolveWith(promise, value);
|
|
307
|
+
return promise;
|
|
308
|
+
}
|
|
309
|
+
settle(promise, completion) {
|
|
310
|
+
if (promise.state !== "pending")
|
|
311
|
+
return;
|
|
312
|
+
this.pending--;
|
|
313
|
+
promise.state = completion.ok ? "fulfilled" : "rejected";
|
|
314
|
+
promise.result = completion.ok ? completion.value : completion.error;
|
|
315
|
+
const reactions = promise.reactions;
|
|
316
|
+
promise.reactions = [];
|
|
317
|
+
if (!completion.ok && !promise.handled && !(completion.error instanceof Interruption)) {
|
|
318
|
+
const failure = normalizeError(completion.error);
|
|
319
|
+
this.unhandled.set(promise, {
|
|
320
|
+
...failure,
|
|
321
|
+
message: `Unhandled rejection from an un-awaited promise: ${failure.message}`,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
for (const reaction of reactions)
|
|
325
|
+
this.enqueue(() => reaction(completion));
|
|
326
|
+
}
|
|
327
|
+
resolveWith(promise, resolution, site) {
|
|
328
|
+
if (resolution === promise) {
|
|
329
|
+
const cycle = typeError("Chaining cycle detected: a promise cannot resolve with itself.", site);
|
|
330
|
+
return this.settle(promise, abrupt(cycle));
|
|
331
|
+
}
|
|
332
|
+
if (!(resolution instanceof Obj))
|
|
333
|
+
return this.settle(promise, normal(resolution));
|
|
334
|
+
const then = get(resolution, "then");
|
|
335
|
+
if (typeofValue(then) !== "function")
|
|
336
|
+
return this.settle(promise, normal(resolution));
|
|
337
|
+
this.enqueue(() => this.thenableJob(promise, resolution, then));
|
|
338
|
+
}
|
|
339
|
+
// NewPromiseResolveThenableJob. Adopting a native promise through the intrinsic `then` takes the same jobs as
|
|
340
|
+
// calling it, and keeps the rejection as thrown so its diagnostic survives.
|
|
341
|
+
thenableJob(promise, thenable, then) {
|
|
342
|
+
const resolving = this.resolving(promise);
|
|
343
|
+
if (then === this.intrinsicThen && thenable instanceof PromiseObj) {
|
|
344
|
+
this.then(thenable, (settled) => settled.ok ? resolving.resolve(settled.value) : resolving.reject(settled.error));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const builtins = this.ctx.builtins;
|
|
348
|
+
const resolve = fn(builtins, "", 1, (_, args) => {
|
|
349
|
+
resolving.resolve(args[0]);
|
|
350
|
+
return undefined;
|
|
351
|
+
});
|
|
352
|
+
const reject = fn(builtins, "", 1, (_, args) => {
|
|
353
|
+
resolving.reject(new Throw(args[0]));
|
|
354
|
+
return undefined;
|
|
355
|
+
});
|
|
356
|
+
const ctx = this.ctx;
|
|
357
|
+
const body = function* () {
|
|
358
|
+
try {
|
|
359
|
+
yield* ctx.call(then, thenable, [resolve, reject]);
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
resolving.reject(error);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
return { task: new Task([{ body: body(), boundary: undefined }]), input: normal(undefined) };
|
|
366
|
+
}
|
|
367
|
+
// --- Driver ---
|
|
368
|
+
// Runs a task until its stack empties or it pauses for a slice or host work, which resumes it before any other job.
|
|
369
|
+
drive(task, input) {
|
|
370
|
+
const stack = task.stack;
|
|
371
|
+
this.current = task;
|
|
372
|
+
let completion = input;
|
|
373
|
+
while (stack.length > 0) {
|
|
374
|
+
const entry = stack[stack.length - 1];
|
|
375
|
+
let step;
|
|
376
|
+
try {
|
|
377
|
+
step = completion.ok ? entry.body.next(completion.value) : entry.body.throw(completion.error);
|
|
378
|
+
}
|
|
379
|
+
catch (error) {
|
|
380
|
+
stack.pop();
|
|
381
|
+
completion = entry.boundary === undefined ? abrupt(error) : entry.boundary.finish(abrupt(error));
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (step.done) {
|
|
385
|
+
stack.pop();
|
|
386
|
+
completion = entry.boundary === undefined ? normal(step.value) : entry.boundary.finish(normal(step.value));
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
const signal = step.value;
|
|
390
|
+
if (signal instanceof Call) {
|
|
391
|
+
stack.push({ body: signal.body, boundary: signal.boundary });
|
|
392
|
+
completion = normal(undefined);
|
|
393
|
+
if (this.tick())
|
|
394
|
+
return { kind: "slice", input: completion };
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (signal instanceof Enter) {
|
|
398
|
+
for (const saved of signal.entries)
|
|
399
|
+
stack.push(saved);
|
|
400
|
+
completion = signal.input;
|
|
401
|
+
if (this.tick())
|
|
402
|
+
return { kind: "slice", input: completion };
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (signal instanceof Await) {
|
|
406
|
+
const index = stack.findLastIndex((item) => item.boundary?.async === true);
|
|
407
|
+
if (index < 0) {
|
|
408
|
+
completion = abrupt(typeError("await is only valid in async functions."));
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
let promise;
|
|
412
|
+
try {
|
|
413
|
+
promise = this.promiseResolve(signal.value);
|
|
414
|
+
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
completion = abrupt(error);
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
const boundary = stack[index].boundary;
|
|
420
|
+
const continuation = new Task(stack.splice(index));
|
|
421
|
+
this.parked.set(continuation, { promise, node: signal.node });
|
|
422
|
+
this.then(promise, (settled) => {
|
|
423
|
+
this.parked.delete(continuation);
|
|
424
|
+
return { task: continuation, input: settled };
|
|
425
|
+
});
|
|
426
|
+
completion = boundary.suspended();
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
if (signal instanceof Yield) {
|
|
430
|
+
const index = stack.findLastIndex((item) => item.boundary?.generator === true);
|
|
431
|
+
if (index < 0) {
|
|
432
|
+
completion = abrupt(typeError("yield is only valid inside a generator."));
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
completion = stack[index].boundary.yielded(signal.value, () => stack.splice(index));
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (signal instanceof Host)
|
|
439
|
+
return { kind: "host", effect: signal.effect };
|
|
440
|
+
return { kind: "slice", input: normal(undefined) };
|
|
441
|
+
}
|
|
442
|
+
this.current = undefined;
|
|
443
|
+
if (!completion.ok)
|
|
444
|
+
throw completion.error;
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
// The innermost stuck `await`: an outer one waits on the result of a parked async body, so prefer one that does not.
|
|
448
|
+
// Bodies that wait on each other in a cycle leave no such await; any located one is then as good as another.
|
|
449
|
+
stuckAwait() {
|
|
450
|
+
const results = new Set();
|
|
451
|
+
for (const task of this.parked.keys()) {
|
|
452
|
+
const boundary = task.stack[0]?.boundary;
|
|
453
|
+
if (boundary instanceof AsyncBoundary)
|
|
454
|
+
results.add(boundary.result);
|
|
455
|
+
}
|
|
456
|
+
const located = [...this.parked.values()].filter((parked) => parked.node !== undefined);
|
|
457
|
+
return (located.find((parked) => !results.has(parked.promise)) ?? located[0])?.node;
|
|
458
|
+
}
|
|
459
|
+
dequeue() {
|
|
460
|
+
if (this.head === this.queue.length) {
|
|
461
|
+
if (this.head > 0) {
|
|
462
|
+
this.queue.length = 0;
|
|
463
|
+
this.head = 0;
|
|
464
|
+
}
|
|
465
|
+
return undefined;
|
|
466
|
+
}
|
|
467
|
+
const job = this.queue[this.head++];
|
|
468
|
+
if (this.head > 4096 && this.head * 2 > this.queue.length) {
|
|
469
|
+
this.queue.splice(0, this.head);
|
|
470
|
+
this.head = 0;
|
|
471
|
+
}
|
|
472
|
+
return job;
|
|
473
|
+
}
|
|
474
|
+
get yieldToHost() {
|
|
475
|
+
return Effect.map(Effect.yieldNow, () => {
|
|
476
|
+
this.sliceStart = performance.now();
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
get hostSettled() {
|
|
480
|
+
return Effect.callback((resume) => {
|
|
481
|
+
if (this.inbox.length > 0)
|
|
482
|
+
return resume(Effect.void);
|
|
483
|
+
this.wake = () => {
|
|
484
|
+
this.wake = undefined;
|
|
485
|
+
resume(Effect.void);
|
|
486
|
+
};
|
|
487
|
+
return Effect.sync(() => {
|
|
488
|
+
this.wake = undefined;
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
package/dist/stdlib/array.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Eval } from "../interpreter/runner.js";
|
|
2
2
|
import { type Method } from "../interpreter/native.js";
|
|
3
3
|
import { Obj, type Value } from "../interpreter/objects.js";
|
|
4
4
|
import type { Interpreter } from "../interpreter/interpreter.js";
|
|
5
|
-
export declare
|
|
6
|
-
export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native
|
|
5
|
+
export declare function sortArray<R>(ctx: Interpreter<R>, target: Array<Value>, comparator: Value, name: string): Eval<Array<Value>>;
|
|
6
|
+
export declare const arrayGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
|
|
7
7
|
/**
|
|
8
8
|
* The callback methods Array and Uint8Array share. They fix the iteration length while reading existing elements
|
|
9
9
|
* live; `wrap` builds the collection `map` and `filter` return.
|