fix(codemode): preserve null in text encoding (#57639)

This commit is contained in:
xjh
2026-10-30 10:40:05 +08:00
committed by GitHub
Unverified
parent 50ea8d5003
commit 96c5c3a11a
2 changed files with 14 additions and 1 deletions
+5 -1
View File
@@ -220,7 +220,11 @@ export const textEncoderGlobal = <R>(ctx: Interpreter<R>) => {
const encoder = new TextEncoder()
defineAccessor(proto, "encoding", () => "utf-8")
methods(builtins, proto, [
["encode", 0, (_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] ?? "")))],
[
"encode",
0,
(_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] === undefined ? "" : args[0]))),
],
])
return constructor(builtins, proto, {
name: "TextEncoder",
+9
View File
@@ -1176,6 +1176,15 @@ describe("Uint8Array", () => {
})
describe("TextEncoder and TextDecoder", () => {
test("encodes null as text while omitted and undefined inputs stay empty", async () => {
expect(
await value(`
const encoder = new TextEncoder()
return [[...encoder.encode(null)], [...encoder.encode()], [...encoder.encode(undefined)]]
`),
).toEqual([[110, 117, 108, 108], [], []])
})
test("round-trips UTF-8 and exposes the standard fields", async () => {
expect(
await value(`