From 0d635100c431a94227c82dcc76f31f19cd5eb506 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 26 Nov 2024 11:43:32 -0500 Subject: [PATCH] LibJS: Port Date.parse string arguments to String --- Libraries/LibJS/Runtime/DateConstructor.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Libraries/LibJS/Runtime/DateConstructor.cpp b/Libraries/LibJS/Runtime/DateConstructor.cpp index 1488bb0259a..49473e6fcdc 100644 --- a/Libraries/LibJS/Runtime/DateConstructor.cpp +++ b/Libraries/LibJS/Runtime/DateConstructor.cpp @@ -25,7 +25,7 @@ namespace JS { GC_DEFINE_ALLOCATOR(DateConstructor); // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse -static Optional parse_simplified_iso8601(ByteString const& iso_8601) +static Optional parse_simplified_iso8601(StringView iso_8601) { // 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format GenericLexer lexer(iso_8601); @@ -148,7 +148,7 @@ static Optional parse_simplified_iso8601(ByteString const& iso_8601) return time_clip(time_ms); } -static double parse_date_string(VM& vm, ByteString const& date_string) +static double parse_date_string(VM& vm, StringView date_string) { if (date_string.is_empty()) return NAN; @@ -267,7 +267,7 @@ ThrowCompletionOr> DateConstructor::construct(FunctionObject& ne if (primitive.is_string()) { // 1. Assert: The next step never returns an abrupt completion because Type(v) is String. // 2. Let tv be the result of parsing v as a date, in exactly the same manner as for the parse method (21.4.3.2). - time_value = parse_date_string(vm, primitive.as_string().byte_string()); + time_value = parse_date_string(vm, primitive.as_string().utf8_string_view()); } // iii. Else, else { @@ -342,9 +342,13 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse) if (!vm.argument_count()) return js_nan(); - auto date_string = TRY(vm.argument(0).to_byte_string(vm)); + // This function applies the ToString operator to its argument. If ToString results in an abrupt completion the + // Completion Record is immediately returned. + auto date_string = TRY(vm.argument(0).to_string(vm)); - return Value(parse_date_string(vm, date_string)); + // Otherwise, this function interprets the resulting String as a date and time; it returns a Number, the UTC time + // value corresponding to the date and time. + return parse_date_string(vm, date_string); } // 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc