Parent: #473
Problem
CompareArrayElements for Array.prototype.sort and Array.prototype.toSorted applies ToString to both values, then performs lexicographic ordering over UTF-16 code units. The runtime currently compares the resulting WTF-8 byte slices with std.mem.order. UTF-8 byte order diverges from UTF-16 code-unit order across the supplementary/BMP boundary; for example U+10000 must sort before U+FFFF because its leading UTF-16 surrogate is 0xD800.
The engine already has a representation-aware, allocation-free UTF-16 string comparator for relational operators. Default array sorting should reuse that owned primitive after preserving ToString order.
Scope
- Preserve undefined ordering and user comparator behavior.
- Preserve left-then-right ToString coercion and abrupt completion.
- Compare default-sort strings as UTF-16 code-unit sequences without materializing UTF-16 buffers.
- Cover Array.prototype.sort and toSorted through their shared comparator.
- Add an astral/BMP regression with exact coercion order and a physical StringData witness.
This issue does not change promises, jobs, queues, workers/threads, or Map/Set iterators.
Acceptance
- U+10000 sorts before U+FFFF under the default comparator.
- Both operands are coerced once, left before right, in the two-element witness.
- Canonical and forced-flat physical string storage produce the same result.
- Focused Debug, ReleaseSafe, and TSan tests pass.
- Full sharded units report zero failures and zero leaks.
- The 54-file Test262 Array.prototype.sort subtree is measured before and after; any flip is reported exactly.