@opencode/codemode 0.0.0-dev-884 → 0.0.0-dev-900
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 +3 -0
- package/dist/codemode.d.ts +2 -0
- package/dist/codemode.js +10 -4
- package/dist/vm/async.d.ts +78 -0
- package/dist/vm/async.js +335 -0
- package/dist/vm/code.d.ts +122 -0
- package/dist/vm/code.js +407 -0
- package/dist/vm/compiler/compile.d.ts +13 -0
- package/dist/vm/compiler/compile.js +1477 -0
- package/dist/vm/compiler/scope.d.ts +74 -0
- package/dist/vm/compiler/scope.js +515 -0
- package/dist/vm/data.d.ts +7 -0
- package/dist/vm/data.js +13 -0
- package/dist/vm/generators.d.ts +42 -0
- package/dist/vm/generators.js +413 -0
- package/dist/vm/host.d.ts +103 -0
- package/dist/vm/host.js +226 -0
- package/dist/vm/index.d.ts +3 -0
- package/dist/vm/index.js +3 -0
- package/dist/vm/internal-prelude.d.ts +45 -0
- package/dist/vm/internal-prelude.js +534 -0
- package/dist/vm/opcodes.d.ts +222 -0
- package/dist/vm/opcodes.js +235 -0
- package/dist/vm/prelude/bytecode.d.ts +18 -0
- package/dist/vm/prelude/bytecode.js +31 -0
- package/dist/vm/prelude/bytecode.json +1 -0
- package/dist/vm/prelude/helpers.d.ts +8 -0
- package/dist/vm/prelude/helpers.js +258 -0
- package/dist/vm/prelude/prelude.d.ts +21 -0
- package/dist/vm/prelude/prelude.js +53 -0
- package/dist/vm/prelude/source.d.ts +1 -0
- package/dist/vm/prelude/source.js +652 -0
- package/dist/vm/runtime/callback.d.ts +25 -0
- package/dist/vm/runtime/callback.js +51 -0
- package/dist/vm/runtime/driver.d.ts +41 -0
- package/dist/vm/runtime/driver.js +142 -0
- package/dist/vm/runtime/errors.d.ts +23 -0
- package/dist/vm/runtime/errors.js +169 -0
- package/dist/vm/runtime/execute.d.ts +22 -0
- package/dist/vm/runtime/execute.js +278 -0
- package/dist/vm/runtime/extensions.d.ts +16 -0
- package/dist/vm/runtime/extensions.js +270 -0
- package/dist/vm/runtime/generators.d.ts +6 -0
- package/dist/vm/runtime/generators.js +44 -0
- package/dist/vm/runtime/globals.d.ts +6 -0
- package/dist/vm/runtime/globals.js +137 -0
- package/dist/vm/runtime/interpreter.d.ts +50 -0
- package/dist/vm/runtime/interpreter.js +94 -0
- package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
- package/dist/vm/runtime/intrinsic-names.js +166 -0
- package/dist/vm/runtime/intrinsics.d.ts +15 -0
- package/dist/vm/runtime/intrinsics.js +111 -0
- package/dist/vm/runtime/limits.d.ts +10 -0
- package/dist/vm/runtime/limits.js +19 -0
- package/dist/vm/runtime/model.d.ts +58 -0
- package/dist/vm/runtime/model.js +57 -0
- package/dist/vm/runtime/modules.d.ts +4 -0
- package/dist/vm/runtime/modules.js +33 -0
- package/dist/vm/runtime/native.d.ts +19 -0
- package/dist/vm/runtime/native.js +41 -0
- package/dist/vm/runtime/objects.d.ts +403 -0
- package/dist/vm/runtime/objects.js +784 -0
- package/dist/vm/runtime/operations.d.ts +49 -0
- package/dist/vm/runtime/operations.js +288 -0
- package/dist/vm/runtime/promises.d.ts +55 -0
- package/dist/vm/runtime/promises.js +232 -0
- package/dist/vm/runtime/references.d.ts +8 -0
- package/dist/vm/runtime/references.js +51 -0
- package/dist/vm/runtime/signals.d.ts +69 -0
- package/dist/vm/runtime/signals.js +68 -0
- package/dist/vm/runtime/source.d.ts +29 -0
- package/dist/vm/runtime/source.js +63 -0
- package/dist/vm/runtime/table.d.ts +43 -0
- package/dist/vm/runtime/table.js +132 -0
- package/dist/vm/scheduler.d.ts +178 -0
- package/dist/vm/scheduler.js +63 -0
- package/dist/vm/snapshot/codec.d.ts +88 -0
- package/dist/vm/snapshot/codec.js +752 -0
- package/dist/vm/snapshot/snapshot.d.ts +39 -0
- package/dist/vm/snapshot/snapshot.js +69 -0
- package/dist/vm/snapshot.d.ts +29 -0
- package/dist/vm/snapshot.js +304 -0
- package/dist/vm/stdlib/array.d.ts +16 -0
- package/dist/vm/stdlib/array.js +311 -0
- package/dist/vm/stdlib/bytes.d.ts +11 -0
- package/dist/vm/stdlib/bytes.js +222 -0
- package/dist/vm/stdlib/collections.d.ts +7 -0
- package/dist/vm/stdlib/collections.js +188 -0
- package/dist/vm/stdlib/console.d.ts +6 -0
- package/dist/vm/stdlib/console.js +93 -0
- package/dist/vm/stdlib/date.d.ts +2 -0
- package/dist/vm/stdlib/date.js +123 -0
- package/dist/vm/stdlib/headers.d.ts +2 -0
- package/dist/vm/stdlib/headers.js +92 -0
- package/dist/vm/stdlib/iterator.d.ts +2 -0
- package/dist/vm/stdlib/iterator.js +30 -0
- package/dist/vm/stdlib/json.d.ts +3 -0
- package/dist/vm/stdlib/json.js +42 -0
- package/dist/vm/stdlib/math.d.ts +3 -0
- package/dist/vm/stdlib/math.js +73 -0
- package/dist/vm/stdlib/number.d.ts +3 -0
- package/dist/vm/stdlib/number.js +72 -0
- package/dist/vm/stdlib/object.d.ts +6 -0
- package/dist/vm/stdlib/object.js +201 -0
- package/dist/vm/stdlib/regexp.d.ts +8 -0
- package/dist/vm/stdlib/regexp.js +119 -0
- package/dist/vm/stdlib/string.d.ts +2 -0
- package/dist/vm/stdlib/string.js +272 -0
- package/dist/vm/stdlib/url.d.ts +25 -0
- package/dist/vm/stdlib/url.js +255 -0
- package/dist/vm/stdlib/value.d.ts +6 -0
- package/dist/vm/stdlib/value.js +32 -0
- package/dist/vm/stdlib/web.d.ts +5 -0
- package/dist/vm/stdlib/web.js +97 -0
- package/dist/vm/vm.d.ts +224 -0
- package/dist/vm/vm.js +2921 -0
- package/package.json +5 -2
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { Throw, typeError } from "./runtime/model.js";
|
|
2
|
+
import { GeneratorObj, get, iteratorResult, Obj, Opaque, Position, } from "./runtime/objects.js";
|
|
3
|
+
import { readProperty, requireIteratorMethod, requireIteratorObject } from "./runtime/operations.js";
|
|
4
|
+
import { capability } from "./runtime/promises.js";
|
|
5
|
+
import { abrupt, Interruption, normal, Resume } from "./runtime/signals.js";
|
|
6
|
+
import { internalCode, VmIntrinsic } from "./internal-prelude.js";
|
|
7
|
+
import { CodeFlags, Op } from "./opcodes.js";
|
|
8
|
+
import { CustomIter, Mode } from "./vm.js";
|
|
9
|
+
/**
|
|
10
|
+
* CreateAsyncFromSyncIterator's object: an async iterator over a synchronous one, for `for await` and an async
|
|
11
|
+
* generator's `yield*`. Its methods are the `%AsyncFromSyncIterator%` intrinsics, so it is plain data.
|
|
12
|
+
*/
|
|
13
|
+
export class AsyncFromSyncIterator extends Opaque {
|
|
14
|
+
sync;
|
|
15
|
+
site;
|
|
16
|
+
tag = "AsyncFromSyncIterator";
|
|
17
|
+
constructor(proto,
|
|
18
|
+
/** The synchronous iterator it drives. */
|
|
19
|
+
sync,
|
|
20
|
+
/** Where it was made, so a failure that rejects one of its promises is located there. */
|
|
21
|
+
site) {
|
|
22
|
+
super(proto);
|
|
23
|
+
this.sync = sync;
|
|
24
|
+
this.site = site;
|
|
25
|
+
}
|
|
26
|
+
get describe() {
|
|
27
|
+
return "an iterator";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const result = (vm, value, done) => iteratorResult(vm.ctx.builtins.Object, value, done);
|
|
31
|
+
// A request reaches a resumed body as a number on its stack (see `RESUME`): its index here.
|
|
32
|
+
const REQUEST_KINDS = ["next", "throw", "return"];
|
|
33
|
+
const NO_THROW_METHOD = "The delegated iterator does not provide a throw() method.";
|
|
34
|
+
// Hands a resumed body its request: the value, then the kind's number.
|
|
35
|
+
const pushRequest = (frame, request) => {
|
|
36
|
+
frame.regs[frame.sp++] = request.value;
|
|
37
|
+
frame.regs[frame.sp++] = request.kind === "next" ? 0 : request.kind === "throw" ? 1 : 2;
|
|
38
|
+
};
|
|
39
|
+
// Takes the generator's body off the job stack, to continue after the instruction at `pc`.
|
|
40
|
+
const suspend = (vm, generator, frame, pc, sp) => {
|
|
41
|
+
generator.frame = frame;
|
|
42
|
+
frame.pc = pc + 1;
|
|
43
|
+
frame.sp = sp;
|
|
44
|
+
vm.frames.pop();
|
|
45
|
+
if (frame.counted)
|
|
46
|
+
vm.depth--;
|
|
47
|
+
};
|
|
48
|
+
// A synchronous generator's step answers the caller now on the stack: a `for...of` waiting at `ITER_STEP` takes the
|
|
49
|
+
// value or leaves the loop; any other caller receives an iterator result.
|
|
50
|
+
const answerCallerWide = (caller, code, pc, prevW, w, value, done) => {
|
|
51
|
+
if (done) {
|
|
52
|
+
caller.pc = ((prevW >>> 16) << 16) | (w >>> 16);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const prev2W = pc > 1 ? code[pc - 2] : 0;
|
|
56
|
+
const slot = (prev2W & 0xff) === Op.EXTRA
|
|
57
|
+
? ((prev2W >>> 16) << 16) | (prevW & 0xff00) | ((w >>> 8) & 0xff)
|
|
58
|
+
: (prevW & 0xff00) | ((w >>> 8) & 0xff);
|
|
59
|
+
caller.regs[slot].done = false;
|
|
60
|
+
caller.regs[caller.sp++] = value;
|
|
61
|
+
caller.pc = pc + ((code[pc + 1] & 0xff) === Op.EXTRA ? 3 : 2);
|
|
62
|
+
};
|
|
63
|
+
const answerCaller = (vm, value, done) => {
|
|
64
|
+
const caller = vm.frames[vm.frames.length - 1];
|
|
65
|
+
const pc = caller.pc;
|
|
66
|
+
const code = caller.code.code;
|
|
67
|
+
const w = code[pc];
|
|
68
|
+
if ((w & 0xff) !== Op.ITER_STEP) {
|
|
69
|
+
caller.regs[caller.sp++] = result(vm, value, done);
|
|
70
|
+
caller.pc = pc + 1;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const prevW = pc > 0 ? code[pc - 1] : 0;
|
|
74
|
+
if ((prevW & 0xff) === Op.EXTRA)
|
|
75
|
+
return answerCallerWide(caller, code, pc, prevW, w, value, done);
|
|
76
|
+
if (done) {
|
|
77
|
+
caller.pc = w >>> 16;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
;
|
|
81
|
+
caller.regs[(w >>> 8) & 0xff].done = false;
|
|
82
|
+
caller.regs[caller.sp++] = value;
|
|
83
|
+
caller.pc = pc + 2;
|
|
84
|
+
};
|
|
85
|
+
/** Generator.prototype.next/return/throw: the body runs on the caller's job stack until it yields, returns, or throws. */
|
|
86
|
+
export const generatorStep = (vm, generator, kind, value) => {
|
|
87
|
+
if (generator.state === "executing") {
|
|
88
|
+
throw typeError(generator.proto === vm.ctx.builtins.IteratorHelper
|
|
89
|
+
? "Iterator helper is already running."
|
|
90
|
+
: "Generator is already running.");
|
|
91
|
+
}
|
|
92
|
+
if (generator.state === "suspendedStart" && kind !== "next")
|
|
93
|
+
generator.state = "completed";
|
|
94
|
+
if (generator.state === "completed") {
|
|
95
|
+
if (kind === "throw")
|
|
96
|
+
throw new Throw(value);
|
|
97
|
+
return result(vm, kind === "return" ? value : undefined, true);
|
|
98
|
+
}
|
|
99
|
+
generator.state = "executing";
|
|
100
|
+
return new Resume(generator, { kind, value });
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* AsyncGenerator.prototype.next/return/throw: queue the request with its promise, and run the body now if it is
|
|
104
|
+
* waiting for one. A request that arrives while the body runs is answered when the body reaches it.
|
|
105
|
+
*/
|
|
106
|
+
export const generatorRequest = (vm, thisValue, kind, value) => {
|
|
107
|
+
const resolving = vm.scheduler.resolving(vm.scheduler.newPromise());
|
|
108
|
+
const promise = resolving.promise;
|
|
109
|
+
if (!(thisValue instanceof GeneratorObj) || !thisValue.asynchronous) {
|
|
110
|
+
resolving.reject(typeError(`AsyncGenerator.prototype.${kind} called on incompatible receiver.`));
|
|
111
|
+
return promise;
|
|
112
|
+
}
|
|
113
|
+
const generator = thisValue;
|
|
114
|
+
const state = generator.state;
|
|
115
|
+
if (kind === "next" && state === "completed") {
|
|
116
|
+
resolving.resolve(result(vm, undefined, true));
|
|
117
|
+
return promise;
|
|
118
|
+
}
|
|
119
|
+
if (kind === "throw") {
|
|
120
|
+
if (state === "suspendedStart")
|
|
121
|
+
generator.state = "completed";
|
|
122
|
+
if (generator.state === "completed") {
|
|
123
|
+
resolving.reject(new Throw(value));
|
|
124
|
+
return promise;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const queued = { kind, value };
|
|
128
|
+
generator.queue.push({ request: queued, resolving });
|
|
129
|
+
if (kind === "return" && (state === "suspendedStart" || state === "completed")) {
|
|
130
|
+
generator.state = "awaiting-return";
|
|
131
|
+
awaitReturn(vm, generator);
|
|
132
|
+
return promise;
|
|
133
|
+
}
|
|
134
|
+
if (state !== "suspendedStart" && state !== "suspendedYield")
|
|
135
|
+
return promise;
|
|
136
|
+
generator.state = "executing";
|
|
137
|
+
return new Resume(generator, queued);
|
|
138
|
+
};
|
|
139
|
+
// AsyncGeneratorCompleteStep: answer the oldest request.
|
|
140
|
+
const completeStep = (vm, generator, completion, done) => {
|
|
141
|
+
const next = generator.queue.shift();
|
|
142
|
+
if (completion.ok)
|
|
143
|
+
next.resolving.resolve(result(vm, completion.value, done));
|
|
144
|
+
else
|
|
145
|
+
next.resolving.reject(completion.error);
|
|
146
|
+
};
|
|
147
|
+
// AsyncGeneratorDrainQueue: once the body has ended, answer the remaining requests in order.
|
|
148
|
+
const drainQueue = (vm, generator) => {
|
|
149
|
+
generator.state = "draining";
|
|
150
|
+
while (true) {
|
|
151
|
+
const next = generator.queue[0];
|
|
152
|
+
if (next === undefined) {
|
|
153
|
+
generator.state = "completed";
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (next.request.kind === "return") {
|
|
157
|
+
generator.state = "awaiting-return";
|
|
158
|
+
return awaitReturn(vm, generator);
|
|
159
|
+
}
|
|
160
|
+
completeStep(vm, generator, next.request.kind === "throw" ? abrupt(new Throw(next.request.value)) : normal(undefined), true);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
// AsyncGeneratorAwaitReturn: a `return(value)` on an ended or unstarted generator settles once `value` does.
|
|
164
|
+
const awaitReturn = (vm, generator) => {
|
|
165
|
+
try {
|
|
166
|
+
vm.scheduler.then(vm.scheduler.promiseResolve(generator.queue[0].request.value), {
|
|
167
|
+
kind: "awaitReturn",
|
|
168
|
+
generator,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
finishAwaitReturn(vm, generator, abrupt(error));
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
/** The `awaitReturn` reaction: answers the `return` request with how its value settled, then drains the queue. */
|
|
176
|
+
export const finishAwaitReturn = (vm, generator, completion) => {
|
|
177
|
+
completeStep(vm, generator, completion, true);
|
|
178
|
+
drainQueue(vm, generator);
|
|
179
|
+
};
|
|
180
|
+
export const execGeneratorStart = (vm, frame, pc, sp) => {
|
|
181
|
+
const fn = frame.fn;
|
|
182
|
+
const isAsync = (frame.code.flags & CodeFlags.ASYNC) !== 0;
|
|
183
|
+
const proto = get(fn, "prototype");
|
|
184
|
+
const builtins = vm.ctx.builtins;
|
|
185
|
+
const gen = new GeneratorObj(proto instanceof Obj ? proto : isAsync ? builtins.AsyncGenerator : builtins.Generator, isAsync);
|
|
186
|
+
gen.state = "suspendedStart";
|
|
187
|
+
frame.mode = Mode.GENERATOR;
|
|
188
|
+
frame.target = gen;
|
|
189
|
+
suspend(vm, gen, frame, pc, sp);
|
|
190
|
+
const caller = vm.frames[vm.frames.length - 1];
|
|
191
|
+
caller.regs[caller.sp++] = gen;
|
|
192
|
+
caller.pc++;
|
|
193
|
+
};
|
|
194
|
+
export const execYield = (vm, frame, pc, sp) => {
|
|
195
|
+
const v = frame.regs[--sp];
|
|
196
|
+
const gen = frame.target;
|
|
197
|
+
if (!gen.asynchronous) {
|
|
198
|
+
gen.state = "suspendedYield";
|
|
199
|
+
suspend(vm, gen, frame, pc, sp);
|
|
200
|
+
answerCaller(vm, v, false);
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
completeStep(vm, gen, normal(v), false);
|
|
204
|
+
const next = gen.queue[0];
|
|
205
|
+
if (next !== undefined) {
|
|
206
|
+
frame.sp = sp;
|
|
207
|
+
frame.pc = pc + 1;
|
|
208
|
+
pushRequest(frame, next.request);
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
gen.state = "suspendedYield";
|
|
212
|
+
suspend(vm, gen, frame, pc, sp);
|
|
213
|
+
vm.deliverAnswer(frame);
|
|
214
|
+
return true;
|
|
215
|
+
};
|
|
216
|
+
export const execResume = (frame, bx, pc, sp) => {
|
|
217
|
+
const kind = frame.regs[--sp];
|
|
218
|
+
if (kind === 0) {
|
|
219
|
+
frame.sp = sp;
|
|
220
|
+
return bx;
|
|
221
|
+
}
|
|
222
|
+
if (kind === 1) {
|
|
223
|
+
const errVal = frame.regs[--sp];
|
|
224
|
+
frame.pc = pc;
|
|
225
|
+
frame.sp = sp;
|
|
226
|
+
throw new Throw(errVal);
|
|
227
|
+
}
|
|
228
|
+
frame.sp = sp;
|
|
229
|
+
return pc + 1;
|
|
230
|
+
};
|
|
231
|
+
export const execAwait = (vm, frame, a, pc, sp) => {
|
|
232
|
+
const v = frame.regs[--sp];
|
|
233
|
+
frame.pc = pc;
|
|
234
|
+
frame.sp = sp;
|
|
235
|
+
const promise = vm.scheduler.promiseResolve(v);
|
|
236
|
+
const site = a === 1 ? undefined : frame.code.siteAt(pc);
|
|
237
|
+
frame.pc = pc + 1;
|
|
238
|
+
let bIdx = vm.frames.length - 1;
|
|
239
|
+
while (bIdx > 0 && vm.frames[bIdx].mode === Mode.CALLER) {
|
|
240
|
+
bIdx--;
|
|
241
|
+
}
|
|
242
|
+
const cut = vm.frames.splice(bIdx);
|
|
243
|
+
for (const f of cut) {
|
|
244
|
+
if (f.counted)
|
|
245
|
+
vm.depth--;
|
|
246
|
+
}
|
|
247
|
+
const root = cut[0];
|
|
248
|
+
const result = root.mode === Mode.ASYNC || root.mode === Mode.REJECT_ONLY
|
|
249
|
+
? root.target.promise
|
|
250
|
+
: root.mode === Mode.GENERATOR
|
|
251
|
+
? root.target.queue[0]?.resolving.promise
|
|
252
|
+
: undefined;
|
|
253
|
+
vm.deliverAnswer(root);
|
|
254
|
+
vm.scheduler.park(cut, promise, site, result);
|
|
255
|
+
};
|
|
256
|
+
export const execYstar = (frame, itSlot, bx, pc, sp) => {
|
|
257
|
+
const kind = frame.regs[sp - 1];
|
|
258
|
+
const recv = frame.regs[sp - 2];
|
|
259
|
+
const rec = frame.regs[itSlot];
|
|
260
|
+
const site = frame.code.siteAt(pc) ?? frame.origin;
|
|
261
|
+
if (kind === 0) {
|
|
262
|
+
frame.regs[sp - 2] = rec.iterator;
|
|
263
|
+
frame.regs[sp - 1] = rec.next;
|
|
264
|
+
frame.regs[sp] = recv;
|
|
265
|
+
frame.sp = sp + 1;
|
|
266
|
+
return bx;
|
|
267
|
+
}
|
|
268
|
+
const methodName = REQUEST_KINDS[kind];
|
|
269
|
+
const method = readProperty(rec.iterator, methodName, site);
|
|
270
|
+
if (method === undefined || method === null) {
|
|
271
|
+
frame.sp = sp - 1;
|
|
272
|
+
return pc + 1;
|
|
273
|
+
}
|
|
274
|
+
const callable = requireIteratorMethod(method, `Iterator ${methodName}`, site);
|
|
275
|
+
frame.regs[sp - 2] = rec.iterator;
|
|
276
|
+
frame.regs[sp - 1] = callable;
|
|
277
|
+
frame.regs[sp] = recv;
|
|
278
|
+
frame.sp = sp + 1;
|
|
279
|
+
return bx;
|
|
280
|
+
};
|
|
281
|
+
export const execYstarResult = (frame, kindSlot, bx, pc, sp) => {
|
|
282
|
+
const res = frame.regs[sp - 1];
|
|
283
|
+
const kind = frame.regs[kindSlot];
|
|
284
|
+
const site = frame.code.siteAt(pc) ?? frame.origin;
|
|
285
|
+
const obj = requireIteratorObject(res, `Iterator ${REQUEST_KINDS[kind]}() result`, site);
|
|
286
|
+
const done = Boolean(readProperty(obj, "done", site));
|
|
287
|
+
const value = readProperty(obj, "value", site);
|
|
288
|
+
frame.regs[sp - 1] = value;
|
|
289
|
+
return done ? bx : pc + 1;
|
|
290
|
+
};
|
|
291
|
+
/** Puts a suspended generator's body back on the job stack, handing it `request` unless it has not started yet. */
|
|
292
|
+
export const resumeVmGenerator = (vm, gen, request) => {
|
|
293
|
+
const f = gen.frame;
|
|
294
|
+
gen.frame = undefined;
|
|
295
|
+
if (gen.asynchronous)
|
|
296
|
+
f.answer = gen.queue[0].resolving.promise;
|
|
297
|
+
if ((f.code.code[f.pc - 1] & 0xff) !== Op.GENERATOR_START)
|
|
298
|
+
pushRequest(f, request);
|
|
299
|
+
if (f.counted)
|
|
300
|
+
vm.depth++;
|
|
301
|
+
vm.frames.push(f);
|
|
302
|
+
};
|
|
303
|
+
export const completeGeneratorReturn = (vm, frame, v) => {
|
|
304
|
+
const gen = frame.target;
|
|
305
|
+
vm.frames.pop();
|
|
306
|
+
gen.frame = undefined;
|
|
307
|
+
if (!gen.asynchronous) {
|
|
308
|
+
gen.state = "completed";
|
|
309
|
+
vm.release(frame);
|
|
310
|
+
answerCaller(vm, v, true);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
vm.deliverAnswer(frame);
|
|
314
|
+
vm.release(frame);
|
|
315
|
+
completeStep(vm, gen, normal(v), true);
|
|
316
|
+
drainQueue(vm, gen);
|
|
317
|
+
};
|
|
318
|
+
export const unwindGenerator = (vm, frame, error) => {
|
|
319
|
+
const gen = frame.target;
|
|
320
|
+
gen.frame = undefined;
|
|
321
|
+
if (!gen.asynchronous) {
|
|
322
|
+
gen.state = "completed";
|
|
323
|
+
vm.release(frame);
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
vm.deliverAnswer(frame);
|
|
327
|
+
vm.release(frame);
|
|
328
|
+
completeStep(vm, gen, abrupt(error), true);
|
|
329
|
+
drainQueue(vm, gen);
|
|
330
|
+
return true;
|
|
331
|
+
};
|
|
332
|
+
const settleAdapterStep = (vm, adapter, value, done, closeOnRejection, made) => {
|
|
333
|
+
const reaction = {
|
|
334
|
+
kind: "asyncFromSync",
|
|
335
|
+
made,
|
|
336
|
+
done,
|
|
337
|
+
close: closeOnRejection ? adapter.sync : undefined,
|
|
338
|
+
site: adapter.site,
|
|
339
|
+
};
|
|
340
|
+
let wrapper;
|
|
341
|
+
try {
|
|
342
|
+
wrapper = vm.scheduler.promiseResolve(value);
|
|
343
|
+
}
|
|
344
|
+
catch (error) {
|
|
345
|
+
if (!done && closeOnRejection && adapter.sync !== undefined && !(error instanceof Interruption)) {
|
|
346
|
+
vm.scheduler.enqueue({ kind: "reaction", reaction, settled: abrupt(error) });
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
made.reject(error);
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
vm.scheduler.then(wrapper, reaction);
|
|
354
|
+
};
|
|
355
|
+
export const handleAdapterPush = (vm, callerFrame, slots) => {
|
|
356
|
+
const adapter = slots[0];
|
|
357
|
+
const kind = slots[1];
|
|
358
|
+
const args = slots[2];
|
|
359
|
+
const site = adapter.site;
|
|
360
|
+
const made = capability(vm.ctx, site);
|
|
361
|
+
const sync = adapter.sync;
|
|
362
|
+
if (sync instanceof Position) {
|
|
363
|
+
if (kind === "next") {
|
|
364
|
+
const step = sync.step();
|
|
365
|
+
settleAdapterStep(vm, adapter, step.value, step.done, true, made);
|
|
366
|
+
}
|
|
367
|
+
else if (kind === "return")
|
|
368
|
+
made.resolve(result(vm, args[0], true));
|
|
369
|
+
else
|
|
370
|
+
made.reject(typeError(NO_THROW_METHOD, site));
|
|
371
|
+
callerFrame.regs[callerFrame.sp++] = made.promise;
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
vm.pushInternal(internalCode("$vmAsyncFromSync"), [adapter, kind, args.length > 0, args[0]], site, Mode.REJECT_ONLY, made, made.promise);
|
|
375
|
+
return 1;
|
|
376
|
+
};
|
|
377
|
+
export const registerGeneratorIntrinsics = (map, vm) => {
|
|
378
|
+
const ctx = vm.ctx;
|
|
379
|
+
const builtins = ctx.builtins;
|
|
380
|
+
const add = (name, fn) => {
|
|
381
|
+
map.set(name, new VmIntrinsic(name, fn));
|
|
382
|
+
};
|
|
383
|
+
// The `$vmAsyncFromSync` frame running now settles this.
|
|
384
|
+
const made = () => vm.frames[vm.frames.length - 1].target;
|
|
385
|
+
add("$WrapAsyncFromSync", ([syncVal]) => {
|
|
386
|
+
const site = vm.currentSite();
|
|
387
|
+
const obj = requireIteratorObject(syncVal, "Iterator method result", site);
|
|
388
|
+
const next = requireIteratorMethod(readProperty(obj, "next", site), "Iterator next", site);
|
|
389
|
+
return new AsyncFromSyncIterator(builtins.AsyncFromSyncIterator, { iterator: obj, next, asynchronous: false }, site);
|
|
390
|
+
});
|
|
391
|
+
add("$AfsGetIter", ([adapter]) => adapter.sync.iterator);
|
|
392
|
+
add("$AfsGetNext", ([adapter]) => adapter.sync.next);
|
|
393
|
+
add("$AfsGetMethod", ([adapter, iterator, kind]) => {
|
|
394
|
+
const site = adapter.site;
|
|
395
|
+
const method = readProperty(iterator, kind, site);
|
|
396
|
+
if (method === undefined || method === null)
|
|
397
|
+
return undefined;
|
|
398
|
+
return requireIteratorMethod(method, `Iterator ${kind}`, site);
|
|
399
|
+
});
|
|
400
|
+
add("$AfsCheckObj", ([adapter, res, kind]) => requireIteratorObject(res, `Iterator ${kind}() result`, adapter.site));
|
|
401
|
+
add("$AfsResolveDone", ([value]) => {
|
|
402
|
+
made().resolve(result(vm, value, true));
|
|
403
|
+
return undefined;
|
|
404
|
+
});
|
|
405
|
+
add("$AfsRejectNoThrow", ([adapter]) => {
|
|
406
|
+
made().reject(typeError(NO_THROW_METHOD, adapter.site));
|
|
407
|
+
return undefined;
|
|
408
|
+
});
|
|
409
|
+
add("$AfsSettle", ([adapter, value, done, close]) => {
|
|
410
|
+
settleAdapterStep(vm, adapter, value, Boolean(done), Boolean(close), made());
|
|
411
|
+
return undefined;
|
|
412
|
+
});
|
|
413
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Diagnostic } from "../codemode.js";
|
|
2
|
+
import { type Json } from "./data.js";
|
|
3
|
+
import type { Interpreter } from "./runtime/interpreter.js";
|
|
4
|
+
import { type AstNode } from "./runtime/model.js";
|
|
5
|
+
import { PromiseObj, type Value } from "./runtime/objects.js";
|
|
6
|
+
import type { Completion } from "./runtime/signals.js";
|
|
7
|
+
import { type ExtensionRequest, HostCall, type HostRequest, Resolving } from "./scheduler.js";
|
|
8
|
+
import type { Code } from "./code.js";
|
|
9
|
+
import { type FrameMode, type Vm } from "./vm.js";
|
|
10
|
+
/** What host calls need from the promise scheduler. */
|
|
11
|
+
export type Promises = {
|
|
12
|
+
newPromise(): PromiseObj;
|
|
13
|
+
resolving(promise: PromiseObj, site?: AstNode): Resolving;
|
|
14
|
+
/** Unhandled rejections, in promise creation order. */
|
|
15
|
+
diagnostics(): Array<Diagnostic>;
|
|
16
|
+
};
|
|
17
|
+
/** A native's answer asking the VM to stop the job until the blocking host call it queued completes. */
|
|
18
|
+
export declare const BLOCK: unique symbol;
|
|
19
|
+
/** A host operation pushed an internal frame that produces its result. */
|
|
20
|
+
export declare const ENTERED: unique symbol;
|
|
21
|
+
export type HostResult = Value | typeof BLOCK | typeof ENTERED;
|
|
22
|
+
/** What the VM gives host operations: its runtime, and a way to run an internal prelude function at the call. */
|
|
23
|
+
export type HostVm = {
|
|
24
|
+
readonly ctx: Interpreter<unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* Runs an internal prelude function at the call `origin`; its result is the call's. In another `mode` than `CALLER`,
|
|
27
|
+
* it settles `target`, and the call's result is `answer`: in `REJECT_ONLY` mode, a failure in it rejects `target`.
|
|
28
|
+
*/
|
|
29
|
+
pushInternal(code: Code, args: ReadonlyArray<unknown>, origin: AstNode | undefined, mode?: FrameMode, target?: unknown, answer?: Value): unknown;
|
|
30
|
+
};
|
|
31
|
+
type Delivery = {
|
|
32
|
+
readonly call: HostCall;
|
|
33
|
+
readonly completion: Completion;
|
|
34
|
+
};
|
|
35
|
+
/** Visited nodes a native fast path may spend before it hands the work to the prelude walk, which slices. */
|
|
36
|
+
export declare const VISIT_BUDGET = 2048;
|
|
37
|
+
/**
|
|
38
|
+
* The host calls of one run: queued (written ahead, for the driver to start at the next stop), running, completed and
|
|
39
|
+
* waiting in the inbox, and the blocking call the current job waits on. Ids count up in program order.
|
|
40
|
+
*/
|
|
41
|
+
export declare class HostCalls {
|
|
42
|
+
private readonly ctx;
|
|
43
|
+
readonly promises: Promises;
|
|
44
|
+
queued: Array<HostCall>;
|
|
45
|
+
readonly running: Map<number, HostCall>;
|
|
46
|
+
readonly inbox: Array<Delivery>;
|
|
47
|
+
nextID: number;
|
|
48
|
+
/** The blocking call the job on the stack waits on. */
|
|
49
|
+
blocked: HostCall | undefined;
|
|
50
|
+
/** A completed blocking call's outcome, which continues the job on the stack before anything else runs. */
|
|
51
|
+
input: {
|
|
52
|
+
readonly call: HostCall;
|
|
53
|
+
readonly completion: Completion;
|
|
54
|
+
} | undefined;
|
|
55
|
+
/** The synchronous extension call the program is in the middle of: see `Engine.activeExtension`. */
|
|
56
|
+
extension: {
|
|
57
|
+
readonly path: string;
|
|
58
|
+
} | undefined;
|
|
59
|
+
extensionResume: ((completion: Completion) => HostResult) | undefined;
|
|
60
|
+
constructor(ctx: Interpreter<unknown>, promises: Promises);
|
|
61
|
+
/** Queues concurrent host work; `resolving` settles its promise in a later job, in completion order. */
|
|
62
|
+
host(request: HostRequest, call: {
|
|
63
|
+
readonly label: string;
|
|
64
|
+
readonly result: "json" | "value";
|
|
65
|
+
readonly resolving: Resolving;
|
|
66
|
+
readonly site: AstNode | undefined;
|
|
67
|
+
}): HostCall;
|
|
68
|
+
/** Queues blocking `search` work: the job stops until it completes, then continues with its result. */
|
|
69
|
+
blockSearch(args: Array<Json | undefined>): typeof BLOCK;
|
|
70
|
+
/** Queues blocking extension work: the job stops until it completes, then continues with `resume`. */
|
|
71
|
+
blockExtension(call: ExtensionRequest, resume: (completion: Completion) => HostResult): typeof BLOCK;
|
|
72
|
+
private block;
|
|
73
|
+
private queue;
|
|
74
|
+
/** Continues the job stopped at a blocking call with its outcome. */
|
|
75
|
+
resume(input: {
|
|
76
|
+
readonly call: HostCall;
|
|
77
|
+
readonly completion: Completion;
|
|
78
|
+
}): HostResult;
|
|
79
|
+
/** Hands the queued calls to the driver: they are running from now on. */
|
|
80
|
+
handOver(): Array<HostCall>;
|
|
81
|
+
complete(id: number, completion: Completion): void;
|
|
82
|
+
/** Settles the oldest completed concurrent call's promise; false when the inbox is empty. */
|
|
83
|
+
deliver(): boolean;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A tool call returns its promise at once. Plain-data arguments cross on the native fast path; anything else
|
|
87
|
+
* (`toJSON`, a promise, a deep or shared value) crosses through `$vmCross` at the call, which queues the call with the
|
|
88
|
+
* same `Resolving` or rejects it. Either way the call is queued once its arguments have crossed, and the driver starts
|
|
89
|
+
* it before any other job runs.
|
|
90
|
+
*/
|
|
91
|
+
export declare const callTool: (vm: HostVm, hosts: HostCalls, path: ReadonlyArray<string>, args: Array<Value>, site: AstNode | undefined) => HostResult;
|
|
92
|
+
/** `search(...)`: blocking; its result can be used directly, or awaited and chained like a Promise. */
|
|
93
|
+
export declare const callSearch: (vm: HostVm, hosts: HostCalls, args: Array<Value>, site: AstNode | undefined) => HostResult;
|
|
94
|
+
/**
|
|
95
|
+
* A synchronous extension function's call, staged here so its host values never enter a frame: the `extension.before`
|
|
96
|
+
* hook (blocking), then the host function; what it returns settles now (blocking) unless it is an Effect or a Promise,
|
|
97
|
+
* which the program holds a promise for. The call is active, holding live host values, until its result is a program
|
|
98
|
+
* value or its concurrent call is queued.
|
|
99
|
+
*/
|
|
100
|
+
export declare const callExtension: (ctx: Interpreter<unknown>, hosts: HostCalls, slots: ReadonlyArray<unknown>, site: AstNode | undefined) => HostResult;
|
|
101
|
+
/** The intrinsics the boundary walk calls to queue a tool or search host call. */
|
|
102
|
+
export declare const registerHostIntrinsics: (map: Map<string, unknown>, vm: Vm) => void;
|
|
103
|
+
export {};
|