mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +00:00
LibJS: Convert TimeZone AOs to ThrowCompletionOr
This commit is contained in:
parent
830d484d78
commit
cc00a726a8
12 changed files with 99 additions and 195 deletions
|
@ -66,7 +66,7 @@ String default_time_zone()
|
|||
}
|
||||
|
||||
// 11.6.1 ParseTemporalTimeZone ( string ), https://tc39.es/proposal-temporal/#sec-temporal-parsetemporaltimezone
|
||||
String parse_temporal_time_zone(GlobalObject& global_object, String const& string)
|
||||
ThrowCompletionOr<String> parse_temporal_time_zone(GlobalObject& global_object, String const& string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
|
@ -74,19 +74,19 @@ String parse_temporal_time_zone(GlobalObject& global_object, String const& strin
|
|||
|
||||
// 2. Let result be ? ParseTemporalTimeZoneString(string).
|
||||
auto result = parse_temporal_time_zone_string(global_object, string);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 3. If result.[[Z]] is not undefined, return "UTC".
|
||||
if (result->z)
|
||||
return "UTC";
|
||||
return String { "UTC" };
|
||||
|
||||
// 4. Return result.[[Name]].
|
||||
return *result->name;
|
||||
}
|
||||
|
||||
// 11.6.2 CreateTemporalTimeZone ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltimezone
|
||||
TimeZone* create_temporal_time_zone(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
|
||||
ThrowCompletionOr<TimeZone*> create_temporal_time_zone(GlobalObject& global_object, String const& identifier, FunctionObject const* new_target)
|
||||
{
|
||||
// 1. If newTarget is not present, set it to %Temporal.TimeZone%.
|
||||
if (!new_target)
|
||||
|
@ -94,12 +94,12 @@ TimeZone* create_temporal_time_zone(GlobalObject& global_object, String const& i
|
|||
|
||||
// 2. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.TimeZone.prototype%", « [[InitializedTemporalTimeZone]], [[Identifier]], [[OffsetNanoseconds]] »).
|
||||
// 3. Set object.[[Identifier]] to identifier.
|
||||
auto* object = TRY_OR_DISCARD(ordinary_create_from_constructor<TimeZone>(global_object, *new_target, &GlobalObject::temporal_time_zone_prototype, identifier));
|
||||
auto* object = TRY(ordinary_create_from_constructor<TimeZone>(global_object, *new_target, &GlobalObject::temporal_time_zone_prototype, identifier));
|
||||
|
||||
// 4. If identifier satisfies the syntax of a TimeZoneNumericUTCOffset (see 13.33), then
|
||||
if (is_valid_time_zone_numeric_utc_offset_syntax(identifier)) {
|
||||
// a. Set object.[[OffsetNanoseconds]] to ! ParseTimeZoneOffsetString(identifier).
|
||||
object->set_offset_nanoseconds(parse_time_zone_offset_string(global_object, identifier));
|
||||
object->set_offset_nanoseconds(TRY(parse_time_zone_offset_string(global_object, identifier)));
|
||||
}
|
||||
// 5. Else,
|
||||
else {
|
||||
|
@ -207,7 +207,7 @@ bool is_valid_time_zone_numeric_utc_offset_syntax(String const& offset_string)
|
|||
}
|
||||
|
||||
// 11.6.8 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/proposal-temporal/#sec-temporal-parsetimezoneoffsetstring
|
||||
double parse_time_zone_offset_string(GlobalObject& global_object, String const& offset_string)
|
||||
ThrowCompletionOr<double> parse_time_zone_offset_string(GlobalObject& global_object, String const& offset_string)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
|
@ -222,10 +222,8 @@ double parse_time_zone_offset_string(GlobalObject& global_object, String const&
|
|||
Optional<StringView> seconds_part;
|
||||
Optional<StringView> fraction_part;
|
||||
auto success = parse_time_zone_numeric_utc_offset_syntax(offset_string, sign_part, hours_part, minutes_part, seconds_part, fraction_part);
|
||||
if (!success) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidFormat, "TimeZone offset");
|
||||
return {};
|
||||
}
|
||||
if (!success)
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidFormat, "TimeZone offset");
|
||||
|
||||
// 4. If either hours or sign are undefined, throw a RangeError exception.
|
||||
// NOTE: Both of these checks are always false, due to the handling of Step 2
|
||||
|
@ -317,7 +315,7 @@ String format_time_zone_offset_string(double offset_nanoseconds)
|
|||
}
|
||||
|
||||
// 11.6.10 ToTemporalTimeZone ( temporalTimeZoneLike ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltimezone
|
||||
Object* to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_zone_like)
|
||||
ThrowCompletionOr<Object*> to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_zone_like)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
|
@ -333,21 +331,21 @@ Object* to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_z
|
|||
|
||||
// b. If ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
|
||||
auto has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
if (!has_property)
|
||||
return &temporal_time_zone_like.as_object();
|
||||
|
||||
// c. Set temporalTimeZoneLike to ? Get(temporalTimeZoneLike, "timeZone").
|
||||
temporal_time_zone_like = temporal_time_zone_like.as_object().get(vm.names.timeZone);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// d. If Type(temporalTimeZoneLike) is Object and ? HasProperty(temporalTimeZoneLike, "timeZone") is false, return temporalTimeZoneLike.
|
||||
if (temporal_time_zone_like.is_object()) {
|
||||
has_property = temporal_time_zone_like.as_object().has_property(vm.names.timeZone);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
if (!has_property)
|
||||
return &temporal_time_zone_like.as_object();
|
||||
}
|
||||
|
@ -355,27 +353,25 @@ Object* to_temporal_time_zone(GlobalObject& global_object, Value temporal_time_z
|
|||
|
||||
// 2. Let identifier be ? ToString(temporalTimeZoneLike).
|
||||
auto identifier = temporal_time_zone_like.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 3. Let result be ? ParseTemporalTimeZone(identifier).
|
||||
auto result = parse_temporal_time_zone(global_object, identifier);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto result = TRY(parse_temporal_time_zone(global_object, identifier));
|
||||
|
||||
// 4. Return ? CreateTemporalTimeZone(result).
|
||||
return create_temporal_time_zone(global_object, result);
|
||||
return TRY(create_temporal_time_zone(global_object, result));
|
||||
}
|
||||
|
||||
// 11.6.11 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor
|
||||
double get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone, Instant& instant)
|
||||
ThrowCompletionOr<double> get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone, Instant& instant)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let getOffsetNanosecondsFor be ? GetMethod(timeZone, "getOffsetNanosecondsFor").
|
||||
auto* get_offset_nanoseconds_for = time_zone.get_method(global_object, vm.names.getOffsetNanosecondsFor);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 2. If getOffsetNanosecondsFor is undefined, set getOffsetNanosecondsFor to %Temporal.TimeZone.prototype.getOffsetNanosecondsFor%.
|
||||
if (!get_offset_nanoseconds_for)
|
||||
|
@ -383,57 +379,45 @@ double get_offset_nanoseconds_for(GlobalObject& global_object, Value time_zone,
|
|||
|
||||
// 3. Let offsetNanoseconds be ? Call(getOffsetNanosecondsFor, timeZone, « instant »).
|
||||
auto offset_nanoseconds_value = vm.call(*get_offset_nanoseconds_for, time_zone, &instant);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 4. If Type(offsetNanoseconds) is not Number, throw a TypeError exception.
|
||||
if (!offset_nanoseconds_value.is_number()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::IsNotA, "Offset nanoseconds value", "number");
|
||||
return {};
|
||||
}
|
||||
if (!offset_nanoseconds_value.is_number())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::IsNotA, "Offset nanoseconds value", "number");
|
||||
|
||||
// 5. If ! IsIntegralNumber(offsetNanoseconds) is false, throw a RangeError exception.
|
||||
if (!offset_nanoseconds_value.is_integral_number()) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::IsNotAn, "Offset nanoseconds value", "integral number");
|
||||
return {};
|
||||
}
|
||||
if (!offset_nanoseconds_value.is_integral_number())
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::IsNotAn, "Offset nanoseconds value", "integral number");
|
||||
|
||||
// 6. Set offsetNanoseconds to ℝ(offsetNanoseconds).
|
||||
auto offset_nanoseconds = offset_nanoseconds_value.as_double();
|
||||
|
||||
// 7. If abs(offsetNanoseconds) > 86400 × 10^9, throw a RangeError exception.
|
||||
if (fabs(offset_nanoseconds) > 86400000000000.0) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::TemporalInvalidOffsetNanosecondsValue);
|
||||
return {};
|
||||
}
|
||||
if (fabs(offset_nanoseconds) > 86400000000000.0)
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidOffsetNanosecondsValue);
|
||||
|
||||
// 8. Return offsetNanoseconds.
|
||||
return offset_nanoseconds;
|
||||
}
|
||||
|
||||
// 11.6.12 BuiltinTimeZoneGetOffsetStringFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetoffsetstringfor
|
||||
Optional<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Value time_zone, Instant& instant)
|
||||
ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(GlobalObject& global_object, Value time_zone, Instant& instant)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
|
||||
auto offset_nanoseconds = get_offset_nanoseconds_for(global_object, time_zone, instant);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
|
||||
|
||||
// 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
|
||||
return format_time_zone_offset_string(offset_nanoseconds);
|
||||
}
|
||||
|
||||
// 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
|
||||
PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Value time_zone, Instant& instant, Object& calendar)
|
||||
ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Value time_zone, Instant& instant, Object& calendar)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
|
||||
auto offset_nanoseconds = get_offset_nanoseconds_for(global_object, time_zone, instant);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
|
||||
|
||||
// 2. Let result be ! GetISOPartsFromEpoch(instant.[[Nanoseconds]]).
|
||||
auto result = get_iso_parts_from_epoch(instant.nanoseconds());
|
||||
|
@ -442,7 +426,10 @@ PlainDateTime* builtin_time_zone_get_plain_date_time_for(GlobalObject& global_ob
|
|||
result = balance_iso_date_time(result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond + offset_nanoseconds);
|
||||
|
||||
// 4. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
|
||||
return create_temporal_date_time(global_object, result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond, calendar);
|
||||
auto* date_time = create_temporal_date_time(global_object, result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond, calendar);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
return date_time;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue