Commit graph

2541 commits

Author SHA1 Message Date
Timothy Flynn
c71877b0a6 LibJS: Implement Date.prototype.setUTCSeconds 2022-01-16 11:07:02 +01:00
Timothy Flynn
efda1724e8 LibJS: Protect [TimeValue]FromTime from non-finite times
Includes HourFromTime, MinFromTime, SecFromTime, msFromTime.
2022-01-16 11:07:02 +01:00
Timothy Flynn
4848f587cd LibJS: Protect DayWithinYear against non-finite times 2022-01-16 11:07:02 +01:00
Timothy Flynn
9be0a0fd28 LibJS: Protect YearFromTime against non-finite times 2022-01-16 11:07:02 +01:00
Timothy Flynn
8ad043fe5e LibJS: Ensure final computation in DayFromYear is performed on a double
When we multiple by 365, ensure the result is a double (not an i32) to
prevent overflow.
2022-01-16 11:07:02 +01:00
Linus Groh
e8519156bc LibJS: Implement create_dynamic_function() according to the spec
The three major changes are:

- Parsing parameters, the function body, and then the full assembled
  function source all separately. This is required by the spec, as
  function parameters and body must be valid each on their own, which
  cannot be guaranteed if we only ever parse the full function.
- Returning an ECMAScriptFunctionObject instead of a FunctionExpression
  that needs to be evaluated separately. This vastly simplifies the
  {Async,AsyncGenerator,Generator,}Function constructor implementations.
  Drop '_node' from the function name accordingly.
- The prototype is now determined via GetPrototypeFromConstructor and
  passed to OrdinaryFunctionCreate.
2022-01-16 01:54:48 +01:00
Linus Groh
13fe4e8c64 LibJS: Allow passing prototype to ECMAScriptFunctionObject::create()
This should have been the default as it roughly represents the
OrdinaryFunctionCreate AO.
For now, keep two overloads and continue to guess the required prototype
from the function kind in most cases. The prototype needs to be passed
in explicitly when it may be derived from user code, such as in the
CreateDynamicFunction AO.
2022-01-16 01:54:48 +01:00
Linus Groh
f0b2179bd7 LibJS: Don't require ParenClose in Parser::parse_formal_parameters()
The parentheses are dealt with outside this function, so we shouldn't
use the (non)existence of one as the condition for consuming another
comma and then parameter. Just check for a comma instead. This becomes
relevant when parsing standalone function parameters as per the spec in
the CreateDynamicFunction AO.
2022-01-16 01:54:48 +01:00
Linus Groh
e730ada07d LibJS: Consume curly braces outside of Parser::parse_function_body()
The curly braces are not part of the FunctionBody production. This
becomes relevant when parsing a standalone function body as per the spec
in the CreateDynamicFunction AO.
2022-01-16 01:54:48 +01:00
Linus Groh
710de821e7 LibJS: Add VM::active_function_object() 2022-01-16 01:54:48 +01:00
Linus Groh
0c73fbbba5 LibJS: Rename FunctionKind::{Regular => Normal}
This is what CreateDynamicFunction calls it.
2022-01-16 01:54:48 +01:00
Timothy Flynn
6576d0291c LibJS: Implement Date.prototype.getTimezoneOffset 2022-01-15 20:13:48 +01:00
Timothy Flynn
032664332b LibJS: Implement MakeDay without using AK::years_to_days_since_epoch
Implementing years_to_days_since_epoch without a loop will be tricky.
The TimeFromYear AO gives a good enough approximation for MakeDay until
we figure that out.
2022-01-15 20:13:48 +01:00
Timothy Flynn
11d7c7ebbd LibJS: Move time conversion constants to the Date header
These are needed outside of the Date object .cpp file, so move them to
the header.
2022-01-15 20:13:48 +01:00
Timothy Flynn
34a1dd4257 LibJS: Remove Core::DateTime logic from the Date object :^) 2022-01-15 20:13:48 +01:00
Timothy Flynn
58ccca6a9d LibJS+js: Pretty-print Date objects using the ToDateString AO 2022-01-15 20:13:48 +01:00
Timothy Flynn
d83ce7dd0b LibJS: Re-implement the Date constructor / prototype for spec compliance
First, this adds a constructor to the Date object to be created from a
plain double. This is a first step to removing Core::DateTime as the
basis for the Date object. A subsequent commit will remove the now-
unused data from the object.

Next, this implements the constructor in accordance to the spec. The
constructor when NewTarget is undefined no longer allocates a Date on
the heap. The other constructor properly uses recently created AOs to
handle time zone and ensure the created [[DateValue]] is valid. Other
methods on the constructor (Date.now) have not been touched yet.

Last, the prototype is reimplemented. Again, we use other AOs to handle
time zones and time clipping. Not all prototypes are fixed; most of them
are, but a few (e.g. Date.prototype.getTimezoneOffset) were not fixed,
but left in a mostly unimplemented state for another commit.

In all of the above, spec comments are added. This is a rather large
change; but it's tough to do any of these parts individually without
breaking everything else.
2022-01-15 20:13:48 +01:00
Timothy Flynn
d31e6b9391 LibJS: Make the thisTimeValue AO public
It will be needed by the Date constructor.
2022-01-15 20:13:48 +01:00
Timothy Flynn
a488ec1ad0 LibJS: Implement spec-compliant ToDateString and its underlying AOs 2022-01-15 20:13:48 +01:00
Timothy Flynn
62dc9958f5 LibJS: Protect LocalTZA against non-finite times
It is undefined behavior to cast from a double to an integer if the
value does not fit in the limits of the integer.
2022-01-15 20:13:48 +01:00
Timothy Flynn
b2aa3c9f84 LibJS: Do not negate offset in LocalTZA for isUTC=false
In commmit 7d2834344a, I think I combined
the definitions of the LocalTZA and UTC AOs in my head, and thought the
offset should be negated within LocalTZA. Instead, the offset should be
left untouched, and the UTC AO is responsible for doing the subtraction.
2022-01-15 20:13:48 +01:00
Timothy Flynn
d93713b874 LibJS: Implement the LocalTime, UTC, and TimeWithinDay AOs 2022-01-15 20:13:48 +01:00
Timothy Flynn
5f5bcd549e LibJS: Sort Date.prototype methods by spec order
When viewing the code side-by-side with the spec, it's much nicer when
everything is in the same order.

Also fixes the spec link for Date.prototype.getMilliseconds (it pointed
at setMilliseconds by mistake).
2022-01-15 20:13:48 +01:00
Timothy Flynn
643992904c LibJS: Clip parsed IS0 8601 strings to +/- 8.64e15 2022-01-14 22:39:06 +01:00
Timothy Flynn
aea4f79b57 LibJS: Implement Date.parse using AK::Time and LibTimeZone
Fixes #4651
2022-01-14 22:39:06 +01:00
Timothy Flynn
7d2834344a LibJS: Implement the localTZA AO for isUTC=false 2022-01-14 22:39:06 +01:00
Nico Weber
db869a0402 LibJS: Add an else in StringPrototype::substr
No behavior change, but makes the code look more like the spec test for
this function.
2022-01-14 11:12:24 +01:00
Nico Weber
1b944b4c41 LibJS: Fix substr() with negative arguments larger than string length
length_in_code_units() returns a size_t, which is 64-bit unsigned
in i686 builds. `size + (i32)int_length` hence produced a 64-bit
unsigned result, so a negative value would wrap around and become
a very large number.

As fix, just omit the cast -- we assign the result of max() to
a double anyways.

With this, all test262 tests in annexB/built-ins/String/prototype pass.
2022-01-14 11:12:24 +01:00
Nico Weber
23cde7685c LibJS: Correcly handle surrogates in escape()
Fixes test/annexB/built-ins/escape/escape-above{,-astral}.js in
test262. All tests in test/annexB/built-ins/escape pass now.
2022-01-14 10:59:46 +01:00
Ali Mohammad Pur
9de33629da AK+Everywhere: Make Variant::visit() respect the Variant's constness
...and fix all the instances of visit() taking non-const arguments.
2022-01-14 11:35:40 +03:30
Timothy Flynn
c7dbe27781 LibJS: Handle the [[LanguageDisplay]] tag when localizing languages 2022-01-13 23:05:31 +01:00
Linus Groh
64f125fe34 LibJS: Mark CreateTemporalTimeZone("UTC") as infallible
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/ea25cfa
2022-01-13 19:25:56 +01:00
Timothy Flynn
4875ec26dd LibJS: Implement per-locale display of calendars and date-time fields 2022-01-13 13:43:57 +01:00
Timothy Flynn
adb762ee48 LibJS: Add FIXME regarding [[LanguageDisplay]] internal slot handling
This is supposed to work as follows (grabbed from SpiderMonkey):

    > opt = { type: "language", languageDisplay: "dialect" };
    > new Intl.DisplayNames([], opt).of("en-US");
    "American English"

    > opt = { type: "language", languageDisplay: "standard" };
    > new Intl.DisplayNames([], opt).of("en-US");
    "English (United States)"

We currently display the "dialect" variant. We will need to figure out
how to display the "standard" variant. I think the way it works is that
we take the display names of "en" (language) and "US" (region) and
format them according to this pattern in localeDisplayNames.json:

    "localeDisplayNames": {
        "localeDisplayPattern": {
            "localePattern": "{0} ({1})",
        },
    },

But I'd like to confirm this before implementing it.
2022-01-13 13:43:57 +01:00
Timothy Flynn
8126cb2545 LibJS+LibUnicode: Remove unnecessary locale currency mapping wrapper
Before LibUnicode generated methods were weakly linked, we had a public
method (get_locale_currency_mapping) for retrieving currency mappings.
That method invoked one of several style-specific methods that only
existed in the generated UnicodeLocale.

One caveat of weakly linked functions is that every such function must
have a public declaration. The result is that each of those styled
methods are declared publicly, which makes the wrapper redundant
because it is just as easy to invoke the method for the desired style.
2022-01-13 13:43:57 +01:00
Timothy Flynn
1a3e6e8a7b LibJS: Add [[LanguageDisplay]] to Intl.DisplayNames's resolvedOptions 2022-01-13 13:43:57 +01:00
Timothy Flynn
71f7e67a20 LibJS: Parse new Intl.DisplayNames "type" and "languageDisplay" options
Intl.DisplayNames v2 adds "calendar" and "dateTimeField" types, as well
as a "languageDisplay" option for the "language" type. This just adds
these options to the constructor.
2022-01-13 13:43:57 +01:00
Timothy Flynn
853ccab9af LibJS: Remove unnecessary braces in Intl.DisplayNames
Just caught my eye as I was modifying this code.
2022-01-13 13:43:57 +01:00
Linus Groh
b9093dd0ab LibJS: Don't validate time zone name when parsing Instant string
This is normative change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/2a81fbc
2022-01-13 10:08:34 +01:00
Linus Groh
cf6ceb956f LibJS: Avoid js_string() allocation in parse_time_zone_offset_string()
No need to take the spec literally here since we know the input values
are guaranteed to be integral numbers. Use AK string to number parsing
functionality instead and save a couple of PrimitiveString allocations.

This matches what we already do in parse_temporal_time_zone_string().
2022-01-12 21:24:12 +01:00
Linus Groh
392f5bfebd LibJS: Fix fraction substring range in parse_temporal_time_zone_string()
Two issues:

- The intended range was 9 characters starting from index 1. Since the
  second argument to String::substring() is the length, 10 is
  potentially reading further than the string's length (when only
  providing one fraction digit), causing an assertion failure crash.
- The spec's intention to skip the decimal separator by starting at
  index 1 is incorrect, no decimal separator is present in the result of
  parsing TimeZoneUTCOffsetFractionalPart. I filed a spec fix for this,
  see: https://github.com/tc39/proposal-temporal/pull/1999
2022-01-12 21:24:12 +01:00
Linus Groh
027e4bd439 LibJS: Fix calculation overflow in parse_temporal_time_zone_string()
As all variables and numeric literals in the expression have an integral
data type, it would evaluate to an int and could easily overflow as
we're multiplying seconds with 10^9.

Introduce a floating point literal into the expression to make it result
in a double.
2022-01-12 21:24:12 +01:00
Linus Groh
323e1e17cf LibJS: Fix sign data type in parse_temporal_time_zone_string()
A sign that's either the value 1 or -1 should obviously not have an
unsigned data type :^)

This would cause it to become 255 for the negative offset case, which
would then completely screw up the offset_nanoseconds calculation as it
serves as a multiplier.
2022-01-12 21:24:12 +01:00
Timothy Flynn
a121c913c0 LibJS: Add some Intl.DateTimeFormat tests for specific time zones
Now that we can use time zones, let's adds tests for them.
2022-01-12 15:43:12 +01:00
Timothy Flynn
d64ea13565 LibJS: Respect the user-provided time zone in Intl.DateTimeFormat
Also update some DateTimeFormat tests to explicitly set the time zone
(usually to UTC). This was already done for most tests, but some were
missed.
2022-01-12 15:43:12 +01:00
Timothy Flynn
8987deb984 LibJS: Partially implement the LocalTZA AO
Intl.DateTimeFormat will invoke this AO with isUTC=true, so this only
implements that branch in LocalTZA.
2022-01-12 15:43:12 +01:00
Timothy Flynn
f6786881aa LibJS: Implement the ECMA-402 definition of DefaultTimeZone
Simply defer to LibTimeZone to retrieve the system's current time zone.
Also update some Temporal tests to explicitly set the time zone to UTC.
2022-01-12 15:43:12 +01:00
Timothy Flynn
c1a1370c2a LibJS: Use new LibUnicode API to format time zone names 2022-01-11 23:56:35 +01:00
Timothy Flynn
cc5e9f0579 LibJS+LibUnicode: Move replacement of number system digits to LibUnicode
There are a few algorithms in TR-35 that need to replace digits before
returning any results to callers. For example, when formatting time zone
offsets, a string like "GMT+12:34" must have its digits replaced with
the default numbering system for the desired locale.
2022-01-11 23:56:35 +01:00
Linus Groh
355fbcb702 LibJS: Actually implement get_iana_time_zone_offset_nanoseconds()
Instead of hard-coding an UTC offset of zero seconds, which worked for
the sole UTC time zone, we can now get the proper offset from the TZDB!
2022-01-11 22:17:39 +01:00