Skip to content

fix(ecmascript): fold string methods by UTF-16 code units - #26752

Merged
Dunqing merged 0 commit into
jsstr-estree-escape-tablefrom
jsstr-utf16-string-folds
Sep 27, 2026
Merged

Dunqing merged 0 commit into
jsstr-estree-escape-tablefrom
jsstr-utf16-string-folds

Conversation

@Dunqing

@Dunqing Dunqing commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Continued in #27096. GitHub marked this PR indirectly merged during stack reordering; the change has not landed in main.

The indexOf, lastIndexOf, slice, and substring folds indexed strings by Rust character and byte offset, but JavaScript string positions are UTF-16 code units, so any astral character made the folded value wrong: "a😀b".indexOf("b") folded to 5 instead of 3, "a😀b".slice(1, 3) to the wrong text, and "a😀b".substring(3, 4) to the empty string. The rewrite also surfaced two adjacent numeric bugs: a position beyond the 32-bit range wrapped to a small index, folding "aa".indexOf("a", 3e9) to 0 instead of -1, and a NaN lastIndexOf position searched from the start instead of the end.

Search, clamp, and slice over the encoded code units, converting positions per ToIntegerOrInfinity. The searches stay over bytes, since a UTF-8 match can only begin on a character boundary; only the positions at the edges convert, so no fold allocates, and a folded slice or substring borrows the arena string. A boundary that falls inside a surrogate pair has no Rust string representation, so the fold declines those ranges. A NaN end also declines, since after evaluation it is indistinguishable from undefined, which means the opposite boundary. charAt, charCodeAt, string indexing, and length already counted code units and are untouched. The StringSubstring trait is removed: the fold slices the arena string directly and no other caller existed. Removing a public trait is a breaking change to oxc_ecmascript.

Example

console.log("a😀b".indexOf("b"), "a😀b".slice(1, 3), "a😀b".substring(3, 4));

Before minification folds this to 5 "😀b" ""; node prints 3 "😀" "b", and the minified output now matches node exactly. A result that would split the emoji, such as "a😀b".substring(1, 2), stays unfolded.

Verified against node with a generated corpus of 2,408 method calls over ASCII, multi-byte, astral, and lone-surrogate haystacks, an independent review oracle of 9,160 cases, byte-identical minify-twice output, an unchanged minification size snapshot, a full conformance run with zero drift, and the crate test suites.

Ninth PR in the JSStr stack, based on #26743. Part of #26242.

Implemented with AI assistance.

Dunqing commented Sep 17, 2026 •

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions Bot added A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler A-ecmascript Area - ECMAScript spec operations, oxc_ecmascript labels Sep 17, 2026
@codspeed

codspeed Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 87 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing jsstr-utf16-string-folds (f6d2e81) with jsstr-estree-escape-table (301441e)2

Open in CodSpeed

Footnotes

  1. 9 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on jsstr-estree-escape-table (4e50dd5) during the generation of this report, so f171bd1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 3 times, most recently from 8420254 to 1e90e0d Compare September 17, 2026 04:14
@Dunqing
Dunqing force-pushed the jsstr-encode-utf16-size-hint branch from 92954b0 to e6fef16 Compare September 18, 2026 02:20
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from d29d07b to ca3e24e Compare September 18, 2026 15:15
@Dunqing
Dunqing force-pushed the jsstr-encode-utf16-size-hint branch from e6fef16 to 81912e4 Compare September 18, 2026 15:15
@github-actions github-actions Bot added the A-ast Area - AST definitions and visitors, oxc_ast / oxc_ast_visit label Sep 18, 2026
@Dunqing
Dunqing force-pushed the jsstr-encode-utf16-size-hint branch from 5615ee7 to e9e1efe Compare September 21, 2026 01:14
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from ee9b3b7 to 3df1998 Compare September 21, 2026 01:14
@github-actions github-actions Bot added the A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details label Sep 21, 2026
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from caada83 to 871acbe Compare September 21, 2026 01:48
@Dunqing
Dunqing force-pushed the jsstr-encode-utf16-size-hint branch 2 times, most recently from 0689ee9 to ab69599 Compare September 21, 2026 01:54
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 871acbe to bbae803 Compare September 21, 2026 01:54
@Dunqing
Dunqing force-pushed the jsstr-encode-utf16-size-hint branch from ab69599 to 6d6357e Compare September 21, 2026 02:54
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch 2 times, most recently from a2e3fd0 to 21771a1 Compare September 22, 2026 02:41
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 4013fd9 to 761e789 Compare September 22, 2026 04:12
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch 2 times, most recently from 14a0af9 to 40d03e5 Compare September 22, 2026 08:20
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 5c24f47 to a92bf91 Compare September 23, 2026 01:55
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch 2 times, most recently from 7597f5a to dc17203 Compare September 23, 2026 02:19
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from a92bf91 to 805fbe7 Compare September 23, 2026 02:19
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch from dc17203 to bab6a6d Compare September 23, 2026 02:24
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from fde800f to 5c35c20 Compare September 23, 2026 02:55
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch from bab6a6d to 1bcb751 Compare September 23, 2026 02:55
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 5c35c20 to 68650e3 Compare September 23, 2026 03:14
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch from 1bcb751 to 301441e Compare September 23, 2026 03:14
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 68650e3 to f6d2e81 Compare September 23, 2026 07:22
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch from 301441e to 4e50dd5 Compare September 23, 2026 07:22
@Dunqing Dunqing closed this Sep 27, 2026
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from f6d2e81 to 8d1a725 Compare September 27, 2026 06:37
@Dunqing
Dunqing merged commit 8d1a725 into jsstr-estree-escape-table Sep 27, 2026
@Dunqing
Dunqing force-pushed the jsstr-estree-escape-table branch from 4e50dd5 to 5c1efa5 Compare September 27, 2026 06:37
@Dunqing
Dunqing deleted the jsstr-utf16-string-folds branch September 27, 2026 06:37
@Dunqing
Dunqing restored the jsstr-utf16-string-folds branch September 27, 2026 06:40
graphite-app Bot pushed a commit that referenced this pull request Oct 19, 2026
String indexing now folds by UTF-16 code unit, so `"😀"[0]` folds to `"\uD83D"` and `"a\uD800b"[1]` to `"\uD800"`. The result is built with `JSStrBuilder` instead of a temporary heap string; ordinary strings keep the native UTF-8 path.

Closes #26636. Based on #26717. Part of #26242. Continues #26752.

Implemented with AI assistance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST definitions and visitors, oxc_ast / oxc_ast_visit A-ecmascript Area - ECMAScript spec operations, oxc_ecmascript A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant