Commit graph

2564 commits

Author SHA1 Message Date
Linus Groh
b2c6206adb LibJS: Add missing definition of AsyncFunction.prototype.constructor
We also forgot to allocate an AsyncFunctionConstructor and assign it to
m_async_function_constructor during GlobalObject initialization, whoops!
2022-01-19 19:42:45 +00:00
Linus Groh
7d0782f308 LibJS: Remove duplicate definition of AsyncGeneratorFunction.prototype 2022-01-19 19:26:03 +00:00
Nico Weber
e09f74903e LibCrypo: Simplify mixed-sign bitwise_or
No behavior change.
2022-01-18 20:04:06 +03:30
Nico Weber
013799a4dd LibCrypto+LibJS: Better bigint bitwise_or binop
Similar to the bitwise_and change, but we have to be careful to
sign-extend two's complement numbers only up to the highest set bit
in the positive number.
2022-01-18 20:04:06 +03:30
Nico Weber
1f98639396 LibCrypto+LibJS: Better bigint bitwise_and binop
Bitwise and is defined in terms of two's complement, so some converting
needs to happen for SignedBigInteger's sign/magnitude representation to
work out.

UnsignedBigInteger::bitwise_not() is repurposed to convert all
high-order zero bits to ones up to a limit, for the two's complement
conversion to work.

Fixes test262/test/language/expressions/bitwise-and/bigint.js.
2022-01-18 20:04:06 +03:30
Nico Weber
945d962322 LibJS+LibCrypto: Fix SignedBitInteger::bitwise_not and use it in LibJS
Bitwise operators are defined on two's complement, but SignedBitInteger
uses sign-magnitude. Correctly convert between the two.

Let LibJS delegate to SignedBitInteger for bitwise_not, like it does
for all other bitwise_ operations on bigints.

No behavior change (LibJS is now the only client of
SignedBitInteger::bitwise_not()).
2022-01-18 20:04:06 +03:30
Timothy Flynn
444b2d9ec2 LibJS: Implement UTF-16 surrogate pair concatenation without iteration
Performance of string concatenation regressed in a57e2f9. That commit
iterates over the LHS string to find the last code unit, to check if it
is a high surrogate. Instead, first look at the 3rd-to-last byte in the
UTF-8 encoded string to check if it is a 3-byte code point; then decode
just those bytes to check if we have a high surrogate. Similarly, check
the first 3 bytes of the RHS string to check if we have a low surrogate.
2022-01-18 09:46:55 +00:00
Timothy Flynn
a57e2f9a76 LibJS: Combine UTF-16 surrogate pairs when concatenating strings
In the following use case:

    "\ud834" + "\udf06"

We were previously combining these as two individual code points. When
concatenating strings, we must take care to combine the high surrogate
from the left-hand side with the low surrogate from the right-hand side.
2022-01-18 00:49:16 +00:00
Idan Horowitz
27ca7bbbd7 LibJS: Make parseFloat slightly more specification-compliant 2022-01-17 14:46:07 +00:00
Idan Horowitz
de2f5bb5a7 LibJS: Rewrite parseInt to be more specification-compliant 2022-01-17 14:46:07 +00:00
Idan Horowitz
877ae85017 LibJS+LibUnicode: Make static const Utf8View variables constexpr 2022-01-17 14:46:07 +00:00
Idan Horowitz
d5507191a9 LibJS: Implement the TrimString Abstract Operation
This was already implemented and duplicated across the
String.prototype.trim{, Start, End} methods, so this simply extracts it
into a separate method that can also be used by other users.
2022-01-17 14:46:07 +00:00
Idan Horowitz
2297d0b4bf LibJS: Rewrite the Encode AO to handle unpaired UTF-16 surrogates 2022-01-16 19:31:59 +01:00
Idan Horowitz
be9cbd428e LibJS: Check validity of encoded unicode code points in the Decode AO
As required by the specification:
7. If Octets does not contain a valid UTF-8 encoding of a Unicode code
point, throw a URIError exception.
2022-01-16 19:31:59 +01:00
davidot
a5b11f7484 LibJS: Fix that '_' no longer accessed the last value in the REPL
This is now also not a concept that VM knows about and handled
completely by the REPL.
2022-01-16 14:57:12 +01:00
Linus Groh
30af8121ce LibJS: Fix value of Generator.prototype.constructor
The spec says:

    27.5.1.1 Generator.prototype.constructor
    https://tc39.es/ecma262/#sec-generator.prototype.constructor

    The initial value of Generator.prototype.constructor is
    %GeneratorFunction.prototype%.

But we had it set to %GeneratorFunction% (the GeneratorFunction
constructor).
2022-01-16 14:50:22 +01:00
Linus Groh
4ed49e05a9 LibJS: Rename GeneratorObjectPrototype to GeneratorPrototype
Given we usually call objects Foo{Object,Constructor,Prototype} or
Foo{,Constructor,Prototype}, this name was an odd choice.
The new one matches the spec better, which calls it the "Generator
Prototype Object", so we simply omit the Object suffix as usual as it's
implied.
2022-01-16 14:50:22 +01:00
Timothy Flynn
c74f75b910 LibJS: Implement Date.prototype.setUTCDate 2022-01-16 11:07:02 +01:00
Timothy Flynn
0d73da0328 LibJS: Implement Date.prototype.setUTCFullYear 2022-01-16 11:07:02 +01:00
Timothy Flynn
51bc973ecc LibJS: Implement Date.prototype.setUTCHours 2022-01-16 11:07:02 +01:00
Timothy Flynn
6998c0a796 LibJS: Implement Date.prototype.setUTCMilliseconds 2022-01-16 11:07:02 +01:00
Timothy Flynn
2f202e8ef4 LibJS: Implement Date.prototype.setUTCMinutes 2022-01-16 11:07:02 +01:00
Timothy Flynn
f2ffe3bf90 LibJS: Implement Date.prototype.setUTCMonth 2022-01-16 11:07:02 +01:00
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