Repository navigation
runtime: implement Array.prototype.toLocaleString element dispatch #2808
Description
Summary
Array.prototype.toLocaleString(locales?, options?) should call each non-nullish element's own toLocaleString(locales, options), join the results with commas, and render null / undefined elements as empty fields. Perry installs the Array prototype method name, but source search does not show an Array-specific implementation or dynamic dispatch branch.
Node parity probe
Run with Node v25.9.0:
const obj = {
toLocaleString(locales, options) {
return `obj:${locales}:${options && options.tag}`;
},
};
for (const [label, fn] of [
["plain", () => [1, null, undefined, "x"].toLocaleString()],
["locales/options forwarded", () => [obj].toLocaleString("xx-YY", { tag: "opt" })],
["number locale", () => [1000.5].toLocaleString("en-US")],
["date locale", () => [new Date(Date.UTC(2020, 0, 2))].toLocaleString("en-US", { timeZone: "UTC" })],
]) {
try {
console.log(label + ": " + JSON.stringify(fn()));
} catch (e) {
console.log(label + ": throws " + e.name + ": " + e.message);
}
}Output:
plain: "1,,,x"
locales/options forwarded: "obj:xx-YY:opt"
number locale: "1,000.5"
date locale: "1/2/2020, 12:00:00 AM"
Current Perry behavior in source
The method is exposed but there is no visible Array implementation:
crates/perry-runtime/src/object/global_this.rs:951installs("toLocaleString", 0)onArray.prototype.- Source search for
ArrayToLocale,ArrayLocale, andtoLocaleStringfinds no Array HIR node, nojs_array_to_locale_stringhelper, and no Array dynamic branch incrates/perry-runtime/src/object/native_call_method.rs. - The only generic runtime
toLocaleStringfallback I found is for primitives:crates/perry-runtime/src/object/native_call_method.rs:1879-1889delegates primitivevalue.toLocaleString()totoString(). - Date/number-specific
toLocaleStringlowerings exist elsewhere, but they do not implement the Array algorithm that calls each element's own method with(locales, options).
So array.toLocaleString(...) currently has no Array-specific path to walk elements, skip nullish values, and forward locale/options arguments.
Expected fix shape
Implement Array-specific toLocaleString semantics:
- Iterate the array from
0tolength - 1. - For
null/undefined/ holes, append an empty string. - For other values, call that element's
toLocaleString(locales, options)and stringify the result. - Join element strings with comma separators.
- Preserve locale/options forwarding for objects, numbers, dates, and other values.
Please add regression coverage for nullish elements, custom element toLocaleString, number/date elements with locales/options, and dynamic dispatch through an any receiver.
Activity