Skip to content

Commit

Permalink
Add valueOf methods to the builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Dec 9, 2024
1 parent c609fbc commit 029a62e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
13 changes: 10 additions & 3 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 @@ -786,24 +787,30 @@ impl Duration {

// TODO: Implement the rest of the new `Temporal.Duration.prototype.total`

Err(JsNativeError::range()
Err(JsNativeError::error()
.with_message("not yet implemented.")
.into())
}

/// 7.3.22 `Temporal.Duration.prototype.toString ( [ options ] )`
pub(crate) fn to_string(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::range()
Err(JsNativeError::error()
.with_message("not yet implemented.")
.into())
}

/// 7.3.23 `Temporal.Duration.prototype.toJSON ( )`
pub(crate) fn to_json(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::range()
Err(JsNativeError::error()
.with_message("not yet implemented.")
.into())
}

pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::error()
.with_message("valueOf not implemented for Temporal objects. 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 @@ -111,6 +111,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 @@ -532,6 +533,12 @@ impl Instant {
.with_message("not yet implemented.")
.into())
}

pub(crate) fn value_of(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::error()
.with_message("valueOf not implemented for Temporal objects. 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 @@ -212,6 +212,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 @@ -778,6 +779,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::error()
.with_message("valueOf not implemented for Temporal objects. 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::error()
.with_message("valueOf not implemented for Temporal objects. 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 @@ -115,6 +115,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::error()
.with_message("valueOf not implemented for Temporal objects. See 'compare', 'equals', or `toString`")
.into())
}
}

impl BuiltInObject for PlainMonthDay {
Expand Down Expand Up @@ -161,6 +167,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
6 changes: 3 additions & 3 deletions core/engine/src/builtins/temporal/plain_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,10 @@ impl PlainTime {
}

/// 4.3.22 Temporal.PlainTime.prototype.valueOf ( )
fn value_of(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
pub(crate) 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.")
Err(JsNativeError::error()
.with_message("valueOf not implemented for Temporal objects. 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::error()
.with_message("valueOf not implemented for Temporal objects. See 'compare', 'equals', or `toString`")
.into())
}
}

// ==== Abstract Operations ====
Expand Down

0 comments on commit 029a62e

Please sign in to comment.