Skip to content

TextDecoder: support UTF-16LE and UTF-16BE - #230

Merged
bkaradzic-microsoft merged 2 commits into
BabylonJS:mainfrom
bkaradzic-microsoft:textdecoder-utf16
Aug 27, 2026
Merged

TextDecoder: support UTF-16LE and UTF-16BE#230
bkaradzic-microsoft merged 2 commits into
BabylonJS:mainfrom
bkaradzic-microsoft:textdecoder-utf16

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

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. That 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; 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.

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".
Copilot AI lite review requested due to automatic review settings August 26, 2026 17:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TextDecoder to 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.

Comment thread Polyfills/TextDecoder/Source/TextDecoder.cpp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

Comment thread Polyfills/TextDecoder/Source/TextDecoder.cpp
Comment thread Tests/UnitTests/Scripts/tests.ts
Comment thread Polyfills/TextDecoder/README.md
@bkaradzic-microsoft
bkaradzic-microsoft merged commit 9fa1f53 into BabylonJS:main Aug 27, 2026
25 checks passed
bkaradzic-microsoft added a commit to BabylonJS/BabylonNative that referenced this pull request Aug 27, 2026
## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants