@opencode/codemode 2.2.5 → 2.2.7

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 (142) hide show
  1. package/README.md +164 -7
  2. package/dist/codemode.d.ts +60 -4
  3. package/dist/codemode.js +63 -8
  4. package/dist/extension.d.ts +27 -9
  5. package/dist/extension.js +7 -3
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/interpreter/callback.d.ts +12 -1
  9. package/dist/interpreter/callback.js +13 -1
  10. package/dist/interpreter/execute.d.ts +1 -1
  11. package/dist/interpreter/execute.js +16 -4
  12. package/dist/interpreter/extensions.d.ts +3 -3
  13. package/dist/interpreter/extensions.js +17 -11
  14. package/dist/interpreter/globals.js +10 -0
  15. package/dist/interpreter/interpreter.d.ts +11 -1
  16. package/dist/interpreter/interpreter.js +88 -4
  17. package/dist/interpreter/modules.d.ts +4 -0
  18. package/dist/interpreter/modules.js +33 -0
  19. package/dist/interpreter/promises.js +2 -2
  20. package/dist/interpreter/runner.d.ts +1 -11
  21. package/dist/interpreter/runner.js +1 -13
  22. package/dist/stdlib/iterator.js +1 -2
  23. package/dist/suspend-error.d.ts +24 -0
  24. package/dist/suspend-error.js +25 -0
  25. package/dist/tool-runtime.d.ts +31 -7
  26. package/dist/tool-runtime.js +116 -26
  27. package/dist/tool.d.ts +45 -2
  28. package/dist/tool.js +30 -7
  29. package/dist/vm/async.d.ts +78 -0
  30. package/dist/vm/async.js +335 -0
  31. package/dist/vm/code.d.ts +122 -0
  32. package/dist/vm/code.js +407 -0
  33. package/dist/vm/compiler/compile.d.ts +13 -0
  34. package/dist/vm/compiler/compile.js +1477 -0
  35. package/dist/vm/compiler/scope.d.ts +74 -0
  36. package/dist/vm/compiler/scope.js +515 -0
  37. package/dist/vm/data.d.ts +7 -0
  38. package/dist/vm/data.js +13 -0
  39. package/dist/vm/generators.d.ts +42 -0
  40. package/dist/vm/generators.js +413 -0
  41. package/dist/vm/host.d.ts +106 -0
  42. package/dist/vm/host.js +231 -0
  43. package/dist/vm/index.d.ts +6 -0
  44. package/dist/vm/index.js +6 -0
  45. package/dist/vm/internal-prelude.d.ts +45 -0
  46. package/dist/vm/internal-prelude.js +534 -0
  47. package/dist/vm/opcodes.d.ts +222 -0
  48. package/dist/vm/opcodes.js +235 -0
  49. package/dist/vm/prelude/bytecode.d.ts +18 -0
  50. package/dist/vm/prelude/bytecode.js +31 -0
  51. package/dist/vm/prelude/bytecode.json +1 -0
  52. package/dist/vm/prelude/helpers.d.ts +8 -0
  53. package/dist/vm/prelude/helpers.js +258 -0
  54. package/dist/vm/prelude/prelude.d.ts +21 -0
  55. package/dist/vm/prelude/prelude.js +53 -0
  56. package/dist/vm/prelude/source.d.ts +1 -0
  57. package/dist/vm/prelude/source.js +652 -0
  58. package/dist/vm/runtime/callback.d.ts +25 -0
  59. package/dist/vm/runtime/callback.js +51 -0
  60. package/dist/vm/runtime/driver.d.ts +68 -0
  61. package/dist/vm/runtime/driver.js +254 -0
  62. package/dist/vm/runtime/errors.d.ts +23 -0
  63. package/dist/vm/runtime/errors.js +169 -0
  64. package/dist/vm/runtime/execute.d.ts +80 -0
  65. package/dist/vm/runtime/execute.js +442 -0
  66. package/dist/vm/runtime/extensions.d.ts +16 -0
  67. package/dist/vm/runtime/extensions.js +337 -0
  68. package/dist/vm/runtime/generators.d.ts +6 -0
  69. package/dist/vm/runtime/generators.js +44 -0
  70. package/dist/vm/runtime/globals.d.ts +6 -0
  71. package/dist/vm/runtime/globals.js +137 -0
  72. package/dist/vm/runtime/interpreter.d.ts +53 -0
  73. package/dist/vm/runtime/interpreter.js +96 -0
  74. package/dist/vm/runtime/intrinsic-names.d.ts +56 -0
  75. package/dist/vm/runtime/intrinsic-names.js +166 -0
  76. package/dist/vm/runtime/intrinsics.d.ts +15 -0
  77. package/dist/vm/runtime/intrinsics.js +111 -0
  78. package/dist/vm/runtime/limits.d.ts +10 -0
  79. package/dist/vm/runtime/limits.js +19 -0
  80. package/dist/vm/runtime/model.d.ts +58 -0
  81. package/dist/vm/runtime/model.js +57 -0
  82. package/dist/vm/runtime/modules.d.ts +4 -0
  83. package/dist/vm/runtime/modules.js +33 -0
  84. package/dist/vm/runtime/native.d.ts +19 -0
  85. package/dist/vm/runtime/native.js +41 -0
  86. package/dist/vm/runtime/objects.d.ts +403 -0
  87. package/dist/vm/runtime/objects.js +784 -0
  88. package/dist/vm/runtime/operations.d.ts +49 -0
  89. package/dist/vm/runtime/operations.js +288 -0
  90. package/dist/vm/runtime/promises.d.ts +55 -0
  91. package/dist/vm/runtime/promises.js +232 -0
  92. package/dist/vm/runtime/references.d.ts +8 -0
  93. package/dist/vm/runtime/references.js +51 -0
  94. package/dist/vm/runtime/signals.d.ts +69 -0
  95. package/dist/vm/runtime/signals.js +68 -0
  96. package/dist/vm/runtime/source.d.ts +29 -0
  97. package/dist/vm/runtime/source.js +63 -0
  98. package/dist/vm/runtime/table.d.ts +43 -0
  99. package/dist/vm/runtime/table.js +132 -0
  100. package/dist/vm/scheduler.d.ts +190 -0
  101. package/dist/vm/scheduler.js +74 -0
  102. package/dist/vm/snapshot/codec.d.ts +117 -0
  103. package/dist/vm/snapshot/codec.js +848 -0
  104. package/dist/vm/snapshot/snapshot.d.ts +53 -0
  105. package/dist/vm/snapshot/snapshot.js +74 -0
  106. package/dist/vm/snapshot.d.ts +32 -0
  107. package/dist/vm/snapshot.js +309 -0
  108. package/dist/vm/stdlib/array.d.ts +16 -0
  109. package/dist/vm/stdlib/array.js +311 -0
  110. package/dist/vm/stdlib/bytes.d.ts +11 -0
  111. package/dist/vm/stdlib/bytes.js +222 -0
  112. package/dist/vm/stdlib/collections.d.ts +7 -0
  113. package/dist/vm/stdlib/collections.js +188 -0
  114. package/dist/vm/stdlib/console.d.ts +6 -0
  115. package/dist/vm/stdlib/console.js +93 -0
  116. package/dist/vm/stdlib/date.d.ts +2 -0
  117. package/dist/vm/stdlib/date.js +123 -0
  118. package/dist/vm/stdlib/headers.d.ts +2 -0
  119. package/dist/vm/stdlib/headers.js +92 -0
  120. package/dist/vm/stdlib/iterator.d.ts +2 -0
  121. package/dist/vm/stdlib/iterator.js +30 -0
  122. package/dist/vm/stdlib/json.d.ts +3 -0
  123. package/dist/vm/stdlib/json.js +42 -0
  124. package/dist/vm/stdlib/math.d.ts +3 -0
  125. package/dist/vm/stdlib/math.js +73 -0
  126. package/dist/vm/stdlib/number.d.ts +3 -0
  127. package/dist/vm/stdlib/number.js +72 -0
  128. package/dist/vm/stdlib/object.d.ts +6 -0
  129. package/dist/vm/stdlib/object.js +201 -0
  130. package/dist/vm/stdlib/regexp.d.ts +8 -0
  131. package/dist/vm/stdlib/regexp.js +119 -0
  132. package/dist/vm/stdlib/string.d.ts +2 -0
  133. package/dist/vm/stdlib/string.js +272 -0
  134. package/dist/vm/stdlib/url.d.ts +25 -0
  135. package/dist/vm/stdlib/url.js +255 -0
  136. package/dist/vm/stdlib/value.d.ts +6 -0
  137. package/dist/vm/stdlib/value.js +32 -0
  138. package/dist/vm/stdlib/web.d.ts +5 -0
  139. package/dist/vm/stdlib/web.js +97 -0
  140. package/dist/vm/vm.d.ts +226 -0
  141. package/dist/vm/vm.js +2931 -0
  142. package/package.json +8 -5
@@ -0,0 +1,335 @@
1
+ import { normalizeError, locate } from "./runtime/errors.js";
2
+ import { MAX_PENDING_PROMISES } from "./runtime/limits.js";
3
+ import { IteratorSymbol, rangeError, typeError } from "./runtime/model.js";
4
+ import { Arr, BoundFn, Fn, get, iteratorResult, Obj, Position, PromiseObj, Resolver, } from "./runtime/objects.js";
5
+ import { builtinCursor, isThenable } from "./runtime/operations.js";
6
+ import { adoptThenable, capability, finishCombination, react } from "./runtime/promises.js";
7
+ import { describeValue, typeofValue } from "./runtime/references.js";
8
+ import { abrupt, Interruption, Invoke, MAX_CALL_DEPTH, normal } from "./runtime/signals.js";
9
+ import { Resolving } from "./scheduler.js";
10
+ import { internalCode, VmIntrinsic } from "./internal-prelude.js";
11
+ import { CodeFlags } from "./opcodes.js";
12
+ import { Mode } from "./vm.js";
13
+ /**
14
+ * Target for a `Promise.prototype.finally` cleanup frame running in `Mode.SETTLE`.
15
+ * When the cleanup completes with `result`, chains `finallyCleaned` and `finallyOutcome` reactions.
16
+ */
17
+ export class FinallyTarget {
18
+ vm;
19
+ made;
20
+ site;
21
+ outcome;
22
+ constructor(vm, made, site, outcome) {
23
+ this.vm = vm;
24
+ this.made = made;
25
+ this.site = site;
26
+ this.outcome = outcome;
27
+ }
28
+ resolve(result) {
29
+ const after = this.vm.scheduler.promiseResolve(result);
30
+ const derived = capability(this.vm.ctx, this.site);
31
+ this.vm.scheduler.then(after, { kind: "finallyCleaned", target: derived });
32
+ this.vm.scheduler.then(derived.promise, {
33
+ kind: "finallyOutcome",
34
+ target: this.made,
35
+ outcome: this.outcome,
36
+ });
37
+ }
38
+ reject(reason) {
39
+ this.made.reject(reason);
40
+ }
41
+ }
42
+ /**
43
+ * Target for `$vmAsyncFromSyncClose`: regardless of whether `iterator.return()` succeeds or throws,
44
+ * the original rejection reason wins.
45
+ */
46
+ export class AsyncFromSyncCloseTarget {
47
+ made;
48
+ reason;
49
+ constructor(made, reason) {
50
+ this.made = made;
51
+ this.reason = reason;
52
+ }
53
+ resolve(_value) {
54
+ this.made.reject(this.reason);
55
+ }
56
+ reject(_error) {
57
+ this.made.reject(this.reason);
58
+ }
59
+ }
60
+ export class VmScheduler {
61
+ vm;
62
+ intrinsicThen;
63
+ parked = new Map();
64
+ queue = [];
65
+ head = 0;
66
+ pending = 0;
67
+ nextID = 0;
68
+ unhandled = new Map();
69
+ constructor(vm) {
70
+ this.vm = vm;
71
+ }
72
+ get hasJobs() {
73
+ return this.head < this.queue.length;
74
+ }
75
+ newPromise() {
76
+ if (this.pending >= MAX_PENDING_PROMISES) {
77
+ throw rangeError(`Too many pending promises (limit ${MAX_PENDING_PROMISES}); await promises before creating more.`);
78
+ }
79
+ this.pending++;
80
+ return new PromiseObj(this.vm.ctx.builtins.Promise, this.nextID++);
81
+ }
82
+ resolving(promise, site) {
83
+ return new Resolving(this, promise, site);
84
+ }
85
+ then(promise, reaction) {
86
+ if (promise.state === "pending") {
87
+ promise.reactions.push(reaction);
88
+ }
89
+ else {
90
+ if (promise.state === "rejected")
91
+ this.unhandled.delete(promise);
92
+ const settled = promise.state === "fulfilled" ? normal(promise.result) : abrupt(promise.result);
93
+ this.enqueue({ kind: "reaction", reaction, settled });
94
+ }
95
+ promise.handled = true;
96
+ }
97
+ detach(promise, reaction) {
98
+ const index = promise.reactions.indexOf(reaction);
99
+ if (index >= 0)
100
+ promise.reactions.splice(index, 1);
101
+ }
102
+ promiseResolve(value) {
103
+ if (value instanceof PromiseObj)
104
+ return value;
105
+ const promise = this.newPromise();
106
+ this.resolveWith(promise, value);
107
+ return promise;
108
+ }
109
+ reject(promise, reason) {
110
+ this.settle(promise, abrupt(reason));
111
+ }
112
+ resolveWith(promise, resolution, site) {
113
+ if (resolution === promise) {
114
+ const cycle = typeError("Chaining cycle detected: a promise cannot resolve with itself.", site);
115
+ this.settle(promise, abrupt(cycle));
116
+ return;
117
+ }
118
+ if (!(resolution instanceof Obj)) {
119
+ this.settle(promise, normal(resolution));
120
+ return;
121
+ }
122
+ const then = get(resolution, "then");
123
+ if (typeofValue(then) !== "function") {
124
+ this.settle(promise, normal(resolution));
125
+ return;
126
+ }
127
+ this.enqueue({ kind: "thenable", promise, thenable: resolution, then });
128
+ }
129
+ settle(promise, completion) {
130
+ if (promise.state !== "pending")
131
+ return;
132
+ this.pending--;
133
+ promise.state = completion.ok ? "fulfilled" : "rejected";
134
+ promise.result = completion.ok ? completion.value : completion.error;
135
+ const reactions = promise.reactions;
136
+ promise.reactions = [];
137
+ if (!completion.ok && !promise.handled && !(completion.error instanceof Interruption)) {
138
+ const failure = normalizeError(completion.error);
139
+ this.unhandled.set(promise, {
140
+ ...failure,
141
+ message: `Unhandled rejection from an un-awaited promise: ${failure.message}`,
142
+ });
143
+ }
144
+ for (const reaction of reactions) {
145
+ this.enqueue({ kind: "reaction", reaction, settled: completion });
146
+ }
147
+ }
148
+ enqueue(job) {
149
+ this.queue.push(job);
150
+ }
151
+ dequeue() {
152
+ if (this.head === this.queue.length) {
153
+ if (this.head > 0) {
154
+ this.queue.length = 0;
155
+ this.head = 0;
156
+ }
157
+ return undefined;
158
+ }
159
+ const job = this.queue[this.head++];
160
+ if (this.head > 4096 && this.head * 2 > this.queue.length) {
161
+ this.queue.splice(0, this.head);
162
+ this.head = 0;
163
+ }
164
+ return job;
165
+ }
166
+ compact() {
167
+ this.queue.splice(0, this.head);
168
+ this.head = 0;
169
+ }
170
+ park(frames, promise, node, result) {
171
+ this.parked.set(frames[0], { promise, node, result });
172
+ this.then(promise, { kind: "await", frames });
173
+ }
174
+ stuckAwait() {
175
+ const results = new Set();
176
+ for (const parked of this.parked.values()) {
177
+ if (parked.result instanceof PromiseObj)
178
+ results.add(parked.result);
179
+ }
180
+ const located = [...this.parked.values()].filter((parked) => parked.node !== undefined);
181
+ return (located.find((parked) => !results.has(parked.promise)) ?? located[0])?.node;
182
+ }
183
+ diagnostics() {
184
+ return [...this.unhandled].sort(([left], [right]) => left.id - right.id).map(([, diagnostic]) => diagnostic);
185
+ }
186
+ beginJob(job) {
187
+ const vm = this.vm;
188
+ if (job.kind === "thenable") {
189
+ const call = adoptThenable(vm.ctx, job.promise, job.thenable, job.then);
190
+ if (call === undefined || call.kind !== "thenable")
191
+ return;
192
+ this.enterSettlingCall(undefined, call.then, call.thenable, call.functions, Mode.REJECT_ONLY, call.resolving, undefined, undefined);
193
+ return;
194
+ }
195
+ const reaction = job.reaction;
196
+ if (reaction.kind === "await") {
197
+ const frames = reaction.frames;
198
+ this.parked.delete(frames[0]);
199
+ for (const f of frames) {
200
+ if (f.counted)
201
+ vm.depth++;
202
+ vm.frames.push(f);
203
+ }
204
+ const top = frames[frames.length - 1];
205
+ if (job.settled.ok) {
206
+ top.regs[top.sp++] = job.settled.value;
207
+ return;
208
+ }
209
+ top.pc--;
210
+ vm.unwind(job.settled.error);
211
+ return;
212
+ }
213
+ if (reaction.kind === "asyncFromSync") {
214
+ if (job.settled.ok) {
215
+ reaction.made.resolve(iteratorResult(vm.ctx.builtins.Object, job.settled.value, reaction.done));
216
+ return;
217
+ }
218
+ if (reaction.done ||
219
+ reaction.close === undefined ||
220
+ job.settled.error instanceof Interruption ||
221
+ reaction.close instanceof Position) {
222
+ reaction.made.reject(job.settled.error);
223
+ return;
224
+ }
225
+ vm.pushInternal(internalCode("$vmAsyncFromSyncClose"), [reaction.close.iterator], reaction.site, Mode.SETTLE, new AsyncFromSyncCloseTarget(reaction.made, job.settled.error));
226
+ return;
227
+ }
228
+ const call = react(vm.ctx, reaction, job.settled);
229
+ if (call === undefined)
230
+ return;
231
+ if (call.kind === "handler") {
232
+ this.enterSettlingCall(undefined, call.handler, undefined, [call.argument], Mode.SETTLE, call.derived, undefined, undefined);
233
+ return;
234
+ }
235
+ if (call.kind === "finally") {
236
+ this.enterSettlingCall(undefined, call.cleanup, undefined, [], Mode.SETTLE, new FinallyTarget(vm, call.made, call.site, call.outcome), undefined, call.site);
237
+ }
238
+ }
239
+ enterSettlingCall(callerFrame, callable, thisVal, args, mode, target, answer, site) {
240
+ const vm = this.vm;
241
+ while (callable instanceof BoundFn) {
242
+ thisVal = callable.boundThis;
243
+ args = [...callable.boundArgs, ...args];
244
+ callable = callable.target;
245
+ }
246
+ if (callable instanceof Resolver) {
247
+ try {
248
+ const res = callable.invoke(thisVal, args);
249
+ if (mode === Mode.SETTLE)
250
+ target.resolve(res);
251
+ }
252
+ catch (err) {
253
+ target.reject(locate(err, site ?? vm.currentSite()));
254
+ }
255
+ if (answer !== undefined && callerFrame !== undefined) {
256
+ callerFrame.regs[callerFrame.sp++] = answer;
257
+ }
258
+ return 0;
259
+ }
260
+ if (callable instanceof Fn) {
261
+ const code = callable.code;
262
+ if ((code.flags & (CodeFlags.ASYNC | CodeFlags.GENERATOR)) === 0) {
263
+ const counted = (code.flags & CodeFlags.INTERNAL) === 0;
264
+ if (counted && vm.depth > MAX_CALL_DEPTH) {
265
+ target.reject(rangeError("Maximum call stack size exceeded", site ?? vm.currentSite()));
266
+ if (answer !== undefined && callerFrame !== undefined) {
267
+ callerFrame.regs[callerFrame.sp++] = answer;
268
+ }
269
+ return 0;
270
+ }
271
+ vm.pushFn(callable, thisVal, args, site ?? callerFrame?.origin, mode, target, answer);
272
+ return 1;
273
+ }
274
+ }
275
+ vm.pushInternal(internalCode("$vmSettleCall"), [callable, thisVal, new Arr(vm.ctx.builtins.Array, args)], site ?? callerFrame?.origin, mode, target, answer);
276
+ return 1;
277
+ }
278
+ handlePromisePush(callerFrame, kindName, slots, site) {
279
+ // An executor's result is ignored and only its failure rejects; `Promise.try` settles with its callback's outcome.
280
+ if (kindName === "Promise executor" || kindName === "Promise.try") {
281
+ const made = slots[0];
282
+ const mode = kindName === "Promise.try" ? Mode.SETTLE : Mode.REJECT_ONLY;
283
+ return this.enterSettlingCall(callerFrame, slots[1], undefined, slots[2], mode, made, made.promise, site);
284
+ }
285
+ if (kindName === "Promise combinator") {
286
+ const made = slots[0];
287
+ this.vm.pushInternal(internalCode("$vmCombine"), [slots[1], slots[2]], site ?? callerFrame.origin, Mode.REJECT_ONLY, made, made.promise);
288
+ return 1;
289
+ }
290
+ return undefined;
291
+ }
292
+ }
293
+ export const registerAsyncIntrinsics = (map, vm) => {
294
+ const ctx = vm.ctx;
295
+ const builtins = ctx.builtins;
296
+ const add = (name, fn) => {
297
+ map.set(name, new VmIntrinsic(name, fn));
298
+ };
299
+ add("$Apply", ([callable, thisVal, args]) => {
300
+ const argItems = args instanceof Arr ? [...args.items] : [];
301
+ return new Invoke(callable, thisVal, argItems, vm.currentSite());
302
+ });
303
+ add("$CombineStart", ([nameVal, items]) => {
304
+ const name = nameVal;
305
+ const isIter = builtinCursor(builtins, items) !== undefined ||
306
+ (items instanceof Obj && get(items, IteratorSymbol) !== undefined && get(items, IteratorSymbol) !== null);
307
+ if (!isIter) {
308
+ throw typeError(`Promise.${name} expects a synchronous iterable, received ${describeValue(items)}.`);
309
+ }
310
+ const top = vm.frames[vm.frames.length - 1];
311
+ const made = top.target;
312
+ const comb = { name, made, values: [], reasons: [], remaining: 1, attached: [] };
313
+ return comb;
314
+ });
315
+ add("$CombineAdd", ([combVal, item]) => {
316
+ const c = combVal;
317
+ const p = vm.scheduler.promiseResolve(item);
318
+ const reaction = { kind: "combinator", combination: c, index: c.values.length, called: false };
319
+ c.values.push(undefined);
320
+ c.reasons.push(undefined);
321
+ c.remaining++;
322
+ c.attached.push([p, reaction]);
323
+ vm.scheduler.then(p, reaction);
324
+ return undefined;
325
+ });
326
+ add("$CombineEnd", ([combVal]) => {
327
+ const c = combVal;
328
+ if (c.name === "race" && c.attached.length === 0) {
329
+ throw typeError("Promise.race([]) would never settle; provide at least one promise or value.");
330
+ }
331
+ finishCombination(ctx, c);
332
+ return undefined;
333
+ });
334
+ add("$IsThenable", ([val]) => isThenable(val));
335
+ };
@@ -0,0 +1,122 @@
1
+ import type { AstNode } from "./runtime/model.js";
2
+ import type { Source } from "./runtime/source.js";
3
+ import { type ConstValue, type OpCode } from "./opcodes.js";
4
+ type SourceRef = {
5
+ readonly id: string;
6
+ readonly hash: string;
7
+ };
8
+ export type Label = {
9
+ pc: number;
10
+ readonly patches: Array<number>;
11
+ depth: number | undefined;
12
+ };
13
+ /**
14
+ * Immutable compiled function or program body. Holds no per-run mutable state.
15
+ */
16
+ export declare class Code {
17
+ readonly source: SourceRef;
18
+ readonly index: number;
19
+ readonly name: string;
20
+ readonly flags: number;
21
+ readonly length: number;
22
+ readonly params: number;
23
+ readonly locals: number;
24
+ readonly maxStack: number;
25
+ readonly code: Int32Array;
26
+ readonly consts: ReadonlyArray<ConstValue>;
27
+ /** Per upvalue: `[from (0 = parent cell slot, 1 = parent upvalue), index]`. */
28
+ readonly captures: Int32Array;
29
+ /** Exception handlers `[start, end, target, depth]*`, innermost first (`end` exclusive). */
30
+ readonly handlers: Int32Array;
31
+ /** Per finally level: `[tagSlot, valSlot, entryDepth, n, target_0 … target_{n-1}]`. */
32
+ readonly finallies: Int32Array;
33
+ /** Offsets into `finallies` indexed by finally level. */
34
+ readonly finallyOffsets: Int32Array;
35
+ /** Run-length encoded `[pc, nodeIndex]*` sorted by `pc`. */
36
+ readonly sites: Int32Array;
37
+ readonly localNames: ReadonlyArray<string>;
38
+ readonly upvalNames: ReadonlyArray<string>;
39
+ readonly sourceObj: Source | undefined;
40
+ constructor(init: {
41
+ readonly source: SourceRef;
42
+ readonly index: number;
43
+ readonly name: string;
44
+ readonly flags: number;
45
+ readonly length: number;
46
+ readonly params: number;
47
+ readonly locals: number;
48
+ readonly maxStack: number;
49
+ readonly code: Int32Array;
50
+ readonly consts: ReadonlyArray<ConstValue>;
51
+ readonly captures: Int32Array;
52
+ readonly handlers: Int32Array;
53
+ readonly finallies: Int32Array;
54
+ readonly finallyOffsets: Int32Array;
55
+ readonly sites: Int32Array;
56
+ readonly localNames: ReadonlyArray<string>;
57
+ readonly upvalNames?: ReadonlyArray<string> | undefined;
58
+ readonly sourceObj?: Source | undefined;
59
+ });
60
+ /** Resolves the AST node index recorded for `pc`, or `-1` when none was recorded. */
61
+ nodeIndexAt(pc: number): number;
62
+ /** Resolves the AST node recorded for `pc`. */
63
+ siteAt(pc: number): AstNode | undefined;
64
+ /** The same code over another parse of its source, so its sites resolve to that tree's nodes. */
65
+ withSource(sourceObj: Source): Code;
66
+ }
67
+ export type CodeJson = {
68
+ readonly [field: string]: unknown;
69
+ };
70
+ /** Code as JSON, for bytecode compiled ahead of time: every field but the source tree, which `codeFromJson` is given. */
71
+ export declare const codeToJson: (code: Code) => CodeJson;
72
+ export declare const codeFromJson: (data: CodeJson, sourceObj: Source) => Code;
73
+ /** Raised when a narrow-jump function exceeds 16-bit jump range (`65,535` words) and must recompile in wide mode. */
74
+ export declare class CodeSizeOverflow extends RangeError {
75
+ constructor(message?: string);
76
+ }
77
+ export declare class CodeBuilder {
78
+ readonly source: SourceRef;
79
+ readonly sourceObj: Source | undefined;
80
+ readonly index: number;
81
+ readonly name: string;
82
+ flags: number;
83
+ readonly length: number;
84
+ readonly params: number;
85
+ locals: number;
86
+ readonly localNames: Array<string>;
87
+ readonly captures: Int32Array;
88
+ readonly upvalNames: ReadonlyArray<string>;
89
+ private readonly words;
90
+ private readonly sitePairs;
91
+ private lastNodeIndex;
92
+ private readonly constList;
93
+ private readonly pooled;
94
+ private readonly handlerWords;
95
+ private readonly finallyWords;
96
+ private readonly finallyLevelOffsets;
97
+ depth: number;
98
+ maxStack: number;
99
+ wideJumps: boolean;
100
+ forceExtra: boolean;
101
+ constructor(source: SourceRef, sourceObj: Source | undefined, index: number, name: string, flags: number, length: number, params: number, locals: number, localNames: Array<string>, captures: Int32Array, upvalNames?: ReadonlyArray<string>);
102
+ get pc(): number;
103
+ adjustDepth(delta: number): void;
104
+ setDepth(depth: number): void;
105
+ unreachable(): void;
106
+ constant(value: ConstValue): number;
107
+ private recordSite;
108
+ private pushOp;
109
+ emit(op: OpCode, a?: number, bx?: number, nodeIndex?: number, stackDelta?: number): number;
110
+ emitNumber(value: number, nodeIndex?: number): void;
111
+ emitString(value: string, nodeIndex?: number): void;
112
+ newLabel(depth?: number): Label;
113
+ mark(label: Label): void;
114
+ emitJump(op: OpCode, a: number, label: Label, nodeIndex?: number, jumpDelta?: number, fallDelta?: number | null): void;
115
+ addHandler(start: number, end: number, target: number, depth: number): void;
116
+ reserveFinally(): number;
117
+ setFinally(level: number, tagSlot: number, valSlot: number, entryDepth: number, targets: ReadonlyArray<number>): void;
118
+ finish(): Code;
119
+ }
120
+ /** Deterministic FNV-1a 64-bit hash over a compiled `Code[]` array. */
121
+ export declare const hashOf: (codes: ReadonlyArray<Code>) => string;
122
+ export {};