@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,222 +0,0 @@
1
- import { Invoke } from "../runtime/signals.js";
2
- import { checkArrayLength, checkStringLength } from "../runtime/limits.js";
3
- import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js";
4
- import { IteratorSymbol, rangeError, syntaxError, typeError } from "../runtime/model.js";
5
- import { define, defineAccessor, get, hidden, Bytes, Obj, coerceToInteger, coerceToNumber, coerceToString, } from "../runtime/objects.js";
6
- import { describeValue } from "../runtime/references.js";
7
- import { requireCallback } from "../runtime/callback.js";
8
- import { sortArray } from "./array.js";
9
- /**
10
- * The bytes a Uint8Array, array, or other iterable of numbers describes, read in the prelude: a new Uint8Array with
11
- * prototype `into`, or set into the Uint8Array `into` at `offset`. The host array clamps each value.
12
- */
13
- const collectBytes = (ctx, source, name, into, offset, mapper, thisArg) => {
14
- const callback = mapper === undefined ? undefined : requireCallback(mapper, name);
15
- if (source instanceof Bytes && callback === undefined)
16
- return finishBytes(new Uint8Array(source.bytes), into, offset);
17
- return new Invoke(ctx.prelude.fn("collectBytes"), undefined, [source, name, callback, thisArg, into, offset], undefined);
18
- };
19
- export const finishBytes = (bytes, into, offset) => {
20
- if (!(into instanceof Bytes))
21
- return new Bytes(into, bytes);
22
- if (!Number.isInteger(offset) || offset < 0 || bytes.length + offset > into.bytes.length) {
23
- throw rangeError("Uint8Array.set: the source does not fit at that offset.");
24
- }
25
- into.bytes.set(bytes, offset);
26
- return undefined;
27
- };
28
- const constructBytes = (ctx, args, proto) => {
29
- const source = args[0];
30
- if (source !== null && typeof source === "object")
31
- return collectBytes(ctx, source, "new Uint8Array(...)", proto, 0);
32
- const length = source === undefined ? 0 : coerceToNumber(source);
33
- if (!Number.isInteger(length) || length < 0)
34
- throw rangeError(`Invalid typed array length: ${coerceToString(source)}`);
35
- checkArrayLength(length);
36
- return new Bytes(proto, new Uint8Array(length));
37
- };
38
- export const uint8ArrayGlobal = (ctx) => {
39
- const builtins = ctx.builtins;
40
- const proto = builtins.Uint8Array;
41
- const wrap = (bytes) => new Bytes(proto, bytes);
42
- const uint8Array = constructor(builtins, proto, {
43
- name: "Uint8Array",
44
- length: 3,
45
- call: requiresNew("Uint8Array"),
46
- construct: (args, newTarget) => constructBytes(ctx, args, prototypeFrom(newTarget, proto)),
47
- });
48
- const decode = (name, args, from) => {
49
- if (typeof args[0] !== "string")
50
- throw typeError(`Uint8Array.${name} expects a string.`);
51
- try {
52
- return wrap(from(args[0]));
53
- }
54
- catch {
55
- throw syntaxError(`Uint8Array.${name}: the string is not valid ${name === "fromHex" ? "hex" : "base64"}.`);
56
- }
57
- };
58
- methods(builtins, uint8Array, [
59
- ["from", 1, (_, args) => collectBytes(ctx, args[0], "Uint8Array.from", proto, 0, args[1], args[2])],
60
- ["of", 0, (_, args) => wrap(Uint8Array.from(args, coerceToNumber))],
61
- ["fromBase64", 1, (_, args) => decode("fromBase64", args, (text) => Uint8Array.fromBase64(text))],
62
- ["fromHex", 1, (_, args) => decode("fromHex", args, (text) => Uint8Array.fromHex(text))],
63
- ]);
64
- const self = (thisValue, name) => receiver(Bytes, thisValue, `Uint8Array.prototype.${name}`);
65
- const optNumber = (value) => (value === undefined ? undefined : coerceToInteger(value));
66
- // Uint8Array cannot contain NaN, so non-number search values never match a byte.
67
- const search = (value) => (typeof value === "number" ? value : NaN);
68
- defineAccessor(proto, "length", (thisValue) => self(thisValue, "length").bytes.length);
69
- methods(builtins, proto, [
70
- ["at", 1, (thisValue, args) => self(thisValue, "at").bytes.at(optNumber(args[0]) ?? 0)],
71
- [
72
- "slice",
73
- 2,
74
- (thisValue, args) => wrap(self(thisValue, "slice").bytes.slice(optNumber(args[0]), optNumber(args[1]))),
75
- ],
76
- // A view on the same bytes, as in JS: writes through one are visible through the other.
77
- [
78
- "subarray",
79
- 2,
80
- (thisValue, args) => wrap(self(thisValue, "subarray").bytes.subarray(optNumber(args[0]), optNumber(args[1]))),
81
- ],
82
- [
83
- "set",
84
- 1,
85
- (thisValue, args) => {
86
- const target = self(thisValue, "set");
87
- return collectBytes(ctx, args[0], "Uint8Array.set", target, optNumber(args[1]) ?? 0);
88
- },
89
- ],
90
- [
91
- "fill",
92
- 1,
93
- (thisValue, args) => {
94
- const target = self(thisValue, "fill");
95
- target.bytes.fill(coerceToNumber(args[0]), optNumber(args[1]), optNumber(args[2]));
96
- return target;
97
- },
98
- ],
99
- [
100
- "reverse",
101
- 0,
102
- (thisValue) => {
103
- const target = self(thisValue, "reverse");
104
- target.bytes.reverse();
105
- return target;
106
- },
107
- ],
108
- [
109
- "sort",
110
- 1,
111
- (thisValue, args) => {
112
- const target = self(thisValue, "sort");
113
- // Without a comparator, typed arrays sort numerically rather than by string.
114
- if (args[0] === undefined) {
115
- target.bytes.sort();
116
- return target;
117
- }
118
- return sortArray(ctx, target, args[0], "Uint8Array.sort");
119
- },
120
- ],
121
- ["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").bytes.indexOf(search(args[0]), optNumber(args[1]))],
122
- [
123
- "lastIndexOf",
124
- 1,
125
- (thisValue, args) => {
126
- const target = self(thisValue, "lastIndexOf").bytes;
127
- return args.length < 2
128
- ? target.lastIndexOf(search(args[0]))
129
- : target.lastIndexOf(search(args[0]), optNumber(args[1]));
130
- },
131
- ],
132
- [
133
- "includes",
134
- 1,
135
- (thisValue, args) => self(thisValue, "includes").bytes.includes(search(args[0]), optNumber(args[1])),
136
- ],
137
- [
138
- "join",
139
- 1,
140
- (thisValue, args) => {
141
- const joined = self(thisValue, "join").bytes.join(args[0] === undefined ? "," : coerceToString(args[0]));
142
- checkStringLength(joined.length);
143
- return joined;
144
- },
145
- ],
146
- ["toString", 0, (thisValue) => self(thisValue, "toString").bytes.join(",")],
147
- ["toBase64", 0, (thisValue) => self(thisValue, "toBase64").bytes.toBase64()],
148
- ["toHex", 0, (thisValue) => self(thisValue, "toHex").bytes.toHex()],
149
- ...iteratorMethods(builtins, self),
150
- // The callback methods are the prelude's.
151
- ]);
152
- define(proto, IteratorSymbol, get(proto, "values"), hidden);
153
- return uint8Array;
154
- };
155
- export const textEncoderGlobal = (ctx) => {
156
- const builtins = ctx.builtins;
157
- const proto = builtins.TextEncoder;
158
- const encoder = new TextEncoder();
159
- defineAccessor(proto, "encoding", () => "utf-8");
160
- methods(builtins, proto, [
161
- [
162
- "encode",
163
- 0,
164
- (_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] === undefined ? "" : args[0]))),
165
- ],
166
- ]);
167
- return constructor(builtins, proto, {
168
- name: "TextEncoder",
169
- call: requiresNew("TextEncoder"),
170
- construct: (_, newTarget) => new Obj(prototypeFrom(newTarget, proto)),
171
- });
172
- };
173
- /** A `TextDecoder` holding the host decoder its label and options configured. */
174
- export class TextDecoderObj extends Obj {
175
- decoder;
176
- constructor(proto, decoder) {
177
- super(proto);
178
- this.decoder = decoder;
179
- }
180
- }
181
- // The WHATWG labels for UTF-8, the only encoding CodeMode decodes.
182
- const utf8Labels = new Set(["unicode-1-1-utf-8", "unicode11utf8", "unicode20utf8", "utf-8", "utf8", "x-unicode20utf8"]);
183
- export const textDecoderGlobal = (ctx) => {
184
- const builtins = ctx.builtins;
185
- const proto = builtins.TextDecoder;
186
- const self = (thisValue, name) => receiver(TextDecoderObj, thisValue, `TextDecoder.prototype.${name}`);
187
- defineAccessor(proto, "encoding", (thisValue) => self(thisValue, "encoding").decoder.encoding);
188
- defineAccessor(proto, "fatal", (thisValue) => self(thisValue, "fatal").decoder.fatal);
189
- defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").decoder.ignoreBOM);
190
- methods(builtins, proto, [
191
- [
192
- "decode",
193
- 0,
194
- (thisValue, args) => {
195
- const decoder = self(thisValue, "decode").decoder;
196
- if (args[0] === undefined)
197
- return "";
198
- if (!(args[0] instanceof Bytes)) {
199
- throw typeError(`TextDecoder.decode expects a Uint8Array, received ${describeValue(args[0])}.`);
200
- }
201
- try {
202
- return decoder.decode(args[0].bytes);
203
- }
204
- catch {
205
- throw typeError(`TextDecoder.decode: the input is not valid ${decoder.encoding}.`);
206
- }
207
- },
208
- ],
209
- ]);
210
- return constructor(builtins, proto, {
211
- name: "TextDecoder",
212
- call: requiresNew("TextDecoder"),
213
- construct: (args, newTarget) => {
214
- const label = args[0] === undefined ? "utf-8" : coerceToString(args[0]).trim().toLowerCase();
215
- if (!utf8Labels.has(label))
216
- throw rangeError(`The "${label}" encoding is not supported; only UTF-8 is.`);
217
- const options = args[1] instanceof Obj ? args[1] : undefined;
218
- const flag = (name) => options !== undefined && Boolean(get(options, name));
219
- return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: flag("ignoreBOM") }));
220
- },
221
- });
222
- };
@@ -1,7 +0,0 @@
1
- import { Obj, type Value } from "../runtime/objects.js";
2
- import type { Interpreter } from "../runtime/interpreter.js";
3
- export declare const mapGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
4
- export declare const setGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
5
- export declare const weakKey: (value: Value, label: string) => Obj;
6
- export declare const weakMapGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
7
- export declare const weakSetGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,188 +0,0 @@
1
- import { Invoke } from "../runtime/signals.js";
2
- import { constructor, methods, prototypeFrom, receiver, requiresNew, iteratorMethods } from "../runtime/native.js";
3
- import { IteratorSymbol, typeError } from "../runtime/model.js";
4
- import { define, defineAccessor, get, hidden, MapObj, Obj, SetObj, WeakMapObj, WeakSetObj, } from "../runtime/objects.js";
5
- import { describeValue } from "../runtime/references.js";
6
- import { requireCallback } from "../runtime/callback.js";
7
- // A collection from an iterable is filled by the prelude, which iterates it.
8
- const filled = (ctx, target, init, kind) => init === undefined || init === null
9
- ? target
10
- : new Invoke(ctx.prelude.fn("fillCollection"), undefined, [target, init, kind], undefined);
11
- // The callback sees the stored key (-0 is +0) and its result wins over anything it inserted itself.
12
- const getOrInsertComputed = (ctx, owner, key, callback) => {
13
- const table = owner.map;
14
- if (table.has(key))
15
- return table.get(key);
16
- return new Invoke(ctx.prelude.fn("insertComputed"), undefined, [owner, key === 0 ? 0 : key, callback], undefined);
17
- };
18
- export const mapGlobal = (ctx) => {
19
- const builtins = ctx.builtins;
20
- const proto = builtins.Map;
21
- const map = constructor(builtins, proto, {
22
- name: "Map",
23
- call: requiresNew("Map"),
24
- construct: (args, newTarget) => filled(ctx, new MapObj(prototypeFrom(newTarget, proto)), args[0], "Map"),
25
- });
26
- const self = (thisValue, name) => receiver(MapObj, thisValue, `Map.prototype.${name}`);
27
- defineAccessor(proto, "size", (thisValue) => receiver(MapObj, thisValue, "Map.prototype.size").map.size);
28
- methods(builtins, proto, [
29
- ["get", 1, (thisValue, args) => self(thisValue, "get").map.get(args[0])],
30
- ["has", 1, (thisValue, args) => self(thisValue, "has").map.has(args[0])],
31
- [
32
- "set",
33
- 2,
34
- (thisValue, args) => {
35
- const target = self(thisValue, "set");
36
- target.map.set(args[0], args[1]);
37
- return target;
38
- },
39
- ],
40
- [
41
- "getOrInsert",
42
- 2,
43
- (thisValue, args) => {
44
- const target = self(thisValue, "getOrInsert").map;
45
- if (!target.has(args[0]))
46
- target.set(args[0], args[1]);
47
- return target.get(args[0]);
48
- },
49
- ],
50
- [
51
- "getOrInsertComputed",
52
- 2,
53
- (thisValue, args) => {
54
- const target = self(thisValue, "getOrInsertComputed");
55
- return getOrInsertComputed(ctx, target, args[0], requireCallback(args[1], "Map.getOrInsertComputed"));
56
- },
57
- ],
58
- ["delete", 1, (thisValue, args) => self(thisValue, "delete").map.delete(args[0])],
59
- [
60
- "clear",
61
- 0,
62
- (thisValue) => {
63
- self(thisValue, "clear").map.clear();
64
- return undefined;
65
- },
66
- ],
67
- ...iteratorMethods(builtins, self),
68
- ]);
69
- define(proto, IteratorSymbol, get(proto, "entries"), hidden);
70
- return map;
71
- };
72
- export const setGlobal = (ctx) => {
73
- const builtins = ctx.builtins;
74
- const proto = builtins.Set;
75
- const set = constructor(builtins, proto, {
76
- name: "Set",
77
- call: requiresNew("Set"),
78
- construct: (args, newTarget) => filled(ctx, new SetObj(prototypeFrom(newTarget, proto)), args[0], "Set"),
79
- });
80
- const self = (thisValue, name) => receiver(SetObj, thisValue, `Set.prototype.${name}`);
81
- defineAccessor(proto, "size", (thisValue) => receiver(SetObj, thisValue, "Set.prototype.size").set.size);
82
- methods(builtins, proto, [
83
- ["has", 1, (thisValue, args) => self(thisValue, "has").set.has(args[0])],
84
- [
85
- "add",
86
- 1,
87
- (thisValue, args) => {
88
- const target = self(thisValue, "add");
89
- target.set.add(args[0]);
90
- return target;
91
- },
92
- ],
93
- ["delete", 1, (thisValue, args) => self(thisValue, "delete").set.delete(args[0])],
94
- [
95
- "clear",
96
- 0,
97
- (thisValue) => {
98
- self(thisValue, "clear").set.clear();
99
- return undefined;
100
- },
101
- ],
102
- ...iteratorMethods(builtins, self),
103
- ]);
104
- define(proto, IteratorSymbol, get(proto, "values"), hidden);
105
- return set;
106
- };
107
- // CanBeHeldWeakly: only program objects; tool references are rebuilt on every access, so they could never be found again.
108
- export const weakKey = (value, label) => {
109
- if (value instanceof Obj)
110
- return value;
111
- throw typeError(`Invalid value used ${label}: ${describeValue(value)} cannot be held weakly.`);
112
- };
113
- export const weakMapGlobal = (ctx) => {
114
- const builtins = ctx.builtins;
115
- const proto = builtins.WeakMap;
116
- const weakMap = constructor(builtins, proto, {
117
- name: "WeakMap",
118
- call: requiresNew("WeakMap"),
119
- construct: (args, newTarget) => filled(ctx, new WeakMapObj(prototypeFrom(newTarget, proto)), args[0], "WeakMap"),
120
- });
121
- const self = (thisValue, name) => receiver(WeakMapObj, thisValue, `WeakMap.prototype.${name}`).map;
122
- // Lookups pass any key through: the host collection answers false for a non-object, as the spec requires.
123
- const key = (value) => weakKey(value, "as weak map key");
124
- methods(builtins, proto, [
125
- [
126
- "get",
127
- 1,
128
- (thisValue, args) => {
129
- const target = self(thisValue, "get");
130
- return args[0] instanceof Obj ? target.get(args[0]) : undefined;
131
- },
132
- ],
133
- ["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0])],
134
- ["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0])],
135
- [
136
- "set",
137
- 2,
138
- (thisValue, args) => {
139
- self(thisValue, "set").set(key(args[0]), args[1]);
140
- return thisValue;
141
- },
142
- ],
143
- [
144
- "getOrInsert",
145
- 2,
146
- (thisValue, args) => {
147
- const target = self(thisValue, "getOrInsert");
148
- const k = key(args[0]);
149
- if (!target.has(k))
150
- target.set(k, args[1]);
151
- return target.get(k);
152
- },
153
- ],
154
- [
155
- "getOrInsertComputed",
156
- 2,
157
- (thisValue, args) => {
158
- const target = receiver(WeakMapObj, thisValue, "WeakMap.prototype.getOrInsertComputed");
159
- const k = key(args[0]);
160
- return getOrInsertComputed(ctx, target, k, requireCallback(args[1], "WeakMap.getOrInsertComputed"));
161
- },
162
- ],
163
- ]);
164
- return weakMap;
165
- };
166
- export const weakSetGlobal = (ctx) => {
167
- const builtins = ctx.builtins;
168
- const proto = builtins.WeakSet;
169
- const weakSet = constructor(builtins, proto, {
170
- name: "WeakSet",
171
- call: requiresNew("WeakSet"),
172
- construct: (args, newTarget) => filled(ctx, new WeakSetObj(prototypeFrom(newTarget, proto)), args[0], "WeakSet"),
173
- });
174
- const self = (thisValue, name) => receiver(WeakSetObj, thisValue, `WeakSet.prototype.${name}`).set;
175
- methods(builtins, proto, [
176
- ["has", 1, (thisValue, args) => self(thisValue, "has").has(args[0])],
177
- ["delete", 1, (thisValue, args) => self(thisValue, "delete").delete(args[0])],
178
- [
179
- "add",
180
- 1,
181
- (thisValue, args) => {
182
- self(thisValue, "add").add(weakKey(args[0], "in weak set"));
183
- return thisValue;
184
- },
185
- ],
186
- ]);
187
- return weakSet;
188
- };
@@ -1,6 +0,0 @@
1
- import { Obj, type Value } from "../runtime/objects.js";
2
- import type { Interpreter } from "../runtime/interpreter.js";
3
- /** Captured console: every method appends one formatted line to `logs`. */
4
- export declare const consoleGlobal: <R>(ctx: Interpreter<R>) => Obj;
5
- /** One value as `console.log` shows it. */
6
- export declare const formatValue: (value: Value) => string;
@@ -1,93 +0,0 @@
1
- import { methods } from "../runtime/native.js";
2
- import { entries, get, Arr, Obj } from "../runtime/objects.js";
3
- import { ToolReference } from "../../tool-runtime.js";
4
- import { containsOpaqueReference } from "../runtime/references.js";
5
- const consoleMethods = ["log", "info", "debug", "warn", "error", "dir", "table"];
6
- /** Captured console: every method appends one formatted line to `logs`. */
7
- export const consoleGlobal = (ctx) => {
8
- const builtins = ctx.builtins;
9
- const console = new Obj(builtins.Object);
10
- methods(builtins, console, consoleMethods.map((name) => [
11
- name,
12
- 0,
13
- (_, args) => {
14
- ctx.logs.push(formatConsoleMessage(name, args));
15
- return undefined;
16
- },
17
- ]));
18
- return console;
19
- };
20
- const MAX_CONSOLE_DEPTH = 32;
21
- const formatConsoleMessage = (name, args) => {
22
- if (name === "dir")
23
- return args.length === 0 ? "undefined" : formatValue(args[0]);
24
- if (name === "table")
25
- return formatConsoleTable(args[0], args[1]);
26
- const prefix = name === "warn" ? "[warn] " : name === "error" ? "[error] " : name === "debug" ? "[debug] " : "";
27
- return `${prefix}${args.map((arg) => formatValue(arg)).join(" ")}`;
28
- };
29
- /** One value as `console.log` shows it. */
30
- export const formatValue = (value) => {
31
- if (value === undefined)
32
- return "undefined";
33
- if (typeof value === "string")
34
- return value;
35
- return formatConsoleValue(value, new Set(), 0);
36
- };
37
- const formatConsoleValue = (value, seen, depth) => {
38
- if (value === null || value === undefined)
39
- return "null";
40
- if (typeof value === "string")
41
- return JSON.stringify(value);
42
- if (!(value instanceof Obj))
43
- return value instanceof ToolReference ? "[opaque reference]" : String(value);
44
- if (depth > MAX_CONSOLE_DEPTH)
45
- return "...";
46
- if (seen.has(value))
47
- return "[Circular]";
48
- seen.add(value);
49
- try {
50
- return value.inspect((item) => formatConsoleValue(item, seen, depth + 1));
51
- }
52
- finally {
53
- seen.delete(value);
54
- }
55
- };
56
- const formatConsoleTable = (value, columnsArgument) => {
57
- if (value === undefined)
58
- return "undefined";
59
- if (containsOpaqueReference(value))
60
- return "[opaque reference]";
61
- const columns = columnsArgument instanceof Arr ? columnsArgument.items.map(String) : undefined;
62
- const rows = consoleTableRows(value, columns);
63
- const keys = columns ?? Array.from(new Set(rows.flatMap((row) => Object.keys(row.values))));
64
- const header = ["(index)", ...keys].join("\t");
65
- return [
66
- header,
67
- ...rows.map((row) => [row.index, ...keys.map((key) => formatConsoleTableCell(row.values[key]))].join("\t")),
68
- ].join("\n");
69
- };
70
- const consoleTableRows = (data, columns) => {
71
- if (data instanceof Arr) {
72
- return data.items.map((item, index) => ({ index: String(index), values: consoleTableValues(item, columns) }));
73
- }
74
- if (data instanceof Obj) {
75
- return entries(data).map(([index, item]) => ({ index, values: consoleTableValues(item, columns) }));
76
- }
77
- return [{ index: "0", values: { Value: data } }];
78
- };
79
- const consoleTableValues = (value, columns) => {
80
- if (value instanceof Obj && !(value instanceof Arr)) {
81
- if (columns !== undefined)
82
- return Object.fromEntries(columns.map((column) => [column, get(value, column)]));
83
- return Object.fromEntries(entries(value));
84
- }
85
- return { Value: value };
86
- };
87
- const formatConsoleTableCell = (value) => {
88
- if (value === undefined)
89
- return "";
90
- if (typeof value === "string")
91
- return value;
92
- return formatConsoleValue(value, new Set(), 0);
93
- };
@@ -1,2 +0,0 @@
1
- import type { Interpreter } from "../runtime/interpreter.js";
2
- export declare const dateGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;
@@ -1,123 +0,0 @@
1
- import { constructor, methods, prototypeFrom, receiver } from "../runtime/native.js";
2
- import { rangeError } from "../runtime/model.js";
3
- import { DateObj, Obj, coerceToNumber, coerceToString } from "../runtime/objects.js";
4
- import { withPrimitives } from "../runtime/callback.js";
5
- const constructDate = (args, proto) => {
6
- if (args.length === 0)
7
- return new DateObj(proto, Date.now());
8
- if (args.length === 1) {
9
- const arg = args[0];
10
- if (arg instanceof DateObj)
11
- return new DateObj(proto, arg.time);
12
- return withPrimitives("default", [arg], ([value]) => typeof value === "string"
13
- ? new DateObj(proto, Date.parse(value))
14
- : new DateObj(proto, new Date(coerceToNumber(value)).getTime()));
15
- }
16
- // The spec converts at most seven components, in order, so extra arguments never run program code.
17
- return toNumbers(args.slice(0, 7), (parts) => new DateObj(proto, new Date(...parts).getTime()));
18
- };
19
- // ToNumber on each value in turn, so a symbol throws before any later value's conversion runs program code.
20
- const toNumbers = (values, body) => withPrimitives("number", values, (primitives) => body(primitives.map(coerceToNumber)), true);
21
- const getters = [
22
- "getFullYear",
23
- "getMonth",
24
- "getDate",
25
- "getDay",
26
- "getHours",
27
- "getMinutes",
28
- "getSeconds",
29
- "getMilliseconds",
30
- "getUTCFullYear",
31
- "getUTCMonth",
32
- "getUTCDate",
33
- "getUTCDay",
34
- "getUTCHours",
35
- "getUTCMinutes",
36
- "getUTCSeconds",
37
- "getUTCMilliseconds",
38
- "getTimezoneOffset",
39
- ];
40
- const setters = [
41
- ["setTime", 1],
42
- ["setMilliseconds", 1],
43
- ["setUTCMilliseconds", 1],
44
- ["setSeconds", 2],
45
- ["setUTCSeconds", 2],
46
- ["setMinutes", 3],
47
- ["setUTCMinutes", 3],
48
- ["setHours", 4],
49
- ["setUTCHours", 4],
50
- ["setDate", 1],
51
- ["setUTCDate", 1],
52
- ["setMonth", 2],
53
- ["setUTCMonth", 2],
54
- ["setFullYear", 3],
55
- ["setUTCFullYear", 3],
56
- ];
57
- export const dateGlobal = (ctx) => {
58
- const builtins = ctx.builtins;
59
- const proto = builtins.Date;
60
- const date = constructor(builtins, proto, {
61
- name: "Date",
62
- length: 7,
63
- // ISO instead of the host's locale string: date strings are deterministic and must not leak the host timezone.
64
- call: () => new Date().toISOString(),
65
- construct: (args, newTarget) => constructDate(args, prototypeFrom(newTarget, proto)),
66
- });
67
- methods(builtins, date, [
68
- ["now", 0, () => Date.now()],
69
- ["parse", 1, (_, args) => Date.parse(coerceToString(args[0]))],
70
- [
71
- "UTC",
72
- 7,
73
- (_, args) => toNumbers(args.slice(0, 7), (parts) => Date.UTC(...parts)),
74
- ],
75
- ]);
76
- const self = (thisValue, name) => receiver(DateObj, thisValue, `Date.prototype.${name}`);
77
- const iso = (value) => {
78
- if (!Number.isFinite(value.time))
79
- throw rangeError("Invalid time value.");
80
- return new Date(value.time).toISOString();
81
- };
82
- methods(builtins, proto, [
83
- ["getTime", 0, (thisValue) => self(thisValue, "getTime").time],
84
- ["valueOf", 0, (thisValue) => self(thisValue, "valueOf").time],
85
- ["toISOString", 0, (thisValue) => iso(self(thisValue, "toISOString"))],
86
- [
87
- "toJSON",
88
- 1,
89
- (thisValue) => {
90
- const value = self(thisValue, "toJSON");
91
- return Number.isFinite(value.time) ? iso(value) : null;
92
- },
93
- ],
94
- ["toString", 0, (thisValue) => coerceToString(self(thisValue, "toString"))],
95
- ["toDateString", 0, (thisValue) => new Date(self(thisValue, "toDateString").time).toDateString()],
96
- ["toTimeString", 0, (thisValue) => new Date(self(thisValue, "toTimeString").time).toTimeString()],
97
- ["toUTCString", 0, (thisValue) => new Date(self(thisValue, "toUTCString").time).toUTCString()],
98
- ["toGMTString", 0, (thisValue) => new Date(self(thisValue, "toGMTString").time).toUTCString()],
99
- ...["toLocaleString", "toLocaleDateString", "toLocaleTimeString"].map((name) => [
100
- name,
101
- 0,
102
- (thisValue) => new Date(self(thisValue, name).time)[name]("en-US", { timeZone: "UTC" }),
103
- ]),
104
- ...getters.map((name) => [name, 0, (thisValue) => new Date(self(thisValue, name).time)[name]()]),
105
- ...setters.map(([name, length]) => [
106
- name,
107
- length,
108
- (thisValue, args) => {
109
- const target = self(thisValue, name);
110
- // Native setters read the current time before argument coercion, whose callbacks may mutate the Date.
111
- return toNumbers([target.time, ...args.slice(0, length)], ([time, ...values]) => {
112
- const hosted = new Date(time);
113
- // Every setter but setTime and setFullYear leaves an invalid Date untouched and answers NaN.
114
- if (Number.isNaN(hosted.getTime()) && name !== "setTime" && !name.endsWith("FullYear"))
115
- return NaN;
116
- target.time = hosted[name](...values);
117
- return target.time;
118
- });
119
- },
120
- ]),
121
- ]);
122
- return date;
123
- };
@@ -1,2 +0,0 @@
1
- import type { Interpreter } from "../runtime/interpreter.js";
2
- export declare const headersGlobal: <R>(ctx: Interpreter<R>) => import("../runtime/objects.js").Native;