@opencode/codemode 0.0.0-dev-1179 → 0.0.0-dev-1189

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