From 25e9fb967e54c1709679f0c793f908ea0daf0f8b Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Sun, 2 Jun 2024 22:51:49 +0100 Subject: [PATCH] attempt to add, add() --- .../builtins/temporal/plain_year_month/mod.rs | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) 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 e2f0eca2e0b..c4323d3a456 100644 --- a/core/engine/src/builtins/temporal/plain_year_month/mod.rs +++ b/core/engine/src/builtins/temporal/plain_year_month/mod.rs @@ -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, @@ -325,7 +328,7 @@ impl PlainYearMonth { .into()); }; - Ok(InnerYearMonth::::get_days_in_year(&year_month, context)?.into()) + Ok(InnerYearMonth::::contextual_get_days_in_year(&year_month, context)?.into()) } fn get_days_in_month( @@ -343,7 +346,7 @@ impl PlainYearMonth { .into()); }; - Ok(InnerYearMonth::::get_days_in_month(&year_month, context)?.into()) + Ok(InnerYearMonth::::contextual_get_days_in_month(&year_month, context)?.into()) } fn get_months_in_year( @@ -385,10 +388,40 @@ impl PlainYearMonth { .into()) } - fn add(_this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult { - Err(JsNativeError::typ() - .with_message("not yet implemented.") - .into()) + fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult { + let obj = this + .as_object() + .ok_or_else(|| JsNativeError::typ().with_message("this must be an object."))?; + + let Ok(year_month) = obj.clone().downcast::() 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::::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 {