Skip to content

Commit

Permalink
attempt to add, add()
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed Jun 2, 2024
1 parent 507e46a commit 25e9fb9
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions core/engine/src/builtins/temporal/plain_year_month/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ use boa_gc::{Finalize, Trace};
use boa_macros::js_str;
use boa_profiler::Profiler;

use super::{calendar::to_temporal_calendar_slot_value, DateTimeValues};
use super::{
calendar::to_temporal_calendar_slot_value, to_temporal_partial_duration, DateTimeValues,
};

use temporal_rs::{
components::{
calendar::{CalendarSlot, GetCalendarSlot},
duration::DurationOperation,
YearMonth as InnerYearMonth,
},
iso::IsoDateSlots,
Expand Down Expand Up @@ -325,7 +328,7 @@ impl PlainYearMonth {
.into());
};

Ok(InnerYearMonth::<JsObject>::get_days_in_year(&year_month, context)?.into())
Ok(InnerYearMonth::<JsObject>::contextual_get_days_in_year(&year_month, context)?.into())
}

fn get_days_in_month(
Expand All @@ -343,7 +346,7 @@ impl PlainYearMonth {
.into());
};

Ok(InnerYearMonth::<JsObject>::get_days_in_month(&year_month, context)?.into())
Ok(InnerYearMonth::<JsObject>::contextual_get_days_in_month(&year_month, context)?.into())
}

fn get_months_in_year(
Expand Down Expand Up @@ -385,10 +388,40 @@ impl PlainYearMonth {
.into())
}

fn add(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Err(JsNativeError::typ()
.with_message("not yet implemented.")
.into())
fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let obj = this
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("this must be an object."))?;

let Ok(year_month) = obj.clone().downcast::<Self>() else {
return Err(JsNativeError::typ()
.with_message("the this object must be a PlainYearMonth object.")
.into());
};

// Convert args to a duration
// TODO: handle string durations
let duration_like = args.get_or_undefined(0);
let options = get_options_object(args.get_or_undefined(1))?;
let overflow = get_option(&options, js_str!("overflow"), context)?
.unwrap_or(ArithmeticOverflow::Constrain);
if duration_like.is_object() {
let duration = to_temporal_partial_duration(duration_like, context)?;
let year_month_result =
InnerYearMonth::<JsObject>::contextual_add_or_subtract_duration(
DurationOperation::Add,
&year_month,
duration,
context,
overflow,
)
.expect("Error adding duration to year month");
create_temporal_year_month(year_month_result, None, context)
} else {
return Err(JsNativeError::typ()
.with_message("cannot handler string durations yet.")
.into());
}
}

fn subtract(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Expand Down

0 comments on commit 25e9fb9

Please sign in to comment.