Commit graph

165 commits

Author SHA1 Message Date
Lenny Maiorani
d00b79568f Libraries: Use default constructors/destructors in LibJS
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-16 16:19:40 +00:00
Idan Horowitz
08d1ae58b1 LibJS: Initialize {Async,}{Generator,}Function constructors properly
We were previously manually initializing them instead of just calling
GlobalObject::initialize_constructor, which aside from duplicating code
also meant we didn't set the required name property.
2022-02-19 18:06:39 +00:00
Idan Horowitz
6c26a02aa8 LibJS: Start implementing Intl Segment Iterator objects 2022-01-30 19:47:01 +00:00
Idan Horowitz
bbacea255f LibJS: Start implementing Intl Segments objects 2022-01-30 19:47:01 +00:00
Idan Horowitz
a3bc06bb23 LibJS: Start implementing Intl.Segmenter 2022-01-30 19:47:01 +00:00
davidot
202de6ed25 LibJS: Expose JSON.parse as an intrinsic value of the global object
This will allow us to safely call it if we need to parse JSON within
LibJS.
2022-01-30 17:40:20 +00:00
Idan Horowitz
fe687412a6 LibJS: Visit m_async_from_sync_iterator_prototype in GlobalObject
This prevents random crashes in for async loops due to GC.
2022-01-29 22:35:43 +00:00
Timothy Flynn
4a3e142d55 LibJS: Implement a nearly empty Intl.Collator object
This adds plumbing for the Intl.Collator object, constructor, and
prototype.
2022-01-29 20:27:24 +00:00
Timothy Flynn
0087804d10 LibJS: Implement a nearly empty Intl.PluralRules object
This adds plumbing for the Intl.PluralRules object, constructor, and
prototype.
2022-01-28 19:38:47 +00:00
Timothy Flynn
79fdec85de LibJS: Implement a nearly empty Intl.RelativeTimeFormat object
This adds plumbing for the Intl.RelativeTimeFormat object, constructor,
and prototype.
2022-01-25 19:02:59 +00:00
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
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
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
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
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
Nick Johnson
08e4a1a4dc AK+Everywhere: Replace __builtin bit functions
In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
2021-12-21 22:13:51 +01:00
Timothy Flynn
4cc8cf9233 LibJS: Add an accessor for the %Date.now% instrinsic property 2021-12-08 11:29:36 +00:00
Timothy Flynn
75b2a09a2f LibJS: Implement a nearly empty Intl.DateTimeFormat object
This adds plumbing for the Intl.DateTimeFormat object, constructor, and
prototype.

Note that unlike other Intl objects, the Intl.DateTimeFormat object has
a LibUnicode structure as a base. This is to prevent wild amounts of
code duplication between LibUnicode, Intl.DateTimeFormat, and other
not-yet-defined Intl structures, because there's 12 fields shared
between them.
2021-11-29 22:48:46 +00:00
davidot
064c8be627 LibJS: Add AsyncFromSyncIteratorPrototype and Async-From-Sync instances
Until we have actual iterator records we have to store the sync iterator
as the raw object.
2021-11-29 15:20:07 +00:00
davidot
7fd38eac98 LibJS: Add AsyncIteratorPrototype 2021-11-29 15:20:07 +00:00
davidot
0982a73d1d LibJS: Parse async generator functions 2021-11-21 21:46:39 +00:00
Luke Wilde
3666d2132b LibJS: Remove fallback value for get_offset_nanoseconds_for
This is a normative change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/664f02d

Note that the tests are not comprehensive.
2021-11-17 11:30:13 +00:00
Idan Horowitz
681787de76 LibJS: Add support for async functions
This commit adds support for the most bare bones version of async
functions, support for async generator functions, async arrow functions
and await expressions are TODO.
2021-11-10 08:48:27 +00:00
Timothy Flynn
e92d14d498 LibJS: Convert GlobalObject to ThrowCompletionOr 2021-10-31 07:50:30 +02:00
Timothy Flynn
875b563664 LibJS: Convert GlobalObject's Encode and Decode AOs to ThrowCompletionOr 2021-10-31 07:50:30 +02:00
Idan Horowitz
40eb3a39d4 LibJS: Rename define_native_function => define_old_native_function
This method will eventually be removed once all native functions are
converted to ThrowCompletionOr
2021-10-20 12:27:19 +01:00
Idan Horowitz
ca27e5eff5 LibJS: Convert NativeFunction callback to ThrowCompletionOr 2021-10-20 12:27:19 +01:00
Idan Horowitz
20163c0584 LibJS: Add ThrowCompletionOr versions of the JS native function macros
The old versions were renamed to JS_DECLARE_OLD_NATIVE_FUNCTION and
JS_DEFINE_OLD_NATIVE_FUNCTION, and will be eventually removed once all
native functions were converted to the new format.
2021-10-20 12:27:19 +01:00
Idan Horowitz
f6a5ff7b00 LibJS: Convert to_i32() to ThrowCompletionOr 2021-10-18 08:01:38 +03:00
Idan Horowitz
20d990563c LibJS: Convert to_number() to ThrowCompletionOr 2021-10-18 08:01:38 +03:00
Linus Groh
cbbf4abb0d LibJS: Add a way to get from a GlobalObject to its associated Realm
This is just another workaround, but it should be much more reliable
than Interpreter::realm(), especially when allocating NativeFunctions
and ECMAScriptFunctionObjects: we're guaranteed to have a GlobalObject
at that point, and it likely was set as the GlobalObject of a Realm and
can lead us back to it. We're however not guaranteed that the VM can
give us an Interpreter, which is why functions in LibWeb can be a bit
crashy at the moment.

We use a WeakPtr<Realm> to properly handle the unlikely case where the
Realm goes away after associating a GlobalObject to it.

We'll always need _something_ of this sort if we want to support
OrdinaryFunctionCreate and CreateBuiltinFunction without the explicit
realm argument while no JS is running, because they want to use the
current Realm Record (always in the first and as fallback in the second
case).
2021-10-14 23:02:19 +01:00
Linus Groh
d40331ef69 LibJS: Start implementing ShadowRealm
This commit adds the ShadowRealm object itself, its constructor, and
prototype (currently empty).
2021-10-14 00:41:41 +01:00
Linus Groh
4d8912a92b LibJS: Convert to_string() to ThrowCompletionOr
Also update get_function_name() to use ThrowCompletionOr, but this is
not a standard AO and should be refactored out of existence eventually.
2021-10-13 09:55:10 +01:00
Linus Groh
4d5bd092ea LibJS: Use MUST() where applicable 2021-10-03 20:14:03 +01:00
Linus Groh
ba6e4c7ae1 LibJS+LibWeb: Use Object::set_prototype() in more places 2021-10-02 14:53:06 +01:00
Andreas Kling
14c57b4b7f LibJS: Remove transition avoidance & start caching prototype transitions
The way that transition avoidance (foo_without_transition) was
implemented led to shapes being unshareable and caused shape explosion
instead, precisely what we were trying to avoid.

This patch removes all the attempts to avoid transitioning shapes, and
instead *adds* transitions when changing an object's prototype.
This makes transitions flow naturally, and as a result we end up with
way fewer shape objects in real-world situations.

When we run out of big problems, we can get back to avoiding transitions
as an optimization, but for now, let's avoid ballooning our processes
with a unique shape for every object.
2021-10-01 02:59:29 +02:00
Linus Groh
73bae7d779 LibJS: Convert internal_prevent_extensions() to ThrowCompletionOr 2021-09-29 23:49:53 +01:00
Linus Groh
8c81c84c18 LibJS: Convert internal_set_prototype_of() to ThrowCompletionOr 2021-09-29 23:49:53 +01:00
Idan Horowitz
1da8faebf5 LibJS: Convert perform_eval to ThrowCompletionOr 2021-09-21 23:28:38 +03:00
Linus Groh
f29a82dd84 LibJS: Move the GlobalEnvironment from GlobalObject to Realm
This is where the spec wants to have it. Requires a couple of hacks as
currently everything that needs a Realm actually has a GlobalObject, so
we need to go via the Interpreter.
2021-09-12 11:10:20 +01:00
Linus Groh
1e79934acf LibJS: Add [[GlobalThisValue]] internal slot to GlobalEnvironment
Instead of hardcoding the environment's global object as the return
value of GlobalEnvironment::global_this_value(), it now stores an Object
reference which is passed to the constructor for this purpose.

From the spec (https://tc39.es/ecma262/#sec-global-environment-records):

[[GlobalThisValue]] | Object | The value returned by this in global
scope. Hosts may provide any ECMAScript Object value.
2021-09-12 11:10:20 +01:00
Timothy Flynn
07f12b108b LibJS: Implement a nearly empty Intl.NumberFormat object
This adds plumbing for the Intl.NumberFormat object, constructor, and
prototype.
2021-09-11 11:05:50 +01:00
Timothy Flynn
8e75e5fabb LibJS: Implement a nearly empty Intl.ListFormat object
This adds plumbing for the Intl.ListFormat object, constructor, and
prototype.
2021-09-06 23:49:56 +01:00
Timothy Flynn
2c10e9fdd3 LibJS: Implement a nearly empty Intl.Locale object
This adds plumbing for the Intl.Locale object, constructor, and
prototype.
2021-09-02 17:56:42 +01:00
Linus Groh
8bcda353a2 LibJS: Avoid transitions for more object properties in global object 2021-08-28 23:17:01 +01:00
Linus Groh
222e518a53 LibJS: Avoid pointless transitions and metadata lookups in storage_set()
- Replace the misleading abuse of the m_transitions_enabled flag for the
  fast path without lookup with a new m_initialized boolean that's set
  either by Heap::allocate() after calling the Object's initialize(), or
  by the GlobalObject in its special initialize_global_object(). This
  makes it work regardless of the shape's uniqueness.
- When we're adding a new property past the initialization phase,
  there's no need to do a second metadata lookup to retrieve the storage
  value offset - it's known to always be the shape's property count
  minus one. Also, instead of doing manual storage resizing and
  assignment via indexing, just use Vector::append().
- When we didn't add a new property but are overwriting an existing one,
  the property count and therefore storage value offset doesn't change,
  so we don't have to retrieve it either.

As a result, Object::set_shape() is now solely responsible for updating
the m_shape pointer and is not resizing storage anymore, so I moved it
into the header.
2021-08-28 23:17:01 +01:00
Timothy Flynn
0fb4e8b749 LibJS: Implement a nearly empty Intl.DisplayNames object
This adds plumbing for the Intl.DisplayNames object, constructor, and
prototype.
2021-08-26 22:04:09 +01:00