|
| 1 | +# TextEncoder / TextDecoder and atob / btoa |
| 2 | + |
| 3 | +Native, WHATWG-conformant `TextEncoder`, `TextDecoder` |
| 4 | +([Encoding Standard](https://encoding.spec.whatwg.org)) and `atob` / `btoa` |
| 5 | +([HTML Standard §8.3](https://html.spec.whatwg.org/multipage/webappapis.html#atob)) |
| 6 | +globals, and the **lazy-global tier** they ride on. |
| 7 | + |
| 8 | +## Lazy globals |
| 9 | + |
| 10 | +These globals are registered on the global template as lazy data properties |
| 11 | +(`LazyGlobals`, `test-app/runtime/src/main/cpp/LazyGlobals.cpp`): the builtin |
| 12 | +behind a name is not compiled, run, or allocated until app code first reads it, |
| 13 | +and V8 then replaces the property with a plain data property so later reads |
| 14 | +cost nothing. Sibling names from one builtin (`TextEncoder` + `TextDecoder`) |
| 15 | +share a single run per isolate. Workers get the same globals — the tier is |
| 16 | +registered in every isolate's template. Assigning over one of these names |
| 17 | +before its first read replaces the global, like any other writable global. |
| 18 | + |
| 19 | +The tier is the intended home for further web globals (`Blob`, `fetch`, |
| 20 | +`crypto`, `DOMException`, …) with zero cost when unused; see |
| 21 | +`test-app/runtime/src/main/cpp/js/README.md` for the rules a lazy builtin |
| 22 | +lives by. |
| 23 | + |
| 24 | +The per-isolate exports cache behind the tier (`BuiltinLoader::GetExports`) is |
| 25 | +shared with the `ns:`/`node:` module registry: `require("ns:util").TextDecoder` |
| 26 | +and `require("node:util").TextDecoder` are the very class objects the globals |
| 27 | +hold, whichever entry point is reached first |
| 28 | +(see [ns-builtin-modules](ns-builtin-modules.md)). |
| 29 | + |
| 30 | +## TextEncoder / TextDecoder |
| 31 | + |
| 32 | +Node's split: `js/text-encoding.js` owns the WebIDL surface (brand checks via |
| 33 | +private fields, enumerable prototype members, `Symbol.toStringTag`), |
| 34 | +`TextEncoding.cpp` owns the bytes. |
| 35 | + |
| 36 | +- **Decoder encodings**: the `TextDecoder` constructor resolves utf-8, |
| 37 | + utf-16le, utf-16be and windows-1252, each with its complete WHATWG label |
| 38 | + set; an unknown label throws `RangeError`. (Precedent: Node without ICU |
| 39 | + ships utf-8/utf-16le; utf-16be and windows-1252 are cheap, and windows-1252 |
| 40 | + covers the `ascii`/`latin1`/`iso-8859-1` aliases web code actually uses.) |
| 41 | + `TextEncoder` is UTF-8-only and takes no label, as the spec defines it. |
| 42 | +- **Streaming**: full `decode(…, { stream: true })` support. Incomplete |
| 43 | + sequences (split BOMs and split utf-16 code units included) carry across |
| 44 | + calls in a 16-byte `Uint8Array` the builtin owns — no per-instance native |
| 45 | + handle, no finalizer. |
| 46 | +- **Replacement semantics**: WHATWG utf-8 state machine with one U+FFFD per |
| 47 | + maximal invalid subpart; `fatal: true` throws `TypeError`; `ignoreBOM` |
| 48 | + honored. |
| 49 | +- `encode()` / `encodeInto()` with correct USV conversion and partial-write |
| 50 | + boundaries (`encodeInto` never splits an encoded code point). |
| 51 | +- **Fast paths**: pure-ASCII utf-8 and C1-free windows-1252 decode straight |
| 52 | + through `String::NewFromOneByte`; results downgrade to one-byte strings when |
| 53 | + possible. `encodeInto` registers a V8 Fast API overload |
| 54 | + (`NATIVESCRIPT_ENABLE_FAST_API`, default on), live once a call site tiers |
| 55 | + up. |
| 56 | + |
| 57 | +## atob / btoa |
| 58 | + |
| 59 | +WHATWG forgiving-base64 (`Base64.cpp`): whitespace stripping, padding rules, |
| 60 | +alphabet validation. With no `DOMException` in the runtime yet, failures throw |
| 61 | +the name-patched `Error` (`InvalidCharacterError`) stand-in the abort-signal |
| 62 | +and performance builtins already use; a follow-up will introduce |
| 63 | +`DOMException` and upgrade these. |
| 64 | + |
| 65 | +## Tests |
| 66 | + |
| 67 | +The shared suite (`test-app/app/src/main/assets/app/shared/TextEncoding`) |
| 68 | +holds the conformance specs, feature-detecting so runtimes without these |
| 69 | +globals report pending rather than failing; it was independently validated |
| 70 | +against Node 24 (full ICU) as a reference. |
0 commit comments