浏览代码

LibJS: Port calendar / time zone members of Temporal* structs to String

Linus Groh 2 年之前
父节点
当前提交
4a7d6670d8

+ 10 - 10
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.cpp

@@ -657,11 +657,11 @@ ThrowCompletionOr<Value> to_relative_temporal_object(VM& vm, Object const& optio
                     return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, *time_zone_name);
                     return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, *time_zone_name);
 
 
                 // 2. Set timeZoneName to ! CanonicalizeTimeZoneName(timeZoneName).
                 // 2. Set timeZoneName to ! CanonicalizeTimeZoneName(timeZoneName).
-                time_zone_name = canonicalize_time_zone_name(*time_zone_name);
+                time_zone_name = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(canonicalize_time_zone_name(time_zone_name->to_deprecated_string())));
             }
             }
 
 
             // ii. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
             // ii. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
-            time_zone = MUST(create_temporal_time_zone(vm, *time_zone_name));
+            time_zone = MUST(create_temporal_time_zone(vm, time_zone_name->to_deprecated_string()));
 
 
             // iii. If result.[[TimeZone]].[[Z]] is true, then
             // iii. If result.[[TimeZone]].[[Z]] is true, then
             if (result.time_zone.z) {
             if (result.time_zone.z) {
@@ -1322,7 +1322,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
         auto name = parse_result.time_zone_identifier;
         auto name = parse_result.time_zone_identifier;
 
 
         // b. Set timeZoneResult.[[Name]] to CodePointsToString(name).
         // b. Set timeZoneResult.[[Name]] to CodePointsToString(name).
-        time_zone_result.name = *name;
+        time_zone_result.name = TRY_OR_THROW_OOM(vm, String::from_utf8(*name));
     }
     }
 
 
     // 21. If parseResult contains a UTCDesignator Parse Node, then
     // 21. If parseResult contains a UTCDesignator Parse Node, then
@@ -1338,12 +1338,12 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
             auto offset = parse_result.time_zone_numeric_utc_offset;
             auto offset = parse_result.time_zone_numeric_utc_offset;
 
 
             // ii. Set timeZoneResult.[[OffsetString]] to CodePointsToString(offset).
             // ii. Set timeZoneResult.[[OffsetString]] to CodePointsToString(offset).
-            time_zone_result.offset_string = *offset;
+            time_zone_result.offset_string = TRY_OR_THROW_OOM(vm, String::from_utf8(*offset));
         }
         }
     }
     }
 
 
     // 23. Let calendar be undefined.
     // 23. Let calendar be undefined.
-    Optional<DeprecatedString> calendar;
+    Optional<String> calendar;
 
 
     // 24. For each Annotation Parse Node annotation contained within parseResult, do
     // 24. For each Annotation Parse Node annotation contained within parseResult, do
     for (auto const& annotation : parse_result.annotations) {
     for (auto const& annotation : parse_result.annotations) {
@@ -1358,7 +1358,7 @@ ThrowCompletionOr<ISODateTime> parse_iso_date_time(VM& vm, ParseResult const& pa
                 auto const& value = annotation.value;
                 auto const& value = annotation.value;
 
 
                 // 2. Let calendar be CodePointsToString(value).
                 // 2. Let calendar be CodePointsToString(value).
-                calendar = value;
+                calendar = TRY_OR_THROW_OOM(vm, String::from_utf8(value));
             }
             }
         }
         }
         // c. Else,
         // c. Else,
@@ -1385,12 +1385,12 @@ ThrowCompletionOr<TemporalInstant> parse_temporal_instant_string(VM& vm, StringV
     auto result = TRY(parse_iso_date_time(vm, *parse_result));
     auto result = TRY(parse_iso_date_time(vm, *parse_result));
 
 
     // 3. Let offsetString be result.[[TimeZone]].[[OffsetString]].
     // 3. Let offsetString be result.[[TimeZone]].[[OffsetString]].
-    Optional<DeprecatedString> offset_string = result.time_zone.offset_string;
+    auto offset_string = result.time_zone.offset_string;
 
 
     // 4. If result.[[TimeZone]].[[Z]] is true, then
     // 4. If result.[[TimeZone]].[[Z]] is true, then
     if (result.time_zone.z) {
     if (result.time_zone.z) {
         // a. Set offsetString to "+00:00".
         // a. Set offsetString to "+00:00".
-        offset_string = "+00:00"sv;
+        offset_string = TRY_OR_THROW_OOM(vm, String::from_utf8("+00:00"sv));
     }
     }
 
 
     // 6. Assert: offsetString is not undefined.
     // 6. Assert: offsetString is not undefined.
@@ -1428,7 +1428,7 @@ ThrowCompletionOr<String> parse_temporal_calendar_string(VM& vm, StringView iso_
             return TRY_OR_THROW_OOM(vm, String::from_utf8("iso8601"sv));
             return TRY_OR_THROW_OOM(vm, String::from_utf8("iso8601"sv));
         // c. Else, return calendar.
         // c. Else, return calendar.
         else
         else
-            return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(*calendar));
+            return calendar.release_value();
     }
     }
     // 3. Else,
     // 3. Else,
     else {
     else {
@@ -1686,7 +1686,7 @@ ThrowCompletionOr<TemporalTimeZone> parse_temporal_time_zone_string(VM& vm, Stri
     // 2. If parseResult is a Parse Node, then
     // 2. If parseResult is a Parse Node, then
     if (parse_result.has_value()) {
     if (parse_result.has_value()) {
         // a. Return the Record { [[Z]]: false, [[OffsetString]]: undefined, [[Name]]: timeZoneString }.
         // a. Return the Record { [[Z]]: false, [[OffsetString]]: undefined, [[Name]]: timeZoneString }.
-        return TemporalTimeZone { .z = false, .offset_string = {}, .name = time_zone_string };
+        return TemporalTimeZone { .z = false, .offset_string = {}, .name = TRY_OR_THROW_OOM(vm, String::from_utf8(time_zone_string)) };
     }
     }
 
 
     // 3. Let result be ? ParseISODateTime(timeZoneString).
     // 3. Let result be ? ParseISODateTime(timeZoneString).

+ 8 - 8
Userland/Libraries/LibJS/Runtime/Temporal/AbstractOperations.h

@@ -57,14 +57,14 @@ struct TemporalInstant {
     u16 millisecond;
     u16 millisecond;
     u16 microsecond;
     u16 microsecond;
     u16 nanosecond;
     u16 nanosecond;
-    Optional<DeprecatedString> time_zone_offset;
+    Optional<String> time_zone_offset;
 };
 };
 
 
 struct TemporalDate {
 struct TemporalDate {
     i32 year;
     i32 year;
     u8 month;
     u8 month;
     u8 day;
     u8 day;
-    Optional<DeprecatedString> calendar;
+    Optional<String> calendar;
 };
 };
 
 
 struct TemporalTime {
 struct TemporalTime {
@@ -74,27 +74,27 @@ struct TemporalTime {
     u16 millisecond;
     u16 millisecond;
     u16 microsecond;
     u16 microsecond;
     u16 nanosecond;
     u16 nanosecond;
-    Optional<DeprecatedString> calendar = {};
+    Optional<String> calendar = {};
 };
 };
 
 
 struct TemporalTimeZone {
 struct TemporalTimeZone {
     bool z;
     bool z;
-    Optional<DeprecatedString> offset_string;
-    Optional<DeprecatedString> name;
+    Optional<String> offset_string;
+    Optional<String> name;
 };
 };
 
 
 struct TemporalYearMonth {
 struct TemporalYearMonth {
     i32 year;
     i32 year;
     u8 month;
     u8 month;
     u8 day;
     u8 day;
-    Optional<DeprecatedString> calendar = {};
+    Optional<String> calendar = {};
 };
 };
 
 
 struct TemporalMonthDay {
 struct TemporalMonthDay {
     Optional<i32> year;
     Optional<i32> year;
     u8 month;
     u8 month;
     u8 day;
     u8 day;
-    Optional<DeprecatedString> calendar = {};
+    Optional<String> calendar = {};
 };
 };
 
 
 struct ISODateTime {
 struct ISODateTime {
@@ -108,7 +108,7 @@ struct ISODateTime {
     u16 microsecond;
     u16 microsecond;
     u16 nanosecond;
     u16 nanosecond;
     TemporalTimeZone time_zone { .z = false, .offset_string = {}, .name = {} };
     TemporalTimeZone time_zone { .z = false, .offset_string = {}, .name = {} };
-    Optional<DeprecatedString> calendar = {};
+    Optional<String> calendar = {};
 };
 };
 
 
 struct SecondsStringPrecision {
 struct SecondsStringPrecision {

+ 4 - 4
Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -349,11 +349,11 @@ ThrowCompletionOr<Object*> to_temporal_time_zone(VM& vm, Value temporal_time_zon
                 return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, name);
                 return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, name);
 
 
             // ii. Set name to ! CanonicalizeTimeZoneName(name).
             // ii. Set name to ! CanonicalizeTimeZoneName(name).
-            name = canonicalize_time_zone_name(name);
+            name = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(canonicalize_time_zone_name(name.to_deprecated_string())));
         }
         }
 
 
         // c. Return ! CreateTemporalTimeZone(name).
         // c. Return ! CreateTemporalTimeZone(name).
-        return MUST(create_temporal_time_zone(vm, name));
+        return MUST(create_temporal_time_zone(vm, name.to_deprecated_string()));
     }
     }
 
 
     // 5. If parseResult.[[Z]] is true, return ! CreateTemporalTimeZone("UTC").
     // 5. If parseResult.[[Z]] is true, return ! CreateTemporalTimeZone("UTC").
@@ -361,7 +361,7 @@ ThrowCompletionOr<Object*> to_temporal_time_zone(VM& vm, Value temporal_time_zon
         return MUST(create_temporal_time_zone(vm, "UTC"sv));
         return MUST(create_temporal_time_zone(vm, "UTC"sv));
 
 
     // 6. Return ! CreateTemporalTimeZone(parseResult.[[OffsetString]]).
     // 6. Return ! CreateTemporalTimeZone(parseResult.[[OffsetString]]).
-    return MUST(create_temporal_time_zone(vm, *parse_result.offset_string));
+    return MUST(create_temporal_time_zone(vm, parse_result.offset_string->to_deprecated_string()));
 }
 }
 
 
 // 11.6.8 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor
 // 11.6.8 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor

+ 3 - 3
Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTime.cpp

@@ -130,7 +130,7 @@ ThrowCompletionOr<ZonedDateTime*> to_temporal_zoned_date_time(VM& vm, Value item
 
 
     Object* calendar = nullptr;
     Object* calendar = nullptr;
     Object* time_zone = nullptr;
     Object* time_zone = nullptr;
-    Optional<DeprecatedString> offset_string;
+    Optional<String> offset_string;
     ISODateTime result;
     ISODateTime result;
 
 
     // 5. If Type(item) is Object, then
     // 5. If Type(item) is Object, then
@@ -203,7 +203,7 @@ ThrowCompletionOr<ZonedDateTime*> to_temporal_zoned_date_time(VM& vm, Value item
                 return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, *time_zone_name);
                 return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, *time_zone_name);
 
 
             // ii. Set timeZoneName to ! CanonicalizeTimeZoneName(timeZoneName).
             // ii. Set timeZoneName to ! CanonicalizeTimeZoneName(timeZoneName).
-            time_zone_name = canonicalize_time_zone_name(*time_zone_name);
+            time_zone_name = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(canonicalize_time_zone_name(time_zone_name->to_deprecated_string())));
         }
         }
 
 
         // g. Let offsetString be result.[[TimeZone]].[[OffsetString]].
         // g. Let offsetString be result.[[TimeZone]].[[OffsetString]].
@@ -221,7 +221,7 @@ ThrowCompletionOr<ZonedDateTime*> to_temporal_zoned_date_time(VM& vm, Value item
         }
         }
 
 
         // j. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
         // j. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
-        time_zone = MUST(create_temporal_time_zone(vm, *time_zone_name));
+        time_zone = MUST(create_temporal_time_zone(vm, time_zone_name->to_deprecated_string()));
 
 
         // k. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
         // k. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
         auto temporal_calendar_like = result.calendar.has_value()
         auto temporal_calendar_like = result.calendar.has_value()