mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:07:34 +00:00
LibJS: Convert remaining Date AOs using JS::Value as in/output to double
There was an awful lot of JS::Value <-> double conversion going on, even through these AOs only work with number values anyway. They don't need a global object either as they won't allocate or throw, that was simply to pass it to infallible calls of ToIntegerOrInfinity.
This commit is contained in:
parent
b9b3d01bea
commit
f7c9bd0760
10 changed files with 357 additions and 354 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
||||
* Copyright (c) 2020, Nico Weber <thakis@chromium.org>
|
||||
* Copyright (c) 2021, Petróczi Zoltán <petroczizoltan@tutanota.com>
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
|
@ -23,7 +23,7 @@
|
|||
namespace JS {
|
||||
|
||||
// 21.4.3.2 Date.parse ( string ), https://tc39.es/ecma262/#sec-date.parse
|
||||
static Value parse_simplified_iso8601(GlobalObject& global_object, String const& iso_8601)
|
||||
static double parse_simplified_iso8601(String const& iso_8601)
|
||||
{
|
||||
// 21.4.1.15 Date Time String Format, https://tc39.es/ecma262/#sec-date-time-string-format
|
||||
GenericLexer lexer(iso_8601);
|
||||
|
@ -126,7 +126,7 @@ static Value parse_simplified_iso8601(GlobalObject& global_object, String const&
|
|||
auto lex_time = [&]() { return lex_hours_minutes(hours, minutes) && (!lexer.consume_specific(':') || lex_seconds_milliseconds()) && lex_timezone(); };
|
||||
|
||||
if (!lex_date() || (lexer.consume_specific('T') && !lex_time()) || !lexer.is_eof()) {
|
||||
return js_nan();
|
||||
return NAN;
|
||||
}
|
||||
|
||||
// We parsed a valid date simplified ISO 8601 string.
|
||||
|
@ -144,22 +144,22 @@ static Value parse_simplified_iso8601(GlobalObject& global_object, String const&
|
|||
else if (timezone == '+')
|
||||
time_ms -= *timezone_hours * 3'600'000 + *timezone_minutes * 60'000;
|
||||
|
||||
return time_clip(global_object, Value(time_ms));
|
||||
return time_clip(time_ms);
|
||||
}
|
||||
|
||||
static Value parse_date_string(GlobalObject& global_object, String const& date_string)
|
||||
static double parse_date_string(String const& date_string)
|
||||
{
|
||||
auto value = parse_simplified_iso8601(global_object, date_string);
|
||||
if (value.is_finite_number())
|
||||
auto value = parse_simplified_iso8601(date_string);
|
||||
if (isfinite(value))
|
||||
return value;
|
||||
|
||||
// Date.parse() is allowed to accept an arbitrary number of implementation-defined formats.
|
||||
// Parse formats of this type: "Wed Apr 17 23:08:53 +0000 2019"
|
||||
auto maybe_datetime = Core::DateTime::parse("%a %b %e %T %z %Y", date_string);
|
||||
if (maybe_datetime.has_value())
|
||||
return Value(1000.0 * maybe_datetime.value().timestamp());
|
||||
return 1000.0 * maybe_datetime->timestamp();
|
||||
|
||||
return js_nan();
|
||||
return NAN;
|
||||
}
|
||||
|
||||
DateConstructor::DateConstructor(GlobalObject& global_object)
|
||||
|
@ -200,20 +200,20 @@ ThrowCompletionOr<Object*> DateConstructor::construct(FunctionObject& new_target
|
|||
auto& vm = this->vm();
|
||||
auto& global_object = this->global_object();
|
||||
|
||||
Value date_value;
|
||||
double date_value;
|
||||
|
||||
// 2. Let numberOfArgs be the number of elements in values.
|
||||
// 3. If numberOfArgs = 0, then
|
||||
if (vm.argument_count() == 0) {
|
||||
// a. Let dv be the time value (UTC) identifying the current time.
|
||||
auto now = AK::Time::now_realtime().to_milliseconds();
|
||||
date_value = Value(static_cast<double>(now));
|
||||
date_value = static_cast<double>(now);
|
||||
}
|
||||
// 4. Else if numberOfArgs = 1, then
|
||||
else if (vm.argument_count() == 1) {
|
||||
// a. Let value be values[0].
|
||||
auto value = vm.argument(0);
|
||||
Value time_value;
|
||||
double time_value;
|
||||
|
||||
// b. If Type(value) is Object and value has a [[DateValue]] internal slot, then
|
||||
if (value.is_object() && is<Date>(value.as_object())) {
|
||||
|
@ -229,28 +229,28 @@ ThrowCompletionOr<Object*> DateConstructor::construct(FunctionObject& new_target
|
|||
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(global_object, primitive.as_string().string());
|
||||
time_value = parse_date_string(primitive.as_string().string());
|
||||
}
|
||||
// iii. Else,
|
||||
else {
|
||||
// 1. Let tv be ? ToNumber(v).
|
||||
time_value = TRY(primitive.to_number(global_object));
|
||||
time_value = TRY(primitive.to_number(global_object)).as_double();
|
||||
}
|
||||
}
|
||||
|
||||
// d. Let dv be TimeClip(tv).
|
||||
date_value = time_clip(global_object, time_value);
|
||||
date_value = time_clip(time_value);
|
||||
}
|
||||
// 5. Else,
|
||||
else {
|
||||
// a. Assert: numberOfArgs ≥ 2.
|
||||
// b. Let y be ? ToNumber(values[0]).
|
||||
auto year = TRY(vm.argument(0).to_number(global_object));
|
||||
auto year = TRY(vm.argument(0).to_number(global_object)).as_double();
|
||||
// c. Let m be ? ToNumber(values[1]).
|
||||
auto month = TRY(vm.argument(1).to_number(global_object));
|
||||
auto month = TRY(vm.argument(1).to_number(global_object)).as_double();
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
auto arg_or = [&vm, &global_object](size_t i, double fallback) -> ThrowCompletionOr<double> {
|
||||
return vm.argument_count() > i ? TRY(vm.argument(i).to_number(global_object)).as_double() : fallback;
|
||||
};
|
||||
|
||||
// d. If numberOfArgs > 2, let dt be ? ToNumber(values[2]); else let dt be 1𝔽.
|
||||
|
@ -266,28 +266,28 @@ ThrowCompletionOr<Object*> DateConstructor::construct(FunctionObject& new_target
|
|||
|
||||
// i. If y is NaN, let yr be NaN.
|
||||
// j. Else,
|
||||
if (!year.is_nan()) {
|
||||
if (!isnan(year)) {
|
||||
// i. Let yi be ! ToIntegerOrInfinity(y).
|
||||
auto year_double = MUST(year.to_integer_or_infinity(global_object));
|
||||
auto year_integer = to_integer_or_infinity(year);
|
||||
|
||||
// ii. If 0 ≤ yi ≤ 99, let yr be 1900𝔽 + 𝔽(yi); otherwise, let yr be y.
|
||||
if (0 <= year_double && year_double <= 99)
|
||||
year = Value(1900 + year_double);
|
||||
if (0 <= year_integer && year_integer <= 99)
|
||||
year = 1900 + year_integer;
|
||||
}
|
||||
|
||||
// k. Let finalDate be MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli)).
|
||||
auto day = make_day(global_object, year, month, date);
|
||||
auto time = make_time(global_object, hours, minutes, seconds, milliseconds);
|
||||
auto day = make_day(year, month, date);
|
||||
auto time = make_time(hours, minutes, seconds, milliseconds);
|
||||
auto final_date = make_date(day, time);
|
||||
|
||||
// l. Let dv be TimeClip(UTC(finalDate)).
|
||||
date_value = time_clip(global_object, Value(utc_time(final_date.as_double())));
|
||||
date_value = time_clip(utc_time(final_date));
|
||||
}
|
||||
|
||||
// 6. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Date.prototype%", « [[DateValue]] »).
|
||||
// 7. Set O.[[DateValue]] to dv.
|
||||
// 8. Return O.
|
||||
return TRY(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, date_value.as_double()));
|
||||
return TRY(ordinary_create_from_constructor<Date>(global_object, new_target, &GlobalObject::date_prototype, date_value));
|
||||
}
|
||||
|
||||
// 21.4.3.1 Date.now ( ), https://tc39.es/ecma262/#sec-date.now
|
||||
|
@ -306,18 +306,18 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::parse)
|
|||
|
||||
auto date_string = TRY(vm.argument(0).to_string(global_object));
|
||||
|
||||
return parse_date_string(global_object, date_string);
|
||||
return Value(parse_date_string(date_string));
|
||||
}
|
||||
|
||||
// 21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] ), https://tc39.es/ecma262/#sec-date.utc
|
||||
JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
|
||||
{
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
auto arg_or = [&vm, &global_object](size_t i, double fallback) -> ThrowCompletionOr<double> {
|
||||
return vm.argument_count() > i ? TRY(vm.argument(i).to_number(global_object)).as_double() : fallback;
|
||||
};
|
||||
|
||||
// 1. Let y be ? ToNumber(year).
|
||||
auto year = TRY(vm.argument(0).to_number(global_object));
|
||||
auto year = TRY(vm.argument(0).to_number(global_object)).as_double();
|
||||
// 2. If month is present, let m be ? ToNumber(month); else let m be +0𝔽.
|
||||
auto month = TRY(arg_or(1, 0));
|
||||
// 3. If date is present, let dt be ? ToNumber(date); else let dt be 1𝔽.
|
||||
|
@ -333,19 +333,19 @@ JS_DEFINE_NATIVE_FUNCTION(DateConstructor::utc)
|
|||
|
||||
// 8. If y is NaN, let yr be NaN.
|
||||
// 9. Else,
|
||||
if (!year.is_nan()) {
|
||||
if (!isnan(year)) {
|
||||
// a. Let yi be ! ToIntegerOrInfinity(y).
|
||||
auto year_double = MUST(year.to_integer_or_infinity(global_object));
|
||||
auto year_integer = to_integer_or_infinity(year);
|
||||
|
||||
// b. If 0 ≤ yi ≤ 99, let yr be 1900𝔽 + 𝔽(yi); otherwise, let yr be y.
|
||||
if (0 <= year_double && year_double <= 99)
|
||||
year = Value(1900 + year_double);
|
||||
if (0 <= year_integer && year_integer <= 99)
|
||||
year = 1900 + year_integer;
|
||||
}
|
||||
|
||||
// 10. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
|
||||
auto day = make_day(global_object, year, month, date);
|
||||
auto time = make_time(global_object, hours, minutes, seconds, milliseconds);
|
||||
return time_clip(global_object, make_date(day, time));
|
||||
auto day = make_day(year, month, date);
|
||||
auto time = make_time(hours, minutes, seconds, milliseconds);
|
||||
return Value(time_clip(make_date(day, time)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue