Uh oh!
There was an error while loading. Please reload this page.
TextDecoder: support UTF-16LE and UTF-16BE - #230
Conversation
Emscripten's UTF16ToString does `new TextDecoder('utf-16le')` at module
scope, so the constructor threw before any decoding was attempted and the
whole module failed to load. This blocks any EXPORT_ES6 Emscripten build,
which is how Babylon.js ships Gaussian Splatting SPZ and CSG2/manifold.
Adds the WHATWG utf-16le and utf-16be label sets and decodes them to
UTF-16 code units, including the leading BOM strip the spec requires.
A trailing odd byte is dropped rather than emitting U+FFFD.
Two existing tests used "utf-16" as their example of an unsupported
encoding; they now use "iso-8859-2".There was a problem hiding this comment.
Pull request overview
Adds UTF-16 decoding support to the native TextDecoder polyfill so Emscripten-generated code (which constructs new TextDecoder('utf-16le') at module scope) no longer fails during module initialization.
Changes:
- Extend
TextDecoderto recognize WHATWG UTF-16LE/UTF-16BE label sets (including aliases) in addition to UTF-8. - Implement UTF-16LE/BE byte-to-code-unit decoding with required leading BOM stripping.
- Update unit tests to use a truly-unsupported encoding label and add coverage for UTF-16LE/BE, aliases, BOM stripping, surrogate pairs, and embedded NULs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Tests/UnitTests/Scripts/tests.ts | Adjusts unsupported-encoding tests and adds new UTF-16 decoding test coverage. |
| Polyfills/TextDecoder/Source/TextDecoder.cpp | Adds UTF-16 label handling and implements UTF-16LE/BE decode path with BOM stripping. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
## Summary Update JsRuntimeHost from [`cb988baa`](BabylonJS/JsRuntimeHost@cb988ba) to [`0d263627`](BabylonJS/JsRuntimeHost@0d26362), picking up three fixes merged since the current pin: - [BabylonJS/JsRuntimeHost#220](BabylonJS/JsRuntimeHost#220) — prevent zero-delay `setInterval` from flooding and starving the JS dispatch queue. In BN this could indefinitely delay async shader-compilation continuations and hang scene readiness. - [BabylonJS/JsRuntimeHost#230](BabylonJS/JsRuntimeHost#230) — support UTF-16LE/UTF-16BE in `TextDecoder`, needed by Emscripten `EXPORT_ES6` modules such as SPZ and CSG2/manifold. - [BabylonJS/JsRuntimeHost#231](BabylonJS/JsRuntimeHost#231) — preserve embedded NUL bytes in XHR string responses, needed for Emscripten modules with inline WASM payloads. ## Validation - Configured Win32 x64 against the updated dependency and confirmed FetchContent resolved the exact pinned SHA. - Built the Debug Playground target successfully. - Ran the `Nested BBG` validation test, which exercises the timer-starvation regression path: **1/1 passed, exit 0**. - The three upstream changes include their own focused JsRuntimeHost unit coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d47cbab2-d751-4cf9-984f-4412dd9ec601
bghgary
commented
Sep 3, 2026
[Commented by Copilot on behalf of @bghgary] This merged before the pending review finding was submitted. The UTF-16 path still drops odd trailing bytes and creates strings from unvalidated lone surrogates, while the Encoding Standard requires U+FFFD for both. Please correct both endiannesses and add regression tests. |
Emscripten's
UTF16ToStringdoesnew TextDecoder('utf-16le')at module scope, so the constructor threw before any decoding was attempted and the whole module failed to load. That blocks anyEXPORT_ES6Emscripten build — which is how Babylon.js ships Gaussian Splatting SPZ and CSG2/manifold.Adds the WHATWG
utf-16leandutf-16belabel sets and decodes them to UTF-16 code units, including the leading BOM strip the spec requires. A trailing odd byte is dropped rather than emitting U+FFFD; every producer we care about hands over whole code units.Two existing tests used
"utf-16"as their example of an unsupported encoding and now use"iso-8859-2".New tests cover both endiannesses, the alias sets, BOM stripping, a non-BMP surrogate pair and embedded NUL code units.