@opencode/codemode 2.2.6 → 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 +115 -29
  2. package/dist/{vm/async.d.ts → async.d.ts} +1 -1
  3. package/dist/codemode.d.ts +13 -22
  4. package/dist/codemode.js +18 -39
  5. package/dist/data.d.ts +4 -19
  6. package/dist/data.js +1 -82
  7. package/dist/extension.d.ts +20 -1
  8. package/dist/extension.js +14 -1
  9. package/dist/{vm/host.d.ts → host.d.ts} +5 -2
  10. package/dist/{vm/host.js → host.js} +9 -4
  11. package/dist/index.d.ts +1 -1
  12. package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
  13. package/dist/{vm/prelude → prelude}/helpers.js +1 -1
  14. package/dist/{vm/runtime → runtime}/driver.d.ts +18 -8
  15. package/dist/{vm/runtime → runtime}/driver.js +90 -20
  16. package/dist/{vm/runtime → runtime}/errors.js +2 -2
  17. package/dist/{vm/runtime → runtime}/execute.d.ts +18 -9
  18. package/dist/{vm/runtime → runtime}/execute.js +45 -15
  19. package/dist/{vm/runtime → runtime}/extensions.d.ts +2 -2
  20. package/dist/{vm/runtime → runtime}/extensions.js +121 -19
  21. package/dist/{vm/runtime → runtime}/globals.js +1 -1
  22. package/dist/{vm/runtime → runtime}/interpreter.d.ts +4 -1
  23. package/dist/{vm/runtime → runtime}/interpreter.js +2 -0
  24. package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
  25. package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
  26. package/dist/{vm/runtime → runtime}/objects.js +1 -1
  27. package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
  28. package/dist/{vm/runtime → runtime}/operations.js +1 -1
  29. package/dist/scan.js +2 -2
  30. package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +16 -4
  31. package/dist/{vm/scheduler.js → scheduler.js} +13 -2
  32. package/dist/{vm/snapshot → snapshot}/codec.d.ts +37 -6
  33. package/dist/{vm/snapshot → snapshot}/codec.js +112 -8
  34. package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +13 -6
  35. package/dist/{vm/snapshot → snapshot}/snapshot.js +19 -4
  36. package/dist/{vm/snapshot.js → snapshot.js} +5 -2
  37. package/dist/stdlib/array.d.ts +14 -9
  38. package/dist/stdlib/array.js +89 -276
  39. package/dist/stdlib/bytes.d.ts +11 -7
  40. package/dist/stdlib/bytes.js +112 -79
  41. package/dist/stdlib/collections.d.ts +7 -7
  42. package/dist/stdlib/collections.js +30 -339
  43. package/dist/stdlib/console.d.ts +2 -2
  44. package/dist/stdlib/console.js +3 -3
  45. package/dist/stdlib/date.d.ts +2 -2
  46. package/dist/stdlib/date.js +21 -30
  47. package/dist/stdlib/headers.d.ts +2 -2
  48. package/dist/stdlib/headers.js +15 -33
  49. package/dist/stdlib/iterator.d.ts +2 -2
  50. package/dist/stdlib/iterator.js +10 -303
  51. package/dist/stdlib/json.d.ts +2 -2
  52. package/dist/stdlib/json.js +15 -31
  53. package/dist/stdlib/math.d.ts +2 -7
  54. package/dist/stdlib/math.js +6 -30
  55. package/dist/stdlib/number.d.ts +3 -3
  56. package/dist/stdlib/number.js +8 -8
  57. package/dist/stdlib/object.d.ts +4 -4
  58. package/dist/stdlib/object.js +12 -30
  59. package/dist/stdlib/regexp.d.ts +6 -5
  60. package/dist/stdlib/regexp.js +18 -13
  61. package/dist/stdlib/string.d.ts +2 -2
  62. package/dist/stdlib/string.js +53 -53
  63. package/dist/stdlib/url.d.ts +21 -9
  64. package/dist/stdlib/url.js +55 -55
  65. package/dist/stdlib/value.d.ts +3 -4
  66. package/dist/stdlib/value.js +5 -6
  67. package/dist/stdlib/web.d.ts +4 -4
  68. package/dist/stdlib/web.js +7 -7
  69. package/dist/tool-runtime.d.ts +34 -12
  70. package/dist/tool-runtime.js +350 -68
  71. package/dist/tool-schema.d.ts +5 -0
  72. package/dist/tool-schema.js +36 -16
  73. package/dist/tool.d.ts +60 -2
  74. package/dist/tool.js +17 -1
  75. package/dist/{vm/vm.d.ts → vm.d.ts} +4 -3
  76. package/dist/{vm/vm.js → vm.js} +1 -1
  77. package/package.json +5 -7
  78. package/dist/interpreter/callback.d.ts +0 -24
  79. package/dist/interpreter/callback.js +0 -86
  80. package/dist/interpreter/errors.js +0 -173
  81. package/dist/interpreter/execute.d.ts +0 -14
  82. package/dist/interpreter/execute.js +0 -207
  83. package/dist/interpreter/extensions.d.ts +0 -9
  84. package/dist/interpreter/extensions.js +0 -248
  85. package/dist/interpreter/generators.d.ts +0 -3
  86. package/dist/interpreter/generators.js +0 -168
  87. package/dist/interpreter/globals.js +0 -144
  88. package/dist/interpreter/interpreter.d.ts +0 -48
  89. package/dist/interpreter/interpreter.js +0 -2253
  90. package/dist/interpreter/intrinsics.d.ts +0 -15
  91. package/dist/interpreter/intrinsics.js +0 -105
  92. package/dist/interpreter/model.d.ts +0 -68
  93. package/dist/interpreter/model.js +0 -54
  94. package/dist/interpreter/native.d.ts +0 -17
  95. package/dist/interpreter/native.js +0 -35
  96. package/dist/interpreter/objects.d.ts +0 -301
  97. package/dist/interpreter/objects.js +0 -592
  98. package/dist/interpreter/promises.d.ts +0 -18
  99. package/dist/interpreter/promises.js +0 -292
  100. package/dist/interpreter/runner.d.ts +0 -188
  101. package/dist/interpreter/runner.js +0 -492
  102. package/dist/interpreter/scope.d.ts +0 -16
  103. package/dist/interpreter/scope.js +0 -79
  104. package/dist/suspend-error.d.ts +0 -23
  105. package/dist/suspend-error.js +0 -25
  106. package/dist/vm/data.d.ts +0 -7
  107. package/dist/vm/data.js +0 -13
  108. package/dist/vm/index.d.ts +0 -6
  109. package/dist/vm/index.js +0 -6
  110. package/dist/vm/runtime/errors.d.ts +0 -23
  111. package/dist/vm/runtime/globals.d.ts +0 -6
  112. package/dist/vm/runtime/limits.d.ts +0 -10
  113. package/dist/vm/runtime/limits.js +0 -19
  114. package/dist/vm/runtime/modules.d.ts +0 -4
  115. package/dist/vm/runtime/modules.js +0 -33
  116. package/dist/vm/runtime/references.d.ts +0 -8
  117. package/dist/vm/runtime/references.js +0 -51
  118. package/dist/vm/stdlib/array.d.ts +0 -16
  119. package/dist/vm/stdlib/array.js +0 -311
  120. package/dist/vm/stdlib/bytes.d.ts +0 -11
  121. package/dist/vm/stdlib/bytes.js +0 -222
  122. package/dist/vm/stdlib/collections.d.ts +0 -7
  123. package/dist/vm/stdlib/collections.js +0 -188
  124. package/dist/vm/stdlib/console.d.ts +0 -6
  125. package/dist/vm/stdlib/console.js +0 -93
  126. package/dist/vm/stdlib/date.d.ts +0 -2
  127. package/dist/vm/stdlib/date.js +0 -123
  128. package/dist/vm/stdlib/headers.d.ts +0 -2
  129. package/dist/vm/stdlib/headers.js +0 -92
  130. package/dist/vm/stdlib/iterator.d.ts +0 -2
  131. package/dist/vm/stdlib/iterator.js +0 -30
  132. package/dist/vm/stdlib/json.d.ts +0 -3
  133. package/dist/vm/stdlib/json.js +0 -42
  134. package/dist/vm/stdlib/math.d.ts +0 -3
  135. package/dist/vm/stdlib/math.js +0 -73
  136. package/dist/vm/stdlib/number.d.ts +0 -3
  137. package/dist/vm/stdlib/number.js +0 -72
  138. package/dist/vm/stdlib/object.d.ts +0 -6
  139. package/dist/vm/stdlib/object.js +0 -201
  140. package/dist/vm/stdlib/regexp.d.ts +0 -8
  141. package/dist/vm/stdlib/regexp.js +0 -119
  142. package/dist/vm/stdlib/string.d.ts +0 -2
  143. package/dist/vm/stdlib/string.js +0 -272
  144. package/dist/vm/stdlib/url.d.ts +0 -25
  145. package/dist/vm/stdlib/url.js +0 -255
  146. package/dist/vm/stdlib/value.d.ts +0 -6
  147. package/dist/vm/stdlib/value.js +0 -32
  148. package/dist/vm/stdlib/web.d.ts +0 -5
  149. package/dist/vm/stdlib/web.js +0 -97
  150. /package/dist/{vm/async.js → async.js} +0 -0
  151. /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
  152. /package/dist/{vm/code.js → code.js} +0 -0
  153. /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
  154. /package/dist/{vm/compiler → compiler}/compile.js +0 -0
  155. /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
  156. /package/dist/{vm/compiler → compiler}/scope.js +0 -0
  157. /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
  158. /package/dist/{vm/generators.js → generators.js} +0 -0
  159. /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
  160. /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
  161. /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
  162. /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
  163. /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
  164. /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
  165. /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
  166. /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
  167. /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
  168. /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
  169. /package/dist/{vm/prelude → prelude}/source.js +0 -0
  170. /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
  171. /package/dist/{vm/runtime → runtime}/callback.js +0 -0
  172. /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
  173. /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
  174. /package/dist/{vm/runtime → runtime}/generators.js +0 -0
  175. /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
  176. /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
  177. /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
  178. /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
  179. /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
  180. /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
  181. /package/dist/{interpreter → runtime}/limits.js +0 -0
  182. /package/dist/{vm/runtime → runtime}/model.js +0 -0
  183. /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
  184. /package/dist/{interpreter → runtime}/modules.js +0 -0
  185. /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
  186. /package/dist/{vm/runtime → runtime}/native.js +0 -0
  187. /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
  188. /package/dist/{vm/runtime → runtime}/promises.js +0 -0
  189. /package/dist/{interpreter → runtime}/references.d.ts +0 -0
  190. /package/dist/{interpreter → runtime}/references.js +0 -0
  191. /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
  192. /package/dist/{vm/runtime → runtime}/signals.js +0 -0
  193. /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
  194. /package/dist/{vm/runtime → runtime}/source.js +0 -0
  195. /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
  196. /package/dist/{vm/runtime → runtime}/table.js +0 -0
  197. /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
@@ -1,592 +0,0 @@
1
- import { ToolReference } from "../tool-runtime.js";
2
- import { checkArrayLength } from "./limits.js";
3
- import { AsyncIteratorSymbol, IteratorSymbol, rangeError, typeError } from "./model.js";
4
- /** Ordinary assignment: writable, enumerable, configurable. */
5
- export const data = { writable: true, enumerable: true, configurable: true };
6
- /** Built-in methods and `constructor`: writable and configurable but hidden from enumeration. */
7
- export const hidden = { writable: true, enumerable: false, configurable: true };
8
- /** Function `name` and `length`: read-only but deletable. */
9
- export const readonly = { writable: false, enumerable: false, configurable: true };
10
- /** Constants such as `Math.PI` and a constructor's `prototype`. */
11
- export const frozen = { writable: false, enumerable: false, configurable: false };
12
- /**
13
- * An object owned by the program: own properties plus a prototype link. Subclasses answer, in one place, how a
14
- * built-in kind of object prints, coerces, iterates, and crosses to the host.
15
- */
16
- export class Obj {
17
- proto;
18
- props = new Map();
19
- /** [[Extensible]]: cleared by `Object.preventExtensions`, `seal`, and `freeze`. */
20
- extensible = true;
21
- constructor(proto) {
22
- this.proto = proto;
23
- }
24
- /** The class name `Object.prototype.toString` reports: `[object Map]`. */
25
- tag = "Object";
26
- /** How diagnostics refer to a value of this kind. */
27
- get describe() {
28
- if (this.tag === "Object")
29
- return "a data object";
30
- return `${/^[AEIO]/.test(this.tag) ? "an" : "a"} ${this.tag}`;
31
- }
32
- /** ToString without consulting program-defined methods. */
33
- toString() {
34
- return `[object ${this.tag}]`;
35
- }
36
- /** ToPrimitive without consulting program-defined methods: only a Date answers a number hint differently. */
37
- toPrimitive(hint) {
38
- return this.toString();
39
- }
40
- /** ToNumber without consulting program-defined methods. */
41
- toNumber() {
42
- return Number(this.toPrimitive("number"));
43
- }
44
- /** How `console.log` shows the value; `item` formats a child with cycle and depth tracking. */
45
- inspect(item) {
46
- return `{${entries(this)
47
- .map(([key, value]) => `${JSON.stringify(key)}:${item(value)}`)
48
- .join(",")}}`;
49
- }
50
- /** A copy the host can hold; `item` converts a child. A `__proto__` key never reaches host code. */
51
- toHost(item) {
52
- return Object.fromEntries(entries(this)
53
- .filter(([key]) => key !== "__proto__")
54
- .map(([key, value]) => [key, item(value)]));
55
- }
56
- /** The built-in iteration `for...of` and spread use, when this kind of object has one. */
57
- iterator(builtins) {
58
- return undefined;
59
- }
60
- }
61
- export class Arr extends Obj {
62
- items;
63
- tag = "Array";
64
- /** The attributes every live element shares; `seal` and `freeze` narrow them since elements have no slots. */
65
- elements = data;
66
- constructor(proto, items = []) {
67
- super(proto);
68
- this.items = items;
69
- }
70
- get describe() {
71
- return "an array";
72
- }
73
- toString() {
74
- return this.items.map((item) => (item === null || item === undefined ? "" : coerceToString(item))).join(",");
75
- }
76
- inspect(item) {
77
- return `[${this.items.map(item).join(",")}]`;
78
- }
79
- toHost(item) {
80
- return this.items.map(item);
81
- }
82
- iterator() {
83
- return this.items.values();
84
- }
85
- }
86
- /** An object with the [[ErrorData]] slot: what `Error.prototype.toString` and the host boundary recognize as an error. */
87
- export class ErrorObj extends Obj {
88
- tag = "Error";
89
- /** The failure this error materialized from, so rethrowing it keeps the diagnostic kind and location. */
90
- host;
91
- /** Error.prototype.toString: "name: message", or just one when the other is empty. */
92
- toString() {
93
- const name = get(this, "name");
94
- const message = get(this, "message");
95
- const shownName = typeof name === "string" ? name : "Error";
96
- const shownMessage = typeof message === "string" ? message : "";
97
- if (shownMessage === "")
98
- return shownName;
99
- if (shownName === "")
100
- return shownMessage;
101
- return `${shownName}: ${shownMessage}`;
102
- }
103
- inspect() {
104
- return this.toString();
105
- }
106
- }
107
- /** Interpreter machinery a program can hold but never inspect, serialize, or hand to the host. */
108
- export class Opaque extends Obj {
109
- inspect() {
110
- return "[opaque reference]";
111
- }
112
- }
113
- export class Callable extends Opaque {
114
- length;
115
- tag = "Function";
116
- constructor(proto, name, length) {
117
- super(proto);
118
- this.length = length;
119
- define(this, "length", length, readonly);
120
- define(this, "name", name, readonly);
121
- }
122
- get describe() {
123
- return "a function";
124
- }
125
- }
126
- export class Fn extends Callable {
127
- parameters;
128
- body;
129
- capturedScopes;
130
- async;
131
- generator;
132
- arrow;
133
- constructor(proto, name, parameters, body, capturedScopes, async, generator,
134
- /** Arrows have no `this` or `arguments` of their own; they read the enclosing function's. */
135
- arrow) {
136
- const optional = parameters.findIndex((p) => p.type === "AssignmentPattern" || p.type === "RestElement");
137
- super(proto, name, optional === -1 ? parameters.length : optional);
138
- this.parameters = parameters;
139
- this.body = body;
140
- this.capturedScopes = capturedScopes;
141
- this.async = async;
142
- this.generator = generator;
143
- this.arrow = arrow;
144
- }
145
- }
146
- /** The strict `arguments` object: an ordinary object with indexed own properties and a hidden `length`. */
147
- export class Arguments extends Obj {
148
- tag = "Arguments";
149
- constructor(proto, args) {
150
- super(proto);
151
- args.forEach((arg, index) => define(this, String(index), arg));
152
- define(this, "length", args.length, hidden);
153
- }
154
- iterator() {
155
- return keys(this)
156
- .map((key) => get(this, key))
157
- .values();
158
- }
159
- }
160
- export class Native extends Callable {
161
- call;
162
- construct;
163
- callback;
164
- constructor(proto, options) {
165
- super(proto, options.name, options.length ?? 0);
166
- this.call = options.call;
167
- this.construct = options.construct;
168
- this.callback = options.callback ?? true;
169
- }
170
- }
171
- export class PromiseObj extends Opaque {
172
- id;
173
- tag = "Promise";
174
- state = "pending";
175
- /** The value, or the failure as thrown; a handler receives it materialized. */
176
- result = undefined;
177
- reactions = [];
178
- /** [[PromiseIsHandled]]: a reaction was attached, so a rejection is not reported as unhandled. */
179
- handled = false;
180
- constructor(proto,
181
- /** Creation order, for reporting unhandled rejections in the order their promises were made. */
182
- id) {
183
- super(proto);
184
- this.id = id;
185
- }
186
- get describe() {
187
- return "an un-awaited Promise";
188
- }
189
- inspect() {
190
- return "[Promise (await it to get its value)]";
191
- }
192
- }
193
- export class GeneratorObj extends Opaque {
194
- asynchronous;
195
- body;
196
- tag = "Generator";
197
- state = "suspendedStart";
198
- /** The suspended body's activations, kept while it waits for the next request. */
199
- entries;
200
- /** An async generator's requests, oldest first, each with the functions that settle its promise. */
201
- queue = [];
202
- constructor(proto, asynchronous,
203
- /** The body, started by the first `next()`. */
204
- body) {
205
- super(proto);
206
- this.asynchronous = asynchronous;
207
- this.body = body;
208
- }
209
- get describe() {
210
- return "a generator";
211
- }
212
- }
213
- /** A cursor over a host iterator; there is nothing to close. */
214
- export const hostCursor = (iterator) => ({
215
- next: function* () {
216
- const step = iterator.next();
217
- return { done: Boolean(step.done), value: step.value };
218
- },
219
- close: function* () { },
220
- });
221
- /** A built-in iterator: a live cursor over a host collection or an iterator helper, yielding program values. */
222
- export class IteratorObj extends Opaque {
223
- cursor;
224
- tag = "Iterator";
225
- constructor(proto, cursor) {
226
- super(proto);
227
- this.cursor = cursor;
228
- }
229
- get describe() {
230
- return "an iterator";
231
- }
232
- }
233
- /** A built-in iterator over a host iterator, e.g. `array.values()`. */
234
- export const hostIterator = (builtins, iterator) => new IteratorObj(builtins.Iterator, hostCursor(iterator));
235
- /** A built-in object around a host value: data-like, so it prints as itself and crosses to extensions as a copy. */
236
- export class Wrapper extends Obj {
237
- inspect(item) {
238
- return this.toString();
239
- }
240
- }
241
- export class DateObj extends Wrapper {
242
- time;
243
- tag = "Date";
244
- constructor(proto, time) {
245
- super(proto);
246
- this.time = time;
247
- }
248
- toString() {
249
- return Number.isFinite(this.time) ? new Date(this.time).toISOString() : "Invalid Date";
250
- }
251
- toPrimitive(hint) {
252
- return hint === "number" ? this.time : this.toString();
253
- }
254
- toHost() {
255
- return new Date(this.time);
256
- }
257
- }
258
- export class RegExpObj extends Wrapper {
259
- tag = "RegExp";
260
- regex;
261
- constructor(proto, pattern, flags) {
262
- super(proto);
263
- this.regex = new RegExp(pattern, flags);
264
- }
265
- toString() {
266
- return `/${this.regex.source}/${this.regex.flags}`;
267
- }
268
- toHost() {
269
- return new RegExp(this.regex.source, this.regex.flags);
270
- }
271
- }
272
- export class MapObj extends Wrapper {
273
- tag = "Map";
274
- map = new Map();
275
- inspect(item) {
276
- return `Map(${this.map.size}) [${[...this.map].map(([key, value]) => `[${item(key)},${item(value)}]`).join(",")}]`;
277
- }
278
- toHost(item) {
279
- return new Map([...this.map].map(([key, value]) => [item(key), item(value)]));
280
- }
281
- iterator(builtins) {
282
- return this.map.entries().map((entry) => new Arr(builtins.Array, entry));
283
- }
284
- }
285
- export class SetObj extends Wrapper {
286
- tag = "Set";
287
- set = new Set();
288
- inspect(item) {
289
- return `Set(${this.set.size}) [${[...this.set].map(item).join(",")}]`;
290
- }
291
- toHost(item) {
292
- return new Set([...this.set].map(item));
293
- }
294
- iterator() {
295
- return this.set.values();
296
- }
297
- }
298
- /** Keys are program objects, so a host WeakMap gives the same lifetime rule as JavaScript without any bookkeeping. */
299
- export class WeakMapObj extends Wrapper {
300
- tag = "WeakMap";
301
- map = new WeakMap();
302
- inspect() {
303
- return "WeakMap { <items unknown> }";
304
- }
305
- }
306
- export class WeakSetObj extends Wrapper {
307
- tag = "WeakSet";
308
- set = new WeakSet();
309
- inspect() {
310
- return "WeakSet { <items unknown> }";
311
- }
312
- }
313
- export class URLSearchParamsObj extends Wrapper {
314
- params;
315
- tag = "URLSearchParams";
316
- constructor(proto, params) {
317
- super(proto);
318
- this.params = params;
319
- }
320
- toString() {
321
- return this.params.toString();
322
- }
323
- toHost() {
324
- return new URLSearchParams(this.params);
325
- }
326
- iterator(builtins) {
327
- return this.params.entries().map((entry) => new Arr(builtins.Array, entry));
328
- }
329
- }
330
- export class HeadersObj extends Wrapper {
331
- headers;
332
- tag = "Headers";
333
- constructor(proto, headers) {
334
- super(proto);
335
- this.headers = headers;
336
- }
337
- inspect() {
338
- return `Headers ${JSON.stringify(Object.fromEntries(this.headers))}`;
339
- }
340
- toHost() {
341
- return new Headers(this.headers);
342
- }
343
- iterator(builtins) {
344
- // Bun's Headers typings lack the iterator helpers, so the host iterator is lifted first.
345
- return Iterator.from(this.headers.entries()).map((entry) => new Arr(builtins.Array, entry));
346
- }
347
- }
348
- export class URLObj extends Wrapper {
349
- url;
350
- tag = "URL";
351
- searchParams;
352
- constructor(proto, searchParamsProto, url) {
353
- super(proto);
354
- this.url = url;
355
- this.searchParams = new URLSearchParamsObj(searchParamsProto, url.searchParams);
356
- }
357
- toString() {
358
- return this.url.href;
359
- }
360
- toHost() {
361
- return new URL(this.url.href);
362
- }
363
- }
364
- /** A `Uint8Array`: the host array does the byte clamping and ignores out-of-range writes, as JS does. */
365
- export class Bytes extends Wrapper {
366
- bytes;
367
- tag = "Uint8Array";
368
- constructor(proto, bytes) {
369
- super(proto);
370
- this.bytes = bytes;
371
- }
372
- toString() {
373
- return this.bytes.join(",");
374
- }
375
- inspect() {
376
- return `Uint8Array(${this.bytes.length}) [${this.bytes.join(",")}]`;
377
- }
378
- toHost() {
379
- return new Uint8Array(this.bytes);
380
- }
381
- iterator() {
382
- return this.bytes.values();
383
- }
384
- }
385
- /** ToString without consulting program-defined methods. */
386
- export const coerceToString = (value) => (value instanceof Obj ? value.toString() : String(value));
387
- /** ToNumber without consulting program-defined methods; tool references are not numbers. */
388
- export const coerceToNumber = (value) => {
389
- if (value instanceof Obj)
390
- return value.toNumber();
391
- return value instanceof ToolReference ? Number.NaN : Number(value);
392
- };
393
- /** ToIntegerOrInfinity: NaN is 0, fractions truncate. */
394
- export const coerceToInteger = (value) => {
395
- const number = coerceToNumber(value);
396
- return Number.isNaN(number) ? 0 : Math.trunc(number);
397
- };
398
- /** Values that cannot cross the data boundary: opaque machinery and host-backed wrappers. */
399
- export const isRuntimeReference = (value) => value instanceof Opaque || value instanceof Wrapper || value instanceof ToolReference;
400
- const MAX_ARRAY_INDEX = 4_294_967_295;
401
- export const parseArrayIndex = (key) => {
402
- const property = String(key);
403
- if (!/^(0|[1-9]\d*)$/.test(property))
404
- return undefined;
405
- const index = Number(property);
406
- return index < MAX_ARRAY_INDEX ? index : undefined;
407
- };
408
- const canonical = (key) => (typeof key === "symbol" ? key : String(key));
409
- const isIndexed = (target) => target instanceof Arr || target instanceof Bytes;
410
- const elements = (target) => (target instanceof Arr ? target.items : target.bytes);
411
- const index = (target, key) => isIndexed(target) && typeof key === "string" ? parseArrayIndex(key) : undefined;
412
- /** The own property under `key`, including an array's live indexes and `length`. */
413
- export const own = (target, key) => {
414
- const name = canonical(key);
415
- if (isIndexed(target)) {
416
- const at = index(target, name);
417
- if (at !== undefined) {
418
- const items = elements(target);
419
- if (!(at in items))
420
- return undefined;
421
- return { value: items[at], ...(target instanceof Arr ? target.elements : data) };
422
- }
423
- if (target instanceof Arr && name === "length") {
424
- return { value: target.items.length, writable: target.elements.writable, enumerable: false, configurable: false };
425
- }
426
- }
427
- return target.props.get(name);
428
- };
429
- const read = (slot, receiver) => "value" in slot ? slot.value : slot.get === undefined ? undefined : slot.get(receiver);
430
- export const hasOwn = (target, key) => own(target, key) !== undefined;
431
- export const getOwn = (target, key) => {
432
- const slot = own(target, key);
433
- return slot === undefined ? undefined : read(slot, target);
434
- };
435
- /** [[Get]]: walks the prototype chain; accessors see `receiver`, which is the primitive for wrapper prototypes. */
436
- export const get = (target, key, receiver = target) => {
437
- for (let current = target; current !== null; current = current.proto) {
438
- const slot = own(current, key);
439
- if (slot !== undefined)
440
- return read(slot, receiver);
441
- }
442
- return undefined;
443
- };
444
- export const has = (target, key) => {
445
- for (let current = target; current !== null; current = current.proto) {
446
- if (own(current, key) !== undefined)
447
- return true;
448
- }
449
- return false;
450
- };
451
- export const hasPrototype = (value, proto) => {
452
- for (let current = value instanceof Obj ? value.proto : null; current !== null; current = current.proto) {
453
- if (current === proto)
454
- return true;
455
- }
456
- return false;
457
- };
458
- const writeElement = (target, name, value) => {
459
- const at = index(target, name);
460
- if (at !== undefined) {
461
- if (target instanceof Bytes) {
462
- target.bytes[at] = typeof value === "number" ? value : Number(value);
463
- return true;
464
- }
465
- if (!(at in target.items))
466
- rejectAddition(target, at);
467
- target.items[at] = value;
468
- return true;
469
- }
470
- if (!(target instanceof Arr) || name !== "length")
471
- return undefined;
472
- const length = typeof value === "number" ? value : Number(value);
473
- if (!Number.isInteger(length) || length < 0)
474
- throw rangeError("Invalid array length");
475
- // Shrinking deletes elements, which a sealed array forbids.
476
- if (length < target.items.length && !target.elements.configurable)
477
- return false;
478
- checkArrayLength(length);
479
- target.items.length = length;
480
- return true;
481
- };
482
- /** [[Set]]: an inherited setter or read-only property decides before an own data property is created. */
483
- export const set = (target, key, value) => {
484
- const name = canonical(key);
485
- for (let current = target; current !== null; current = current.proto) {
486
- const slot = own(current, name);
487
- if (slot === undefined)
488
- continue;
489
- if (!("value" in slot)) {
490
- if (slot.set === undefined)
491
- return false;
492
- slot.set(target, value);
493
- return true;
494
- }
495
- if (!slot.writable)
496
- return false;
497
- if (current !== target)
498
- break;
499
- if (isIndexed(target)) {
500
- const written = writeElement(target, name, value);
501
- if (written !== undefined)
502
- return written;
503
- }
504
- slot.value = value;
505
- return true;
506
- }
507
- if (isIndexed(target)) {
508
- const written = writeElement(target, name, value);
509
- if (written !== undefined)
510
- return written;
511
- }
512
- rejectAddition(target, name);
513
- target.props.set(name, { value, ...data });
514
- return true;
515
- };
516
- /** Creating a property on a non-extensible object is the one [[Set]] failure with its own message. */
517
- export const rejectAddition = (target, key) => {
518
- if (target.extensible)
519
- return;
520
- throw typeError(`Cannot add property ${String(key)}, object is not extensible.`);
521
- };
522
- /** [[DefineOwnProperty]] for a data property, ignoring the chain. */
523
- export const define = (target, key, value, attrs = data) => {
524
- const name = canonical(key);
525
- if (isIndexed(target) && writeElement(target, name, value) !== undefined)
526
- return;
527
- target.props.set(name, { value, ...attrs });
528
- };
529
- export const defineAccessor = (target, key, get, set) => {
530
- target.props.set(canonical(key), { get, set, enumerable: false, configurable: true });
531
- };
532
- export const remove = (target, key) => {
533
- const name = canonical(key);
534
- if (isIndexed(target)) {
535
- const at = index(target, name);
536
- if (at !== undefined) {
537
- if (target instanceof Bytes)
538
- return !(at in target.bytes);
539
- if (at in target.items && !target.elements.configurable)
540
- return false;
541
- return delete target.items[at];
542
- }
543
- if (target instanceof Arr && name === "length")
544
- return false;
545
- }
546
- const slot = target.props.get(name);
547
- if (slot === undefined)
548
- return true;
549
- if (!slot.configurable)
550
- return false;
551
- target.props.delete(name);
552
- return true;
553
- };
554
- // JS order: array indexes, integer-like keys ascending, other strings, then symbols.
555
- export const ownKeys = (target) => {
556
- const strings = [...target.props.keys()].filter((key) => typeof key === "string");
557
- const symbols = [...target.props.keys()].filter((key) => typeof key === "symbol");
558
- return [
559
- ...(isIndexed(target) ? Object.keys(elements(target)) : []),
560
- ...(target instanceof Arr ? ["length"] : []),
561
- ...strings.filter((key) => parseArrayIndex(key) !== undefined).sort((a, b) => Number(a) - Number(b)),
562
- ...strings.filter((key) => parseArrayIndex(key) === undefined),
563
- ...symbols,
564
- ];
565
- };
566
- const enumerable = (target, key) => own(target, key)?.enumerable === true;
567
- /** Own enumerable keys, including the iterator symbols; what spread and `Object.assign` copy. */
568
- export const enumerableKeys = (target) => ownKeys(target).filter((key) => (typeof key === "string" || key === IteratorSymbol || key === AsyncIteratorSymbol) && enumerable(target, key));
569
- /** Own enumerable string keys: `Object.keys`. */
570
- export const keys = (target) => ownKeys(target).filter((key) => typeof key === "string" && enumerable(target, key));
571
- /** Own enumerable string entries: `Object.entries` and serialization. */
572
- export const entries = (target) => keys(target).map((key) => [key, getOwn(target, key)]);
573
- /** An iterator result `{ value, done }`, built directly since every iteration step makes one. */
574
- export const iteratorResult = (proto, value, done) => {
575
- const target = new Obj(proto);
576
- target.props.set("value", { value, writable: true, enumerable: true, configurable: true });
577
- target.props.set("done", { value: done, writable: true, enumerable: true, configurable: true });
578
- return target;
579
- };
580
- export const record = (proto, fields) => {
581
- const target = new Obj(proto);
582
- for (const [key, value] of Object.entries(fields))
583
- define(target, key, value);
584
- return target;
585
- };
586
- export const assign = (target, source, skip) => {
587
- for (const key of enumerableKeys(source)) {
588
- if (skip?.has(key))
589
- continue;
590
- set(target, key, getOwn(source, key));
591
- }
592
- };
@@ -1,18 +0,0 @@
1
- import { type AstNode } from "./model.js";
2
- import { PromiseObj, type Value } from "./objects.js";
3
- import type { Interpreter } from "./interpreter.js";
4
- /**
5
- * NewPromiseCapability(%Promise%): a pending promise with the host functions that settle it once. A built-in's own
6
- * failure that rejects it is located at `site`, the call that created it.
7
- */
8
- export declare const capability: <R>(ctx: Interpreter<R>, site: AstNode | undefined) => {
9
- resolve: (resolution: Value) => void;
10
- reject: (reason: unknown) => void;
11
- promise: PromiseObj;
12
- };
13
- /**
14
- * Lets a synchronous object result also be awaited or chained like a Promise. It inherits non-enumerable
15
- * then/catch/finally that settle with a plain copy, so data views never show them and awaiting does not recurse.
16
- */
17
- export declare const promiseLike: <R>(ctx: Interpreter<R>, value: Value) => Value;
18
- export declare const promiseGlobal: <R>(ctx: Interpreter<R>) => import("./objects.js").Native;