Parent: #473 Related: #493, #495, #623
Problem
String.prototype.includes, startsWith, and endsWith clamped their positions against WTF-8 byte length and sliced the backing bytes at that numeric offset. ECMAScript indexes Strings by UTF-16 code unit. BMP/astral/lone-surrogate input could therefore search from the wrong position, slice through an encoded sequence, or fail to match a surrogate half inside an astral scalar. The existing private-ABI stringIncludesUtf16 helper was exact but repeatedly rescanned both strings from the beginning, giving adversarial inputs a superlinear implementation path.
Scope
- Preserve the specified RequireObjectCoercible, IsRegExp, ToString, and ToIntegerOrInfinity ordering.
- Clamp positions against UTF-16 length and compare exact UTF-16 code-unit sequences for includes/startsWith/endsWith.
- Preserve the allocation-free byte-search fast path when receiver/search representations prove byte offsets equal code-unit offsets.
- Give non-ASCII search a bounded linear-time implementation, including astral halves and lone surrogates.
- Share one helper boundary so #623 can adopt the same exact matcher for indexOf/lastIndexOf.
- Preserve the private
JSC__JSValue__stringIncludescontract and pending-exception behavior.
No-workaround rules
- No normalization, replacement-character substitution, code-point comparison, ASCII-only acceptance, test exclusion, or byte-position reinterpretation.
- No repeated random-access decoding that multiplies source length by search length.
- OOM must propagate; no silent fallback to the known-inexact byte matcher.
Acceptance
- Exact unit fixtures cover ASCII, BMP, astral pairs, high/low surrogate needles, lone surrogates, empty needles, boundary positions, and coercion order.
- Search predicates use UTF-16 lengths and exact code-unit equality while retaining the proven ASCII fast path.
- Non-ASCII includes is bounded linear in receiver plus needle length, with freeable invocation scratch.
- Relevant Test262 subtrees have nonzero exact before → after accounting with no regressions.
- Focused/full units, private ABI, and applicable sanitizer gates pass; parents and follow-up are cross-linked.