Repository navigation
Add opt-in TextEncoder, TextDecoder, atob and btoa - #3091
Merged
Merged
Conversation
Two more features on the opt-in web-API surface, both still installing nothing unless a host names them. - WebApiFeatures.Encoding installs TextEncoder and TextDecoder per the Encoding Standard. The encoder is UTF-8 only, as the standard requires: encode() replaces an unpaired surrogate with U+FFFD (USVString), and encodeInto() fills a Uint8Array without ever splitting a code point, reporting UTF-16 code units read and bytes written. - TextDecoder reads UTF-8, UTF-16LE and UTF-16BE, resolved through the spec's label table for exactly those three - the legacy single-byte and multi-byte encodings each need their own index and are out of scope, so their labels are a RangeError rather than a silent substitution. Label matching is ASCII case-insensitive after ASCII-whitespace trimming, and deliberately spelled out rather than handed to OrdinalIgnoreCase, which folds U+017F onto s. - Streaming keeps one BCL Decoder per instance for as long as the stream lasts, so a code point split across two chunks decodes once its last byte arrives; fatal is DecoderExceptionFallback reported as a TypeError, and ignoreBOM=false drops one leading U+FEFF per stream, which works across a chunk boundary because the BOM is dropped from scalar values rather than from bytes. - WebApiFeatures.Base64 installs atob and btoa. btoa maps each UTF-16 code unit to a byte and raises an InvalidCharacterError DOMException above U+00FF; atob is Infra's forgiving-base64 decode written out, because Convert.FromBase64String is a different function - it rejects unpadded input the algorithm accepts and accepts padding the algorithm refuses. - Both features are in WebApiFeatures.Default, and a host may enable either without the other. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the opt-in web API series (#3079):
TextEncoder/TextDecoderbehindWebApiFeatures.Encodingandatob/btoabehindWebApiFeatures.Base64(two independent flags, both inDefault). Usual rules: BCL only, net8+, opt-in, default engine unchanged.encode(USVString semantics — unpaired surrogates → U+FFFD in one pass via the encoder's replacement fallback) andencodeInto, whose rune-walk never splits a code point at the destination boundary and countsreadin UTF-16 code units. The result object uses a sharedJsObjectLayout, so a loop reading.writtenkeeps a monomorphic inline cache.OrdinalIgnoreCase, which would fold U+017F ſ ontosand acceptcſunicode(pinned as refused); any other label is aRangeError.fatal(→TypeError, decoder reset to a clean stream afterwards),ignoreBOM, and streamingdecode(chunk, { stream: true })via a per-instance BCLDecoder— a multi-byte code point and a BOM split across two chunks both decode correctly (pinned). Detached/out-of-bounds buffer sources contribute the empty byte sequence per WebIDL's get-buffer-source-copy.InvalidCharacterErrorDOMException, per https://html.spec.whatwg.org/multipage/webappapis.html#dom-btoa.Convert.FromBase64Stringis not that function in either direction ("YQ"must decode;"YQ="must not), and the padding rule (strip at most two=, only when the whitespace-stripped length divides by four) is implemented exactly as written.WebIDL attribute/operation shapes follow the precedent set in #3079 (attributes as enumerable brand-checked accessors; operations non-enumerable, documented simplification). 97 new tests plus public-surface pins (default engine has neither global; host-registered names win). Verified: all-TFM build, both suites × both frameworks × both host-contract-verification configurations.
🤖 Generated with Claude Code