Skip to content

runtime: implement Array.prototype.toLocaleString element dispatch #2808

Description

@andrewtdiz

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:951 installs ("toLocaleString", 0) on Array.prototype.
  • Source search for ArrayToLocale, ArrayLocale, and toLocaleString finds no Array HIR node, no js_array_to_locale_string helper, and no Array dynamic branch in crates/perry-runtime/src/object/native_call_method.rs.
  • The only generic runtime toLocaleString fallback I found is for primitives: crates/perry-runtime/src/object/native_call_method.rs:1879-1889 delegates primitive value.toLocaleString() to toString().
  • Date/number-specific toLocaleString lowerings 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 0 to length - 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions