Commit graph

533 commits

Author SHA1 Message Date
Andreas Kling
0c746366cc LibJS: Keep return value in a call frame register 2023-09-28 06:09:16 +02:00
Andreas Kling
c833885fb5 LibJS: Keep cached this value in a call frame register
Just moving more things to call frame registers..
2023-09-28 06:09:16 +02:00
Andreas Kling
3887b840a3 LibJS: Keep current exception in a call frame register
Instead of keeping it in a Bytecode::Interpreter member, move it into
a dedicated call frame register.
2023-09-28 06:09:16 +02:00
Andreas Kling
4c5b9fa6a2 LibJS: Remove unused SetVariable::InitializationMode::InitializeOrSet 2023-09-26 09:38:32 +02:00
Andreas Kling
4f488f7e07 LibJS: Avoid creating empty environment for catch without parameter
When there is no `catch` parameter to bind the error, we don't need
to allocate an environment, since there's nothing to add to it.

This avoids one environment allocation every time we catch like this:

    try {
        ...
    } catch {
        ...
    }
2023-09-25 19:47:24 +02:00
Andreas Kling
3d5cd23393 LibJS: Remove unused Instruction::is_terminator() 2023-09-21 16:19:13 +02:00
Andreas Kling
3ccac0cf6e LibJS: Remove unused realm parameter from run_and_return_frame() 2023-09-21 16:19:13 +02:00
Andreas Kling
d6e959686d LibJS: Remove automatic ExecutionContext creation in BC interpreter
This was a remnant from the AST/BC hybrid interpreter times. We've had
a VERIFY in here for weeks now that would catch anything depending on
this behavior, and nothing has hit it, so let's remove the unnecessary
code (but leave the VERIFY) :^)
2023-09-21 16:19:13 +02:00
Shannon Booth
30ab198b40 LibJS: Create const variables in ForIn/OfBodyEvaluation in strict mode
Our implementation of environment.CreateImmutableBinding(name, true)
in this AO was not correctly initializing const variables in strict
mode. This would mean that constant declarations in for loop bodies
would not throw if they were modified.

To fix this, add a new parameter to CreateVariable to set strict mode.
Also remove the vm.is_strict mode check here, as it doesn't look like
anywhere in the spec will change strict mode depending on whether the
script itself is running in script mode or not.

This fixes two of our test-js tests, no change to test262.
2023-09-21 16:19:05 +02:00
Andreas Kling
a7c1af08ca LibJS: Store bytecode instruction length in instruction itself
Instead of running a big switch statement on the opcode when checking
how long an instruction is, we now simply store that in a member
variable at construction time for instant access.

This yields a 10.2% speed-up on Kraken/ai-astar :^)
2023-09-14 16:11:14 +02:00
Timothy Flynn
54d1f4e234 LibJS: Stop propagating small OOM errors from the Error object 2023-09-09 13:03:25 -04:00
Andreas Kling
c78506d79b LibJS: Only update EC instruction pointer when pushing to EC stack
Instead of trying to keep a live reference to the bytecode interpreter's
current instruction stream iterator, we now simply copy the current
iterator whenever pushing to the ExecutionContext stack.

This fixes a stack-use-after-return issue reported by ASAN.
2023-09-02 19:24:29 +02:00
Andreas Kling
1c06111cbd LibJS: Add file & line number to bytecode VM stack traces :^)
This works by adding source start/end offset to every bytecode
instruction. In the future we can make this more efficient by keeping
a map of bytecode ranges to source ranges in the Executable instead,
but let's just get traces working first.

Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
2023-09-02 15:37:53 +02:00
Luke Wilde
b3fa79e64d LibJS: Cover all property kinds when stringifying Put bytecode ops 2023-08-29 21:38:54 -04:00
Luke Wilde
2aaae6fc70 LibJS: Avoid ToPropertyKey for spreading in PutByValue(WithThis)
This is not we're supposed to do according to https://tc39.es/ecma262/#sec-runtime-semantics-propertydefinitionevaluation
Furthermore, this was observable by ToPrimitive looking up toString and
valueOf and potentially calling them if they exist. The big ticket
issue however is that for objects without toString and valueOf, such as
null-proto objects, this would unexpectedly throw.
2023-08-29 21:38:54 -04:00
Luke Wilde
ae7a0c43a9 LibJS: Implement await properly for async functions
Fixes #20275

```
Summary:
    Diff Tests:
        +4     -4    

Diff Tests:
    test/built-ins/Array/fromAsync/non-iterable-input-with-thenable
    -async-mapped-awaits-callback-result-once.js  -> 
    test/language/expressions/await/async-await-interleaved.js  -> 
    test/language/expressions/await/await-awaits-thenables-that-
    throw.js  -> 
    test/language/expressions/await/await-awaits-thenables.js  -> 
```
2023-08-10 05:12:07 +02:00
Andreas Kling
97ebfd9f0f LibJS: Make Value::to_string_without_side_effects() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
1a27c525d5 LibJS: Make PrimitiveString::create() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
2eaa528a0e LibJS: Rip out the AST interpreter :^)
This has been superseded by the bytecode VM, which is both faster
and more capable.
2023-08-08 13:07:13 +02:00
Andreas Kling
e2c8d5859e LibJS: Remove ability to disable bytecode VM 2023-08-08 13:07:13 +02:00
Andreas Kling
d1d24569f8 LibJS/Bytecode: Remove unused AST interpreter hacks from bytecode VM 2023-08-08 13:07:13 +02:00
Andreas Kling
34344120f2 AK: Make "foo"_string infallible
Stop worrying about tiny OOMs.

Work towards #20405.
2023-08-07 16:03:27 +02:00
Andreas Kling
e91bdedc93 LibJS: Use correct this value when callee is a with binding
If we're inside of a `with` statement scope, we have to take care to
extract the correct `this` value for use in calls when calling a method
on the binding object via an Identifier instead of a MemberExpression.

This makes Vue.js work way better in the bytecode VM. :^)

Also, 1 new pass on test262.
2023-08-01 16:08:21 +02:00
Aliaksandr Kalenik
2bdc69c42c LibJS: Add caching of this value in ResolveThisBinding instruction
Because "this" value cannot be changed during function execution it is
safe to compute it once and then use for future access.

This optimization makes ai-astar.js run 8% faster.
2023-07-29 04:23:49 +02:00
Andreas Kling
9f06e130a2 LibJS/Bytecode: Keep saved return value in call frame register
This fixes an issue where returning inside a `try` block and then
calling a function inside `finally` would clobber the saved return
value from the `try` block.

Note that we didn't need to change the base of register allocation,
since it was already 1 too high.

With this fixed, https://microsoft.com/edge loads in bytecode mode. :^)

Thanks to Luke for reducing the issue!
2023-07-21 19:15:33 +02:00
Timothy Flynn
77d7f715e3 LibJS+CI: Remove bytecode optimization passes for now
These passes have not been shown to actually optimize any JS, and tests
have become very flaky with optimizations enabled. Until some measurable
benefit is shown, remove the optimization passes to reduce overhead of
maintaining bytecode operations and to reduce CI churn. The framework
for optimizations will live on in git history, and can be restored once
proven useful.
2023-07-21 19:47:36 +03:30
Andreas Kling
c90bf22d29 LibJS/Bytecode: Use primitive this for strict mode GetByValue
GetByValue now shares code with GetById to elide the synthetic wrapper
objects for primitive values in strict mode.

Fixes 2 test-js tests in bytecode mode. :^)
2023-07-21 14:14:00 +02:00
Andreas Kling
7253d021fc LibJS/Bytecode: Factor out determining the base object from GetById
This will allow us to use the same logic in GetByValue (in a subsequent
commit.)
2023-07-21 14:14:00 +02:00
Andreas Kling
6de22ec789 LibJS/Bytecode: Rename RegisterWindow to CallFrame
This is a better name for what it actually represents.
2023-07-21 14:14:00 +02:00
MacDue
17a5af04e3 LibJS: Actually generate the bytecode CFG
This is just something I spotted looking around the code, previously
the PassPipelineExecutable was passed by value to
generate_cfg_for_block, which generated the CFG then just dropped it on
the floor. Making this a reference results in the CFG actually getting
generated.
2023-07-21 06:59:51 +02:00
Shannon Booth
a0d1ef34e2 LibJS: Deduplicate labelled continue & break bytecode generation
Similar to the scoped continue and break, the only two differences
between these functions is the scope that is scanned for a matching
label, and the specific handling of a continue/break boundary.
2023-07-20 07:24:31 +02:00
Shannon Booth
663e4507ea LibJS: Deduplicate scoped continue & break bytecode generation
The only two differences between these two functions are the name of the
block that is created and the specific jump/break handling per boundary.
2023-07-20 07:24:31 +02:00
Timothy Flynn
9d7215c636 LibJS+LibWeb: Move IteratorOperations.h AOs to Iterator.h
Rather than splitting the Iterator type and its AOs into two files,
let's combine them into one file to match every other JS runtime object
that we have.
2023-07-19 14:11:43 +01:00
Timothy Flynn
1760361304 LibJS: Make GetIterator's hint parameter required
This is an editorial change in the ECMA-262 spec. See:
https://github.com/tc39/ecma262/commit/2562811
2023-07-19 06:56:51 +02:00
Daniel Bertalan
cff8f56ce7 LibJS/Bytecode: Do not unwind eagerly after throwing Await
If an exception was thrown while evaluating the argument of an `await`
expression, we should jump to the continuation block instead of eagerly
rejecting the caller async function.

This restores the behavior prior to the addition of the separate `Await`
instruction in d66eb4e3.
2023-07-15 11:27:43 +02:00
Luke Wilde
d1cb78c411 LibJS/Bytecode: Implement async generators 2023-07-15 01:08:52 +02:00
Luke Wilde
265a9b5ffc LibJS/Bytecode: Add function to determine if we're in an async generator 2023-07-15 01:08:52 +02:00
Luke Wilde
c37ef5694b LibJS/Bytecode: Class async generators as async functions 2023-07-15 01:08:52 +02:00
Luke Wilde
d66eb4e3ba LibJS/Bytecode: Add Await and AsyncIteratorClose instructions 2023-07-15 01:08:52 +02:00
Daniel Bertalan
e3f65f215d LibJS/Bytecode: Do not rethrow caught exception from finally
If the exception from the `try` block has already been caught by
`catch`, we need to clear the saved exception before entering `finally`
so that ContinuePendingUnwind will not re-throw it.

9 new passes on test262 :^)
2023-07-14 17:05:29 +02:00
Gabriel Dinner-David
d29bd55b48 LibJS: Implement import.meta for bytecode 2023-07-14 06:06:04 +02:00
Aliaksandr Kalenik
a1692931af LibJS/Bytecode: Skip CreateVariable for locals in "for" loop head
CreateVariable is not needed for locals because they are not stored in
environment and created binding will not be used. Also if all variables
in loop initialization sections are local then CreateLexicalEnvironment
and LeaveLexicalEnvironment can also be ommitted.
2023-07-14 05:58:30 +02:00
Andreas Kling
c0f985ffcf LibJS/Bytecode: Don't reparse regular expressions on instantiation
The RegExpLiteral AST node already has the parsed regex::Parser::Result
so let's plumb that over to the bytecode executable instead of reparsing
the regex every time NewRegExp is executed.

~12% speed-up on language/literals/regexp/S7.8.5_A2.1_T2.js in test262.
2023-07-13 13:30:49 +02:00
Andreas Kling
640d48255b LibJS: Remove unhelpful environment lookup optimization for globals
This optimization was no longer helpful after the bug fix for missing
invalidation on global delete was introduced in 331f6a9e6, since we
now have to check bindings for presence in the global environment every
time anyway.

Since the bytecode VM now has fast GetGlobal in most cases, let's not
even worry about this and just remove the unhelpful "optimization".

In fact, removing this is actually an *optimization*, since we avoid
a redundant has_binding() check on every global variable access. :^)
2023-07-13 10:56:59 +02:00
Andreas Kling
9e468579ce LibJS/Bytecode: Use malloc instead of mmap for BasicBlock buffers
Using mmap was quite punishing on inputs with lots of basic blocks,
such as test262 tests with eval() in a loop that goes 64k times..

~27% speed-up on language/literals/regexp/S7.8.5_A2.1_T2.js but
presumably everything everywhere will benefit from this. :^)
2023-07-13 10:19:10 +02:00
Andreas Kling
acd8c94e88 LibJS/Bytecode: Make Bytecode::Register constexpr
A trivial change for 2% speed-up on Kraken/ai-astar.js :^)
2023-07-12 22:57:03 +02:00
Aliaksandr Kalenik
3661d674ae LibJS: Add optimized GetGlobal instruction to access global variables
Using a special instruction to access global variables allows skipping
the environment chain traversal for them and going directly to the
module/global environment. Currently, this instruction only caches the
offset for bindings that belong to the global object environment.
However, there is also an opportunity to cache the offset in the global
declarative record.

This change results in a 57% increase in speed for
imaging-gaussian-blur.js in Kraken.
2023-07-12 16:03:16 +02:00
Andreas Kling
cf6792ec40 LibJS/Bytecode: Invalidate inline caches on unique shape mutation
Since we can't rely on shape identity (i.e its pointer address) for
unique shapes, give them a serial number that increments whenever a
mutation occurs.

Inline caches can then compare this serial number against what they
have seen before.
2023-07-11 00:14:50 +02:00
Andreas Kling
e0b2757f95 LibJS/Bytecode: Always make own properties in object expressions
When building an object from an object expression, we don't want to
go through the full property setting machinery. This patch adds a new
PropertyKind::DirectKeyValue for PutById which guarantees that the
property becomes an own property.

This fixes an issue where setting the "__proto__" property in object
expressions wasn't working right.

12 new passes on test262. :^)
2023-07-10 09:29:54 +01:00
Andreas Kling
de8e4b1853 LibJS/Bytecode: Cache object own property accesses
The instructions GetById and GetByIdWithThis now remember the last-seen
Shape, and if we see the same object again, we reuse the property offset
from last time without doing a new lookup.

This allows us to use Object::get_direct(), bypassing the entire lookup
machinery and saving lots of time.

~23% speed-up on Kraken/ai-astar.js :^)
2023-07-09 12:54:06 +02:00