@opencode/codemode 2.2.7 → 2.3.0

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 (197) hide show
  1. package/README.md +11 -13
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +8 -22
  4. package/dist/codemode.js +17 -38
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +8 -2
  8. package/dist/extension.js +9 -0
  9. package/dist/{vm/host.d.ts → host.d.ts} +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  12. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  13. package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.js +1 -1
  15. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  16. package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
  17. package/dist/{vm/runtime → runtime}/execute.js +11 -9
  18. package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
  19. package/dist/{vm/runtime → runtime}/extensions.js +39 -4
  20. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  21. package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
  22. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  23. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  24. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  25. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  27. package/dist/scan.js +2 -2
  28. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
  29. package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
  30. package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
  31. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
  32. package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
  33. package/dist/{vm/snapshot.js → snapshot.js} +1 -1
  34. package/dist/stdlib/array.d.ts +14 -9
  35. package/dist/stdlib/array.js +89 -276
  36. package/dist/stdlib/bytes.d.ts +11 -7
  37. package/dist/stdlib/bytes.js +112 -79
  38. package/dist/stdlib/collections.d.ts +7 -7
  39. package/dist/stdlib/collections.js +30 -339
  40. package/dist/stdlib/console.d.ts +2 -2
  41. package/dist/stdlib/console.js +3 -3
  42. package/dist/stdlib/date.d.ts +2 -2
  43. package/dist/stdlib/date.js +21 -30
  44. package/dist/stdlib/headers.d.ts +2 -2
  45. package/dist/stdlib/headers.js +15 -33
  46. package/dist/stdlib/iterator.d.ts +2 -2
  47. package/dist/stdlib/iterator.js +10 -302
  48. package/dist/stdlib/json.d.ts +2 -2
  49. package/dist/stdlib/json.js +15 -31
  50. package/dist/stdlib/math.d.ts +2 -7
  51. package/dist/stdlib/math.js +6 -30
  52. package/dist/stdlib/number.d.ts +3 -3
  53. package/dist/stdlib/number.js +8 -8
  54. package/dist/stdlib/object.d.ts +4 -4
  55. package/dist/stdlib/object.js +12 -30
  56. package/dist/stdlib/regexp.d.ts +6 -5
  57. package/dist/stdlib/regexp.js +18 -13
  58. package/dist/stdlib/string.d.ts +2 -2
  59. package/dist/stdlib/string.js +53 -53
  60. package/dist/stdlib/url.d.ts +21 -9
  61. package/dist/stdlib/url.js +55 -55
  62. package/dist/stdlib/value.d.ts +3 -4
  63. package/dist/stdlib/value.js +5 -6
  64. package/dist/stdlib/web.d.ts +4 -4
  65. package/dist/stdlib/web.js +7 -7
  66. package/dist/tool-runtime.d.ts +14 -13
  67. package/dist/tool-runtime.js +245 -53
  68. package/dist/tool-schema.d.ts +5 -0
  69. package/dist/tool-schema.js +36 -16
  70. package/dist/tool.d.ts +24 -9
  71. package/dist/tool.js +15 -22
  72. package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
  73. package/dist/{vm/vm.js → vm.js} +1 -1
  74. package/package.json +3 -5
  75. package/dist/interpreter/callback.d.ts +0 -35
  76. package/dist/interpreter/callback.js +0 -98
  77. package/dist/interpreter/errors.js +0 -173
  78. package/dist/interpreter/execute.d.ts +0 -14
  79. package/dist/interpreter/execute.js +0 -207
  80. package/dist/interpreter/extensions.d.ts +0 -9
  81. package/dist/interpreter/extensions.js +0 -248
  82. package/dist/interpreter/generators.d.ts +0 -3
  83. package/dist/interpreter/generators.js +0 -168
  84. package/dist/interpreter/globals.js +0 -144
  85. package/dist/interpreter/interpreter.d.ts +0 -48
  86. package/dist/interpreter/interpreter.js +0 -2253
  87. package/dist/interpreter/intrinsics.d.ts +0 -15
  88. package/dist/interpreter/intrinsics.js +0 -105
  89. package/dist/interpreter/model.d.ts +0 -68
  90. package/dist/interpreter/model.js +0 -54
  91. package/dist/interpreter/native.d.ts +0 -17
  92. package/dist/interpreter/native.js +0 -35
  93. package/dist/interpreter/objects.d.ts +0 -301
  94. package/dist/interpreter/objects.js +0 -592
  95. package/dist/interpreter/promises.d.ts +0 -18
  96. package/dist/interpreter/promises.js +0 -292
  97. package/dist/interpreter/runner.d.ts +0 -178
  98. package/dist/interpreter/runner.js +0 -480
  99. package/dist/interpreter/scope.d.ts +0 -16
  100. package/dist/interpreter/scope.js +0 -79
  101. package/dist/suspend-error.d.ts +0 -24
  102. package/dist/suspend-error.js +0 -25
  103. package/dist/vm/data.d.ts +0 -7
  104. package/dist/vm/data.js +0 -13
  105. package/dist/vm/index.d.ts +0 -6
  106. package/dist/vm/index.js +0 -6
  107. package/dist/vm/runtime/errors.d.ts +0 -23
  108. package/dist/vm/runtime/globals.d.ts +0 -6
  109. package/dist/vm/runtime/limits.d.ts +0 -10
  110. package/dist/vm/runtime/limits.js +0 -19
  111. package/dist/vm/runtime/modules.d.ts +0 -4
  112. package/dist/vm/runtime/modules.js +0 -33
  113. package/dist/vm/runtime/references.d.ts +0 -8
  114. package/dist/vm/runtime/references.js +0 -51
  115. package/dist/vm/stdlib/array.d.ts +0 -16
  116. package/dist/vm/stdlib/array.js +0 -311
  117. package/dist/vm/stdlib/bytes.d.ts +0 -11
  118. package/dist/vm/stdlib/bytes.js +0 -222
  119. package/dist/vm/stdlib/collections.d.ts +0 -7
  120. package/dist/vm/stdlib/collections.js +0 -188
  121. package/dist/vm/stdlib/console.d.ts +0 -6
  122. package/dist/vm/stdlib/console.js +0 -93
  123. package/dist/vm/stdlib/date.d.ts +0 -2
  124. package/dist/vm/stdlib/date.js +0 -123
  125. package/dist/vm/stdlib/headers.d.ts +0 -2
  126. package/dist/vm/stdlib/headers.js +0 -92
  127. package/dist/vm/stdlib/iterator.d.ts +0 -2
  128. package/dist/vm/stdlib/iterator.js +0 -30
  129. package/dist/vm/stdlib/json.d.ts +0 -3
  130. package/dist/vm/stdlib/json.js +0 -42
  131. package/dist/vm/stdlib/math.d.ts +0 -3
  132. package/dist/vm/stdlib/math.js +0 -73
  133. package/dist/vm/stdlib/number.d.ts +0 -3
  134. package/dist/vm/stdlib/number.js +0 -72
  135. package/dist/vm/stdlib/object.d.ts +0 -6
  136. package/dist/vm/stdlib/object.js +0 -201
  137. package/dist/vm/stdlib/regexp.d.ts +0 -8
  138. package/dist/vm/stdlib/regexp.js +0 -119
  139. package/dist/vm/stdlib/string.d.ts +0 -2
  140. package/dist/vm/stdlib/string.js +0 -272
  141. package/dist/vm/stdlib/url.d.ts +0 -25
  142. package/dist/vm/stdlib/url.js +0 -255
  143. package/dist/vm/stdlib/value.d.ts +0 -6
  144. package/dist/vm/stdlib/value.js +0 -32
  145. package/dist/vm/stdlib/web.d.ts +0 -5
  146. package/dist/vm/stdlib/web.js +0 -97
  147. /package/dist/{vm/async.js → async.js} +0 -0
  148. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  149. /package/dist/{vm/code.js → code.js} +0 -0
  150. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  151. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  152. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  153. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  154. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  155. /package/dist/{vm/generators.js → generators.js} +0 -0
  156. /package/dist/{vm/host.js → host.js} +0 -0
  157. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  158. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  159. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  160. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  161. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  163. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  164. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  165. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  166. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  168. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  169. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  170. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  172. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  173. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
  175. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  179. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  180. /package/dist/{interpreter → runtime}/limits.js +0 -0
  181. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  182. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  183. /package/dist/{interpreter → runtime}/modules.js +0 -0
  184. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  186. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  188. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  189. /package/dist/{interpreter → runtime}/references.js +0 -0
  190. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  192. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  194. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  196. /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -1,292 +0,0 @@
1
- import { Throw, typeError } from "./model.js";
2
- import { Callable, define, get, hidden, Arr, Fn, Obj, PromiseObj, record } from "./objects.js";
3
- import { constructor, fn, methods, native, receiver, requiresNew } from "./native.js";
4
- import { createAggregateErrorValue, materialize } from "./errors.js";
5
- import { describeValue, typeofValue } from "./references.js";
6
- import { Interruption, isSupportedCallback } from "./callback.js";
7
- import { normal, Task } from "./runner.js";
8
- // A `resolve`/`reject` handed to an executor or thenable: calling it settles the capability.
9
- const resolvingFunctions = (ctx, resolving) => ({
10
- resolve: fn(ctx.builtins, "resolve", 1, (_, args) => {
11
- resolving.resolve(args[0]);
12
- return undefined;
13
- }),
14
- reject: fn(ctx.builtins, "reject", 1, (_, args) => {
15
- resolving.reject(new Throw(args[0]));
16
- return undefined;
17
- }),
18
- });
19
- /**
20
- * NewPromiseCapability(%Promise%): a pending promise with the host functions that settle it once. A built-in's own
21
- * failure that rejects it is located at `site`, the call that created it.
22
- */
23
- export const capability = (ctx, site) => {
24
- const promise = ctx.runner.newPromise();
25
- return { promise, ...ctx.runner.resolving(promise, site) };
26
- };
27
- /** Settles `resolving` with a completion: a fulfillment value is resolved, so a thenable is adopted. */
28
- const settleWith = (resolving, completion) => completion.ok ? resolving.resolve(completion.value) : resolving.reject(completion.error);
29
- /**
30
- * A promise reaction that calls program code: the handler runs as a job, and its result or failure settles the
31
- * derived promise. A missing handler passes the outcome through.
32
- */
33
- const handlerReaction = (ctx, onFulfilled, onRejected, derived) => (settled) => {
34
- const handler = settled.ok ? onFulfilled : onRejected;
35
- if (handler === undefined || (!settled.ok && settled.error instanceof Interruption)) {
36
- if (derived !== undefined)
37
- settleWith(derived, settled);
38
- return;
39
- }
40
- const argument = settled.ok ? settled.value : materialize(ctx, settled.error);
41
- const body = function* () {
42
- let result;
43
- try {
44
- result = yield* ctx.call(handler, undefined, [argument]);
45
- }
46
- catch (error) {
47
- derived?.reject(error);
48
- return;
49
- }
50
- derived?.resolve(result);
51
- };
52
- return { task: new Task([{ body: body(), boundary: undefined }]), input: normal(undefined) };
53
- };
54
- const promiseStatics = ["all", "allSettled", "race", "any", "resolve", "reject", "withResolvers", "try"];
55
- function* invokePromiseMethod(ctx, name, args, site) {
56
- const runner = ctx.runner;
57
- if (name === "resolve")
58
- return runner.promiseResolve(args[0]);
59
- if (name === "reject") {
60
- const made = capability(ctx, site);
61
- made.reject(new Throw(args[0]));
62
- return made.promise;
63
- }
64
- if (name === "withResolvers") {
65
- const made = capability(ctx, site);
66
- const functions = resolvingFunctions(ctx, made);
67
- return record(ctx.builtins.Object, { promise: made.promise, resolve: functions.resolve, reject: functions.reject });
68
- }
69
- if (name === "try") {
70
- if (typeofValue(args[0]) !== "function") {
71
- throw typeError(`Promise.try expects a function, received ${describeValue(args[0])}.`);
72
- }
73
- const made = capability(ctx, site);
74
- try {
75
- made.resolve(yield* ctx.call(args[0], undefined, args.slice(1)));
76
- }
77
- catch (error) {
78
- made.reject(error);
79
- }
80
- return made.promise;
81
- }
82
- return yield* combinator(ctx, name, args[0], site);
83
- }
84
- // Promise.all, allSettled, race, and any: each element is resolved to a promise and observed through a reaction, so
85
- // the result settles in the job where the deciding element's reaction runs, or at once for an empty input. A failure
86
- // while iterating rejects the result.
87
- function* combinator(ctx, name, iterable, site) {
88
- const runner = ctx.runner;
89
- const made = capability(ctx, site);
90
- try {
91
- const cursor = yield* ctx.iterate(iterable);
92
- if (cursor === undefined) {
93
- throw typeError(`Promise.${name} expects a synchronous iterable, received ${describeValue(iterable)}.`);
94
- }
95
- const values = [];
96
- const reasons = [];
97
- // Once the result settles early, the remaining members' reactions can do nothing: detach them so a member that
98
- // stays pending for the rest of the program does not keep them.
99
- const attached = [];
100
- const decided = (settle) => {
101
- settle();
102
- for (const [item, reaction] of attached)
103
- runner.detach(item, reaction);
104
- };
105
- let remaining = 1;
106
- let index = 0;
107
- const finish = () => {
108
- if (--remaining > 0)
109
- return;
110
- if (name === "any") {
111
- made.reject(new Throw(createAggregateErrorValue(ctx, reasons, "All promises were rejected")));
112
- return;
113
- }
114
- made.resolve(new Arr(ctx.builtins.Array, values));
115
- };
116
- while (true) {
117
- const step = yield* cursor.next();
118
- if (step.done)
119
- break;
120
- const item = runner.promiseResolve(step.value);
121
- const at = index++;
122
- values.push(undefined);
123
- reasons.push(undefined);
124
- remaining++;
125
- let called = false;
126
- const reaction = (settled) => {
127
- if (called)
128
- return;
129
- called = true;
130
- if (name === "race" || (!settled.ok && settled.error instanceof Interruption)) {
131
- return decided(() => settleWith(made, settled));
132
- }
133
- if (name === "all") {
134
- if (!settled.ok)
135
- return decided(() => made.reject(settled.error));
136
- values[at] = settled.value;
137
- return finish();
138
- }
139
- if (name === "any") {
140
- if (settled.ok)
141
- return decided(() => made.resolve(settled.value));
142
- reasons[at] = materialize(ctx, settled.error);
143
- return finish();
144
- }
145
- values[at] = settled.ok
146
- ? record(ctx.builtins.Object, { status: "fulfilled", value: settled.value })
147
- : record(ctx.builtins.Object, { status: "rejected", reason: materialize(ctx, settled.error) });
148
- finish();
149
- };
150
- attached.push([item, reaction]);
151
- runner.then(item, reaction);
152
- }
153
- if (name === "race" && index === 0) {
154
- throw typeError("Promise.race([]) would never settle; provide at least one promise or value.");
155
- }
156
- finish();
157
- }
158
- catch (error) {
159
- made.reject(error);
160
- }
161
- return made.promise;
162
- }
163
- const instanceMethod = (ctx, name, thisValue, args, site) => {
164
- const method = `Promise.prototype.${name}`;
165
- const promise = receiver(PromiseObj, thisValue, method);
166
- if (name === "finally")
167
- return chainFinally(ctx, promise, reactionHandler(args[0], method), site);
168
- const onFulfilled = name === "then" ? reactionHandler(args[0], method) : undefined;
169
- const onRejected = reactionHandler(name === "then" ? args[1] : args[0], method);
170
- const made = capability(ctx, site);
171
- ctx.runner.then(promise, handlerReaction(ctx, onFulfilled, onRejected, made));
172
- return made.promise;
173
- };
174
- // Promise.prototype.finally: the cleanup runs as a reaction, then the original outcome passes through once the
175
- // cleanup's own result settles; a failing cleanup wins.
176
- const chainFinally = (ctx, source, cleanup, site) => {
177
- const runner = ctx.runner;
178
- const made = capability(ctx, site);
179
- if (cleanup === undefined) {
180
- runner.then(source, (settled) => settleWith(made, settled));
181
- return made.promise;
182
- }
183
- runner.then(source, (settled) => {
184
- if (!settled.ok && settled.error instanceof Interruption)
185
- return made.reject(settled.error);
186
- const body = function* () {
187
- let result;
188
- try {
189
- result = yield* ctx.call(cleanup, undefined, []);
190
- }
191
- catch (error) {
192
- made.reject(error);
193
- return;
194
- }
195
- let after;
196
- try {
197
- after = runner.promiseResolve(result);
198
- }
199
- catch (error) {
200
- made.reject(error);
201
- return;
202
- }
203
- // The spec chains `then(valueThunk)` onto the cleanup's promise: one more job before the outcome passes.
204
- const derived = capability(ctx, site);
205
- runner.then(after, (cleaned) => (cleaned.ok ? derived.resolve(undefined) : derived.reject(cleaned.error)));
206
- runner.then(derived.promise, (cleaned) => settleWith(made, cleaned.ok ? settled : cleaned));
207
- };
208
- return { task: new Task([{ body: body(), boundary: undefined }]), input: normal(undefined) };
209
- });
210
- return made.promise;
211
- };
212
- const reactionHandler = (value, method) => {
213
- if (isSupportedCallback(value))
214
- return value;
215
- if (typeofValue(value) === "function") {
216
- throw typeError(`${method} cannot use this callable as a handler; wrap it in an arrow function, e.g. (value) => tools.ns.tool(value).`);
217
- }
218
- return undefined;
219
- };
220
- function* constructPromise(ctx, executor, site) {
221
- if (!(executor instanceof Fn)) {
222
- throw typeError("new Promise(...) expects an executor function (e.g. new Promise((resolve, reject) => { ... })).");
223
- }
224
- const made = capability(ctx, site);
225
- const functions = resolvingFunctions(ctx, made);
226
- try {
227
- yield* ctx.call(executor, undefined, [functions.resolve, functions.reject]);
228
- }
229
- catch (error) {
230
- made.reject(error);
231
- }
232
- return made.promise;
233
- }
234
- const promiseLikePrototypes = new WeakMap();
235
- /**
236
- * Lets a synchronous object result also be awaited or chained like a Promise. It inherits non-enumerable
237
- * then/catch/finally that settle with a plain copy, so data views never show them and awaiting does not recurse.
238
- */
239
- export const promiseLike = (ctx, value) => {
240
- if (!(value instanceof Obj) || value.proto !== ctx.builtins.Object)
241
- return value;
242
- value.proto = promiseLikePrototype(ctx);
243
- return value;
244
- };
245
- const promiseLikePrototype = (ctx) => {
246
- const cached = promiseLikePrototypes.get(ctx);
247
- if (cached !== undefined)
248
- return cached;
249
- const proto = new Obj(ctx.builtins.Object);
250
- const settled = (thisValue, name) => {
251
- const source = receiver(Obj, thisValue, name);
252
- const copy = new Obj(ctx.builtins.Object);
253
- for (const [key, slot] of source.props)
254
- copy.props.set(key, { ...slot });
255
- return ctx.runner.promiseResolve(copy);
256
- };
257
- methods(ctx.builtins, proto, ["then", "catch", "finally"].map((name) => [
258
- name,
259
- name === "then" ? 2 : 1,
260
- (thisValue, args, site) => instanceMethod(ctx, name, settled(thisValue, name), args, site),
261
- ]));
262
- promiseLikePrototypes.set(ctx, proto);
263
- return proto;
264
- };
265
- export const promiseGlobal = (ctx) => {
266
- const builtins = ctx.builtins;
267
- const proto = builtins.Promise;
268
- const promise = constructor(builtins, proto, {
269
- name: "Promise",
270
- length: 1,
271
- call: requiresNew("Promise"),
272
- construct: (args, _, site) => constructPromise(ctx, args[0], site),
273
- });
274
- // Combinators are not callbacks: `[p].map(Promise.resolve)` must ask for an arrow function.
275
- for (const name of promiseStatics) {
276
- define(promise, name, native(builtins, {
277
- name,
278
- length: 1,
279
- call: (_, args, site) => invokePromiseMethod(ctx, name, args, site),
280
- callback: false,
281
- }), hidden);
282
- }
283
- methods(builtins, proto, [
284
- ["then", 2, (thisValue, args, site) => instanceMethod(ctx, "then", thisValue, args, site)],
285
- ["catch", 1, (thisValue, args, site) => instanceMethod(ctx, "catch", thisValue, args, site)],
286
- ["finally", 1, (thisValue, args, site) => instanceMethod(ctx, "finally", thisValue, args, site)],
287
- ]);
288
- const then = get(proto, "then");
289
- if (then instanceof Callable)
290
- ctx.runner.intrinsicThen = then;
291
- return promise;
292
- };
@@ -1,178 +0,0 @@
1
- import { 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
- /** Let the host run; the current job continues before any other job. */
50
- export declare const Slice: unique symbol;
51
- export type Signal = Call | Enter | Await | Yield | Host | typeof Slice;
52
- /** What a generator body continues with: the `next`, `return`, or `throw` call that resumed it. */
53
- export type GeneratorRequest = {
54
- readonly kind: GeneratorRequestKind;
55
- readonly value: Value;
56
- };
57
- /** Where the stack splits when the body above suspends, and what the activation below receives. */
58
- export interface Boundary {
59
- /** An `await` above suspends here: an async function, an async generator, or the program. */
60
- readonly async: boolean;
61
- /** A `yield` above suspends here. */
62
- readonly generator: boolean;
63
- /** The body above returned or threw. */
64
- finish(completion: Completion): Completion;
65
- /** The body above suspended at an `await`; its activations are saved in the continuation. */
66
- suspended(): Completion;
67
- /** The body above yielded; `split` takes its activations off the stack when it suspends. */
68
- yielded(value: Value, split: () => Array<Entry>): Completion;
69
- }
70
- /** The boundary of an async body: the body's completion settles it, and the caller receives `result` at once. */
71
- export declare class AsyncBoundary implements Boundary {
72
- private readonly settle;
73
- readonly result: Value;
74
- readonly async = true;
75
- readonly generator = false;
76
- constructor(settle: (completion: Completion) => void, result: Value);
77
- finish(completion: Completion): Completion;
78
- suspended(): Completion;
79
- yielded(): Completion;
80
- }
81
- export type Entry = {
82
- readonly body: Eval<unknown>;
83
- readonly boundary: Boundary | undefined;
84
- };
85
- /** Paused or running program code: activations on an explicit stack, innermost last. */
86
- export declare class Task {
87
- readonly stack: Array<Entry>;
88
- constructor(stack: Array<Entry>);
89
- }
90
- /** A settled promise's reaction, run as a job: it either finishes at once or starts a task. */
91
- export type Reaction = (settled: Completion) => Start | void;
92
- export type Start = {
93
- readonly task: Task;
94
- readonly input: Completion;
95
- };
96
- /** The functions that settle one promise, at most once between them. */
97
- export type Resolving = {
98
- readonly resolve: (resolution: Value) => void;
99
- readonly reject: (reason: unknown) => void;
100
- };
101
- type Job = () => Start | void;
102
- /** Most activations one task may hold, as the call-stack limit. */
103
- export declare const MAX_CALL_DEPTH = 10000;
104
- /**
105
- * One execution's job queue. Program code runs only inside a job, one job at a time and each to completion, as in a
106
- * JavaScript engine: promise reactions and resumed awaits queue in order, host work runs concurrently and settles its
107
- * promise as a later job, and the program is stuck exactly when no job is queued and no host work is running.
108
- */
109
- export declare class Runner<R> {
110
- private readonly ctx;
111
- /** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
112
- private readonly services;
113
- /** The intrinsic `Promise.prototype.then`, so adopting a native promise skips exposing its rejection first. */
114
- intrinsicThen: Callable | undefined;
115
- private readonly queue;
116
- private head;
117
- private current;
118
- private fuel;
119
- private sliceStart;
120
- private pending;
121
- private nextID;
122
- /** Rejections nothing handled yet, described when they happened so reporting them at the end is cheap. */
123
- private readonly unhandled;
124
- /** Bodies suspended at an `await` and the promise each waits on, so a stall can point at the one that is stuck. */
125
- private readonly parked;
126
- private readonly fibers;
127
- private readonly inbox;
128
- private wake;
129
- private stopping;
130
- constructor(ctx: Interpreter<R>,
131
- /** The services host work runs with: the caller's, so tools see its context, tracing, and clock. */
132
- services: Context.Context<R>);
133
- /** Activations on the running task's stack. */
134
- get depth(): number;
135
- /**
136
- * Runs the program as the first job until its body completes, then returns how it ended. Host work it started may
137
- * still be running; `interrupt` stops it.
138
- */
139
- run(main: Eval<unknown>): Effect.Effect<Completion, unknown, R>;
140
- /** Stops host work still running, waiting for its cleanup. */
141
- interrupt(): Effect.Effect<void>;
142
- /**
143
- * Rejections no handler observed, in the order their promises were created. Host work that finished before the
144
- * program did settles first, so its failures count even though no later job ran.
145
- */
146
- diagnostics(): Array<Diagnostic>;
147
- /** Counts one interpreter step; true when the slice is over and the caller should yield `Slice`. */
148
- tick(): boolean;
149
- /** A blocking host call, with the caller's services provided so the runner can run it as is. */
150
- blocking<A, E>(effect: Effect.Effect<A, E, R>): Host;
151
- enqueue(job: Job): void;
152
- /**
153
- * Starts host work now; its completion settles through `settle` in a later job, in completion order. A defect is
154
- * located at `site`, the call that started the work.
155
- */
156
- host(effect: Effect.Effect<unknown, unknown, R>, settle: (completion: Completion) => void, site?: AstNode): void;
157
- newPromise(): PromiseObj;
158
- /**
159
- * CreateResolvingFunctions, for host callers; `reject` takes the failure as thrown. A failure without a location is
160
- * located at `site`, the call that created the promise.
161
- */
162
- resolving(promise: PromiseObj, site?: AstNode): Resolving;
163
- /** PerformPromiseThen with one reaction for either outcome; marks the promise handled. */
164
- then(promise: PromiseObj, reaction: Reaction): void;
165
- /** Removes a reaction an internal consumer no longer needs, so a long-pending promise does not keep it. */
166
- detach(promise: PromiseObj, reaction: Reaction): void;
167
- /** PromiseResolve(%Promise%, value). */
168
- promiseResolve(value: Value): PromiseObj;
169
- private settle;
170
- private resolveWith;
171
- private thenableJob;
172
- private drive;
173
- private stuckAwait;
174
- private dequeue;
175
- private get yieldToHost();
176
- private get hostSettled();
177
- }
178
- export {};