LibJS: Clip parsed IS0 8601 strings to +/- 8.64e15

This commit is contained in:
Timothy Flynn 2022-01-14 08:34:47 -05:00 committed by Linus Groh
parent aea4f79b57
commit 643992904c
Notes: sideshowbarker 2024-07-17 20:52:59 +09:00
2 changed files with 11 additions and 7 deletions

View File

@ -21,7 +21,7 @@
namespace JS { namespace JS {
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse // 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
static Value parse_simplified_iso8601(const String& iso_8601) static Value parse_simplified_iso8601(GlobalObject& global_object, const String& iso_8601)
{ {
// 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format // 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format
GenericLexer lexer(iso_8601); GenericLexer lexer(iso_8601);
@ -113,13 +113,12 @@ static Value parse_simplified_iso8601(const String& iso_8601)
else if (timezone == '+') else if (timezone == '+')
time_ms -= *timezone_hours * 3'600'000 + *timezone_minutes * 60'000; time_ms -= *timezone_hours * 3'600'000 + *timezone_minutes * 60'000;
// FIXME: reject time_ms if resulting value wouldn't fit in a double return time_clip(global_object, Value(time_ms));
return Value(time_ms);
} }
static Value parse_date_string(String const& date_string) static Value parse_date_string(GlobalObject& global_object, String const& date_string)
{ {
auto value = parse_simplified_iso8601(date_string); auto value = parse_simplified_iso8601(global_object, date_string);
if (value.is_finite_number()) if (value.is_finite_number())
return value; return value;
@ -199,7 +198,7 @@ ThrowCompletionOr<Object*> DateConstructor::construct(FunctionObject& new_target
if (vm.argument_count() == 1) { if (vm.argument_count() == 1) {
auto value = vm.argument(0); auto value = vm.argument(0);
if (value.is_string()) if (value.is_string())
value = parse_date_string(value.as_string().string()); value = parse_date_string(global_object, value.as_string().string());
else else
value = TRY(value.to_number(global_object)); value = TRY(value.to_number(global_object));
@ -288,7 +287,7 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse)
auto date_string = TRY(vm.argument(0).to_string(global_object)); auto date_string = TRY(vm.argument(0).to_string(global_object));
return parse_date_string(date_string); return parse_date_string(global_object, date_string);
} }
// 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc // 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc

View File

@ -31,3 +31,8 @@ test("basic functionality", () => {
expect(Date.parse("1970-06-30T13:30Zoo")).toBe(NaN); expect(Date.parse("1970-06-30T13:30Zoo")).toBe(NaN);
expect(Date.parse("2020T13:30.40:")).toBe(NaN); expect(Date.parse("2020T13:30.40:")).toBe(NaN);
}); });
test("time clip", () => {
expect(Date.parse("+999999")).toBeNaN();
expect(Date.parse("-999999")).toBeNaN();
});