feat(runtime): lazy-global tier with native TextEncoder/TextDecoder and atob/btoa - #448
Draft
edusperoni wants to merge 3 commits into
Draft
feat(runtime): lazy-global tier with native TextEncoder/TextDecoder and atob/btoa#448edusperoni wants to merge 3 commits into
edusperoni wants to merge 3 commits into
Conversation
Adds a lazy-global tier and the first four globals on it. LazyGlobals registers each name on the global template as a lazy data property, so the builtin behind it is not compiled, run or allocated until app code first reads the name; V8 then replaces the property with a plain data property. Sibling names share one run per isolate through a Caches state slot, and the metadata interceptor declines every name the tier owns. TextEncoder/TextDecoder follow Node's split: text-encoding.js owns the WebIDL shapes and TextEncoding.cpp the bytes — the complete WHATWG label sets for utf-8, utf-16le, utf-16be and windows-1252, a hand-rolled utf-8 decode state machine with per-maximal-subpart replacement, the shared utf-16 decoder, BOM handling and full streaming. Per-decoder state is a Uint8Array the builtin owns, so no instance needs a native handle. atob/btoa sit on the WHATWG forgiving-base64 codec in Base64.cpp and, with no DOMException in the runtime yet, fail with the name-patched Error stand-in the other builtins use. encodeInto registers a v8::CFunction fast-call overload behind NATIVESCRIPT_ENABLE_FAST_API. It is inert on iOS, which runs V8 jitless.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: trueThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Node puts the two encoding interfaces on util, so both the standard module and
its shim carry them, and they are the very objects the globals of those names
hold: require("node:util").TextDecoder === globalThis.TextDecoder, whichever is
reached first.
That identity needs one cache. The lazy-global tier had its own per-isolate
exports slots and the builtin-module registry another one keyed by specifier,
so a builtin reached through both would have run twice and exported two sets of
classes. Both now go through BuiltinLoader::GetExports, which runs a builtin at
most once per isolate — with its binding, built only when the run actually
happens — and hands back that one module.exports. TextEncoding and Base64 own
the accessor for their file; the registry gained a per-specifier binding factory
in place of the switch, which is what let the two schemes converge.
Requiring util still costs nothing extra: ns:util's binding carries the two
names as lazy data properties and both files keep the read inside a getter, so
the text-encoding builtin runs on the first read of util.TextEncoder, not on
the require.
edusperoni
added a commit
to NativeScript/android
that referenced
this pull request
Aug 24, 2026
Matches the updated NativeScript/ios#448. The lazy tier's private exports cache generalizes into BuiltinLoader::GetExports, one per-isolate cache every entry point to a builtin shares — the ns:/node: module registry (whose per-specifier exports map it replaces), the lazy globals, and any binding factory. ns:util re-exports TextEncoder/TextDecoder as the very class objects the globals hold, lazily end to end (SetLazyDataProperty on the binding, getters in ns-util.js/node-util.js), and node:util forwards them as Node does.
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.
Adds native, WHATWG-conformant
TextEncoder,TextDecoder,atobandbtoaglobals — and, more importantly, the lazy-global tier they ride on, which is the foundation for bringing further web globals (Blob,fetch,crypto,DOMException, …) into the runtime with zero cost when unused.Lazy-global tier (
LazyGlobals)SetLazyDataPropertybeforeContext::New: the builtin behind it is not compiled, run, or allocated until app code first reads the name, and V8 then replaces the property with a plain data property so later reads cost nothing.TextEncoder+TextDecoder) share a single run per isolate via aCachesstate slot.runtime/js/README.md: lazy builtins run at arbitrary times, so they may only consumeinternalskeys published by eager builtins.TextEncoder / TextDecoder
Node's split:
js/text-encoding.jsowns the WebIDL surface (brand checks via private fields, enumerable prototype members,Symbol.toStringTag),TextEncoding.cppowns the bytes.RangeError. (Precedent: Node without ICU ships utf-8/utf-16le; utf-16be and windows-1252 are cheap, and windows-1252 covers theascii/latin1/iso-8859-1aliases web code actually uses. More encodings can follow via CFString if ever needed.)decode(…, {stream}): incomplete sequences (including split BOMs and split utf-16 code units) carry across calls in a 16-byteUint8Arraythe builtin owns — no per-instance native handle, no finalizer.fatalthrowsTypeError;ignoreBOMhonored.encode()/encodeInto()with correct USV conversion and partial-write boundaries (never splits an encoded code point).String::NewFromOneByte; results downgrade to one-byte strings when possible.atob / btoa
WHATWG forgiving-base64 in
Base64.cpp(whitespace stripping, padding rules, alphabet validation). With noDOMExceptionin the runtime yet, failures throw the name-patchedError(InvalidCharacterError) stand-in the other builtins already use — a follow-up PR will introduceDOMExceptionand upgrade these plusAbortSignal's reasons.V8 Fast API
encodeIntoregisters av8::CFunctionfast-call overload behindNATIVESCRIPT_ENABLE_FAST_API(default on). It is inert on iOS, which runs V8 in lite/jitless mode, but positions the runtime for JIT-enabled embeds (macOS/Catalyst). This build's V8 restricts fast returns to scalars, so the string-returning ops (decode,atob,btoa) have no fast overload — current Node makes the same call in its encoding binding.Tests
0f45dc8adds 94 feature-detecting specs (pending, not failing, on runtimes without these globals; per-encoding sub-suites probe constructor support so runtimes with different encoding coverage still pass). Independently validated against Node 24 (full ICU) as a conformance reference: 94/94.ns:util/node:utilMatching Node, both util modules export
TextEncoderandTextDecoder— and they are the very objects the globals hold (require('node:util').TextDecoder === globalThis.TextDecoder, whichever is reached first). Guaranteeing that identity unified the two builtin-exports caches (the lazy tier's and the module registry's) into a singleBuiltinLoader::GetExportsthat runs a builtin at most once per isolate, building its native binding only when the run actually happens. The exports stay lazy on the modules too: requiring util does not run the text-encoding builtin; the first read ofutil.TextEncoderdoes. (atob/btoadeliberately stay off util — Node keeps those onbuffer.)