Browse documentation

TextDecoder decodes the legacy multi-byte encodings natively from generated indexes

Date: 2026-12-04 Area: web, encoding, generated-data Refines: ADR 0233, which left the legacy multi-byte encodings' labels a RangeError Related: #2585, #2491, ADR 0252, Built-ins — TextDecoder

Context

ADR 0233 left out GBK, gb18030, Big5, EUC-JP, ISO-2022-JP, Shift_JIS and EUC-KR because their indexes are large and their decoders keep state, and nothing had asked for them. web-platform-tests' Encoding suites (#2491) now run in the test suite, and 142 of their subtests need these decoders. Browsers, Deno and Bun decode them, and code that reads Chinese, Japanese or Korean legacy text relies on that.

The standard gives each encoding a decoder that reads one byte at a time and may "restore" bytes it has already read, to be read again: an ASCII byte after a lead byte, gb18030's second and third bytes after a bad fourth, an unknown ISO-2022-JP escape's bytes. A restored byte can come from an earlier streaming call, and ISO-2022-JP's mode lasts across any number of characters, so the UTF-8 and UTF-16 decoders' way of keeping a stream's undecoded bytes and decoding them again on the next call (ADR 0252) does not fit.

Decision

  • The decoders run in the host, as the standard writes them. multiByteDecode in Goccia.HostAPI runs each encoding's handler over the bytes, with the restored bytes on a small stack read before the next input byte, and shares the UTF-8 decoder's way of charging and allocating: a counting pass charged to the limits, one string allocated at the counted length, a writing pass, and a copy of a SharedArrayBuffer's bytes.
  • A stream keeps the decoder's state, not bytes. The handler's fields (gb18030's three bytes, the other decoders' leading byte, EUC-JP's jis0212 flag, ISO-2022-JP's decoder state, output state and output flag) are packed into one number that TextDecoder holds between { stream: true } calls and resets when a call ends the stream or a fatal decoder throws.
  • The indexes are generated constants, compiled in. scripts/generate-encoding-data.js writes index gb18030 and its ranges, index Big5, index jis0208, index jis0212 and index EUC-KR into Generated.EncodingData from the same pinned indexes.json, each trimmed to the pointers from its first to its last with a code point. index Big5 holds the low 16 bits of each code point, with a bit set marking those in plane 2, which halves it. Measured on Linux x86-64, the production GocciaRunner grows by 187,648 bytes (1.1%, 16,942,672 to 17,130,320), almost all of it .data. An empty script's run time is unchanged (103.4 ms before, 102.9 ms after, 40 runs each), and so is its anonymous memory; its file-backed resident pages grow by about 1 MiB as the binary's layout shifts, which is page cache shared between processes. Nothing reads the tables until a decoder does, so lazy loading would save nothing at startup, and 1.1% is too little to justify a build option without them.
  • No encoders. TextEncoder encodes UTF-8 only, as the standard defines it. The legacy encoders serve a document's non-UTF-8 encoding in the URL parser and in form submission, and GocciaScript has neither.

Consequences

  • Every label of the seven encodings gives a TextDecoder, and encoding answers the encoding's lowercased name.
  • The decoders' output matched WebKit's (Bun's TextDecoder) on 321,218 inputs when this was written: every single byte, every pair whose first byte is 0x80 or above (or ESC), gb18030's four-byte sequences with a second byte of 0x30 or 0x39, and 28,000 random inputs, each also decoded fatally or in streamed pieces.
  • The indexes are whatwg/encoding's at the pinned commit; a change to the standard's indexes reaches the engine only when the generator runs again.