Commit graph

167 commits

Author SHA1 Message Date
Andreas Kling
18c54d8d40 LibJS: Make Cell::initialize() return void
Stop worrying about tiny OOMs.

Work towards #20405
2023-08-08 07:39:11 +02:00
Hediadyoin1
50bf303edd LibJS: Capture UnrealizedSourceRanges in ExecutionContext, not ASTNodes
This loosens the connection to the AST interpreter and will allow us to
generate SourceRanges for the Bytecode interpreter in the future as well

Moves UnrealizedSourceRanges from TracebackFrame to the JS namespace for
this
2023-08-05 06:39:06 +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
Aliaksandr Kalenik
0fa47405df LibJS: Delete Declaration::for_each_lexically_declared_name
1. Replaces for_each_lexically_declared_name usage with more generic
for_each_lexically_declared_identifier.
2. Deletes for_each_lexically_declared_name.
2023-07-20 20:19:15 +02:00
Shannon Booth
7b00b4d6f6 LibJS: Add spec comments to FunctionDeclarationInstantiation 2023-07-19 14:14:03 +01:00
Shannon Booth
80b48b708f LibJS: Allow AsyncBlockStart to accept a SafeFunction
This is needed for the implementation of Array.fromAsync
2023-07-16 14:56:10 +01:00
Shannon Booth
98c4606544 LibJS: Link AsyncBlockStart AO to Explicit Resource Management proposal
As this function already implements that proposal.
2023-07-16 14:56:10 +01:00
Shannon Booth
930dd2948f LibJS: Make AsyncFunctionStart and AsyncBlockStart templates
This will allow implementing a version of these functions that accepts a
JS::SafeFunction, which is needed for the implementation of
Array.fromAsync.
2023-07-16 14:56:10 +01:00
Shannon Booth
7b5362fea6 LibJS: Make AsyncFunctionStart a standalone AO
This function may be called by places outside of
ECMAScriptFunctionObject.
2023-07-16 14:56:10 +01:00
Luke Wilde
d1cb78c411 LibJS/Bytecode: Implement async generators 2023-07-15 01:08:52 +02:00
Luke Wilde
d4e30710e7 LibJS: Enable bytecode default parameter values for async generators 2023-07-15 01:08:52 +02:00
Luke Wilde
3373626dd5 LibJS/Bytecode: Enable local variables for async generators
See 8b64508 and 71c54dd
2023-07-15 01:08:52 +02:00
Luke Wilde
53e527281f LibJS/Bytecode: Propagate FDI errors normally for async generators
Previously it returned a rejected promise, which is not correct:
https://tc39.es/ecma262/#sec-runtime-semantics-evaluateasyncgeneratorbody
```
1. Perform ? FunctionDeclarationInstantiation(functionObject,
   argumentsList).
```
2023-07-15 01:08:52 +02:00
Aliaksandr Kalenik
8b6450842e LibJS: Use local variables for function declarations when possible
Previously, the usage of local variables was limited for all function
declarations. This change relaxes the restriction and only prohibits
locals for hoistable annexB declarations.
2023-07-09 06:26:10 +02:00
Aliaksandr Kalenik
71c54dd37b LibJS: Always init arguments stored in locals for generator functions
Since AST interpreter switches to bytecode to execute generator
functions, arguments stored in local variables always need to be
initialized for such functions.
2023-07-08 05:38:45 +02:00
Aliaksandr Kalenik
b1af91d8c4 LibJS: Use local variables to store function parameters in some cases
Using local variables to store function parameters makes Kraken tests
run 7-10% faster.

For now this optimization is limited to only be applied if:
- Parameter does not use destructuring assignment
- None of the function params has default value
- There is no access to "arguments" variable inside function body
2023-07-07 19:35:08 +02:00
Aliaksandr Kalenik
2e81cc4cf7 LibJS: Use Identifier to represent FunctionParameter name
Using identifier instead of string allows to store supplemental
information about whether it can be represented as local variable.
2023-07-07 19:35:08 +02:00
Aliaksandr Kalenik
ae3a7fd4b8 LibJS: Update bytecode generator to use local variables
- Update ECMAScriptFunctionObject::function_declaration_instantiation
  to initialize local variables
- Introduce GetLocal, SetLocal, TypeofLocal that will be used to
  operate on local variables.
- Update bytecode generator to emit instructions for local variables
2023-07-05 21:03:01 +02:00
Aliaksandr Kalenik
0daff637e2 LibJS: Add vector of local variables in ExecutionContext
Now ExecutionContext has vector of values that will represent values
of local variables.

This vector is initialized in ECMAScriptFunctionObject::internal_call()
or ECMAScriptFunctionObject::internal_const() using number of local
variables provided to ECMAScriptFunctionObject by the parser.
2023-07-05 21:03:01 +02:00
Aliaksandr Kalenik
7765ebb5f2 LibJS+LibWeb: Plumb local variables names to ECMAScriptFunctionObject
Saving vector of local variables names in ECMAScriptFunctionObject
will allow to get a name by index in case message of ReferenceError
needs to contain a variable name.
2023-07-05 21:03:01 +02:00
Aliaksandr Kalenik
a6cdb1655b LibJS: Use Identifier to represent name of FunctionNode 2023-07-05 21:03:01 +02:00
Andreas Kling
9430bbcc62 LibJS/Bytecode: Propagate FunctionDeclarationInstantiation exceptions
If an exception is thrown by FunctionDeclarationInstantiation for an
async or async-generator function, we still need to return a promise.
We can't just throw the exception.

81 new passes on test262. :^)
2023-06-27 00:21:52 +02:00
Andreas Kling
467ea86179 LibJS: Use bytecode for generator function default parameters
Otherwise there's a mismatch between which interpreter we use.
This fixes a regression in the AST interpreter score on test262.
2023-06-23 08:09:50 +02:00
Andreas Kling
6537ed8fff LibJS/Bytecode: Simplify Bytecode::Interpreter lifetime model
The JS::VM now owns the one Bytecode::Interpreter. We no longer have
multiple bytecode interpreters, and there is no concept of a "current"
bytecode interpreter.

If you ask for VM::bytecode_interpreter_if_exists(), it will return null
if we're not running the program in "bytecode enabled" mode.

If you ask for VM::bytecode_interpreter(), it will return a bytecode
interpreter in all modes. This is used for situations where even the AST
interpreter switches to bytecode mode (generators, etc.)
2023-06-22 21:16:17 +02:00
Andreas Kling
743943a042 LibJS/Bytecode: Reorder function compilation to ensure correct hoisting
Fixes 37 test262 tests. :^)

Co-authored-by: Luke Wilde <lukew@serenityos.org>
2023-06-17 15:37:33 +02:00
Andreas Kling
9c568282dc Ladybird+LibJS: Add CLI option to run browser with LibJS bytecode VM
This required quite a bit of plumbing, but now you can run

    ladybird --use-bytecode
2023-06-17 14:16:45 +02:00
Andreas Kling
872d798951 LibJS/Bytecode: Leave FunctionDeclarationInstantantiation in C++
Instead of trying to implement this AO in bytecode, we can just let it
be a C++ thing. Once we implement fast uncaptured locals, we won't even
be calling it super often.
2023-06-16 08:40:45 +02:00
Linus Groh
b84f8fb55b LibJS: Make intrinsics getters return NonnullGCPtr
Some of these are allocated upon initialization of the intrinsics, and
some lazily, but in neither case the getters actually return a nullptr.

This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has
an `operator T&()`), and also has the interesting side effect of forcing
us to explicitly use the FunctionObject& overload of call(), as passing
a NonnullGCPtr is ambigous - it could implicitly be turned into a Value
_or_ a FunctionObject& (so we have to dereference manually).
2023-04-13 14:29:42 +02:00
Matthew Olsson
7c0c1c8f49 LibJS+LibWeb: Wrap raw JS::Cell*/& fields in GCPtr/NonnullGCPtr 2023-03-15 08:48:49 +01:00
Linus Groh
8f1d13e73b LibJS: Use a Variant instead of two Optionals for ThrowCompletionOr
Comes with the usual benefit of saving some space on the stack, as well
as making a situation where both or neither Optionals hold a value
impossible.

The various unwrapping additions are required as we can no longer
construct a ThrowCompletionOr<T> from an Optional<T> - rightfully so.
2023-03-01 17:55:42 +00:00
Luke Wilde
f4be95af69 LibJS: Don't discard ThrowCompletionOr<void> from declaration iteration 2023-02-27 23:57:08 +00:00
Andreas Kling
bd5d8e9d35 LibJS: Make RefPtr and NonnullRefPtr usage const-correct
This mainly affected the AST, which is now const throughout.
2023-02-21 00:54:04 +01:00
Timothy Flynn
88814acbd3 LibJS+Everywhere: Convert JS::Error to String
This includes an Error::create overload to create an Error from a UTF-8
StringView. If creating a String from that view fails, the factory will
return an OOM InternalError instead. VM::throw_completion can also make
use of this overload via its perfect forwarding.
2023-02-17 09:14:23 -05:00
Timothy Flynn
b75b7f0c0d LibJS+Everywhere: Propagate Cell::initialize errors from Heap::allocate
Callers that are already in a fallible context will now TRY to allocate
cells. Callers in infallible contexts get a FIXME.
2023-01-29 00:02:45 +00:00
Timothy Flynn
2692db8699 LibJS+Everywhere: Allow Cell::initialize overrides to throw OOM errors
Note that as of this commit, there aren't any such throwers, and the
call site in Heap::allocate will drop exceptions on the floor. This
commit only serves to change the declaration of the overrides, make sure
they return an empty value, and to propagate OOM errors frm their base
initialize invocations.
2023-01-29 00:02:45 +00:00
davidot
541637e15a LibJS: Add using declaration support, RAII like operation in js
In this patch only top level and not the more complicated for loop using
statements are supported. Also, as noted in the latest meeting of tc39
async parts of the spec are not stage 3 thus not included.
2023-01-23 09:56:50 +00:00
davidot
a746739cb0 LibJS: Add an initialize binding hint to all initialize_binding methods
This will allow us to specify things like SyncDispose and perhaps
AsyncDispose in the future.
2023-01-23 09:56:50 +00:00
Timothy Flynn
f3db548a3d AK+Everywhere: Rename FlyString to DeprecatedFlyString
DeprecatedFlyString relies heavily on DeprecatedString's StringImpl, so
let's rename it to A) match the name of DeprecatedString, B) write a new
FlyString class that is tied to String.
2023-01-09 23:00:24 +00:00
Linus Groh
208be8b86d LibJS: Convert new_function_environment() to NonnullGCPtr 2022-12-16 09:59:56 +01:00
Linus Groh
107e06a396 LibJS: Convert new_declarative_environment() to NonnullGCPtr 2022-12-16 09:59:56 +01:00
Linus Groh
03acbf0beb LibJS: Convert FunctionObject::internal_construct() to NonnullGCPtr 2022-12-15 06:56:37 -05:00
Linus Groh
1c24b82dd7 LibJS: Convert ordinary_create_from_constructor() to NonnullGCPtr 2022-12-15 06:56:37 -05:00
Linus Groh
22089436ed LibJS: Convert Heap::allocate{,_without_realm}() to NonnullGCPtr 2022-12-15 06:56:37 -05:00
Andreas Kling
4abdb68655 LibJS: Remove Object(Object& prototype) footgun
This constructor was easily confused with a copy constructor, and it was
possible to accidentally copy-construct Objects in at least one way that
we dicovered (via generic ThrowCompletionOr construction).

This patch adds a mandatory ConstructWithPrototypeTag parameter to the
constructor to disambiguate it.
2022-12-14 15:11:57 +01:00
Linus Groh
b42e293ddd LibJS: Convert NativeFunction::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
73efdb1cc4 LibJS: Convert ECMAScriptFunctionObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
91b0123eaf LibJS: Convert Array::create{,_from}() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Andreas Kling
2e98c17347 LibJS: Shrink DeclarativeEnvironment bindings vector to fit
After setting up all the bindings in function_declaration_instantiation,
we now ask DeclarativeEnvironment to do a shrink_to_fit() on its vector
of bindings.

This ends up saving 5.6 MiB on twitter.com/awesomekling :^)
2022-12-08 23:36:17 +00:00
Linus Groh
525f22d018 LibJS: Replace standalone js_string() with PrimitiveString::create()
Note that js_rope_string() has been folded into this, the old name was
misleading - it would not always create a rope string, only if both
sides are not empty strings. Use a three-argument create() overload
instead.
2022-12-07 16:43:06 +00:00
Linus Groh
daec065fde LibJS: Move initialize_instance_elements() from VM to Object
This makes more sense as an Object method rather than living within the
VM class for no good reason. Most of the other 7.3.xx AOs already work
the same way.
Also add spec comments while we're here.
2022-12-07 00:23:51 +00:00