typedarray: use locale-sensitive separator in %TypedArray%.prototype.toLocaleString#5089
typedarray: use locale-sensitive separator in %TypedArray%.prototype.toLocaleString#5089mrhapile wants to merge 9 commits intoboa-dev:mainfrom
Conversation
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
There was a problem hiding this comment.
Pull request overview
Updates %TypedArray%.prototype.toLocaleString to use the same locale-sensitive list separator logic as Array.prototype.toLocaleString when the intl feature is enabled, keeping behavior aligned with the spec and existing array behavior.
Changes:
- Compute a locale-aware separator via ICU
ListFormatterbehind theintlfeature flag. - Fall back to the default
", "separator whenintlis disabled. - Hoist
locales/optionsextraction outside the iteration loop and remove the unusedutf16import.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Test262 conformance changes
Tested main commit: |
Preallocate the result buffer to reduce reallocations when concatenating separator and element strings, similar to the Array.prototype.toLocaleString implementation. Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
There was a problem hiding this comment.
Pull request overview
Updates %TypedArray%.prototype.toLocaleString to use the same locale-sensitive list separator behavior as Array.prototype.toLocaleString when the intl feature is enabled, while keeping the ", " fallback when intl is disabled.
Changes:
- Use ICU
ListFormatter(viaintl_provider) to compute a locale-sensitive separator undercfg(feature = "intl"). - Hoist
locales/optionsextraction outside the element iteration and remove the unusedutf16import. - Adjust separator handling to work with
JsString(iterating code units) and pre-allocate injoin.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Reserve capacity for the result buffer in %TypedArray%.prototype.toLocaleString to reduce repeated reallocations when concatenating separators and element strings. This mirrors the approach used in Array.prototype.toLocaleString. Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5089 +/- ##
===========================================
+ Coverage 47.24% 59.13% +11.88%
===========================================
Files 476 563 +87
Lines 46892 62691 +15799
===========================================
+ Hits 22154 37071 +14917
- Misses 24738 25620 +882 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| use crate::builtins::intl::locale::default_locale; | ||
| use icu_list::{ | ||
| ListFormatter, ListFormatterPreferences, options::ListFormatterOptions, | ||
| }; | ||
|
|
||
| let locale = default_locale(context.intl_provider().locale_canonicalizer()?); | ||
| let preferences = ListFormatterPreferences::from(&locale); | ||
| let formatter = ListFormatter::try_new_unit_with_buffer_provider( | ||
| context.intl_provider().erased_provider(), | ||
| preferences, | ||
| ListFormatterOptions::default(), | ||
| ) | ||
| .map_err(|e| JsNativeError::typ().with_message(e.to_string()))?; | ||
|
|
||
| // Ask ICU for the list pattern literal by formatting two empty elements. | ||
| // For many locales this yields ", ", but it may differ. | ||
| js_string!( | ||
| formatter.format_to_string(std::iter::once("").chain(std::iter::once(""))) | ||
| ) |
There was a problem hiding this comment.
In this case we kinda don't want to follow the implementation of Array, because it would create one NumberFormat per element, which is extremely inefficient
Instead, create a NumberFormat and a ListFormat then do a format of all numbers followed by a format with ListFormat
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
|
You didn't address my comment |
|
@jedel1043 I did attempt the shared NumberFormat + ListFormatter approach, but it caused several Test262 failures (particularly for BigInt TypedArrays). To keep the implementation spec-correct for now, I temporarily removed the NumberFormat change and kept the ListFormatter. |
|
I'm currently investigating how to correctly integrate a shared NumberFormat while preserving full Test262 conformance , working on it will update sooon!!! |
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
|
right now |
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
|
Results (ECMAScript Next): |
…g shared NumberFormat optimization Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
3b43a7c to
b78e36f
Compare
…d ListFormatter Avoid creating a NumberFormat per element by reusing a single NumberFormat and ListFormatter instance for the entire typed array. Falls back to the spec path when: - Number.prototype.toLocaleString is overridden - elements contain NaN or Infinity - the typed array uses BigInt Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
|
I see what's happening. The test262 suite specifically tests that you call Okay, I guess we'll have to return to the old approach of using the separator and manually calling toLocaleString. I'll think of a way to optimise this in the future. |
|
@jedel1043 nahh now I have updated it I was just confirming and testing things before making this pr ready for review |
|
I did see that, but I still put my comment becusse if the spec requires you to call Number.prototype.toLocaleString, then I don't think we should do special handling in TypedArray.prototype.toLocaleString. We should instead optimize Number.prototype.toLocaleString directly |
|
should I revert it back to the simple approach |
|
yep, revert back |
Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
| /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.tolocalestring | ||
| #[inline] | ||
| #[must_use] | ||
| pub fn number_prototype_to_locale_string(&self) -> JsFunction { |
There was a problem hiding this comment.
We don't need this anymore since the changes were reverted

Summary
Update
%TypedArray%.prototype.toLocaleStringto use a locale-sensitive separator when theintlfeature is enabled, aligning its behavior withArray.prototype.toLocaleString.ref - #5081
Previously, the TypedArray implementation always used the hardcoded separator
", ". When theintlfeature is enabled,Array.prototype.toLocaleStringalready derives the separator using ICU'sListFormatter. This PR updates the TypedArray implementation to use the same locale-aware separator logic.When the
intlfeature is disabled, the implementation correctly falls back to", ".Changes
ListFormattervia theintl_providerto compute a locale-sensitive separator when theintlfeature is enabled.", "whenintlis disabled.localesandoptionsarguments outside the iteration loop to avoid repeated lookups.utf16import from the module.Specification
This aligns the implementation with:
%TypedArray%.prototype.toLocaleStringwhich specifies that the algorithm should behave the same as:
Array.prototype.toLocaleStringexcept that
TypedArrayLengthis used instead ofLengthOfArrayLike.Verification
The following checks were performed:
cargo fmtcargo clippycargo testAll existing tests pass and no regressions were introduced.
Notes
The behavior now matches the locale-sensitive list separator behavior used by
Array.prototype.toLocaleStringwhenintlsupport is enabled.