@opencode/codemode 2.2.7 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -13
- package/dist/{vm/async.d.ts → async.d.ts} +1 -1
- package/dist/codemode.d.ts +8 -22
- package/dist/codemode.js +17 -38
- package/dist/data.d.ts +4 -19
- package/dist/data.js +1 -82
- package/dist/extension.d.ts +8 -2
- package/dist/extension.js +9 -0
- package/dist/{vm/host.d.ts → host.d.ts} +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/{vm/opcodes.d.ts → opcodes.d.ts} +1 -1
- package/dist/{vm/prelude → prelude}/helpers.js +1 -1
- package/dist/{vm/runtime → runtime}/driver.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/driver.js +1 -1
- package/dist/{vm/runtime → runtime}/errors.js +2 -2
- package/dist/{vm/runtime → runtime}/execute.d.ts +17 -8
- package/dist/{vm/runtime → runtime}/execute.js +11 -9
- package/dist/{vm/runtime → runtime}/extensions.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/extensions.js +39 -4
- package/dist/{vm/runtime → runtime}/globals.js +1 -1
- package/dist/{vm/runtime → runtime}/interpreter.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/model.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/objects.js +1 -1
- package/dist/{vm/runtime → runtime}/operations.d.ts +1 -1
- package/dist/{vm/runtime → runtime}/operations.js +1 -1
- package/dist/scan.js +2 -2
- package/dist/{vm/scheduler.d.ts → scheduler.d.ts} +1 -1
- package/dist/{vm/snapshot → snapshot}/codec.d.ts +6 -5
- package/dist/{vm/snapshot → snapshot}/codec.js +12 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.d.ts +8 -5
- package/dist/{vm/snapshot → snapshot}/snapshot.js +14 -3
- package/dist/{vm/snapshot.js → snapshot.js} +1 -1
- package/dist/stdlib/array.d.ts +14 -9
- package/dist/stdlib/array.js +89 -276
- package/dist/stdlib/bytes.d.ts +11 -7
- package/dist/stdlib/bytes.js +112 -79
- package/dist/stdlib/collections.d.ts +7 -7
- package/dist/stdlib/collections.js +30 -339
- package/dist/stdlib/console.d.ts +2 -2
- package/dist/stdlib/console.js +3 -3
- package/dist/stdlib/date.d.ts +2 -2
- package/dist/stdlib/date.js +21 -30
- package/dist/stdlib/headers.d.ts +2 -2
- package/dist/stdlib/headers.js +15 -33
- package/dist/stdlib/iterator.d.ts +2 -2
- package/dist/stdlib/iterator.js +10 -302
- package/dist/stdlib/json.d.ts +2 -2
- package/dist/stdlib/json.js +15 -31
- package/dist/stdlib/math.d.ts +2 -7
- package/dist/stdlib/math.js +6 -30
- package/dist/stdlib/number.d.ts +3 -3
- package/dist/stdlib/number.js +8 -8
- package/dist/stdlib/object.d.ts +4 -4
- package/dist/stdlib/object.js +12 -30
- package/dist/stdlib/regexp.d.ts +6 -5
- package/dist/stdlib/regexp.js +18 -13
- package/dist/stdlib/string.d.ts +2 -2
- package/dist/stdlib/string.js +53 -53
- package/dist/stdlib/url.d.ts +21 -9
- package/dist/stdlib/url.js +55 -55
- package/dist/stdlib/value.d.ts +3 -4
- package/dist/stdlib/value.js +5 -6
- package/dist/stdlib/web.d.ts +4 -4
- package/dist/stdlib/web.js +7 -7
- package/dist/tool-runtime.d.ts +14 -13
- package/dist/tool-runtime.js +245 -53
- package/dist/tool-schema.d.ts +5 -0
- package/dist/tool-schema.js +36 -16
- package/dist/tool.d.ts +24 -9
- package/dist/tool.js +15 -22
- package/dist/{vm/vm.d.ts → vm.d.ts} +3 -3
- package/dist/{vm/vm.js → vm.js} +1 -1
- package/package.json +3 -5
- package/dist/interpreter/callback.d.ts +0 -35
- package/dist/interpreter/callback.js +0 -98
- package/dist/interpreter/errors.js +0 -173
- package/dist/interpreter/execute.d.ts +0 -14
- package/dist/interpreter/execute.js +0 -207
- package/dist/interpreter/extensions.d.ts +0 -9
- package/dist/interpreter/extensions.js +0 -248
- package/dist/interpreter/generators.d.ts +0 -3
- package/dist/interpreter/generators.js +0 -168
- package/dist/interpreter/globals.js +0 -144
- package/dist/interpreter/interpreter.d.ts +0 -48
- package/dist/interpreter/interpreter.js +0 -2253
- package/dist/interpreter/intrinsics.d.ts +0 -15
- package/dist/interpreter/intrinsics.js +0 -105
- package/dist/interpreter/model.d.ts +0 -68
- package/dist/interpreter/model.js +0 -54
- package/dist/interpreter/native.d.ts +0 -17
- package/dist/interpreter/native.js +0 -35
- package/dist/interpreter/objects.d.ts +0 -301
- package/dist/interpreter/objects.js +0 -592
- package/dist/interpreter/promises.d.ts +0 -18
- package/dist/interpreter/promises.js +0 -292
- package/dist/interpreter/runner.d.ts +0 -178
- package/dist/interpreter/runner.js +0 -480
- package/dist/interpreter/scope.d.ts +0 -16
- package/dist/interpreter/scope.js +0 -79
- package/dist/suspend-error.d.ts +0 -24
- package/dist/suspend-error.js +0 -25
- package/dist/vm/data.d.ts +0 -7
- package/dist/vm/data.js +0 -13
- package/dist/vm/index.d.ts +0 -6
- package/dist/vm/index.js +0 -6
- package/dist/vm/runtime/errors.d.ts +0 -23
- package/dist/vm/runtime/globals.d.ts +0 -6
- package/dist/vm/runtime/limits.d.ts +0 -10
- package/dist/vm/runtime/limits.js +0 -19
- package/dist/vm/runtime/modules.d.ts +0 -4
- package/dist/vm/runtime/modules.js +0 -33
- package/dist/vm/runtime/references.d.ts +0 -8
- package/dist/vm/runtime/references.js +0 -51
- package/dist/vm/stdlib/array.d.ts +0 -16
- package/dist/vm/stdlib/array.js +0 -311
- package/dist/vm/stdlib/bytes.d.ts +0 -11
- package/dist/vm/stdlib/bytes.js +0 -222
- package/dist/vm/stdlib/collections.d.ts +0 -7
- package/dist/vm/stdlib/collections.js +0 -188
- package/dist/vm/stdlib/console.d.ts +0 -6
- package/dist/vm/stdlib/console.js +0 -93
- package/dist/vm/stdlib/date.d.ts +0 -2
- package/dist/vm/stdlib/date.js +0 -123
- package/dist/vm/stdlib/headers.d.ts +0 -2
- package/dist/vm/stdlib/headers.js +0 -92
- package/dist/vm/stdlib/iterator.d.ts +0 -2
- package/dist/vm/stdlib/iterator.js +0 -30
- package/dist/vm/stdlib/json.d.ts +0 -3
- package/dist/vm/stdlib/json.js +0 -42
- package/dist/vm/stdlib/math.d.ts +0 -3
- package/dist/vm/stdlib/math.js +0 -73
- package/dist/vm/stdlib/number.d.ts +0 -3
- package/dist/vm/stdlib/number.js +0 -72
- package/dist/vm/stdlib/object.d.ts +0 -6
- package/dist/vm/stdlib/object.js +0 -201
- package/dist/vm/stdlib/regexp.d.ts +0 -8
- package/dist/vm/stdlib/regexp.js +0 -119
- package/dist/vm/stdlib/string.d.ts +0 -2
- package/dist/vm/stdlib/string.js +0 -272
- package/dist/vm/stdlib/url.d.ts +0 -25
- package/dist/vm/stdlib/url.js +0 -255
- package/dist/vm/stdlib/value.d.ts +0 -6
- package/dist/vm/stdlib/value.js +0 -32
- package/dist/vm/stdlib/web.d.ts +0 -5
- package/dist/vm/stdlib/web.js +0 -97
- /package/dist/{vm/async.js → async.js} +0 -0
- /package/dist/{vm/code.d.ts → code.d.ts} +0 -0
- /package/dist/{vm/code.js → code.js} +0 -0
- /package/dist/{vm/compiler → compiler}/compile.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/compile.js +0 -0
- /package/dist/{vm/compiler → compiler}/scope.d.ts +0 -0
- /package/dist/{vm/compiler → compiler}/scope.js +0 -0
- /package/dist/{vm/generators.d.ts → generators.d.ts} +0 -0
- /package/dist/{vm/generators.js → generators.js} +0 -0
- /package/dist/{vm/host.js → host.js} +0 -0
- /package/dist/{vm/internal-prelude.d.ts → internal-prelude.d.ts} +0 -0
- /package/dist/{vm/internal-prelude.js → internal-prelude.js} +0 -0
- /package/dist/{vm/opcodes.js → opcodes.js} +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.js +0 -0
- /package/dist/{vm/prelude → prelude}/bytecode.json +0 -0
- /package/dist/{vm/prelude → prelude}/helpers.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/prelude.js +0 -0
- /package/dist/{vm/prelude → prelude}/source.d.ts +0 -0
- /package/dist/{vm/prelude → prelude}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/callback.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/callback.js +0 -0
- /package/dist/{interpreter → runtime}/errors.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/generators.js +0 -0
- /package/dist/{interpreter → runtime}/globals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/interpreter.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsic-names.js +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/intrinsics.js +0 -0
- /package/dist/{interpreter → runtime}/limits.d.ts +0 -0
- /package/dist/{interpreter → runtime}/limits.js +0 -0
- /package/dist/{vm/runtime → runtime}/model.js +0 -0
- /package/dist/{interpreter → runtime}/modules.d.ts +0 -0
- /package/dist/{interpreter → runtime}/modules.js +0 -0
- /package/dist/{vm/runtime → runtime}/native.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/native.js +0 -0
- /package/dist/{vm/runtime → runtime}/promises.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/promises.js +0 -0
- /package/dist/{interpreter → runtime}/references.d.ts +0 -0
- /package/dist/{interpreter → runtime}/references.js +0 -0
- /package/dist/{vm/runtime → runtime}/signals.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/signals.js +0 -0
- /package/dist/{vm/runtime → runtime}/source.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/source.js +0 -0
- /package/dist/{vm/runtime → runtime}/table.d.ts +0 -0
- /package/dist/{vm/runtime → runtime}/table.js +0 -0
- /package/dist/{vm/scheduler.js → scheduler.js} +0 -0
- /package/dist/{vm/snapshot.d.ts → snapshot.d.ts} +0 -0
package/dist/stdlib/bytes.js
CHANGED
|
@@ -1,48 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
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
|
|
10
|
-
*
|
|
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
|
-
|
|
13
|
-
const
|
|
14
|
-
if (source instanceof Bytes &&
|
|
15
|
-
return new Uint8Array(source.bytes);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
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
|
-
|
|
85
|
+
(thisValue, args) => {
|
|
101
86
|
const target = self(thisValue, "set");
|
|
102
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
173
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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").
|
|
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")
|
|
227
|
-
if (args[0]
|
|
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
|
-
|
|
233
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const weakMapGlobal: <R>(ctx: Interpreter<R>) => import("../
|
|
7
|
-
export declare const weakSetGlobal: <R>(ctx: Interpreter<R>) => import("../
|
|
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;
|