From 4cc816ff5edeb8257591daac9993f046dccf6276 Mon Sep 17 00:00:00 2001 From: Kevin Ness <46825870+nekevss@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:19:38 -0600 Subject: [PATCH] Add valueOf methods to the Temporal builtins (#4079) * Add valueOf methods to the builtins * Adjust PlainTime valueOf implmentation * Fix error to TypeError * Add ZonedDateTime.prototype.valueOf * Change wording on valueOf error message --- core/engine/src/builtins/temporal/duration/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/instant/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/plain_date/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/plain_date_time/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/plain_month_day/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/plain_time/mod.rs | 4 ++-- core/engine/src/builtins/temporal/plain_year_month/mod.rs | 7 +++++++ core/engine/src/builtins/temporal/zoneddatetime/mod.rs | 7 +++++++ 8 files changed, 51 insertions(+), 2 deletions(-) diff --git a/core/engine/src/builtins/temporal/duration/mod.rs b/core/engine/src/builtins/temporal/duration/mod.rs index 20d61326a10..57794db61ab 100644 --- a/core/engine/src/builtins/temporal/duration/mod.rs +++ b/core/engine/src/builtins/temporal/duration/mod.rs @@ -191,6 +191,7 @@ impl IntrinsicObject for Duration { .method(Self::total, js_string!("total"), 1) .method(Self::to_string, js_string!("toString"), 1) .method(Self::to_json, js_string!("toJSON"), 0) + .method(Self::value_of, js_string!("valueOf"), 0) .build(); } @@ -794,6 +795,12 @@ impl Duration { .with_message("not yet implemented.") .into()) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // -- Duration Abstract Operations -- diff --git a/core/engine/src/builtins/temporal/instant/mod.rs b/core/engine/src/builtins/temporal/instant/mod.rs index df9e35af89b..1103adc5d5a 100644 --- a/core/engine/src/builtins/temporal/instant/mod.rs +++ b/core/engine/src/builtins/temporal/instant/mod.rs @@ -91,6 +91,7 @@ impl IntrinsicObject for Instant { .method(Self::round, js_string!("round"), 1) .method(Self::equals, js_string!("equals"), 1) .method(Self::to_zoned_date_time, js_string!("toZonedDateTime"), 1) + .method(Self::value_of, js_string!("valueOf"), 0) .method( Self::to_zoned_date_time_iso, js_string!("toZonedDateTimeISO"), @@ -474,6 +475,12 @@ impl Instant { .with_message("not yet implemented.") .into()) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // -- Instant Abstract Operations -- diff --git a/core/engine/src/builtins/temporal/plain_date/mod.rs b/core/engine/src/builtins/temporal/plain_date/mod.rs index c8d53f76cfc..1895cee55a0 100644 --- a/core/engine/src/builtins/temporal/plain_date/mod.rs +++ b/core/engine/src/builtins/temporal/plain_date/mod.rs @@ -216,6 +216,7 @@ impl IntrinsicObject for PlainDate { .method(Self::since, js_string!("since"), 1) .method(Self::equals, js_string!("equals"), 1) .method(Self::to_plain_datetime, js_string!("toPlainDateTime"), 0) + .method(Self::value_of, js_string!("valueOf"), 0) .build(); } @@ -783,6 +784,12 @@ impl PlainDate { // 4. Return ? CreateTemporalDateTime(temporalDate.[[ISOYear]], temporalDate.[[ISOMonth]], temporalDate.[[ISODay]], temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], temporalDate.[[Calendar]]). create_temporal_datetime(date.inner.to_date_time(time)?, None, context).map(Into::into) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // -- `PlainDate` Abstract Operations -- diff --git a/core/engine/src/builtins/temporal/plain_date_time/mod.rs b/core/engine/src/builtins/temporal/plain_date_time/mod.rs index 811e0ce2ca1..39395e643b1 100644 --- a/core/engine/src/builtins/temporal/plain_date_time/mod.rs +++ b/core/engine/src/builtins/temporal/plain_date_time/mod.rs @@ -277,6 +277,7 @@ impl IntrinsicObject for PlainDateTime { .method(Self::since, js_string!("since"), 1) .method(Self::round, js_string!("round"), 1) .method(Self::equals, js_string!("equals"), 1) + .method(Self::value_of, js_string!("valueOf"), 0) .build(); } @@ -904,6 +905,12 @@ impl PlainDateTime { // 6. Return ? CalendarEquals(dateTime.[[Calendar]], other.[[Calendar]]). Ok((dt.inner == other).into()) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // ==== `PlainDateTime` Abstract Operations` ==== diff --git a/core/engine/src/builtins/temporal/plain_month_day/mod.rs b/core/engine/src/builtins/temporal/plain_month_day/mod.rs index 57ef80d2c7f..29d3144891e 100644 --- a/core/engine/src/builtins/temporal/plain_month_day/mod.rs +++ b/core/engine/src/builtins/temporal/plain_month_day/mod.rs @@ -118,6 +118,12 @@ impl PlainMonthDay { Ok(month_day_to_string(inner, show_calendar)) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } impl BuiltInObject for PlainMonthDay { @@ -164,6 +170,7 @@ impl IntrinsicObject for PlainMonthDay { Attribute::CONFIGURABLE, ) .method(Self::to_string, js_string!("toString"), 1) + .method(Self::value_of, js_string!("valueOf"), 0) .static_method(Self::from, js_string!("from"), 2) .build(); } diff --git a/core/engine/src/builtins/temporal/plain_time/mod.rs b/core/engine/src/builtins/temporal/plain_time/mod.rs index 31acfb735de..321338d4809 100644 --- a/core/engine/src/builtins/temporal/plain_time/mod.rs +++ b/core/engine/src/builtins/temporal/plain_time/mod.rs @@ -566,10 +566,10 @@ impl PlainTime { } /// 4.3.22 Temporal.PlainTime.prototype.valueOf ( ) - fn value_of(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { // 1. Throw a TypeError exception. Err(JsNativeError::typ() - .with_message("valueOf cannot be called on PlainTime.") + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") .into()) } } diff --git a/core/engine/src/builtins/temporal/plain_year_month/mod.rs b/core/engine/src/builtins/temporal/plain_year_month/mod.rs index d4dcdfd7f3a..7f6349cec40 100644 --- a/core/engine/src/builtins/temporal/plain_year_month/mod.rs +++ b/core/engine/src/builtins/temporal/plain_year_month/mod.rs @@ -140,6 +140,7 @@ impl IntrinsicObject for PlainYearMonth { .method(Self::since, js_string!("since"), 2) .method(Self::equals, js_string!("equals"), 1) .method(Self::to_string, js_string!("toString"), 1) + .method(Self::value_of, js_string!("valueOf"), 0) .build(); } @@ -406,6 +407,12 @@ impl PlainYearMonth { Ok(year_month_to_string(inner, show_calendar)) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // ==== Abstract Operations ==== diff --git a/core/engine/src/builtins/temporal/zoneddatetime/mod.rs b/core/engine/src/builtins/temporal/zoneddatetime/mod.rs index f4d16cf5a7f..bfee3ae071f 100644 --- a/core/engine/src/builtins/temporal/zoneddatetime/mod.rs +++ b/core/engine/src/builtins/temporal/zoneddatetime/mod.rs @@ -319,6 +319,7 @@ impl IntrinsicObject for ZonedDateTime { .static_method(Self::from, js_string!("from"), 1) .method(Self::add, js_string!("add"), 1) .method(Self::subtract, js_string!("subtract"), 1) + .method(Self::value_of, js_string!("valueOf"), 0) .build(); } @@ -838,6 +839,12 @@ impl ZonedDateTime { ) .map(Into::into) } + + pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { + Err(JsNativeError::typ() + .with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`") + .into()) + } } // -- ZonedDateTime Abstract Operations --