Skip to content

Commit f9fbf3e

Browse files
committed
Instant.prototype.round tests now passing
1 parent 7a80b4c commit f9fbf3e

File tree

3 files changed

+86
-169
lines changed

3 files changed

+86
-169
lines changed

nova_vm/src/ecmascript/builtins/temporal.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ pub mod instant;
88
pub mod options;
99
pub mod plain_time;
1010

11-
use temporal_rs::options::{DifferenceSettings, RoundingMode, Unit, UnitGroup};
11+
use temporal_rs::options::{DifferenceSettings, RoundingIncrement, RoundingMode, Unit, UnitGroup};
1212

1313
use crate::{
1414
ecmascript::{
1515
builders::ordinary_object_builder::OrdinaryObjectBuilder,
1616
builtins::temporal::{
17-
instant::instant_prototype::{DefaultOption, get_temporal_unit_valued_option},
17+
instant::instant_prototype::get_temporal_unit_valued_option,
1818
options::{get_rounding_increment_option, get_rounding_mode_option},
1919
},
2020
execution::{Agent, JsResult, Realm},
@@ -81,6 +81,8 @@ impl Temporal {
8181
trivially_bindable!(DifferenceSettings);
8282
trivially_bindable!(UnitGroup);
8383
trivially_bindable!(Unit);
84+
trivially_bindable!(RoundingMode);
85+
trivially_bindable!(RoundingIncrement);
8486

8587
/// [13.42 GetDifferenceSettings ( operation, options, unitGroup, disallowedUnits, fallbackSmallestUnit, smallestLargestDefaultUnit )](https://tc39.es/proposal-temporal/#sec-temporal-getdifferencesettings)
8688
/// The abstract operation GetDifferenceSettings takes arguments operation (since or until),
@@ -111,7 +113,6 @@ pub(crate) fn get_difference_settings<'gc, const IS_UNTIL: bool>(
111113
agent,
112114
options.get(agent),
113115
BUILTIN_STRING_MEMORY.largestUnit.to_property_key(),
114-
DefaultOption::Unset,
115116
gc.reborrow(),
116117
)
117118
.unbind()?
@@ -135,7 +136,6 @@ pub(crate) fn get_difference_settings<'gc, const IS_UNTIL: bool>(
135136
agent,
136137
options.get(agent),
137138
BUILTIN_STRING_MEMORY.smallestUnit.to_property_key(),
138-
DefaultOption::Unset,
139139
gc.reborrow(),
140140
)
141141
.unbind()?
@@ -157,8 +157,8 @@ pub(crate) fn get_difference_settings<'gc, const IS_UNTIL: bool>(
157157
// 17. If maximum is not unset, perform ? ValidateTemporalRoundingIncrement(roundingIncrement, maximum, false).
158158
// 18. Return the Record { [[SmallestUnit]]: smallestUnit, [[LargestUnit]]: largestUnit, [[RoundingMode]]: roundingMode, [[RoundingIncrement]]: roundingIncrement, }.
159159
let mut diff_settings = temporal_rs::options::DifferenceSettings::default();
160-
diff_settings.largest_unit = Some(largest_unit);
161-
diff_settings.smallest_unit = Some(smallest_unit);
160+
diff_settings.largest_unit = largest_unit;
161+
diff_settings.smallest_unit = smallest_unit;
162162
diff_settings.rounding_mode = Some(rounding_mode);
163163
diff_settings.increment = Some(rounding_increment);
164164
Ok(diff_settings)

nova_vm/src/ecmascript/builtins/temporal/instant/instant_prototype.rs

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::str::FromStr;
2-
31
use temporal_rs::options::{RoundingMode, RoundingOptions, Unit};
42

53
use crate::{
@@ -18,7 +16,7 @@ use crate::{
1816
require_internal_slot_temporal_instant, to_temporal_instant,
1917
},
2018
options::{
21-
OptionType, get_option, get_options_object, get_rounding_increment_option,
19+
get_option, get_options_object, get_rounding_increment_option,
2220
get_rounding_mode_option,
2321
},
2422
},
@@ -30,7 +28,7 @@ use crate::{
3028
types::{BUILTIN_STRING_MEMORY, BigInt, IntoValue, Object, PropertyKey, String, Value},
3129
},
3230
engine::{
33-
context::{Bindable, GcScope, NoGcScope, trivially_bindable},
31+
context::{Bindable, GcScope, NoGcScope},
3432
rootable::Scopable,
3533
},
3634
heap::WellKnownSymbolIndexes,
@@ -298,7 +296,7 @@ impl TemporalInstantPrototype {
298296
}
299297

300298
// 4. If roundTo is a String, then
301-
let round_to = if let Value::String(round_to) = round_to.unbind() {
299+
let round_to = if round_to.unbind().is_string() {
302300
// a. Let paramString be roundTo.
303301
let param_string = round_to;
304302
// b. Set roundTo to OrdinaryObjectCreate(null).
@@ -319,17 +317,21 @@ impl TemporalInstantPrototype {
319317
.unbind()?
320318
.bind(gc.nogc())
321319
};
320+
322321
let round_to = round_to.scope(agent, gc.nogc());
322+
323323
// 6. NOTE: The following steps read options and perform independent validation in
324324
// alphabetical order (GetRoundingIncrementOption reads "roundingIncrement" and
325325
// GetRoundingModeOption reads "roundingMode").
326326
let mut options = RoundingOptions::default();
327+
327328
// 7. Let roundingIncrement be ? GetRoundingIncrementOption(roundTo).
328329
let rounding_increment =
329330
get_rounding_increment_option(agent, round_to.get(agent), gc.reborrow())
330331
.unbind()?
331332
.bind(gc.nogc());
332333
options.increment = Some(rounding_increment);
334+
333335
// 8. Let roundingMode be ? GetRoundingModeOption(roundTo, half-expand).
334336
let rounding_mode = get_rounding_mode_option(
335337
agent,
@@ -340,17 +342,18 @@ impl TemporalInstantPrototype {
340342
.unbind()?
341343
.bind(gc.nogc());
342344
options.rounding_mode = Some(rounding_mode);
345+
343346
// 9. Let smallestUnit be ? GetTemporalUnitValuedOption(roundTo, "smallestUnit", required).
344347
let smallest_unit = get_temporal_unit_valued_option(
345348
agent,
346349
round_to.get(agent),
347350
BUILTIN_STRING_MEMORY.smallestUnit.into(),
348-
DefaultOption::Required,
349351
gc.reborrow(),
350352
)
351353
.unbind()?
352354
.bind(gc.nogc());
353-
options.smallest_unit = Some(smallest_unit);
355+
options.smallest_unit = smallest_unit;
356+
354357
// 10. Perform ? ValidateTemporalUnitValue(smallestUnit, time).
355358
// 11. If smallestUnit is hour, then
356359
// a. Let maximum be HoursPerDay.
@@ -521,7 +524,7 @@ pub(crate) fn to_integer_with_truncation<'gc>(
521524
// 2. If number is NaN, +∞𝔽 or -∞𝔽, throw a RangeError exception.
522525
if number.is_nan(agent) || number.is_pos_infinity(agent) || number.is_neg_infinity(agent) {
523526
return Err(agent.throw_exception_with_static_message(
524-
ExceptionType::TypeError,
527+
ExceptionType::RangeError,
525528
"Number cannot be NaN, positive infinity, or negative infinity",
526529
gc.into_nogc(),
527530
));
@@ -541,74 +544,12 @@ pub(crate) fn get_temporal_unit_valued_option<'gc>(
541544
agent: &mut Agent,
542545
options: Object,
543546
key: PropertyKey,
544-
default: DefaultOption,
545-
mut gc: GcScope<'gc, '_>,
546-
) -> JsResult<'gc, Unit> {
547+
gc: GcScope<'gc, '_>,
548+
) -> JsResult<'gc, Option<Unit>> {
547549
let options = options.bind(gc.nogc());
548-
let default = default.bind(gc.nogc());
549550
let key = key.bind(gc.nogc());
550-
// 1. Let allowedStrings be a List containing all values in the "Singular property name" and
551-
// "Plural property name" columns of Table 21, except the header row.
552-
const ALLOWED: &[&str] = &[
553-
"year",
554-
"years",
555-
"month",
556-
"months",
557-
"week",
558-
"weeks",
559-
"day",
560-
"days,",
561-
"hour",
562-
"hours",
563-
"minute",
564-
"minutes",
565-
"second",
566-
"seconds",
567-
"millisecond",
568-
"milliseconds",
569-
"microsecond",
570-
"microseconds",
571-
"nanosecond",
572-
"nanoseconds",
573-
"auto",
574-
];
575-
// 2. Append "auto" to allowedStrings.
576-
// 3. NOTE: For each singular Temporal unit name that is contained within allowedStrings, the
577-
// corresponding plural name is also contained within it.
578-
// 4. If default is unset, then
579-
// a. Let defaultValue be undefined.
580-
// 5. Else,
581-
// a. Let defaultValue be default.
582-
// 6. Let value be ? GetOption(options, key, string, allowedStrings, defaultValue).
583-
let string_value = get_option(
584-
agent,
585-
options.unbind(),
586-
key.unbind(),
587-
OptionType::String,
588-
ALLOWED,
589-
gc.reborrow(),
590-
)
591-
.unbind()?
592-
.bind(gc.nogc());
593-
594-
let js_str = string_value
595-
.unbind()
596-
.to_string(agent, gc.reborrow())
597-
.unbind()?
598-
.bind(gc.nogc());
599551

600-
let rust_str = js_str.as_str(agent).expect("aaa");
601-
// 7. If value is undefined, return unset.
602-
// 8. If value is "auto", return auto.
603-
// 9. Return the value in the "Value" column of Table 21 corresponding to the row with value in
604-
// its "Singular property name" or "Plural property name" column.
605-
Ok(Unit::from_str(rust_str).unwrap())
606-
}
552+
let opt = get_option::<Unit>(agent, options.unbind(), key.unbind(), gc)?;
607553

608-
#[allow(dead_code)]
609-
pub(crate) enum DefaultOption {
610-
Required,
611-
Unset,
554+
Ok(opt)
612555
}
613-
614-
trivially_bindable!(DefaultOption);

0 commit comments

Comments
 (0)