Commit graph

627 commits

Author SHA1 Message Date
Daniel Bertalan
c6fafd3e90 AK+Userland: Add generic AK::abs() function and use it
Previously, in LibGFX's `Point` class, calculated distances were passed
to the integer `abs` function, even if the stored type was a float. This
caused the value to unexpectedly be truncated. Luckily, this API was not
used with floating point types, but that can change in the future, so
why not fix it now :^)

Since we are in C++, we can use function overloading to make things
easy, and to automatically use the right version.

This is even better than the LibC/LibM functions, as using a bit of
hackery, they are able to be constant-evaluated. They use compiler
intrinsics, so they do not depend on external code and the compiler can
emit the most optimized code by default.

Since we aren't using the C++ standard library's trick of importing
everything into the `AK` namespace, this `abs` function cannot be
exported to the global namespace, as the names would clash.
2021-07-08 10:11:00 +02:00
Idan Horowitz
eeb4c1eec9 LibJS: Reorder and add missing name & length properties to Built-ins
The specification dicatates that each built-in will have a length and
name property. (defined in that order)
2021-07-08 01:45:15 +01:00
Linus Groh
7e4b0681e1 LibJS: Implement Date.prototype.toTemporalInstant() 2021-07-08 01:25:49 +01:00
Linus Groh
cc64efac44 LibJS: Split out NumberToBigInt from the BigInt constructor
This is supposed to be its own AO, but since it was only used in one
place, we inlined it. Now that it's also being used in the Temporal
proposal (Date.prototype.toTemporalInstant() specifically), it makes
sense to have it as a standalone function.
A small difference is that we now construct the SignedBigInteger without
casting to i32 but instead take the (known to be integral) double and
cast it to i64. Not perfect, but slightly better.
Also clean up the BigInt constructor a bit while we're here and sprinkle
some spec comments.
2021-07-08 01:25:49 +01:00
Timothy Flynn
35a2ba8ed8 LibJS: Implement RegExp.prototype [ @@search ]
String.prototype.search is already implemented, but relies on the well-
known Symbol.search, which was not implemented.
2021-07-08 00:01:20 +01:00
Timothy Flynn
aaf5339fae LibJS: Do not downcast from Object to RegExpObject unless needed
Several test262 tests rely on creating phony RegExp objects that do not
have the internal slots used to test for a valid RegExp object. To allow
these tests to run (and because the spec doesn't require real RegExp
objects in these methods), do not attempt to downcast where it isn't
needed.
2021-07-08 00:01:20 +01:00
Timothy Flynn
ce2651a320 LibJS: Remove RegExp 'do_match' helper
This was previously used as a wrapper for Regex::match when that method
was invoked by multiple RegExp.prototype implementations. But now all
implementations go through the RegExpExec abstraction, so this helper
is not needed. Remove it to discourage its usage.

Also update a comment about using dynamic properties for lastIndex; this
is no longer a FIXME.
2021-07-08 00:01:20 +01:00
Timothy Flynn
2d0589f93c LibJS: Implement global RegExp.prototype.match
Also rename the 'rx' variable to 'regexp_object' to match other RegExp
methods.
2021-07-08 00:01:20 +01:00
Timothy Flynn
b6b5adb47d LibJS: Implement RegExp.prototype.match with RegExpExec abstraction 2021-07-08 00:01:20 +01:00
Timothy Flynn
ec898a3370 LibJS: Implement RegExp.prototype.replace with RegExpExec abstraction
Also rename the 'rx' variable to 'regexp_object' to match other RegExp
methods.
2021-07-08 00:01:20 +01:00
Timothy Flynn
6c53475143 LibJS: Implement RegExp.prototype.test with RegExpExec abstraction 2021-07-08 00:01:20 +01:00
Timothy Flynn
a90d5cb622 LibJS: Extract RegExp.prototype.exec to RegExpExec and RegExpBuiltinExec
The RegExp specification dictates that the internal implementation of
RegExp.prototype.exec must go through the RegExpBuiltinExec abstraction.

Note there is currently no functional difference in this commit. However
this now allows other RegExp.prototype methods to use RegExpExec rather
than calling RegExp.prototype.exec. Then, if JavaScript in the wild has
overwritten exec, RegExpExec has some protections to defer to
RegExpBuiltinExec.
2021-07-08 00:01:20 +01:00
Hendi
0dc4e722e6 LibJS: Make FunctionExpression more spec-compliant 2021-07-07 23:31:51 +01:00
Idan Horowitz
795786387b LibJS: Remove the NativeProperty mechanism from LibJS
These were an ad-hoc way to implement special behaviour when reading or
writing to specific object properties. Because these were effectively
replaced by the abillity to override the internal methods of Object,
they are no longer needed.
2021-07-07 21:47:22 +01:00
Idan Horowitz
306d59276a LibJS: Stop using a native property for RegExp's lastIndex property
This is not a functional change, the exposed (incorrect) behaviour is
the same as it was before, this simply removes the last user of
NativeProperties, allowing us to remove them completely from LibJS.
2021-07-07 21:47:22 +01:00
davidot
ae8c4618b7 LibJS: Use as_object instead of as_array in flatten_into_array
Since is_array does not guarantee that it is<Array> we must use
as_object here.
2021-07-07 21:24:26 +01:00
davidot
4846c4a94e LibJS: Fix types and small spec discrepancy in ArrayPrototype::fill 2021-07-07 21:24:26 +01:00
davidot
6c47b77998 LibJS: Fix types and small spec discrepancy in ArrayPrototype::splice 2021-07-07 21:24:26 +01:00
davidot
7a41c758c0 LibJS: Fix types and small spec discrepancy in ArrayPrototype::includes 2021-07-07 21:24:26 +01:00
davidot
cb44fc528b LibJS: Add a way of constructing PropertyName with values above 2**32-1
This is often needed in ArrayPrototype when getting items with indices
above 2**32-1 is possible since length is at most 2**53-1.
This also fixes a number of these cases in ArrayPrototype where the type
was not big enough to hold the potential values.
2021-07-07 21:24:26 +01:00
davidot
a70033481d LibJS: Fix that length was sometimes cast to [ui]32
Since array-like objects can have much larger lengths even a u32 is not
sufficient.
2021-07-07 21:24:26 +01:00
davidot
7310713d11 LibJS: Remove fast array paths in ArrayPrototype::{pop, push}
Unfortunately this fast path leads to problems if Array.prototype is
changed. We probably need to find out some way to optimize these methods
by detecting changes to the prototype or other mechanisms.
2021-07-07 21:24:26 +01:00
Linus Groh
b180e154aa LibJS: Add thousands separators to nanoseconds multiplier value
Increases readability. Thanks to @nico for noticing this!
2021-07-07 20:01:08 +01:00
Linus Groh
3a39ff8f40 LibJS: Implement Temporal.now.instant() 2021-07-07 19:00:42 +01:00
Linus Groh
47fb4286c7 LibJS: Start implementing Temporal.Instant
Just like the initial Temporal.TimeZone commit, this patch adds the
Instant object itself, its constructor and prototype (currently empty),
and two required abstract operations.
2021-07-07 19:00:42 +01:00
Linus Groh
d9cff591b6 LibJS: Add error message for invalid time zone in Temporal.TimeZone() 2021-07-07 19:00:42 +01:00
Idan Horowitz
c351b4ad0d LibJS: Stop using a native property for Array lengths
Specifically, replace it with a specification-based implementation that
overrides the internal methods that interact with the length property
instead.
2021-07-07 10:14:44 +01:00
Idan Horowitz
fff112c8a3 LibJS: Add missing spec link to ValidateAndApplyPropertyDescriptor 2021-07-07 10:14:44 +01:00
Idan Horowitz
dd27490ee1 LibJS: Throw if the trap result of OwnPropertyKeys contains duplicates 2021-07-07 01:38:10 +01:00
Idan Horowitz
d577678658 LibJS: Add a name property to the proxy revoker anonymous function
As per the specification this should an empty string.
2021-07-07 01:38:10 +01:00
Linus Groh
6cd16eceb3 LibJS: Implement Temporal.now.timeZone() 2021-07-07 00:42:01 +01:00
Linus Groh
265e89367e LibJS: Start implementing Temporal.TimeZone
Here we got our first Temporal object :^)
This patch adds the TimeZone object itself, its constructor and
prototype (currently empty), and a bunch of required abstract operations
2021-07-07 00:42:01 +01:00
Linus Groh
6735353b96 LibJS: Add preparation for Temporal constructors and prototypes
Add a JS_ENUMERATE_TEMPORAL_OBJECTS macro and use it to generate:

- Forward declarations
- CommonPropertyNames class name members
- Constructor and prototype GlobalObject members, getters, visitors,
  and initialize_constructor() calls
2021-07-07 00:42:01 +01:00
Linus Groh
7da1fcb2ef LibJS: Add the Temporal.now namespace object
This will be home to various functions:

- Temporal.now.timeZone()
- Temporal.now.instant()
- Temporal.now.plainDateTime()
- Temporal.now.plainDateTimeISO()
- Temporal.now.zonedDateTime()
- Temporal.now.zonedDateTimeISO()
- Temporal.now.plainDate()
- Temporal.now.plainDateISO()
- Temporal.now.plainTimeISO()
2021-07-07 00:42:01 +01:00
Linus Groh
8269921212 LibJS: Add the Temporal namespace object :^)
Currently empty, but we gotta start somewhere! This is the start of
implementing the Temporal proposal (currently stage 3).

I have decided to start a new subdirectory (Runtime/Temporal/) as well
as a new C++ namespace (JS::Temporal) for this so we don't have to
prefix all the files and classes with "Temporal" - there will be a lot.

https://tc39.es/proposal-temporal/
2021-07-07 00:42:01 +01:00
Timothy Flynn
e0d26fff8c LibJS: Replace strings with the search value coerced to a string
This only causes 1 new test262 test to pass. Other tests that rely on
this coercion fail due to receiving an unexpected value for 'this' when
invoking a functional replacement. For example:

    String/prototype/replaceAll/replaceValue-call-matching-empty.js

Receives 'undefined' for 'this' in the functional replacement invocation
but is expected to receive the global 'this'.
2021-07-06 22:33:17 +01:00
Timothy Flynn
81fec49ac3 LibJS: Evaluate replacement value before searching source string
The String.prototype.replace spec requires evaluating the replacement
value (if it is not a function) before searching the source string.

Fixes 4 test262 tests.
2021-07-06 22:33:17 +01:00
Linus Groh
24c490c520 LibJS: Remove unused Lexer/Parser includes from GlobalObject.cpp 2021-07-06 18:41:24 +01:00
Idan Horowitz
5e621e494f LibJS: Implement Object.getOwnPropertyDescriptors() 2021-07-06 18:41:15 +01:00
Idan Horowitz
0f91883b17 LibJS: Rename ObjectConstructor::{define_property_ => define_property}
As the non-standard helper define_property is now removed, this doesnt
clash with it anymore.
2021-07-06 18:41:15 +01:00
Linus Groh
83f61748a5 LibJS: Don't treat 2^32 - 1 as numeric PropertyName
10.4.2 Array Exotic Objects
    https://tc39.es/ecma262/#sec-array-exotic-objects

    A String property name P is an array index if and only if
    ToString(ToUint32(P)) equals P and ToUint32(P) is not the same value
    as 𝔽(2^32 - 1).
2021-07-06 17:29:12 +01:00
Linus Groh
47bd25a2f1 LibJS: Make Value::as_u32() slightly less broken
Still a horrible mess, but at least it can actually return numbers > i32
max now.
2021-07-06 17:29:12 +01:00
Linus Groh
30fe0529bd LibJS: Fix second argument passed to Proxy [[Call]] trap (thisArgument) 2021-07-06 17:29:12 +01:00
Timothy Flynn
8fcdc57ae1 LibJS: Coerce named captures to an object before calling GetSubstitution
Per the spec, before invoking the GetSubstitution abstraction, the named
capture groups (if not undefined) should be coerced to an object via the
ToObject abstraction.
2021-07-06 15:07:26 +01:00
Timothy Flynn
424c7eaa40 LibJS: Fix replaceAll crash for overlapping search string positions
The implementation of String.prototype.replaceAll cannot use AK's
implementation of String::find_all when finding the indices of the
search string in the source string. String::find_all will return indices
[0, 1] for String("aaa").find_all("aa") - i.e. it returns overlapping
results. This is not allowed by the JavaScript specification for
replaceAll.
2021-07-06 15:07:26 +01:00
Linus Groh
30615ac2bb LibJS: Do s/define_property/define_direct_property/ in a comment 2021-07-06 14:36:00 +01:00
Linus Groh
0ba81dc0b7 LibJS: Remove Object::is_array() in favor of Value::is_array() and RTTI
It's way too easy to get this wrong: for the IsArray abstract operation,
Value::is_array() needs to be called. Since we have RTTI, the virtual
Object::is_array() method is not needed anymore - if we need to know
whether something is *actually* a JS::Array (we currently check in more
cases than we should, I think) and not a Proxy with an Array target, we
should do that in a way that doesn't look like an abstract operation.
2021-07-06 14:26:18 +01:00
Idan Horowitz
e3ef241108 LibJS: Remove the non-standard put helper and replace it's usages
This removes all usages of the non-standard put helper method and
replaces all of it's usages with the specification required alternative
or with define_direct_property where appropriate.
2021-07-06 14:20:30 +01:00
Idan Horowitz
53f70e5208 LibJS: Remove the default length & attributes from define_native_*
These are usually incorrect, and people sometimes forget to add the
correct values as a result of them being optional, so they should just
be specified explicitly.
2021-07-06 14:20:30 +01:00
Idan Horowitz
a6b8291a9b LibJS: Add define_direct_property and remove the define_property helper
This removes all usages of the non-standard define_property helper
method and replaces all it's usages with the specification required
alternative or with define_direct_property where appropriate.
2021-07-06 14:20:30 +01:00