Commit Graph

5417 Commits

Author SHA1 Message Date
rmg-x
81d786d9e1 LibJS: Parse dates like "Wed Nov 20 2024"
(cherry picked from commit 74b27d620daa80f94e67e6a76a83149c7266c710)
2024-11-20 22:51:34 -05:00
Luke Wilde
a57f1cde9f LibJS: Perform TLA async function construction in the module context
Previously it was only pushing the module context for the call to
capture the module execution context. This is incorrect, as the capture
occurs upon function construction. This resulted in it capturing the
execution context that execute_module was called from, instead of the
newly created module_context.
f87041bf3a/Libraries/LibJS/Runtime/ECMAScriptFunctionObject.cpp (L92)

This can be demonstrated with the following setup:
index.html:
```html
<script>
    var foo = 1;
</script>
<script type="module">
    import {test} from "./scriptA.mjs";
</script>
```

scriptA.mjs:
```js
function foo() {
	return {a: "b"};
}
export let test = await foo();
```

Before this fix, this would throw:
```
[TypeError] 1 is not a function (evaluated from 'foo')
    at module code with top-level await
    at module code with top-level await
    at <unknown>
    at <unknown>
```

Fixes #2245.

(cherry picked from commit 6319dedbcd4d6e448b4cc2615e62eba5ac576887)
2024-11-15 23:09:44 -05:00
Shannon Booth
7dc49c9038 LibJS: Align ShadowRealmConstructor closer with latest spec
The current shadow realm constructor implementation was based off a
merge request to the shadow realm proposal for integration into the
web platform. However, this merge request had a bug which we had
applied a workaround for by popping off the execution stack.

Since then, the spec has had an update of:

https://github.com/tc39/proposal-shadowrealm/commit/28b0cc

Which closer aligned the mainline spec to the proposed merge request.

Now, the original shadow realm proposal merge request has been closed
and a new one has been reopened with a much more minimal set of changes
that merely adds extra arguments to HostInitializeShadowRealm, which
this commit aligns with.

(cherry picked from commit 424a0cda936e018a9881f3e5898beb79d2212caa)
2024-11-13 10:17:34 -05:00
Shannon Booth
6f48342e3f LibJS: Update ShadowRealm to not hold an ExecutionContext
Noone needs to use this any more :^)

This is somewhat AD-HOC in the constructor as it is based on an open
shadow realm merge request, but applies the intent of the change without
any change in behaviour.

(cherry picked from commit b927d7f65870c94d0c7ead3f6bdd8e6f6f0ac887)
2024-11-13 10:17:34 -05:00
Shannon Booth
2fedb5a87e LibJS: Use GetShadowRealmContext for PerformShadowRealmEval
(cherry picked from commit 3cb62265ab203d652a3fffab431f842fc46d375c)
2024-11-13 10:17:34 -05:00
Shannon Booth
93732e2ca3 LibJS: Use GetShadowRealmContext for ShadowRealmImportValue
Removing one user of the ExecutionContext slot of ShadowRealm.

(cherry picked from commit 6a55865bf61ba0ad8aebd1b72ea0be82d849600d)
2024-11-13 10:17:34 -05:00
Shannon Booth
b0ac3039f2 LibJS: Implement ShadowRealm AO GetShadowRealmContext
This is a new AO introduced in the spec which allows the
[[ExecutionContext]] slot of the ShadowRealm object to be removed.

(cherry picked from commit 3f24008b31d36c41c3bb841312914019073fac33)
2024-11-13 10:17:34 -05:00
Shannon Booth
5ce612166f LibJS: Update HostEnsureCanCompileStrings arguments to latest spec
(cherry picked from commit c1998f96c29f04cdad822e14867c3a7ffb6cf4b3)
2024-11-13 10:17:34 -05:00
Shannon Booth
10f2c11f3b LibJS: Update CreateDynamicFunction to latest spec
The use of extract_parameter_arguments_and_body() here is to make things
a little less awkward. If we were to exactly follow spec there would be
an awkward handling of the case that no arguments were provided and we
needed to provide an empty string.

To do this, we would need to either:
  - Provide an Optional<Value> for bodyString to CreateDynamicFunction
  - Create a new empty PrimitiveString wrapped in a JS Value.

Either case is somewhat awkward. Instead, just refactor this logic
outside of CreateDynamicFunction and make the caller do it.

Otherwise, this commit prepares for the new definition of
HostEnsureCanCompileStrings.

(cherry picked from commit 6da0ac3aa71c991e18e96d721b1843c417cdab78)
2024-11-13 10:17:34 -05:00
Pavel Shliak
209df06ab7 LibJS: Add calendar id getter to ZonedDateTimePrototype
(cherry picked from commit ba71cb1ca43738096a1c93ea02904e7eaa4012dd)
2024-11-13 10:17:34 -05:00
Pavel Shliak
404b965b2e LibJS: Add calendar id getter to PlainMonthDayPrototype
(cherry picked from commit 2ad48b64caaed0e1b4bec6819d71a20a2a184bad)
2024-11-13 10:17:34 -05:00
Pavel Shliak
f6865454ae LibJS: Add calendar id getter to PlainYearMonthPrototype
(cherry picked from commit 0e1e8c908e59766ceafbdcbb6dd07ff0e06c5632)
2024-11-13 10:17:34 -05:00
Pavel Shliak
556ba1edd1 LibJS: Add calendar id getter to PlainDateTimePrototype
(cherry picked from commit 44fa8410c0dfac07ac1b293eab120d467ef8461a)
2024-11-13 10:17:34 -05:00
Pavel Shliak
dded35e06d LibJS: Add calendar id getter to PlainDatePrototype
(cherry picked from commit f7cf7382c9b4502b4292ed6aa592cfe4727ec9cf)
2024-11-13 10:17:34 -05:00
Pavel Shliak
879da0b7e7 LibJS: Adds calendar_id identifier
(cherry picked from commit e60c1ddd4d5259281cd71017f7d3a89eea99b507)
2024-11-13 10:17:34 -05:00
Hermes Junior
34ae4521ff LibJS: Correctly return cached value for global var bindings
When the cached value was not an accessor, it was simply ignored.
This is the value we really want, so we can just return it.
Shows up to 5x improvements on some benchmarks,
and 1.4x in general js-benchmarks.

(cherry picked from commit 77a46ab1b8d13f35b11a7bd08459752912dca1bf)
2024-11-12 19:57:55 -05:00
Andreas Kling
7b2568bc9a LibJS: Stop having AsyncFunctionDriverWrapper leak itself
Async functions whose promise is never resolved were leaking, since they
had a strong root JS::Handle on themselves.

This doesn't appear to actually be necessary, since the wrapper will be
kept alive as long as it's reachable (and if it's not reachable, nobody
is going to resolve/reject the promise either).

This fixes the vast majority of leaks on Speedometer, bringing memory
usage at the end of a full run from ~12 GiB to ~3 GiB.

(cherry picked from commit b6a5b7e18626ce0dee11a25bb0d51d2e84d3c057)
2024-11-12 19:57:55 -05:00
Andreas Kling
8486d6e8aa LibJS: Use ConservativeVector when instantiating static class fields
This fixes an issue where a badly-timed garbage collection could swallow
a static field initializer.

Caught by running test262 in GC-on-every-allocation mode.

(cherry picked from commit 10724a7cb346e57b0b97e4bd54c13c5c604dbf9c)
2024-11-12 19:57:55 -05:00
Andreas Kling
1c7e69a5b5 LibJS: Make ConservativeVector<T> visit all possible values
We were miscalculating the length of the buffer in pointer-sized chunks,
which is what the conservative root scan cares about.

This could cause some values to be prematurely garbage-collected.

(cherry picked from commit 2fb3b6c542db3a86cbd1aa7e34d470e34c485372)
2024-11-12 19:57:55 -05:00
Luke Wilde
2c0e59c402 LibJS: Parse dates like "November 19 2024 00:00:00 +0900"
This format is used on https://jojowiki.com/ to show countdowns to new
releases.

(cherry picked from commit 956b279ae1f6ee9f76ff1120a759ab9d86218687)
2024-11-12 19:57:55 -05:00
Jonne Ransijn
5d6ef182f4 LibJS+LibWeb: Prevent double invocation of [[GetOwnProperty]]
The `[[GetOwnProperty]]` internal method invocation in
`OrdinarySetWithOwnDescriptor` was being invocated again with the same
parameters in the `[[DefineOwnProperty]]` internal method that is also
later called in `OrdinarySetWithOwnDescriptor`.

The `PlatformObject.[[DefineOwnProperty]]` has similair logic.

This change adds an optional parameter to the `[[DefineOwnProperty]]`
internal method so the results of the previous `[[GetOwnProperty]]`
internal method invocation can be re-used.

(cherry picked from commit 69f96122b6150c22d1e8dc848c097cead2d2ae3f)
2024-11-12 07:14:25 -05:00
Andreas Kling
5cd1016714 LibJS: Add RawNonnullGCPtr<T>
This is really just a type alias for NonnullGCPtr<T>, but it provides
a way to have non-owning non-visited NonnullGCPtr<T> without getting
yelled at by the Clang plugin for catching GC errors.

(cherry picked from commit 6a6618f5eab6e6a2c7c3fc03f8063cc6b497a0ec)
2024-11-11 17:22:05 -05:00
Andreas Kling
3d36c75eee LibJS: Use correct cell address for HeapFunction captures in GC dumps
We were previously dumping the address of the cell pointer instead of
the address of the cell itself. This was causing mysterious orphans
in GC dumps, and it took me way too long to figure this out.

(cherry picked from commit e240084437ea0bfe03d83dc3533d4ee6214b7206)
2024-11-11 17:22:05 -05:00
Luke Wilde
c7f707afa6 LibJS: Allow division after IdentifierNames in optional chain
The following syntax is valid:
```js
e?.example / 1.2
```

Previously, the `/` would be treated as a unterminated regex literal,
because it was calling the regular `consume` instead of
`consume_and_allow_division`.

This is what is done when parsing IdentifierNames in
parse_secondary_expression when a period is encountered.

Allows us to parse clients-main-[hash].js on https://ubereats.com/

(cherry picked from commit bd4c29322c945647c52ff4d8045c7529f8152b08)
2024-11-11 17:22:05 -05:00
Jonne Ransijn
201d36665b LibJS: Remember the position into the cached indices
There is no need to do a full linear search from start to end when
we can just remember the position and continue where we left off.

(cherry picked from commit f4e24762846cfb7a98054f700319d940173086bb)
2024-11-11 10:11:22 -05:00
Christoffer Sandberg
e40ddaff92 LibJS: Count code-points instead of bytes for syntax highlight
This fixes issue #1847, a crash on view-source
for https://chalmers.se

(cherry picked from commit 84c881fc66975bdbfc8fa6d139bc38f6ec37005b)
2024-11-11 10:11:22 -05:00
Andreas Kling
e9f9716ef6 LibJS: Parse dates like "Tuesday, October 29, 2024, 18:00 UTC"
This format is used on https://jetbrains.com/

(cherry picked from commit 3180df3337cc319cdc30b33b70a19b3aafa5642b)
2024-11-11 10:11:22 -05:00
samu698
980de1b160 LibJS: Don't generate useless jumps for if statement
If statements without an else clause generated jumps to the next
instruction, this commit fixes the if statement generation so that it
dosen't produce them anymore.

This is an example of JS code that generates the useless jumps
(a => if(a){}) ();

(cherry picked from commit 7865fbfe6d04a79a253a38eaec5c21c2bde110b5)
2024-11-11 10:11:22 -05:00
Andreas Kling
37c9b1ce7b LibJS: Store RegExp flags as a bitmask
This avoids having to do O(n) contains() in the various flag accessors.

Yields a ~20% speed-up on the following microbenchmark:

    const re = /foo/dgimsvy;
    for (let i = 0; i < 1_000_000; ++i)
        re.flags;

(cherry picked from commit 257ebea3645ab709be4a984100cc6478b289d0e6)
2024-11-11 10:11:22 -05:00
Jonne Ransijn
c1acf77d6b LibJS: Add HashMap for finding Bindings by name
`find_binding_and_index` was doing a linear search, and while most
environments are small, websites using JavaScript bundlers can have
functions with very large environments, like youtube.com, which has
environments with over 13K bindings, causing environment lookups to
take a noticeable amount of time, showing up high while profiling.

Adding a HashMap significantly increases performance on such websites.

(cherry picked from commit 78ecde9923e954e8ae9bb8d7a8ceefc08a8130ae)
2024-11-11 10:11:22 -05:00
Timothy Flynn
68940f0d37 LibJS: Update spec numbers for the Iterator Helpers proposal
This proposal has reached stage 4 and was merged into the ECMA-262 spec.
See: https://github.com/tc39/ecma262/commit/961f269

(cherry picked from commit 84ad36de0692b8890a2aa7ab66ed4d679cf630c8;
amended to replace one ASSERT() with VERIFY())
2024-11-11 10:11:22 -05:00
Timothy Flynn
391797ca52 LibJS: Close iterator records inside the Iterator{Next,Step} AOs
This is an editorial change in the ECMA-262 spec. See:
https://github.com/tc39/ecma262/commit/c4c55b6

(cherry picked from commit 896c2e2f0f08d20770e63055225b627cd4969241)
2024-11-11 10:11:22 -05:00
Timothy Flynn
c2d206a9e0 LibJS: Update spec numbers for the Promise.try proposal
This proposal has reached stage 4 and was merged into the ECMA-262 spec.
See: https://github.com/tc39/ecma262/commit/d72630f

(cherry picked from commit 3aca12d2fadca1628a4dd2f800050b4e54194d65)
2024-11-11 10:11:22 -05:00
Shannon Booth
7816e173fa LibJS: Update comments for WrappedFunction [[Call]] implementation
(cherry picked from commit 01c2ecf355868f7c8d3d757488e7d20b039fcc61)
2024-11-11 09:14:29 -05:00
Shannon Booth
4696edae82 LibJS: Return void from SetDefaultGlobalBindings
Aligning with spec change:

https://github.com/tc39/ecma262/commit/052def
(cherry picked from commit b5536db915623908f2f7488ab32793ba8f4e2b31)
2024-11-11 09:14:29 -05:00
Shannon Booth
70bc70acb0 LibJS: Inline somes AO's into InitializeHostDefinedRealm
Aligning with the editorial change in the spec of:

https://github.com/tc39/ecma262/commit/977a6c
(cherry picked from commit 0a1c9e4038dddbe0078e7c0ef82d8374b928bde1)
2024-11-11 09:14:29 -05:00
Shannon Booth
ab6418f5a0 LibJS: Allow host to create ShadowRealm global object
This implements the proposed update to the ShadowRealm proposal for
integrating the ShadowRealm specification into the web platform.

(cherry picked from commit d1fc76bffdafe5a057c0da5855d9d643608bd726)
2024-11-11 09:14:29 -05:00
Shannon Booth
108ffa8eee LibJS: Initialize ShadowRealm internal slots through setters
This allows us to align our implementation in the same order as the
specification.

No functional change with the current implementation of this AO.

However, this change is required in order to correctly implement a
proposed update of the shadow realm proposal for integration with
the HTML spec host bindings in order to give the ShadowRealm
object the correct 'intrinsic' realm.

This is due to that proposed change adding a step which manipulates the
currently executing Javascript execution context, making the ordering
important.

(cherry picked from commit 0ec8af5b70702e9ee2edc7269c103fa49e6987c1)
2024-11-11 09:14:29 -05:00
Andreas Kling
3590a94fc4 LibJS: Don't leak class field initializers
We were storing these in Handle (strong GC roots) hanging off of
ECMAScriptFunctionObject which effectively turned into world leaks.

(cherry picked from commit 5aa1d7837fe37dd203763178df3325ff8b24abbd)
2024-11-10 19:39:05 -05:00
Shannon Booth
23ff912009 LibJS: Allow unpaired surrogates in String.prototype.replace
This was resulting in a crash for the WPT test case:

https://wpt.live/xhr/send-data-string-invalid-unicode.any.html
(cherry picked from commit e02ca0480f7d39ff79fde59df024df9b53fe945e)
2024-11-10 19:39:05 -05:00
Shannon Booth
fdeeac157c LibJS: Implement Error.isError
Implementing the stage 2.7 proposal:

https://github.com/tc39/proposal-is-error
(cherry picked from commit e4891af9709c7f604a5118c341f99bc9f19d72a9)
2024-11-10 19:39:05 -05:00
Gasim Gasimzada
696021e252 LibJS: Set configurable toStringTag property for console
(cherry picked from commit c5b8e75204cb1603e2470e6cfeee3f2161a49b91)
2024-11-10 18:24:10 -05:00
Andreas Kling
16473663ea LibJS: Don't infinite loop on unknown console.log formatting specifiers
(cherry picked from commit ef9208047dc8770f6263b483d7a442df703bc42b)
2024-11-10 10:46:01 -05:00
Sam Atkins
d727545a1f LibJS: Update wording from Console spec
https://github.com/whatwg/console/pull/240 is an editorial change to use
the term "implementation-defined" more consistently. This seems to be
the only instance in the spec text which we quote verbatim.

(cherry picked from commit 51f82c1d939dd28a3e719d7fa495cf9f30d0921c)
2024-11-10 10:46:01 -05:00
Aliaksandr Kalenik
6f7297446a LibJS: Reset in_formal_parameter_context after entering a new function
Fixes a bug when "'Await' expression is not allowed in formal parameters
of an async function" is thrown for "await" encountered in a function
definition assigned to a default function parameter.

Fixes loading of https://excalidraw.com/

(cherry picked from commit 10064d0e1a89a4f48dc1e896de35a65f2ab946f4)
2024-11-09 16:08:01 -05:00
Andreas Kling
33bfcc467b LibJS: Don't copy current program counter into new execution contexts
This didn't make any sense, and was already handled by pushing a new
execution context anyway.

By simply removing these bogus lines of code, we fix a bug where
throwing inside a function whose bytecode was shorter than the calling
function would crash trying to generate an Error stack trace (because
the bytecode offset we were trying to symbolicate was actually from
the longer caller function, and not valid in the callee function.)

This makes --log-all-js-exceptions less crash prone and more helpful.

(cherry picked from commit b3f77e47690cfd07058d824ea6f0b652489778bf)
2024-11-09 16:08:01 -05:00
Timothy Flynn
4afa0a1973 LibJS: Use the IteratorStepValue AO in Iterator prototypes
This is an editorial change in the Iterator Helpers proposal. See:
https://github.com/tc39/proposal-iterator-helpers/commit/b4ccc31

(cherry picked from commit e782947a03d00f08fcfb16d4164c9edcd9155caf)
2024-11-09 16:08:01 -05:00
Timothy Flynn
464a0b9756 LibJS: Change generator state enum casing in IteratorHelperPrototype
This is an editorial change in the Iterator Helpers proposal. See:
https://github.com/tc39/proposal-iterator-helpers/commit/f1427ec

(cherry picked from commit 48bef246a0720ae4610b6f9c5dcd42eb8c5b448d)
2024-11-09 16:08:01 -05:00
Shannon Booth
bdb21f3279 LibJS: Use HeapFunction for IteratorHelper
(cherry picked from commit 87d964b2f5e3bb10e05fc7b12d13e153d0ba8b1e)
2024-11-09 07:30:40 -05:00
Andreas Kling
c4a11363dc LibJS: Fix mix-up when re-exporting an imported symbol with a new name
This makes https://cling.com/ load, and more of the animated elements
on https://shopify.com/ start appearing.

(cherry picked from commit 0f9444fa06031e7d8e3c1ad36e701090e0b17ec1)
2024-11-09 07:29:52 -05:00