Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions crates/perry-runtime/src/intl/date_collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,8 @@ pub(crate) fn temporal_locale_string(
}
};

let mut date_style = get_opt("dateStyle");
let mut time_style = get_opt("timeStyle");
let date_style = get_opt("dateStyle");
let time_style = get_opt("timeStyle");
let year_opt = get_opt("year");
let month_opt = get_opt("month");
let day_opt = get_opt("day");
Expand Down Expand Up @@ -1414,26 +1414,26 @@ pub(crate) fn temporal_locale_string(
// timeStyle is silently ignored (the date-only value is formatted
// using dateStyle alone). Only throw when timeStyle is the sole
// style selector (no dateStyle to fall back on).
if time_style.is_some() && date_style.is_none() {
// Date-only Temporal types run `ToDateTimeOptions(options, "date",
// "date")`, whose `required = "date"` step throws a TypeError if ANY
// time-only style is present — `timeStyle` is rejected even when
// `dateStyle` is also supplied (test262
// .../toLocaleString/datestyle-and-timestyle for
// PlainDate/PlainYearMonth/PlainMonthDay).
if time_style.is_some() {
throw_type_error(
"timeStyle option is not valid for this Temporal type (no time component)",
);
}
// Silence timeStyle so downstream formatting uses date-only logic.
if time_style.is_some() && date_style.is_some() {
time_style = None;
}
}
TemporalLocaleCtx::PlainTime => {
// Symmetric: dateStyle alone throws; combined → drop dateStyle.
if date_style.is_some() && time_style.is_none() {
// Symmetric: `ToDateTimeOptions(options, "time", "time")` rejects any
// `dateStyle` on a time-only value, combined with `timeStyle` or not.
if date_style.is_some() {
throw_type_error(
"dateStyle option is not valid for Temporal.PlainTime (no date component)",
);
}
if date_style.is_some() && time_style.is_some() {
date_style = None;
}
}
TemporalLocaleCtx::ZonedDateTime => {
// The timeZone option is disallowed (ZDT carries its own timezone).
Expand Down
57 changes: 37 additions & 20 deletions crates/perry-runtime/src/temporal/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,27 +387,42 @@ fn reject_bare_islamic(id: &str) {

// ---- `Temporal.*.prototype.toLocaleString` ---------------------------------

/// Resolve the calendar the `Intl.DateTimeFormat` created for a
/// `toLocaleString(locales, options)` call would use: the explicit
/// `options.calendar` when present (validated as a real calendar identifier),
/// otherwise the locale default — which Perry's `Intl.DateTimeFormat` reports as
/// `gregory`. Returns a lowercase calendar id string. An `options.calendar`
/// that isn't a valid identifier throws a `RangeError` (matching
/// `Intl.DateTimeFormat`'s own validation).
fn formatter_calendar(options_arg: f64) -> String {
if let Some(obj) = as_obj(options_arg) {
let raw = field(obj, "calendar");
if !is_undefined(raw) {
let s = read_string(raw);
// Validate against `temporal_rs`'s calendar table; an unknown id is a
// `RangeError`, exactly like the `Intl.DateTimeFormat` constructor.
ok_or_throw(temporal_rs::Calendar::try_from_utf8(s.as_bytes()));
return s.to_ascii_lowercase();
}
}
"gregory".to_string()
}
Comment on lines +397 to +409

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

temporal_rs 0.2.3 Calendar::try_from_utf8 case sensitivity

💡 Result:

In temporal_rs version 0.2.3, the Calendar::try_from_utf8 function is case-insensitive [1]. The internal implementation of Calendar::try_kind_from_utf8 explicitly handles case insensitivity by converting the input byte slice to lowercase using to_ascii_lowercase before parsing the calendar identifier [1]: let icu_locale_value = Value::try_from_utf8(&bytes.to_ascii_lowercase).map_err(|_| TemporalError::range.with_message("unknown calendar"))?; Additionally, the library includes unit tests that confirm this behavior, such as parsing strings like "iSo8601" and "iSO8601" successfully as the default ISO 8601 calendar [1]. This aligns with the broader requirement for Temporal implementations to treat calendar names as case-insensitive [2].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate formatter_calendar and read_string =="
rg -n "fn formatter_calendar|read_string\(" crates/perry-runtime/src/temporal/options.rs crates/perry-runtime/src -g '*.rs'

echo
echo "== show surrounding code in crates/perry-runtime/src/temporal/options.rs =="
sed -n '360,430p' crates/perry-runtime/src/temporal/options.rs

echo
echo "== locate read_string definition =="
rg -n "fn read_string|read_string\s*\(" crates/perry-runtime/src -g '*.rs'

Repository: PerryTS/perry

Length of output: 17856


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== read_string definition in temporal/dispatch.rs =="
sed -n '130,210p' crates/perry-runtime/src/temporal/dispatch.rs

echo
echo "== options helpers around formatter_calendar to see how options values are read elsewhere =="
sed -n '320,380p' crates/perry-runtime/src/temporal/options.rs

Repository: PerryTS/perry

Length of output: 5887


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== field() definition and string option helpers =="
rg -n "fn field\(|field\(obj, \"calendar\"\)|JSValue::from_bits\(.*\)\.is_string\(\)" crates/perry-runtime/src -g '*.rs'

echo
echo "== likely option helpers around string coercion in options.rs =="
sed -n '1,120p' crates/perry-runtime/src/temporal/options.rs

echo
echo "== inspect field() implementation if found =="
rg -n "pub fn field\(|fn field\(" crates/perry-runtime/src -g '*.rs'

Repository: PerryTS/perry

Length of output: 9582


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== field() implementation =="
sed -n '520,555p' crates/perry-runtime/src/temporal/options.rs

echo
echo "== nearby helpers that gate string reads in options.rs =="
sed -n '556,610p' crates/perry-runtime/src/temporal/options.rs

echo
echo "== formatter calendar use site and related option readers =="
sed -n '1228,1255p' crates/perry-runtime/src/temporal/options.rs
sed -n '1460,1485p' crates/perry-runtime/src/temporal/options.rs

Repository: PerryTS/perry

Length of output: 6738


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== find other calendar option readers and formatter calendar callers =="
rg -n "formatter_calendar\(|calendar.*GetOption|calendar.*str_field_coerce|options\.calendar|checkStringOptionWrongType|ToString coercion" crates/perry-runtime/src -g '*.rs'

echo
echo "== inspect the toLocaleString/calendar match logic around formatter_calendar use =="
sed -n '1235,1260p' crates/perry-runtime/src/temporal/options.rs
sed -n '1370,1395p' crates/perry-runtime/src/temporal/options.rs

Repository: PerryTS/perry

Length of output: 3854


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate js_get_string_pointer_unified =="
rg -n "js_get_string_pointer_unified" crates/perry-runtime/src crates/perry-codegen/src -g '*.rs'

echo
echo "== inspect the implementation / callers around string pointer unification =="
sed -n '1,120p' crates/perry-runtime/src/value.rs

Repository: PerryTS/perry

Length of output: 22786


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== js_get_string_pointer_unified implementation =="
sed -n '220,290p' crates/perry-runtime/src/value/nanbox.rs

echo
echo "== any docs/comments describing coercion behavior =="
sed -n '290,340p' crates/perry-runtime/src/value/nanbox.rs

Repository: PerryTS/perry

Length of output: 5123


Coerce options.calendar before parsing it. formatter_calendar reads the raw property and passes it straight to read_string, so non-string values skip ToString/GetOption(type: string) semantics and can be misread instead of converted. temporal_rs::Calendar::try_from_utf8 already handles mixed-case IDs, so the remaining fix is to use the string-option coercion path here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-runtime/src/temporal/options.rs` around lines 397 - 409, The
formatter_calendar helper is reading options.calendar directly with read_string,
which skips the normal string coercion path. Update formatter_calendar to coerce
the calendar option through the same string-option/GetOption semantics used
elsewhere before validating with temporal_rs::Calendar::try_from_utf8, and keep
the existing fallback/default handling in place.


/// `Temporal.*.prototype.toLocaleString` calendar check for `PlainDate`,
/// `PlainDateTime`, and `ZonedDateTime` — per `HandleDateTimeTemporalDate`
/// (ecma402 sup-intl.html #1107) and the analogous `ZonedDateTime` clause
/// (#1953), a value renders only when its calendar matches the formatter's:
/// the ISO-8601 calendar (always permitted) or the calendar the locale
/// resolves to. With no explicit `calendar` option that resolved calendar is
/// the locale default, which Perry's `Intl.DateTimeFormat` reports as
/// `gregory`. Any other calendar is a `RangeError` (the test262
/// the ISO-8601 calendar (always permitted) or the calendar the formatter
/// resolves to. The formatter's calendar is the explicit `options.calendar`
/// (2nd argument) when supplied, else the locale default (`gregory`). Any other
/// instance calendar is a `RangeError` (the test262
/// `toLocaleString/calendar-mismatch` cases).
///
/// `PlainYearMonth`/`PlainMonthDay` do NOT get the ISO carve-out — see
/// [`assert_locale_string_calendar_no_iso_carveout`].
///
/// NOTE: the `locales`/`options` arguments — which could name a *different*
/// calendar to validate against — are dropped by the `Expr::DateToLocaleString`
/// HIR lowering before the call reaches the runtime, so only the locale-default
/// calendar is consulted here. Honoring an explicit `calendar`/`dateStyle`/
/// `timeStyle` option (and the spec's option-conflict `TypeError`s) is a
/// follow-up that depends on threading those arguments through that lowering.
pub fn assert_locale_string_calendar(instance_calendar: &str) {
if instance_calendar != "iso8601" && instance_calendar != "gregory" {
pub fn assert_locale_string_calendar(instance_calendar: &str, options_arg: f64) {
let formatter = formatter_calendar(options_arg);
if instance_calendar != "iso8601" && instance_calendar != formatter {
range("calendar mismatch: the value's calendar differs from the locale calendar");
}
}
Expand All @@ -416,13 +431,15 @@ pub fn assert_locale_string_calendar(instance_calendar: &str) {
/// check. `HandleDateTimeTemporalYearMonth`/`HandleDateTimeTemporalMonthDay`
/// (ecma402 sup-intl.html #1132, #1157) require the instance calendar to
/// equal the formatter's calendar exactly — unlike `PlainDate`/`PlainDateTime`
/// /`ZonedDateTime`, there is no `"iso8601"`-is-always-permitted carve-out, so
/// an ISO-calendar year-month/month-day always mismatches the locale's
/// default (`gregory`) calendar and throws (test262
/// `toLocaleString/calendar-mismatch` "even when instance has the ISO
/// calendar" cases).
pub fn assert_locale_string_calendar_no_iso_carveout(instance_calendar: &str) {
if instance_calendar != "gregory" {
/// /`ZonedDateTime`, there is no `"iso8601"`-is-always-permitted carve-out. The
/// formatter's calendar is the explicit `options.calendar` (2nd argument) when
/// supplied, else the locale default (`gregory`). So an ISO-calendar
/// year-month/month-day renders only when the caller passes
/// `{ calendar: "iso8601" }`, and mismatches the locale default otherwise
/// (test262 `toLocaleString/calendar-mismatch` "even when instance has the ISO
/// calendar" cases as well as `return-string` / `dateStyle`).
pub fn assert_locale_string_calendar_no_iso_carveout(instance_calendar: &str, options_arg: f64) {
if instance_calendar != formatter_calendar(options_arg) {
range("calendar mismatch: the value's calendar differs from the locale calendar");
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/perry-runtime/src/temporal/plain_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ pub fn call(recv: f64, d: &PlainDate, name: &str, args: &[f64]) -> f64 {
}
"toJSON" => string(&d.to_string()),
"toLocaleString" => {
super::options::assert_locale_string_calendar(d.calendar().identifier());
super::options::assert_locale_string_calendar(
d.calendar().identifier(),
raw_arg(args, 1),
);
let epoch_ms = crate::date::components_to_timestamp(
d.year(),
d.month() as u32,
Expand Down
5 changes: 4 additions & 1 deletion crates/perry-runtime/src/temporal/plain_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ pub fn call(recv: f64, dt: &PlainDateTime, name: &str, args: &[f64]) -> f64 {
}
"toJSON" => string(&dt.to_string()),
"toLocaleString" => {
super::options::assert_locale_string_calendar(dt.calendar().identifier());
super::options::assert_locale_string_calendar(
dt.calendar().identifier(),
raw_arg(args, 1),
);
let epoch_ms = crate::date::components_to_timestamp(
dt.year(),
dt.month() as u32,
Expand Down
1 change: 1 addition & 0 deletions crates/perry-runtime/src/temporal/plain_month_day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn call(recv: f64, md: &PlainMonthDay, name: &str, args: &[f64]) -> f64 {
"toLocaleString" => {
super::options::assert_locale_string_calendar_no_iso_carveout(
md.calendar().identifier(),
raw_arg(args, 1),
);
let epoch_ms = crate::date::components_to_timestamp(
1970,
Expand Down
1 change: 1 addition & 0 deletions crates/perry-runtime/src/temporal/plain_year_month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ pub fn call(recv: f64, ym: &PlainYearMonth, name: &str, args: &[f64]) -> f64 {
"toLocaleString" => {
super::options::assert_locale_string_calendar_no_iso_carveout(
ym.calendar().identifier(),
raw_arg(args, 1),
);
let epoch_ms =
crate::date::components_to_timestamp(ym.year(), ym.month() as u32, 1, 0, 0, 0)
Expand Down
5 changes: 4 additions & 1 deletion crates/perry-runtime/src/temporal/zoned_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ pub fn call(recv: f64, z: &ZonedDateTime, name: &str, args: &[f64]) -> f64 {
}
"toJSON" => string(&z.to_string()),
"toLocaleString" => {
super::options::assert_locale_string_calendar(z.calendar().identifier());
super::options::assert_locale_string_calendar(
z.calendar().identifier(),
raw_arg(args, 1),
);
let epoch_ms = z.epoch_milliseconds() as f64;
crate::intl::temporal_locale_string(
epoch_ms,
Expand Down
Loading