@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,48 +1,40 @@
1
- import { checkArrayLength, checkStringLength } from "../interpreter/limits.js";
2
- import { constructor, methods, prototypeFrom, receiver, requiresNew } from "../interpreter/native.js";
3
- import { IteratorSymbol, rangeError, syntaxError, typeError } from "../interpreter/model.js";
4
- import { define, defineAccessor, get, hidden, Arr, Bytes, hostIterator, Obj, coerceToInteger, coerceToNumber, coerceToString, } from "../interpreter/objects.js";
5
- import { describeValue } from "../interpreter/references.js";
6
- import { applyCollectionCallback } from "../interpreter/callback.js";
7
- import { callbackMethods, sortArray } from "./array.js";
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";
8
9
  /**
9
- * The bytes a Uint8Array, array, or other iterable of numbers describes; the host array clamps each value. As in
10
- * TypedArray.from, a mapper sees the original values, after the source is consumed in full.
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.
11
12
  */
12
- function* collectBytes(ctx, source, name, mapper, thisArg) {
13
- const apply = mapper === undefined ? undefined : applyCollectionCallback(ctx, mapper, name);
14
- if (source instanceof Bytes && apply === undefined)
15
- return new Uint8Array(source.bytes);
16
- const cursor = yield* ctx.iterate(source);
17
- if (cursor === undefined) {
18
- throw typeError(`${name} expects a Uint8Array, an array, or an iterable of numbers, received ${describeValue(source)}.`);
19
- }
20
- const values = [];
21
- while (true) {
22
- const step = yield* cursor.next();
23
- if (step.done)
24
- break;
25
- values.push(step.value);
26
- checkArrayLength(values.length);
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.");
27
24
  }
28
- if (apply === undefined)
29
- return Uint8Array.from(values, coerceToNumber);
30
- const mapped = [];
31
- for (const [index, value] of values.entries())
32
- mapped.push(yield* apply([value, index], thisArg));
33
- return Uint8Array.from(mapped, coerceToNumber);
34
- }
35
- function* constructBytes(ctx, args, proto) {
25
+ into.bytes.set(bytes, offset);
26
+ return undefined;
27
+ };
28
+ const constructBytes = (ctx, args, proto) => {
36
29
  const source = args[0];
37
- if (source !== null && typeof source === "object") {
38
- return new Bytes(proto, yield* collectBytes(ctx, source, "new Uint8Array(...)"));
39
- }
30
+ if (source !== null && typeof source === "object")
31
+ return collectBytes(ctx, source, "new Uint8Array(...)", proto, 0);
40
32
  const length = source === undefined ? 0 : coerceToNumber(source);
41
33
  if (!Number.isInteger(length) || length < 0)
42
34
  throw rangeError(`Invalid typed array length: ${coerceToString(source)}`);
43
35
  checkArrayLength(length);
44
36
  return new Bytes(proto, new Uint8Array(length));
45
- }
37
+ };
46
38
  export const uint8ArrayGlobal = (ctx) => {
47
39
  const builtins = ctx.builtins;
48
40
  const proto = builtins.Uint8Array;
@@ -64,13 +56,7 @@ export const uint8ArrayGlobal = (ctx) => {
64
56
  }
65
57
  };
66
58
  methods(builtins, uint8Array, [
67
- [
68
- "from",
69
- 1,
70
- function* (_, args) {
71
- return wrap(yield* collectBytes(ctx, args[0], "Uint8Array.from", args[1], args[2]));
72
- },
73
- ],
59
+ ["from", 1, (_, args) => collectBytes(ctx, args[0], "Uint8Array.from", proto, 0, args[1], args[2])],
74
60
  ["of", 0, (_, args) => wrap(Uint8Array.from(args, coerceToNumber))],
75
61
  ["fromBase64", 1, (_, args) => decode("fromBase64", args, (text) => Uint8Array.fromBase64(text))],
76
62
  ["fromHex", 1, (_, args) => decode("fromHex", args, (text) => Uint8Array.fromHex(text))],
@@ -79,7 +65,6 @@ export const uint8ArrayGlobal = (ctx) => {
79
65
  const optNumber = (value) => (value === undefined ? undefined : coerceToInteger(value));
80
66
  // Uint8Array cannot contain NaN, so non-number search values never match a byte.
81
67
  const search = (value) => (typeof value === "number" ? value : NaN);
82
- const wrapAll = (items) => new Arr(builtins.Array, items);
83
68
  defineAccessor(proto, "length", (thisValue) => self(thisValue, "length").bytes.length);
84
69
  methods(builtins, proto, [
85
70
  ["at", 1, (thisValue, args) => self(thisValue, "at").bytes.at(optNumber(args[0]) ?? 0)],
@@ -97,15 +82,9 @@ export const uint8ArrayGlobal = (ctx) => {
97
82
  [
98
83
  "set",
99
84
  1,
100
- function* (thisValue, args) {
85
+ (thisValue, args) => {
101
86
  const target = self(thisValue, "set");
102
- const offset = optNumber(args[1]) ?? 0;
103
- const source = yield* collectBytes(ctx, args[0], "Uint8Array.set");
104
- if (!Number.isInteger(offset) || offset < 0 || source.length + offset > target.bytes.length) {
105
- throw rangeError("Uint8Array.set: the source does not fit at that offset.");
106
- }
107
- target.bytes.set(source, offset);
108
- return undefined;
87
+ return collectBytes(ctx, args[0], "Uint8Array.set", target, optNumber(args[1]) ?? 0);
109
88
  },
110
89
  ],
111
90
  [
@@ -129,16 +108,14 @@ export const uint8ArrayGlobal = (ctx) => {
129
108
  [
130
109
  "sort",
131
110
  1,
132
- function* (thisValue, args) {
111
+ (thisValue, args) => {
133
112
  const target = self(thisValue, "sort");
134
113
  // Without a comparator, typed arrays sort numerically rather than by string.
135
114
  if (args[0] === undefined) {
136
115
  target.bytes.sort();
137
116
  return target;
138
117
  }
139
- const sorted = yield* sortArray(ctx, [...target.bytes], args[0], "Uint8Array.sort");
140
- target.bytes.set(Uint8Array.from(sorted, coerceToNumber));
141
- return target;
118
+ return sortArray(ctx, target, args[0], "Uint8Array.sort");
142
119
  },
143
120
  ],
144
121
  ["indexOf", 1, (thisValue, args) => self(thisValue, "indexOf").bytes.indexOf(search(args[0]), optNumber(args[1]))],
@@ -169,16 +146,8 @@ export const uint8ArrayGlobal = (ctx) => {
169
146
  ["toString", 0, (thisValue) => self(thisValue, "toString").bytes.join(",")],
170
147
  ["toBase64", 0, (thisValue) => self(thisValue, "toBase64").bytes.toBase64()],
171
148
  ["toHex", 0, (thisValue) => self(thisValue, "toHex").bytes.toHex()],
172
- ["keys", 0, (thisValue) => hostIterator(builtins, self(thisValue, "keys").bytes.keys())],
173
- ["values", 0, (thisValue) => hostIterator(builtins, self(thisValue, "values").bytes.values())],
174
- [
175
- "entries",
176
- 0,
177
- (thisValue) => hostIterator(builtins, self(thisValue, "entries")
178
- .bytes.entries()
179
- .map(([index, byte]) => wrapAll([index, byte]))),
180
- ],
181
- ...callbackMethods(ctx, "Uint8Array", self, (target) => target.bytes, (values) => wrap(Uint8Array.from(values, coerceToNumber))),
149
+ ...iteratorMethods(builtins, self),
150
+ // The callback methods are the prelude's.
182
151
  ]);
183
152
  define(proto, IteratorSymbol, get(proto, "values"), hidden);
184
153
  return uint8Array;
@@ -201,12 +170,78 @@ export const textEncoderGlobal = (ctx) => {
201
170
  construct: (_, newTarget) => new Obj(prototypeFrom(newTarget, proto)),
202
171
  });
203
172
  };
204
- /** A `TextDecoder` holding the host decoder its label and options configured. */
173
+ // Replacement mode only needs to identify an incomplete suffix (at most three bytes).
174
+ // Fatal mode also needs the queue position after the first error, which the host doesn't expose.
175
+ const streamBoundary = (bytes, fatal) => {
176
+ let next = fatal ? 0 : Math.max(0, bytes.length - 3);
177
+ while (next < bytes.length) {
178
+ const start = next;
179
+ const lead = bytes[next++];
180
+ if (lead < 0x80)
181
+ continue;
182
+ if (lead < 0xc2 || lead > 0xf4) {
183
+ if (fatal)
184
+ return { end: next, error: true };
185
+ continue;
186
+ }
187
+ const width = lead < 0xe0 ? 2 : lead < 0xf0 ? 3 : 4;
188
+ let lower = lead === 0xe0 ? 0xa0 : lead === 0xf0 ? 0x90 : 0x80;
189
+ let upper = lead === 0xed ? 0x9f : lead === 0xf4 ? 0x8f : 0xbf;
190
+ while (next < start + width) {
191
+ if (next === bytes.length)
192
+ return { end: start, error: false };
193
+ const byte = bytes[next];
194
+ // A bad continuation is reprocessed as a leading byte, even after a fatal error.
195
+ if (byte < lower || byte > upper) {
196
+ if (fatal)
197
+ return { end: next, error: true };
198
+ break;
199
+ }
200
+ next++;
201
+ lower = 0x80;
202
+ upper = 0xbf;
203
+ }
204
+ }
205
+ return { end: next, error: false };
206
+ };
207
+ const empty = new Uint8Array();
205
208
  export class TextDecoderObj extends Obj {
206
209
  decoder;
207
- constructor(proto, decoder) {
210
+ ignoreBOM;
211
+ queued = empty;
212
+ seen = false;
213
+ constructor(proto, decoder, ignoreBOM) {
208
214
  super(proto);
209
215
  this.decoder = decoder;
216
+ this.ignoreBOM = ignoreBOM;
217
+ }
218
+ decode(bytes, stream) {
219
+ if (this.queued.length && bytes.length) {
220
+ checkArrayLength(this.queued.length + bytes.length);
221
+ const joined = new Uint8Array(this.queued.length + bytes.length);
222
+ joined.set(this.queued);
223
+ joined.set(bytes, this.queued.length);
224
+ bytes = joined;
225
+ }
226
+ if (!bytes.length)
227
+ bytes = this.queued;
228
+ const { end, error } = stream ? streamBoundary(bytes, this.decoder.fatal) : { end: bytes.length, error: false };
229
+ this.queued = end < bytes.length ? bytes.slice(end) : empty;
230
+ if (error)
231
+ throw typeError("TextDecoder.decode: the input is not valid utf-8.");
232
+ const stripBOM = !this.ignoreBOM && !this.seen;
233
+ if (!stream)
234
+ this.seen = false;
235
+ try {
236
+ // Keep the host decoder stateless: snapshots own the pending bytes and BOM decision.
237
+ const text = this.decoder.decode(end === bytes.length ? bytes : bytes.subarray(0, end));
238
+ if (stream && text.length)
239
+ this.seen = true;
240
+ return stripBOM && text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
241
+ }
242
+ catch {
243
+ throw typeError("TextDecoder.decode: the input is not valid utf-8.");
244
+ }
210
245
  }
211
246
  }
212
247
  // The WHATWG labels for UTF-8, the only encoding CodeMode decodes.
@@ -217,24 +252,22 @@ export const textDecoderGlobal = (ctx) => {
217
252
  const self = (thisValue, name) => receiver(TextDecoderObj, thisValue, `TextDecoder.prototype.${name}`);
218
253
  defineAccessor(proto, "encoding", (thisValue) => self(thisValue, "encoding").decoder.encoding);
219
254
  defineAccessor(proto, "fatal", (thisValue) => self(thisValue, "fatal").decoder.fatal);
220
- defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").decoder.ignoreBOM);
255
+ defineAccessor(proto, "ignoreBOM", (thisValue) => self(thisValue, "ignoreBOM").ignoreBOM);
221
256
  methods(builtins, proto, [
222
257
  [
223
258
  "decode",
224
259
  0,
225
260
  (thisValue, args) => {
226
- const decoder = self(thisValue, "decode").decoder;
227
- if (args[0] === undefined)
228
- return "";
229
- if (!(args[0] instanceof Bytes)) {
261
+ const decoder = self(thisValue, "decode");
262
+ if (args[0] !== undefined && !(args[0] instanceof Bytes)) {
230
263
  throw typeError(`TextDecoder.decode expects a Uint8Array, received ${describeValue(args[0])}.`);
231
264
  }
232
- try {
233
- return decoder.decode(args[0].bytes);
234
- }
235
- catch {
236
- throw typeError(`TextDecoder.decode: the input is not valid ${decoder.encoding}.`);
265
+ const options = args[1];
266
+ if (options !== undefined && typeof options !== "object") {
267
+ throw typeError("TextDecoder.decode expects an options object.");
237
268
  }
269
+ const stream = options instanceof Obj && Boolean(get(options, "stream"));
270
+ return decoder.decode(args[0]?.bytes ?? empty, stream);
238
271
  },
239
272
  ],
240
273
  ]);
@@ -247,7 +280,7 @@ export const textDecoderGlobal = (ctx) => {
247
280
  throw rangeError(`The "${label}" encoding is not supported; only UTF-8 is.`);
248
281
  const options = args[1] instanceof Obj ? args[1] : undefined;
249
282
  const flag = (name) => options !== undefined && Boolean(get(options, name));
250
- return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: flag("ignoreBOM") }));
283
+ return new TextDecoderObj(prototypeFrom(newTarget, proto), new TextDecoder("utf-8", { fatal: flag("fatal"), ignoreBOM: true }), flag("ignoreBOM"));
251
284
  },
252
285
  });
253
286
  };
@@ -1,7 +1,7 @@
1
- import type { Interpreter } from "../interpreter/interpreter.js";
2
- /** `Map.groupBy` and `Object.groupBy`: the same iteration, keyed into a Map or a data object. */
3
- export declare const groupBy: <R>(ctx: Interpreter<R>, namespace: "Map" | "Object") => import("../interpreter/objects.js").Native;
4
- export declare const mapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
5
- export declare const setGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
6
- export declare const weakMapGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
7
- export declare const weakSetGlobal: <R>(ctx: Interpreter<R>) => import("../interpreter/objects.js").Native;
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;