1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
// Boa's implementation of the `Temporal.Duration` built-in object.
use super::{
DateTimeValues, get_relative_to_option,
options::{TemporalUnitGroup, get_digits_option, get_temporal_unit},
};
use crate::value::JsVariant;
use crate::{
Context, JsArgs, JsData, JsError, JsNativeError, JsObject, JsResult, JsString, JsSymbol,
JsValue,
builtins::{
BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
options::{get_option, get_options_object},
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
js_string,
object::internal_methods::get_prototype_from_constructor,
property::Attribute,
realm::Realm,
string::StaticJsStrings,
};
use boa_gc::{Finalize, Trace};
use temporal_rs::{
Duration as InnerDuration,
options::{RoundingIncrement, RoundingMode, RoundingOptions, ToStringRoundingOptions, Unit},
partial::PartialDuration,
};
#[cfg(test)]
mod tests;
/// The `Temporal.Duration` built-in implementation
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal-duration-objects
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html
#[derive(Debug, Clone, Trace, Finalize, JsData)]
#[boa_gc(unsafe_empty_trace)] // Safety: Does not contain any traceable fields.
pub struct Duration {
pub(crate) inner: Box<InnerDuration>,
}
impl Duration {
pub(crate) fn new(inner: InnerDuration) -> Self {
Self {
inner: Box::new(inner),
}
}
}
impl BuiltInObject for Duration {
const NAME: JsString = StaticJsStrings::DURATION_NAME;
}
impl IntrinsicObject for Duration {
fn init(realm: &Realm) {
let get_years = BuiltInBuilder::callable(realm, Self::get_years)
.name(js_string!("get Years"))
.build();
let get_months = BuiltInBuilder::callable(realm, Self::get_months)
.name(js_string!("get Months"))
.build();
let get_weeks = BuiltInBuilder::callable(realm, Self::get_weeks)
.name(js_string!("get Weeks"))
.build();
let get_days = BuiltInBuilder::callable(realm, Self::get_days)
.name(js_string!("get Days"))
.build();
let get_hours = BuiltInBuilder::callable(realm, Self::get_hours)
.name(js_string!("get Hours"))
.build();
let get_minutes = BuiltInBuilder::callable(realm, Self::get_minutes)
.name(js_string!("get Minutes"))
.build();
let get_seconds = BuiltInBuilder::callable(realm, Self::get_seconds)
.name(js_string!("get Seconds"))
.build();
let get_milliseconds = BuiltInBuilder::callable(realm, Self::get_milliseconds)
.name(js_string!("get Milliseconds"))
.build();
let get_microseconds = BuiltInBuilder::callable(realm, Self::get_microseconds)
.name(js_string!("get Microseconds"))
.build();
let get_nanoseconds = BuiltInBuilder::callable(realm, Self::get_nanoseconds)
.name(js_string!("get Nanoseconds"))
.build();
let get_sign = BuiltInBuilder::callable(realm, Self::get_sign)
.name(js_string!("get Sign"))
.build();
let is_blank = BuiltInBuilder::callable(realm, Self::get_blank)
.name(js_string!("get blank"))
.build();
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.property(
JsSymbol::to_string_tag(),
StaticJsStrings::DURATION_TAG,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.accessor(
js_string!("years"),
Some(get_years),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("months"),
Some(get_months),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("weeks"),
Some(get_weeks),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("days"),
Some(get_days),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("hours"),
Some(get_hours),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("minutes"),
Some(get_minutes),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("seconds"),
Some(get_seconds),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("milliseconds"),
Some(get_milliseconds),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("microseconds"),
Some(get_microseconds),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("nanoseconds"),
Some(get_nanoseconds),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("sign"),
Some(get_sign),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("blank"),
Some(is_blank),
None,
Attribute::CONFIGURABLE,
)
.static_method(Self::from, js_string!("from"), 1)
.static_method(Self::compare, js_string!("compare"), 2)
.method(Self::with, js_string!("with"), 1)
.method(Self::negated, js_string!("negated"), 0)
.method(Self::abs, js_string!("abs"), 0)
.method(Self::add, js_string!("add"), 1)
.method(Self::subtract, js_string!("subtract"), 1)
.method(Self::round, js_string!("round"), 1)
.method(Self::total, js_string!("total"), 1)
.method(Self::to_string, js_string!("toString"), 0)
.method(Self::to_locale_string, js_string!("toLocaleString"), 0)
.method(Self::to_json, js_string!("toJSON"), 0)
.method(Self::value_of, js_string!("valueOf"), 0)
.build();
}
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
}
impl BuiltInConstructor for Duration {
const CONSTRUCTOR_ARGUMENTS: usize = 0;
const PROTOTYPE_STORAGE_SLOTS: usize = 36;
const CONSTRUCTOR_STORAGE_SLOTS: usize = 2;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::duration;
fn constructor(
new_target: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. If NewTarget is undefined, then
if new_target.is_undefined() {
// a. Throw a TypeError exception.
return Err(JsNativeError::typ()
.with_message("NewTarget cannot be undefined for Temporal.Duration constructor.")
.into());
}
// 2. If years is undefined, let y be 0; else let y be ? ToIntegerIfIntegral(years).
let years = args.get_or_undefined(0).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 3. If months is undefined, let mo be 0; else let mo be ? ToIntegerIfIntegral(months).
let months = args.get_or_undefined(1).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 4. If weeks is undefined, let w be 0; else let w be ? ToIntegerIfIntegral(weeks).
let weeks = args.get_or_undefined(2).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 5. If days is undefined, let d be 0; else let d be ? ToIntegerIfIntegral(days).
let days = args.get_or_undefined(3).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 6. If hours is undefined, let h be 0; else let h be ? ToIntegerIfIntegral(hours).
let hours = args.get_or_undefined(4).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 7. If minutes is undefined, let m be 0; else let m be ? ToIntegerIfIntegral(minutes).
let minutes = args.get_or_undefined(5).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 8. If seconds is undefined, let s be 0; else let s be ? ToIntegerIfIntegral(seconds).
let seconds = args.get_or_undefined(6).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 9. If milliseconds is undefined, let ms be 0; else let ms be ? ToIntegerIfIntegral(milliseconds).
let milliseconds = args.get_or_undefined(7).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})?;
// 10. If microseconds is undefined, let mis be 0; else let mis be ? ToIntegerIfIntegral(microseconds).
let microseconds = args.get_or_undefined(8).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i128>()
.map_err(JsError::from)
})?;
// 11. If nanoseconds is undefined, let ns be 0; else let ns be ? ToIntegerIfIntegral(nanoseconds).
let nanoseconds = args.get_or_undefined(9).map_or(Ok(0), |v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i128>()
.map_err(JsError::from)
})?;
let record = InnerDuration::new(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
)?;
// 12. Return ? CreateTemporalDuration(y, mo, w, d, h, m, s, ms, mis, ns, NewTarget).
create_temporal_duration(record, Some(new_target), context).map(Into::into)
}
}
// ==== Duration accessor property implementations ====
impl Duration {
// Internal utility function for getting `Duration` field values.
fn get_internal_field(this: &JsValue, field: &DateTimeValues) -> JsResult<JsValue> {
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let inner = &duration.inner;
match field {
DateTimeValues::Year => Ok(JsValue::new(inner.years())),
DateTimeValues::Month => Ok(JsValue::new(inner.months())),
DateTimeValues::Week => Ok(JsValue::new(inner.weeks())),
DateTimeValues::Day => Ok(JsValue::new(inner.days())),
DateTimeValues::Hour => Ok(JsValue::new(inner.hours())),
DateTimeValues::Minute => Ok(JsValue::new(inner.minutes())),
DateTimeValues::Second => Ok(JsValue::new(inner.seconds())),
DateTimeValues::Millisecond => Ok(JsValue::new(inner.milliseconds())),
DateTimeValues::Microsecond => Ok(JsValue::new(inner.microseconds() as f64)),
DateTimeValues::Nanosecond => Ok(JsValue::new(inner.nanoseconds() as f64)),
DateTimeValues::MonthCode => unreachable!(
"Any other DateTimeValue fields on Duration would be an implementation error."
),
}
}
/// 7.3.3 get `Temporal.Duration.prototype.years`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.years
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/years
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.years
fn get_years(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Year)
}
// 7.3.4 get `Temporal.Duration.prototype.months`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.months
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/months
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.months
fn get_months(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Month)
}
/// 7.3.5 get `Temporal.Duration.prototype.weeks`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.weeks
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/weeks
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.weeks
fn get_weeks(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Week)
}
/// 7.3.6 get `Temporal.Duration.prototype.days`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.days
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/days
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.days
fn get_days(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Day)
}
/// 7.3.7 get `Temporal.Duration.prototype.hours`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.hours
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/hours
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.hours
fn get_hours(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Hour)
}
/// 7.3.8 get `Temporal.Duration.prototype.minutes`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.minutes
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/minutes
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.minutes
fn get_minutes(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Minute)
}
/// 7.3.9 get `Temporal.Duration.prototype.seconds`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.seconds
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/seconds
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.seconds
fn get_seconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Second)
}
/// 7.3.10 get `Temporal.Duration.prototype.milliseconds`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.milliseconds
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/milliseconds
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.milliseconds
fn get_milliseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Millisecond)
}
/// 7.3.11 get `Temporal.Duration.prototype.microseconds`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.microseconds
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/microseconds
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.microseconds
fn get_microseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Microsecond)
}
/// 7.3.12 get `Temporal.Duration.prototype.nanoseconds`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.nanoseconds
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/nanoseconds
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.nanoseconds
fn get_nanoseconds(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
Self::get_internal_field(this, &DateTimeValues::Nanosecond)
}
/// 7.3.13 get `Temporal.Duration.prototype.sign`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.sign
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/sign
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.sign
fn get_sign(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
// 3. Return 𝔽(! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]],
// duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]],
// duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]])).
Ok((duration.inner.sign() as i8).into())
}
/// 7.3.14 get `Temporal.Duration.prototype.blank`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-get-temporal.duration.prototype.blank
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/blank
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.blank
fn get_blank(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
// 3. Let sign be ! DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]],
// duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]],
// duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]).
// 4. If sign = 0, return true.
// 5. Return false.
Ok(duration.inner.is_zero().into())
}
}
// ==== Duration static methods implementation ====
impl Duration {
/// 7.2.2 `Temporal.Duration.from ( item )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.from
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/from
fn from(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let item = args.get_or_undefined(0);
// 1. If item is an Object and item has an [[InitializedTemporalDuration]] internal slot, then
let object = item.as_object();
if let Some(duration) = object.as_ref().and_then(JsObject::downcast_ref::<Self>) {
// a. Return ! CreateTemporalDuration(item.[[Years]], item.[[Months]], item.[[Weeks]],
// item.[[Days]], item.[[Hours]], item.[[Minutes]], item.[[Seconds]], item.[[Milliseconds]],
// item.[[Microseconds]], item.[[Nanoseconds]]).
return create_temporal_duration(*duration.inner, None, context).map(Into::into);
}
// 2. Return ? ToTemporalDuration(item).
create_temporal_duration(to_temporal_duration_record(item, context)?, None, context)
.map(Into::into)
}
/// 7.2.3 `Temporal.Duration.compare ( one, two [ , options ] )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.compare
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/compare
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.compare
fn compare(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Set one to ? ToTemporalDuration(one).
let one = to_temporal_duration(args.get_or_undefined(0), context)?;
// 2. Set two to ? ToTemporalDuration(two).
let two = to_temporal_duration(args.get_or_undefined(1), context)?;
// 3. Let resolvedOptions be ? GetOptionsObject(options).
let options = get_options_object(args.get_or_undefined(2))?;
// 4. Let relativeToRecord be ? GetTemporalRelativeToOption(resolvedOptions).
let relative_to = get_relative_to_option(&options, context)?;
Ok((one.compare_with_provider(&two, relative_to, context.tz_provider())? as i8).into())
}
}
// ==== Duration methods implementation ====
impl Duration {
/// 7.3.15 `Temporal.Duration.prototype.with ( temporalDurationLike )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.with
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/with
pub(crate) fn with(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
// 3. Let temporalDurationLike be ? ToTemporalPartialDurationRecord(temporalDurationLike).
let temporal_duration_like =
to_temporal_partial_duration(args.get_or_undefined(0), context)?;
// 4. If temporalDurationLike.[[Years]] is not undefined, then
// a. Let years be temporalDurationLike.[[Years]].
// 5. Else,
// a. Let years be duration.[[Years]].
let years = temporal_duration_like
.years
.unwrap_or(duration.inner.years());
// 6. If temporalDurationLike.[[Months]] is not undefined, then
// a. Let months be temporalDurationLike.[[Months]].
// 7. Else,
// a. Let months be duration.[[Months]].
let months = temporal_duration_like
.months
.unwrap_or(duration.inner.months());
// 8. If temporalDurationLike.[[Weeks]] is not undefined, then
// a. Let weeks be temporalDurationLike.[[Weeks]].
// 9. Else,
// a. Let weeks be duration.[[Weeks]].
let weeks = temporal_duration_like
.weeks
.unwrap_or(duration.inner.weeks());
// 10. If temporalDurationLike.[[Days]] is not undefined, then
// a. Let days be temporalDurationLike.[[Days]].
// 11. Else,
// a. Let days be duration.[[Days]].
let days = temporal_duration_like.days.unwrap_or(duration.inner.days());
// 12. If temporalDurationLike.[[Hours]] is not undefined, then
// a. Let hours be temporalDurationLike.[[Hours]].
// 13. Else,
// a. Let hours be duration.[[Hours]].
let hours = temporal_duration_like
.hours
.unwrap_or(duration.inner.hours());
// 14. If temporalDurationLike.[[Minutes]] is not undefined, then
// a. Let minutes be temporalDurationLike.[[Minutes]].
// 15. Else,
// a. Let minutes be duration.[[Minutes]].
let minutes = temporal_duration_like
.minutes
.unwrap_or(duration.inner.minutes());
// 16. If temporalDurationLike.[[Seconds]] is not undefined, then
// a. Let seconds be temporalDurationLike.[[Seconds]].
// 17. Else,
// a. Let seconds be duration.[[Seconds]].
let seconds = temporal_duration_like
.seconds
.unwrap_or(duration.inner.seconds());
// 18. If temporalDurationLike.[[Milliseconds]] is not undefined, then
// a. Let milliseconds be temporalDurationLike.[[Milliseconds]].
// 19. Else,
// a. Let milliseconds be duration.[[Milliseconds]].
let milliseconds = temporal_duration_like
.milliseconds
.unwrap_or(duration.inner.milliseconds());
// 20. If temporalDurationLike.[[Microseconds]] is not undefined, then
// a. Let microseconds be temporalDurationLike.[[Microseconds]].
// 21. Else,
// a. Let microseconds be duration.[[Microseconds]].
let microseconds = temporal_duration_like
.microseconds
.unwrap_or(duration.inner.microseconds());
// 22. If temporalDurationLike.[[Nanoseconds]] is not undefined, then
// a. Let nanoseconds be temporalDurationLike.[[Nanoseconds]].
// 23. Else,
// a. Let nanoseconds be duration.[[Nanoseconds]].
let nanoseconds = temporal_duration_like
.nanoseconds
.unwrap_or(duration.inner.nanoseconds());
// 24. Return ? CreateTemporalDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
let new_duration = InnerDuration::new(
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
)?;
create_temporal_duration(new_duration, None, context).map(Into::into)
}
/// 7.3.16 `Temporal.Duration.prototype.negated ( )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.negated
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/negated
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.negated
pub(crate) fn negated(
this: &JsValue,
_: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
// 3. Return ! CreateNegatedTemporalDuration(duration).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
create_temporal_duration(duration.inner.negated(), None, context).map(Into::into)
}
/// 7.3.17 `Temporal.Duration.prototype.abs ( )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.abs
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/abs
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.abs
pub(crate) fn abs(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
// 3. Return ! CreateTemporalDuration(abs(duration.[[Years]]), abs(duration.[[Months]]),
// abs(duration.[[Weeks]]), abs(duration.[[Days]]), abs(duration.[[Hours]]), abs(duration.[[Minutes]]),
// abs(duration.[[Seconds]]), abs(duration.[[Milliseconds]]), abs(duration.[[Microseconds]]), abs(duration.[[Nanoseconds]])).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
create_temporal_duration(duration.inner.abs(), None, context).map(Into::into)
}
/// 7.3.18 `Temporal.Duration.prototype.add ( other [ , options ] )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.add
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/add
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.add
pub(crate) fn add(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1.Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
// 3. Return ? AddDurations(add, duration, other).
let other = to_temporal_duration_record(args.get_or_undefined(0), context)?;
create_temporal_duration(duration.inner.add(&other)?, None, context).map(Into::into)
}
/// 7.3.19 `Temporal.Duration.prototype.subtract ( other [ , options ] )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.subtract
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/subtract
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.subtract
pub(crate) fn subtract(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1.Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let other = to_temporal_duration_record(args.get_or_undefined(0), context)?;
// 3. Return ? AddDurations(add, duration, other).
create_temporal_duration(duration.inner.subtract(&other)?, None, context).map(Into::into)
}
/// 7.3.20 `Temporal.Duration.prototype.round ( roundTo )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.round
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/round
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.round
pub(crate) fn round(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let round_to = match args.first().map(JsValue::variant) {
// 3. If roundTo is undefined, then
None | Some(JsVariant::Undefined) => {
return Err(JsNativeError::typ()
.with_message("roundTo cannot be undefined.")
.into());
}
// 4. If Type(roundTo) is String, then
Some(JsVariant::String(rt)) => {
// a. Let paramString be roundTo.
let param_string = rt.clone();
// b. Set roundTo to OrdinaryObjectCreate(null).
let new_round_to = JsObject::with_null_proto();
// c. Perform ! CreateDataPropertyOrThrow(roundTo, "smallestUnit", paramString).
new_round_to.create_data_property_or_throw(
js_string!("smallestUnit"),
param_string,
context,
)?;
new_round_to
}
// 5. Else,
Some(round_to) => {
// TODO: remove this clone.
// a. Set roundTo to ? GetOptionsObject(roundTo).
get_options_object(&JsValue::from(round_to))?
}
};
// NOTE: 6 & 7 unused in favor of `is_none()`.
// 6. Let smallestUnitPresent be true.
// 7. Let largestUnitPresent be true.
let mut options = RoundingOptions::default();
// 8. NOTE: The following steps read options and perform independent validation in alphabetical order (ToRelativeTemporalObject reads "relativeTo", ToTemporalRoundingIncrement reads "roundingIncrement" and ToTemporalRoundingMode reads "roundingMode").
// 9. Let largestUnit be ? GetTemporalUnit(roundTo, "largestUnit", datetime, undefined, « "auto" »).
options.largest_unit = get_temporal_unit(
&round_to,
js_string!("largestUnit"),
TemporalUnitGroup::DateTime,
Some([Unit::Auto].into()),
context,
)?;
// 10. Let relativeToRecord be ? ToRelativeTemporalObject(roundTo).
// 11. Let zonedRelativeTo be relativeToRecord.[[ZonedRelativeTo]].
// 12. Let plainRelativeTo be relativeToRecord.[[PlainRelativeTo]].
let relative_to = get_relative_to_option(&round_to, context)?;
// 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo).
options.increment =
get_option::<RoundingIncrement>(&round_to, js_string!("roundingIncrement"), context)?;
// 14. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand").
options.rounding_mode =
get_option::<RoundingMode>(&round_to, js_string!("roundingMode"), context)?;
// 15. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", datetime, undefined).
options.smallest_unit = get_temporal_unit(
&round_to,
js_string!("smallestUnit"),
TemporalUnitGroup::DateTime,
None,
context,
)?;
// NOTE: execute step 21 earlier before initial values are shadowed.
// 21. If smallestUnitPresent is false and largestUnitPresent is false, then
let rounded_duration =
duration
.inner
.round_with_provider(options, relative_to, context.tz_provider())?;
create_temporal_duration(rounded_duration, None, context).map(Into::into)
}
/// 7.3.21 `Temporal.Duration.prototype.total ( totalOf )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.total
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/total
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.total
pub(crate) fn total(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let total_of = args.get_or_undefined(0);
let total_of = match total_of.variant() {
// 3. If totalOf is undefined, throw a TypeError exception.
JsVariant::Undefined => {
return Err(JsNativeError::typ()
.with_message("totalOf cannot be undefined.")
.into());
}
// 4. If Type(totalOf) is String, then
JsVariant::String(param_string) => {
// a. Let paramString be totalOf.
// b. Set totalOf to OrdinaryObjectCreate(null).
let total_of = JsObject::with_null_proto();
// c. Perform ! CreateDataPropertyOrThrow(totalOf, "unit", paramString).
total_of.create_data_property_or_throw(
js_string!("unit"),
param_string.clone(),
context,
)?;
total_of
}
// 5. Else,
_ => {
// a. Set totalOf to ? GetOptionsObject(totalOf).
get_options_object(total_of)?
}
};
// 6. NOTE: The following steps read options and perform independent validation in alphabetical order (ToRelativeTemporalObject reads "relativeTo").
// 7. Let relativeToRecord be ? ToRelativeTemporalObject(totalOf).
// 8. Let zonedRelativeTo be relativeToRecord.[[ZonedRelativeTo]].
// 9. Let plainRelativeTo be relativeToRecord.[[PlainRelativeTo]].
let relative_to = get_relative_to_option(&total_of, context)?;
// 10. Let unit be ? GetTemporalUnit(totalOf, "unit", datetime, required).
let unit = get_temporal_unit(
&total_of,
js_string!("unit"),
TemporalUnitGroup::DateTime,
None,
context,
)?
.ok_or_else(|| JsNativeError::range().with_message("unit cannot be undefined."))?;
Ok(duration
.inner
.total_with_provider(unit, relative_to, context.tz_provider())?
.as_inner()
.into())
}
/// 7.3.22 `Temporal.Duration.prototype.toString ( [ options ] )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
/// - [`temporal_rs` documentation][temporal_rs-docs]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tostring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/toString
/// [temporal_rs-docs]: https://docs.rs/temporal_rs/latest/temporal_rs/struct.Duration.html#method.as_temporal_string
pub(crate) fn to_string(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let options = get_options_object(args.get_or_undefined(0))?;
let precision = get_digits_option(&options, context)?;
let rounding_mode =
get_option::<RoundingMode>(&options, js_string!("roundingMode"), context)?;
let smallest_unit = get_option::<Unit>(&options, js_string!("smallestUnit"), context)?;
let result = duration.inner.as_temporal_string(ToStringRoundingOptions {
precision,
smallest_unit,
rounding_mode,
})?;
Ok(JsString::from(result).into())
}
/// 7.3.23 `Temporal.Duration.prototype.toJSON ( )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tojson
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/toJSON
pub(crate) fn to_json(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let result = duration
.inner
.as_temporal_string(ToStringRoundingOptions::default())?;
Ok(JsString::from(result).into())
}
/// 7.3.24 `Temporal.Duration.prototype.toLocaleString ( )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.tolocalestring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/toLocaleString
pub(crate) fn to_locale_string(
this: &JsValue,
_: &[JsValue],
_: &mut Context,
) -> JsResult<JsValue> {
// TODO: Update for ECMA-402 compliance
let object = this.as_object();
let duration = object
.as_ref()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("this value must be a Duration object.")
})?;
let result = duration
.inner
.as_temporal_string(ToStringRoundingOptions::default())?;
Ok(JsString::from(result).into())
}
/// 7.3.25 `Temporal.Duration.prototype.valueOf ( )`
///
/// More information:
///
/// - [ECMAScript Temporal proposal][spec]
/// - [MDN reference][mdn]
///
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal.duration.prototype.valueof
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Duration/valueOf
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 --
/// 7.5.12 `ToTemporalDuration ( item )`
pub(crate) fn to_temporal_duration(
item: &JsValue,
context: &mut Context,
) -> JsResult<InnerDuration> {
// 1a. If Type(item) is Object
// 1b. and item has an [[InitializedTemporalDuration]] internal slot, then
let object = item.as_object();
if let Some(duration) = object.as_ref().and_then(JsObject::downcast_ref::<Duration>) {
return Ok(*duration.inner);
}
// 2. Let result be ? ToTemporalDurationRecord(item).
let result = to_temporal_duration_record(item, context)?;
// 3. Return ! CreateTemporalDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
Ok(result)
}
/// 7.5.13 `ToTemporalDurationRecord ( temporalDurationLike )`
pub(crate) fn to_temporal_duration_record(
temporal_duration_like: &JsValue,
context: &mut Context,
) -> JsResult<InnerDuration> {
// 1. If Type(temporalDurationLike) is not Object, then
let Some(duration_obj) = temporal_duration_like.as_object() else {
// a. If temporalDurationLike is not a String, throw a TypeError exception.
let Some(duration_string) = temporal_duration_like.as_string() else {
return Err(JsNativeError::typ()
.with_message("Invalid TemporalDurationLike value.")
.into());
};
// b. Return ? ParseTemporalDurationString(temporalDurationLike).
return duration_string
.to_std_string_escaped()
.parse::<InnerDuration>()
.map_err(Into::into);
};
// 2. If temporalDurationLike has an [[InitializedTemporalDuration]] internal slot, then
if let Some(duration) = duration_obj.downcast_ref::<Duration>() {
// a. Return ! CreateDurationRecord(temporalDurationLike.[[Years]], temporalDurationLike.[[Months]], temporalDurationLike.[[Weeks]], temporalDurationLike.[[Days]], temporalDurationLike.[[Hours]], temporalDurationLike.[[Minutes]], temporalDurationLike.[[Seconds]], temporalDurationLike.[[Milliseconds]], temporalDurationLike.[[Microseconds]], temporalDurationLike.[[Nanoseconds]]).
return Ok(*duration.inner);
}
// 3. Let result be a new Duration Record with each field set to 0.
// 4. Let partial be ? ToTemporalPartialDurationRecord(temporalDurationLike).
let partial = to_temporal_partial_duration(temporal_duration_like, context)?;
// 5. If partial.[[Years]] is not undefined, set result.[[Years]] to partial.[[Years]].
// 6. If partial.[[Months]] is not undefined, set result.[[Months]] to partial.[[Months]].
// 7. If partial.[[Weeks]] is not undefined, set result.[[Weeks]] to partial.[[Weeks]].
// 8. If partial.[[Days]] is not undefined, set result.[[Days]] to partial.[[Days]].
// 9. If partial.[[Hours]] is not undefined, set result.[[Hours]] to partial.[[Hours]].
// 10. If partial.[[Minutes]] is not undefined, set result.[[Minutes]] to partial.[[Minutes]].
// 11. If partial.[[Seconds]] is not undefined, set result.[[Seconds]] to partial.[[Seconds]].
// 12. If partial.[[Milliseconds]] is not undefined, set result.[[Milliseconds]] to partial.[[Milliseconds]].
// 13. If partial.[[Microseconds]] is not undefined, set result.[[Microseconds]] to partial.[[Microseconds]].
// 14. If partial.[[Nanoseconds]] is not undefined, set result.[[Nanoseconds]] to partial.[[Nanoseconds]].
// 15. If ! IsValidDuration(result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
// a. Throw a RangeError exception.
// 16. Return result.
InnerDuration::from_partial_duration(partial).map_err(Into::into)
}
/// 7.5.14 `CreateTemporalDuration ( years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds [ , newTarget ] )`
pub(crate) fn create_temporal_duration(
inner: InnerDuration,
new_target: Option<&JsValue>,
context: &mut Context,
) -> JsResult<JsObject> {
// 1. If ! IsValidDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds) is false, throw a RangeError exception.
// 2. If newTarget is not present, set newTarget to %Temporal.Duration%.
let new_target = if let Some(target) = new_target {
target.clone()
} else {
context
.realm()
.intrinsics()
.constructors()
.duration()
.constructor()
.into()
};
// 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.Duration.prototype%", « [[InitializedTemporalDuration]], [[Years]], [[Months]], [[Weeks]], [[Days]], [[Hours]], [[Minutes]], [[Seconds]], [[Milliseconds]], [[Microseconds]], [[Nanoseconds]] »).
let prototype =
get_prototype_from_constructor(&new_target, StandardConstructors::duration, context)?;
// 4. Set object.[[Years]] to ℝ(𝔽(years)).
// 5. Set object.[[Months]] to ℝ(𝔽(months)).
// 6. Set object.[[Weeks]] to ℝ(𝔽(weeks)).
// 7. Set object.[[Days]] to ℝ(𝔽(days)).
// 8. Set object.[[Hours]] to ℝ(𝔽(hours)).
// 9. Set object.[[Minutes]] to ℝ(𝔽(minutes)).
// 10. Set object.[[Seconds]] to ℝ(𝔽(seconds)).
// 11. Set object.[[Milliseconds]] to ℝ(𝔽(milliseconds)).
// 12. Set object.[[Microseconds]] to ℝ(𝔽(microseconds)).
// 13. Set object.[[Nanoseconds]] to ℝ(𝔽(nanoseconds)).
let obj = JsObject::from_proto_and_data(prototype, Duration::new(inner));
// 14. Return object.
Ok(obj)
}
/// Equivalent to 7.5.13 `ToTemporalPartialDurationRecord ( temporalDurationLike )`
pub(crate) fn to_temporal_partial_duration(
duration_like: &JsValue,
context: &mut Context,
) -> JsResult<PartialDuration> {
// 1. If Type(temporalDurationLike) is not Object, then
let Some(unknown_object) = duration_like.as_object() else {
// a. Throw a TypeError exception.
return Err(JsNativeError::typ()
.with_message("temporalDurationLike must be an object.")
.into());
};
// 2. Let result be a new partial Duration Record with each field set to undefined.
// 3. NOTE: The following steps read properties and perform independent validation in alphabetical order.
// 4. Let days be ? Get(temporalDurationLike, "days").
// 5. If days is not undefined, set result.[[Days]] to ? ToIntegerIfIntegral(days).
let days = unknown_object
.get(js_string!("days"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 6. Let hours be ? Get(temporalDurationLike, "hours").
// 7. If hours is not undefined, set result.[[Hours]] to ? ToIntegerIfIntegral(hours).
let hours = unknown_object
.get(js_string!("hours"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 8. Let microseconds be ? Get(temporalDurationLike, "microseconds").
// 9. If microseconds is not undefined, set result.[[Microseconds]] to ? ToIntegerIfIntegral(microseconds).
let microseconds = unknown_object
.get(js_string!("microseconds"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i128>()
.map_err(JsError::from)
})
.transpose()?;
// 10. Let milliseconds be ? Get(temporalDurationLike, "milliseconds").
// 11. If milliseconds is not undefined, set result.[[Milliseconds]] to ? ToIntegerIfIntegral(milliseconds).
let milliseconds = unknown_object
.get(js_string!("milliseconds"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 12. Let minutes be ? Get(temporalDurationLike, "minutes").
// 13. If minutes is not undefined, set result.[[Minutes]] to ? ToIntegerIfIntegral(minutes).
let minutes = unknown_object
.get(js_string!("minutes"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 14. Let months be ? Get(temporalDurationLike, "months").
// 15. If months is not undefined, set result.[[Months]] to ? ToIntegerIfIntegral(months).
let months = unknown_object
.get(js_string!("months"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 16. Let nanoseconds be ? Get(temporalDurationLike, "nanoseconds").
// 17. If nanoseconds is not undefined, set result.[[Nanoseconds]] to ? ToIntegerIfIntegral(nanoseconds).
let nanoseconds = unknown_object
.get(js_string!("nanoseconds"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i128>()
.map_err(JsError::from)
})
.transpose()?;
// 18. Let seconds be ? Get(temporalDurationLike, "seconds").
// 19. If seconds is not undefined, set result.[[Seconds]] to ? ToIntegerIfIntegral(seconds).
let seconds = unknown_object
.get(js_string!("seconds"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 20. Let weeks be ? Get(temporalDurationLike, "weeks").
// 21. If weeks is not undefined, set result.[[Weeks]] to ? ToIntegerIfIntegral(weeks).
let weeks = unknown_object
.get(js_string!("weeks"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
// 22. Let years be ? Get(temporalDurationLike, "years").
// 23. If years is not undefined, set result.[[Years]] to ? ToIntegerIfIntegral(years).
let years = unknown_object
.get(js_string!("years"), context)?
.map(|v| {
let finite = v.to_finitef64(context)?;
finite
.as_integer_if_integral::<i64>()
.map_err(JsError::from)
})
.transpose()?;
let partial = PartialDuration {
years,
months,
weeks,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
};
// 24. If years is undefined, and months is undefined, and weeks is undefined, and days
// is undefined, and hours is undefined, and minutes is undefined, and seconds is
// undefined, and milliseconds is undefined, and microseconds is undefined, and
// nanoseconds is undefined, throw a TypeError exception.
if partial.is_empty() {
return Err(JsNativeError::typ()
.with_message("PartialDurationRecord must have a defined field.")
.into());
}
// 25. Return result.
Ok(partial)
}