@ -508,13 +508,9 @@ impl Date {
) -> JsResult < JsValue > {
) -> JsResult < JsValue > {
// 1. Let O be the this value.
// 1. Let O be the this value.
// 2. If Type(O) is not Object, throw a TypeError exception.
// 2. If Type(O) is not Object, throw a TypeError exception.
let o = if let Some ( o ) = this . as_object ( ) {
let o = this . as_object ( ) . ok_or_else ( | | {
o
JsNativeError ::typ ( ) . with_message ( "Date.prototype[@@toPrimitive] called on non object" )
} else {
} ) ? ;
return Err ( JsNativeError ::typ ( )
. with_message ( "Date.prototype[@@toPrimitive] called on non object" )
. into ( ) ) ;
} ;
let hint = args . get_or_undefined ( 0 ) ;
let hint = args . get_or_undefined ( 0 ) ;
@ -908,25 +904,13 @@ impl Date {
}
}
// 3. Let y be ? ToNumber(year).
// 3. Let y be ? ToNumber(year).
let y = args
let y = args . get_or_undefined ( 0 ) . to_number ( context ) ? ;
. get ( 0 )
. cloned ( )
. unwrap_or_default ( )
. to_number ( context ) ? ;
// 4. If month is not present, let m be MonthFromTime(t); otherwise, let m be ? ToNumber(month).
// 4. If month is not present, let m be MonthFromTime(t); otherwise, let m be ? ToNumber(month).
let m = if let Some ( m ) = args . get ( 1 ) {
let m = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( m . to_number ( context ) ? )
} else {
None
} ;
// 5. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
// 5. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
let dt = if let Some ( dt ) = args . get ( 2 ) {
let dt = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( dt . to_number ( context ) ? )
} else {
None
} ;
// 6. Let newDate be MakeDate(MakeDay(y, m, dt), TimeWithinDay(t)).
// 6. Let newDate be MakeDate(MakeDay(y, m, dt), TimeWithinDay(t)).
t . set_components ( false , Some ( y ) , m , dt , None , None , None , None ) ;
t . set_components ( false , Some ( y ) , m , dt , None , None , None , None ) ;
@ -965,25 +949,13 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 3. If min is not present, let m be MinFromTime(t); otherwise, let m be ? ToNumber(min).
// 3. If min is not present, let m be MinFromTime(t); otherwise, let m be ? ToNumber(min).
let m = if let Some ( m ) = args . get ( 1 ) {
let m = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( m . to_number ( context ) ? )
} else {
None
} ;
// 4. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
// 4. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
let sec = if let Some ( sec ) = args . get ( 2 ) {
let sec = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( sec . to_number ( context ) ? )
} else {
None
} ;
// 5. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
// 5. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
let milli = if let Some ( milli ) = args . get ( 3 ) {
let milli = args . get ( 3 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( milli . to_number ( context ) ? )
} else {
None
} ;
// 6. Let date be MakeDate(Day(t), MakeTime(h, m, s, milli)).
// 6. Let date be MakeDate(Day(t), MakeTime(h, m, s, milli)).
t . set_components ( false , None , None , None , Some ( h ) , m , sec , milli ) ;
t . set_components ( false , None , None , None , Some ( h ) , m , sec , milli ) ;
@ -1062,18 +1034,10 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 3. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
// 3. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
let s = if let Some ( s ) = args . get ( 1 ) {
let s = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( s . to_number ( context ) ? )
} else {
None
} ;
// 4. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
// 4. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
let milli = if let Some ( milli ) = args . get ( 2 ) {
let milli = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( milli . to_number ( context ) ? )
} else {
None
} ;
// 5. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli)).
// 5. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli)).
t . set_components ( false , None , None , None , None , Some ( m ) , s , milli ) ;
t . set_components ( false , None , None , None , None , Some ( m ) , s , milli ) ;
@ -1110,11 +1074,7 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 3. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
// 3. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
let dt = if let Some ( date ) = args . get ( 1 ) {
let dt = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( date . to_number ( context ) ? )
} else {
None
} ;
// 4. Let newDate be MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t)).
// 4. Let newDate be MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t)).
t . set_components ( false , None , Some ( m ) , dt , None , None , None , None ) ;
t . set_components ( false , None , Some ( m ) , dt , None , None , None , None ) ;
@ -1155,11 +1115,7 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 3. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
// 3. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
let milli = if let Some ( milli ) = args . get ( 1 ) {
let milli = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( milli . to_number ( context ) ? )
} else {
None
} ;
// 4. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli)).
// 4. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli)).
t . set_components ( false , None , None , None , None , None , Some ( s ) , milli ) ;
t . set_components ( false , None , None , None , None , None , Some ( s ) , milli ) ;
@ -1341,18 +1297,10 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 4. If month is not present, let m be MonthFromTime(t); otherwise, let m be ? ToNumber(month).
// 4. If month is not present, let m be MonthFromTime(t); otherwise, let m be ? ToNumber(month).
let m = if let Some ( m ) = args . get ( 1 ) {
let m = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( m . to_number ( context ) ? )
} else {
None
} ;
// 5. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
// 5. If date is not present, let dt be DateFromTime(t); otherwise, let dt be ? ToNumber(date).
let dt = if let Some ( dt ) = args . get ( 2 ) {
let dt = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( dt . to_number ( context ) ? )
} else {
None
} ;
// 6. Let newDate be MakeDate(MakeDay(y, m, dt), TimeWithinDay(t)).
// 6. Let newDate be MakeDate(MakeDay(y, m, dt), TimeWithinDay(t)).
t . set_components ( true , Some ( y ) , m , dt , None , None , None , None ) ;
t . set_components ( true , Some ( y ) , m , dt , None , None , None , None ) ;
@ -1395,25 +1343,13 @@ impl Date {
. to_number ( context ) ? ;
. to_number ( context ) ? ;
// 3. If min is not present, let m be MinFromTime(t); otherwise, let m be ? ToNumber(min).
// 3. If min is not present, let m be MinFromTime(t); otherwise, let m be ? ToNumber(min).
let m = if let Some ( m ) = args . get ( 1 ) {
let m = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( m . to_number ( context ) ? )
} else {
None
} ;
// 4. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
// 4. If sec is not present, let s be SecFromTime(t); otherwise, let s be ? ToNumber(sec).
let sec = if let Some ( s ) = args . get ( 2 ) {
let sec = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( s . to_number ( context ) ? )
} else {
None
} ;
// 5. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
// 5. If ms is not present, let milli be msFromTime(t); otherwise, let milli be ? ToNumber(ms).
let ms = if let Some ( ms ) = args . get ( 3 ) {
let ms = args . get ( 3 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
Some ( ms . to_number ( context ) ? )
} else {
None
} ;
// 6. Let newDate be MakeDate(Day(t), MakeTime(h, m, s, milli)).
// 6. Let newDate be MakeDate(Day(t), MakeTime(h, m, s, milli)).
t . set_components ( true , None , None , None , Some ( h ) , m , sec , ms ) ;
t . set_components ( true , None , None , None , Some ( h ) , m , sec , ms ) ;
@ -1493,21 +1429,13 @@ impl Date {
// 3. If sec is not present, let s be SecFromTime(t).
// 3. If sec is not present, let s be SecFromTime(t).
// 4. Else,
// 4. Else,
let s = if let Some ( s ) = args . get ( 1 ) {
// a. Let s be ? ToNumber(sec).
// a. Let s be ? ToNumber(sec).
Some ( s . to_number ( context ) ? )
let s = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
} else {
None
} ;
// 5. If ms is not present, let milli be msFromTime(t).
// 5. If ms is not present, let milli be msFromTime(t).
// 6. Else,
// 6. Else,
let milli = if let Some ( ms ) = args . get ( 2 ) {
// a. Let milli be ? ToNumber(ms).
// a. Let milli be ? ToNumber(ms).
Some ( ms . to_number ( context ) ? )
let milli = args . get ( 2 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
} else {
None
} ;
// 7. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli)).
// 7. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli)).
t . set_components ( true , None , None , None , None , Some ( m ) , s , milli ) ;
t . set_components ( true , None , None , None , None , Some ( m ) , s , milli ) ;
@ -1549,12 +1477,8 @@ impl Date {
// 3. If date is not present, let dt be DateFromTime(t).
// 3. If date is not present, let dt be DateFromTime(t).
// 4. Else,
// 4. Else,
let dt = if let Some ( dt ) = args . get ( 1 ) {
// a. Let dt be ? ToNumber(date).
// a. Let dt be ? ToNumber(date).
Some ( dt . to_number ( context ) ? )
let dt = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
} else {
None
} ;
// 5. Let newDate be MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t)).
// 5. Let newDate be MakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t)).
t . set_components ( true , None , Some ( m ) , dt , None , None , None , None ) ;
t . set_components ( true , None , Some ( m ) , dt , None , None , None , None ) ;
@ -1596,12 +1520,8 @@ impl Date {
// 3. If ms is not present, let milli be msFromTime(t).
// 3. If ms is not present, let milli be msFromTime(t).
// 4. Else,
// 4. Else,
let milli = if let Some ( milli ) = args . get ( 1 ) {
// a. Let milli be ? ToNumber(ms).
// a. Let milli be ? ToNumber(ms).
Some ( milli . to_number ( context ) ? )
let milli = args . get ( 1 ) . map ( | v | v . to_number ( context ) ) . transpose ( ) ? ;
} else {
None
} ;
// 5. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli)).
// 5. Let date be MakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli)).
t . set_components ( true , None , None , None , None , None , Some ( s ) , milli ) ;
t . set_components ( true , None , None , None , None , None , Some ( s ) , milli ) ;
@ -1841,9 +1761,7 @@ impl Date {
// This method is implementation-defined and discouraged, so we just require the same format as the string
// This method is implementation-defined and discouraged, so we just require the same format as the string
// constructor.
// constructor.
let date = if let Some ( arg ) = args . get ( 0 ) {
let Some ( date ) = args . get ( 0 ) else {
arg
} else {
return Ok ( JsValue ::nan ( ) ) ;
return Ok ( JsValue ::nan ( ) ) ;
} ;
} ;