Skip to content

Commit

Permalink
Add valueOf methods to the Temporal builtins (#4079)
Browse files Browse the repository at this point in the history
* Add valueOf methods to the builtins

* Adjust PlainTime valueOf implmentation

* Fix error to TypeError

* Add ZonedDateTime.prototype.valueOf

* Change wording on valueOf error message
  • Loading branch information
nekevss authored Dec 11, 2024
1 parent f5ba695 commit 4cc816f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/duration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -794,6 +795,12 @@ impl Duration {
.with_message("not yet implemented.")
.into())
}

pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// -- Duration Abstract Operations --
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/instant/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -474,6 +475,12 @@ impl Instant {
.with_message("not yet implemented.")
.into())
}

pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// -- Instant Abstract Operations --
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/plain_date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// -- `PlainDate` Abstract Operations --
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/plain_date_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// ==== `PlainDateTime` Abstract Operations` ====
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/plain_month_day/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

impl BuiltInObject for PlainMonthDay {
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions core/engine/src/builtins/temporal/plain_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,10 @@ impl PlainTime {
}

/// 4.3.22 Temporal.PlainTime.prototype.valueOf ( )
fn value_of(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 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())
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/plain_year_month/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// ==== Abstract Operations ====
Expand Down
7 changes: 7 additions & 0 deletions core/engine/src/builtins/temporal/zoneddatetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -838,6 +839,12 @@ impl ZonedDateTime {
)
.map(Into::into)
}

pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::typ()
.with_message("`valueOf` not supported by Temporal built-ins. See 'compare', 'equals', or `toString`")
.into())
}
}

// -- ZonedDateTime Abstract Operations --
Expand Down

0 comments on commit 4cc816f

Please sign in to comment.