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
//! Boa's implementation of the ECMAScript `Temporal.PlainTime` builtin object.
use crate::{
builtins::{
options::{get_option, get_options_object},
BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject,
},
context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
js_string,
object::internal_methods::get_prototype_from_constructor,
property::Attribute,
realm::Realm,
string::StaticJsStrings,
Context, JsArgs, JsData, JsNativeError, JsObject, JsResult, JsString, JsSymbol, JsValue,
};
use boa_gc::{Finalize, Trace};
use boa_macros::js_str;
use boa_profiler::Profiler;
use temporal_rs::{
components::Time,
options::{ArithmeticOverflow, TemporalRoundingMode},
};
use super::{
options::{get_temporal_unit, TemporalUnitGroup},
to_integer_with_truncation, to_temporal_duration_record, PlainDateTime, ZonedDateTime,
};
/// The `Temporal.PlainTime` object.
#[derive(Debug, Clone, Copy, Trace, Finalize, JsData)]
// Safety: Time does not contain any traceable types.
#[boa_gc(unsafe_empty_trace)]
pub struct PlainTime {
inner: Time,
}
impl BuiltInObject for PlainTime {
const NAME: JsString = StaticJsStrings::PLAIN_TIME;
}
impl IntrinsicObject for PlainTime {
fn init(realm: &Realm) {
let _timer = Profiler::global().start_event(std::any::type_name::<Self>(), "init");
let get_hour = BuiltInBuilder::callable(realm, Self::get_hour)
.name(js_string!("get hour"))
.build();
let get_minute = BuiltInBuilder::callable(realm, Self::get_minute)
.name(js_string!("get minute"))
.build();
let get_second = BuiltInBuilder::callable(realm, Self::get_second)
.name(js_string!("get second"))
.build();
let get_millisecond = BuiltInBuilder::callable(realm, Self::get_millisecond)
.name(js_string!("get millisecond"))
.build();
let get_microsecond = BuiltInBuilder::callable(realm, Self::get_microsecond)
.name(js_string!("get microsecond"))
.build();
let get_nanosecond = BuiltInBuilder::callable(realm, Self::get_nanosecond)
.name(js_string!("get nanosecond"))
.build();
BuiltInBuilder::from_standard_constructor::<Self>(realm)
.property(
JsSymbol::to_string_tag(),
Self::NAME,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("hour"),
Some(get_hour),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("minute"),
Some(get_minute),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("second"),
Some(get_second),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("millisecond"),
Some(get_millisecond),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("microsecond"),
Some(get_microsecond),
None,
Attribute::CONFIGURABLE,
)
.accessor(
js_string!("nanosecond"),
Some(get_nanosecond),
None,
Attribute::CONFIGURABLE,
)
.static_method(Self::from, js_string!("from"), 2)
.method(Self::add, js_string!("add"), 1)
.method(Self::subtract, js_string!("subtract"), 1)
.method(Self::round, js_string!("round"), 1)
.method(Self::equals, js_string!("equals"), 1)
.method(Self::get_iso_fields, js_string!("getISOFields"), 0)
.method(Self::value_of, js_string!("valueOf"), 0)
.build();
}
fn get(intrinsics: &Intrinsics) -> JsObject {
Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
}
}
impl BuiltInConstructor for PlainTime {
const LENGTH: usize = 0;
const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
StandardConstructors::plain_time;
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.")
.into());
}
// 2. If hour is undefined, set hour to 0; else set hour to ? ToIntegerWithTruncation(hour).
let hour = args
.first()
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
// 3. If minute is undefined, set minute to 0; else set minute to ? ToIntegerWithTruncation(minute).
let minute = args
.get(1)
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
// 4. If second is undefined, set second to 0; else set second to ? ToIntegerWithTruncation(second).
let second = args
.get(2)
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
// 5. If millisecond is undefined, set millisecond to 0; else set millisecond to ? ToIntegerWithTruncation(millisecond).
let millisecond = args
.get(3)
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
// 6. If microsecond is undefined, set microsecond to 0; else set microsecond to ? ToIntegerWithTruncation(microsecond).
let microsecond = args
.get(4)
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
// 7. If nanosecond is undefined, set nanosecond to 0; else set nanosecond to ? ToIntegerWithTruncation(nanosecond).
let nanosecond = args
.get(5)
.map(|v| to_integer_with_truncation(v, context))
.transpose()?
.unwrap_or(0);
let inner = Time::new(
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
ArithmeticOverflow::Reject,
)?;
// 8. Return ? CreateTemporalTime(hour, minute, second, millisecond, microsecond, nanosecond, NewTarget).
create_temporal_time(inner, Some(new_target), context).map(Into::into)
}
}
// ==== PlainTime Accessor methods ====
impl PlainTime {
/// 4.3.3 get `Temporal.PlainTime.prototype.hour`
fn get_hour(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISOHour]]).
Ok(time.inner.hour().into())
}
/// 4.3.4 get `Temporal.PlainTime.prototype.minute`
fn get_minute(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISOMinute]]).
Ok(time.inner.minute().into())
}
/// 4.3.5 get `Temporal.PlainTime.prototype.second`
fn get_second(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISOSecond]]).
Ok(time.inner.second().into())
}
/// 4.3.6 get `Temporal.PlainTime.prototype.millisecond`
fn get_millisecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISOMillisecond]]).
Ok(time.inner.millisecond().into())
}
/// 4.3.7 get `Temporal.PlainTime.prototype.microsecond`
fn get_microsecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISOMicrosecond]]).
Ok(time.inner.microsecond().into())
}
/// 4.3.8 get `Temporal.PlainTime.prototype.nanosecond`
fn get_nanosecond(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Return 𝔽(temporalTime.[[ISONanosecond]]).
Ok(time.inner.nanosecond().into())
}
}
// ==== PlainTime method implementations ====
impl PlainTime {
fn from(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
let item = args.get_or_undefined(0);
// 1. Set options to ? GetOptionsObject(options).
// 2. Let overflow be ? GetTemporalOverflowOption(options).
let overflow = get_option::<ArithmeticOverflow>(
&get_options_object(args.get_or_undefined(1))?,
js_str!("overflow"),
context,
)?;
// 3. If item is an Object and item has an [[InitializedTemporalTime]] internal slot, then
let time = if let Some(time) = item
.as_object()
.and_then(JsObject::downcast_ref::<PlainTime>)
{
// a. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]],
// item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]],
// item.[[ISONanosecond]]).
time.inner
} else {
to_temporal_time(item, overflow, context)?
};
// 4. Return ? ToTemporalTime(item, overflow).
create_temporal_time(time, None, context).map(Into::into)
}
}
// ==== PlainTime.prototype method implementations ====
impl PlainTime {
/// 4.3.9 Temporal.PlainTime.prototype.add ( temporalDurationLike )
fn add(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<PlainTime>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
let temporal_duration_like = args.get_or_undefined(0);
let duration = to_temporal_duration_record(temporal_duration_like, context)?;
// 3. Return ? AddDurationToOrSubtractDurationFromPlainTime(add, temporalTime, temporalDurationLike).
create_temporal_time(time.inner.add(&duration)?, None, context).map(Into::into)
}
/// 4.3.10 Temporal.PlainTime.prototype.subtract ( temporalDurationLike )
fn subtract(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<PlainTime>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
let temporal_duration_like = args.get_or_undefined(0);
let duration = to_temporal_duration_record(temporal_duration_like, context)?;
// 3. Return ? AddDurationToOrSubtractDurationFromPlainTime(subtract, temporalTime, temporalDurationLike).
create_temporal_time(time.inner.subtract(&duration)?, None, context).map(Into::into)
}
/// 4.3.14 Temporal.PlainTime.prototype.round ( roundTo )
fn round(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<PlainTime>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
let round_to = match args.first() {
// 3. If roundTo is undefined, then
None | Some(JsValue::Undefined) => {
return Err(JsNativeError::typ()
.with_message("roundTo cannot be undefined.")
.into())
}
// 4. If Type(roundTo) is String, then
Some(JsValue::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_str!("smallestUnit"),
param_string,
context,
)?;
new_round_to
}
// 5. Else,
Some(round_to) => {
// a. Set roundTo to ? GetOptionsObject(roundTo).
get_options_object(round_to)?
}
};
// 6. NOTE: The following steps read options and perform independent validation in alphabetical order (ToTemporalRoundingIncrement reads "roundingIncrement" and ToTemporalRoundingMode reads "roundingMode").
// 7. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo).
let rounding_increment =
get_option::<f64>(&round_to, js_str!("roundingIncrement"), context)?;
// 8. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand").
let rounding_mode =
get_option::<TemporalRoundingMode>(&round_to, js_str!("roundingMode"), context)?;
// 9. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", time, required).
let smallest_unit = get_temporal_unit(
&round_to,
js_str!("smallestUnit"),
TemporalUnitGroup::Time,
None,
context,
)?
.ok_or_else(|| JsNativeError::range().with_message("smallestUnit cannot be undefined."))?;
// 10. Let maximum be MaximumTemporalDurationRoundingIncrement(smallestUnit).
// 11. Assert: maximum is not undefined.
// 12. Perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false).
// 13. Let result be RoundTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], roundingIncrement, smallestUnit, roundingMode).
let result = time
.inner
.round(smallest_unit, rounding_increment, rounding_mode)?;
// 14. Return ! CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
create_temporal_time(result, None, context).map(Into::into)
}
/// 4.3.15 Temporal.PlainTime.prototype.equals ( other )
fn equals(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<Self>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Set other to ? ToTemporalTime(other).
let other = to_temporal_time(args.get_or_undefined(0), None, context)?;
// 4. If temporalTime.[[ISOHour]] ≠ other.[[ISOHour]], return false.
// 5. If temporalTime.[[ISOMinute]] ≠ other.[[ISOMinute]], return false.
// 6. If temporalTime.[[ISOSecond]] ≠ other.[[ISOSecond]], return false.
// 7. If temporalTime.[[ISOMillisecond]] ≠ other.[[ISOMillisecond]], return false.
// 8. If temporalTime.[[ISOMicrosecond]] ≠ other.[[ISOMicrosecond]], return false.
// 9. If temporalTime.[[ISONanosecond]] ≠ other.[[ISONanosecond]], return false.
// 10. Return true.
Ok((time.inner == other).into())
}
/// 4.3.18 Temporal.PlainTime.prototype.getISOFields ( )
fn get_iso_fields(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let temporalTime be the this value.
// 2. Perform ? RequireInternalSlot(temporalTime, [[InitializedTemporalTime]]).
let time = this
.as_object()
.and_then(JsObject::downcast_ref::<PlainTime>)
.ok_or_else(|| {
JsNativeError::typ().with_message("the this object must be a PlainTime object.")
})?;
// 3. Let fields be OrdinaryObjectCreate(%Object.prototype%).
let fields = JsObject::with_object_proto(context.intrinsics());
// 4. Perform ! CreateDataPropertyOrThrow(fields, "isoHour", 𝔽(temporalTime.[[ISOHour]])).
fields.create_data_property_or_throw(js_str!("isoHour"), time.inner.hour(), context)?;
// 5. Perform ! CreateDataPropertyOrThrow(fields, "isoMicrosecond", 𝔽(temporalTime.[[ISOMicrosecond]])).
fields.create_data_property_or_throw(
js_str!("isoMicrosecond"),
time.inner.microsecond(),
context,
)?;
// 6. Perform ! CreateDataPropertyOrThrow(fields, "isoMillisecond", 𝔽(temporalTime.[[ISOMillisecond]])).
fields.create_data_property_or_throw(
js_str!("isoMillisecond"),
time.inner.millisecond(),
context,
)?;
// 7. Perform ! CreateDataPropertyOrThrow(fields, "isoMinute", 𝔽(temporalTime.[[ISOMinute]])).
fields.create_data_property_or_throw(js_str!("isoMinute"), time.inner.minute(), context)?;
// 8. Perform ! CreateDataPropertyOrThrow(fields, "isoNanosecond", 𝔽(temporalTime.[[ISONanosecond]])).
fields.create_data_property_or_throw(
js_str!("isoNanosecond"),
time.inner.nanosecond(),
context,
)?;
// 9. Perform ! CreateDataPropertyOrThrow(fields, "isoSecond", 𝔽(temporalTime.[[ISOSecond]])).
fields.create_data_property_or_throw(js_str!("isoSecond"), time.inner.second(), context)?;
// 10. Return fields.
Ok(fields.into())
}
/// 4.3.22 Temporal.PlainTime.prototype.valueOf ( )
fn value_of(_: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Throw a TypeError exception.
Err(JsNativeError::typ()
.with_message("valueOf cannot be called on PlainTime.")
.into())
}
}
// ==== PlainTime Abstract Operations ====
pub(crate) fn create_temporal_time(
inner: Time,
new_target: Option<&JsValue>,
context: &mut Context,
) -> JsResult<JsObject> {
// Note: IsValidTime is enforced by Time.
// 1. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
// 2. If newTarget is not present, set newTarget to %Temporal.PlainTime%.
let new_target = if let Some(new_target) = new_target {
new_target.clone()
} else {
context
.realm()
.intrinsics()
.constructors()
.plain_time()
.constructor()
.into()
};
// 3. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainTime.prototype%", « [[InitializedTemporalTime]], [[ISOHour]], [[ISOMinute]], [[ISOSecond]], [[ISOMillisecond]], [[ISOMicrosecond]], [[ISONanosecond]] »).
let prototype =
get_prototype_from_constructor(&new_target, StandardConstructors::plain_time, context)?;
// 4. Set object.[[ISOHour]] to hour.
// 5. Set object.[[ISOMinute]] to minute.
// 6. Set object.[[ISOSecond]] to second.
// 7. Set object.[[ISOMillisecond]] to millisecond.
// 8. Set object.[[ISOMicrosecond]] to microsecond.
// 9. Set object.[[ISONanosecond]] to nanosecond.
let obj = JsObject::from_proto_and_data(prototype, PlainTime { inner });
// 10. Return object.
Ok(obj)
}
pub(crate) fn to_temporal_time(
value: &JsValue,
_overflow: Option<ArithmeticOverflow>,
_context: &mut Context,
) -> JsResult<Time> {
// 1.If overflow is not present, set overflow to "constrain".
// 2. If item is an Object, then
match value {
JsValue::Object(object) => {
// a. If item has an [[InitializedTemporalTime]] internal slot, then
if let Some(time) = object.downcast_ref::<PlainTime>() {
// i. Return item.
return Ok(time.inner);
// b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
} else if let Some(_zdt) = object.downcast_ref::<ZonedDateTime>() {
// i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
// ii. Let timeZoneRec be ? CreateTimeZoneMethodsRecord(item.[[TimeZone]], « get-offset-nanoseconds-for »).
// iii. Let plainDateTime be ? GetPlainDateTimeFor(timeZoneRec, instant, item.[[Calendar]]).
// iv. Return ! CreateTemporalTime(plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]],
// plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]],
// plainDateTime.[[ISONanosecond]]).
return Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into());
// c. If item has an [[InitializedTemporalDateTime]] internal slot, then
} else if let Some(dt) = object.downcast_ref::<PlainDateTime>() {
// i. Return ! CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]],
// item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]],
// item.[[ISONanosecond]]).
return Ok(Time::new(
dt.inner.hour().into(),
dt.inner.minute().into(),
dt.inner.second().into(),
dt.inner.millisecond().into(),
dt.inner.microsecond().into(),
dt.inner.nanosecond().into(),
ArithmeticOverflow::Reject,
)?);
}
// d. Let result be ? ToTemporalTimeRecord(item).
// e. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]],
// result.[[Second]], result.[[Millisecond]], result.[[Microsecond]],
// result.[[Nanosecond]], overflow).
Err(JsNativeError::range()
.with_message("Not yet implemented.")
.into())
}
// 3. Else,
JsValue::String(_str) => {
// b. Let result be ? ParseTemporalTimeString(item).
// c. Assert: IsValidTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]) is true.
// TODO: Add time parsing to `temporal_rs`
Err(JsNativeError::typ()
.with_message("Invalid value for converting to PlainTime.")
.into())
}
// a. If item is not a String, throw a TypeError exception.
_ => Err(JsNativeError::typ()
.with_message("Invalid value for converting to PlainTime.")
.into()),
}
// 4. Return ! CreateTemporalTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]]).
}