Commit Graph

159 Commits

Author SHA1 Message Date
Andreas Kling
9721da2e6a LibJS: Call shrink_to_fit() on various Vectors created during parse
Vectors that stick around in the AST were wasting a fair bit of memory
due to the growth padding we keep by default. This patch goes after some
of these vectors with the shrink_to_fit() stick to reduce waste.

Since the AST can stay around for a long time, it is worth making an
effort to shrink it down when we have a chance.
2022-12-08 23:36:17 +00:00
Andreas Kling
35ed82d5e6 LibJS: Shrink FunctionNode by using bitfields
By making the boolean members be bitfields, we shrink FunctionNode by a
total of 8 bytes.
2022-12-08 23:36:17 +00:00
Andreas Kling
dd1720f2cb LibJS: Shrink ObjectProperty AST nodes by rearranging members
By putting smaller members in the padding hole after the ASTNode base
class, we shrink ObjectProperty by 8 bytes.
2022-12-08 23:36:17 +00:00
Andreas Kling
b894acd6b2 LibJS: Make one compact allocation for CallExpression and its Arguments
Instead of CallExpression storing its arguments in a Vector<Argument>,
we now custom-allocate the memory slot for CallExpression (and its
subclass NewExpression) so that it fits both CallExpression and its list
of Arguments in one allocation.

This reduces memory usage on twitter.com/awesomekling by 8.8 MiB :^)
2022-12-08 23:36:17 +00:00
Andreas Kling
8a8d8ecb35 LibJS: Add ASTNodeWithTailArray template to pack AST node + array
This template allows us to allocate an AST node and an array of some
arbitrary type T with one allocation instead of two. This can save
a lot of memory in some cases.

Thanks to Jonathan Müller for suggesting this technique! :^)
2022-12-08 23:36:17 +00:00
Andreas Kling
c767535ca2 LibJS: Use move() on the SourceRange in create_ast_node() 2022-12-08 23:36:17 +00:00
Andreas Kling
1f909d24b6 LibJS: Shrink MemberExpression by rearranging members slightly
By putting m_computed in the padding hole after our base class,
we shrink MemberExpression by 8 bytes. :^)
2022-12-08 23:36:17 +00:00
Andreas Kling
e70f944e11 LibJS: Rearrange ASTNode members so there's a padding hole at the end
ASTNode inherits from RefCounted, which has a single 32-bit member.
This means that there's a 32-bit padding hole after RefCounted,
where we are free to put something (or it will go to waste!)

This patch moves ASTNode::m_start_offset into that padding hole,
and we now have a 32-bit padding hole at the end of ASTNode instead.

This will allow ASTNode subclasses to put things in the ASTNode hole
by moving them to the head of the member list.
2022-12-08 23:36:17 +00:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
davidot
2c26ee89ac LibJS: Remove m_first_invalid_property_range from ObjectExpression
This was state only used by the parser to output an error with
appropriate location. This shrinks the size of ObjectExpression from
120 bytes down to just 56. This saves roughly 2.5 MiB when loading
twitter.
2022-11-27 12:31:37 +01:00
davidot
3acbd96851 LibJS: Remove is_use_strict_directive for all StringLiterals
This value was only used in the parser so no need to have this in every
string literal in the ast.
2022-11-27 12:31:37 +01:00
davidot
0c4befd811 LibJS: Use the source offset to sort imports in module
Using source offsets directly means we don't have to synthesize any
SourceRanges and circumvents that SourceRange returns invalid values for
the last range of a file.
2022-11-27 00:51:36 +00:00
Andreas Kling
d16fab5815 LibJS: Avoid unnecessary SourceRange construction in parse_program()
This takes `test-js` runtime from 4.3 sec to 4.1 sec on my machine.
2022-11-24 16:06:20 +00:00
Andreas Kling
3503c658fb LibJS+LibWeb: Move JS::ModuleRequest to its own header file
This allows us to not include LibJS/AST.h in a couple more places.
2022-11-23 16:05:59 +00:00
Andreas Kling
835d7aac96 LibJS: Make FunctionNode::Parameter be a standalone FunctionParameter
This will allow us to forward declare it and avoid including AST.h in a
number of places.
2022-11-23 16:05:59 +00:00
Andreas Kling
e0916dbb35 LibJS: Move {Import,Export}Entry out of {Import,Export}Statement
By making these be standalone instead of nested structs, we can forward
declare them. This will allow us to stop including AST.h in some places.
2022-11-23 16:05:59 +00:00
Andreas Kling
0f1f925532 LibJS: Shrink Identifier's environment coordinate cache
This patch does two things:

- We now use u32 instead of size_t for the hops and index fields
  in EnvironmentCoordinate. This means we're limited to an environment
  nesting level and variable count of 4Gs respectively.

- Instead of wrapping it in an Optional, EnvironmentCoordinate now has
  a custom valid/invalid state using a magic marker value.

These two changes reduce the size of Identifier by 16 bytes. :^)
2022-11-22 21:13:35 +01:00
Andreas Kling
76f438eb3e LibJS: Remove unused "lexical argument index" metadata from Identifier
This shrinks Identifier by 16 bytes. :^)
2022-11-22 21:13:35 +01:00
Andreas Kling
b0b022507b LibJS: Reduce AST memory usage by shrink-wrapping source range info
Before this change, each AST node had a 64-byte SourceRange member.
This SourceRange had the following layout:

    filename:       StringView (16 bytes)
    start:          Position (24 bytes)
    end:            Position (24 bytes)

The Position structs have { line, column, offset }, all members size_t.

To reduce memory consumption, AST nodes now only store the following:

    source_code:    NonnullRefPtr<SourceCode> (8 bytes)
    start_offset:   u32 (4 bytes)
    end_offset:     u32 (4 bytes)

SourceCode is a new ref-counted data structure that keeps the filename
and original parsed source code in a single location, and all AST nodes
have a pointer to it.

The start_offset and end_offset can be turned into (line, column) when
necessary by calling SourceCode::range_from_offsets(). This will walk
the source code string and compute line/column numbers on the fly, so
it's not necessarily fast, but it should be rare since this information
is primarily used for diagnostics and exception stack traces.

With this, ASTNode shrinks from 80 bytes to 32 bytes. This gives us a
~23% reduction in memory usage when loading twitter.com/awesomekling
(330 MiB before, 253 MiB after!) :^)
2022-11-22 21:13:35 +01:00
Hendiadyoin1
490c097bc4 LibJS: Forward a string aproximation of the CallExpression to Call Ops
This gives us better debug output when analysing calls to `undefined`
and also fixes multiple test-js cases expecting an
`(evaluated from $Expression)` in the error message.

This also refactors out the generation of that string, to avoid code
duplication with the AST interpreter.
2022-10-17 01:36:41 +02:00
Hendiadyoin1
ab763a56f6 LibJS: Allow SpreadExpressions to generate bytecode 2022-10-01 00:04:02 +01:00
davidot
3b1c3e574f LibJS: Handle empty named export
This is an export which looks like `export {} from "module"`, and
although it doesn't have any real export entries it should still add
"module" to the required modules to load.
2022-09-02 02:07:37 +01:00
Hendiadyoin1
21ae882cfd LibJS: Implement SuperCall for the Bytecode-VM 2022-08-31 15:22:36 +01:00
Linus Groh
5398dcc55e LibJS: Remove GlobalObject from execute() and related AST functions
This is a continuation of the previous four commits.

Passing a global object here is largely redundant, we definitely need
the interpreter but can get the VM and (later) current active realm from
there - and also the global object while we still need it, although I'd
like to remove Interpreter::global_object() in the future.

This now matches the bytecode interpreter's execute_impl() functions.
2022-08-23 13:58:30 +01:00
Linus Groh
b99cc7d050 LibJS+LibWeb: Replace GlobalObject with Realm in create() functions
This is a continuation of the previous two commits.

As allocating a JS cell already primarily involves a realm instead of a
global object, and we'll need to pass one to the allocate() function
itself eventually (it's bridged via the global object right now), the
create() functions need to receive a realm as well.
The plan is for this to be the highest-level function that actually
receives a realm and passes it around, AOs on an even higher level will
use the "current realm" concept via VM::current_realm() as that's what
the spec assumes; passing around realms (or global objects, for that
matter) on higher AO levels is pointless and unlike for allocating
individual objects, which may happen outside of regular JS execution, we
don't need control over the specific realm that is being used there.
2022-08-23 13:58:30 +01:00
davidot
ae349ec6a8 LibJS: Use a synthetic constructor if class with parent doesn't have one
We already did this but it called the @@iterator method of
%Array.prototype% visible to the user for example by overriding that
method. This should not be visible so we use a special version of
SuperCall now.
2022-08-20 23:53:55 +01:00
davidot
3a8dd3e78d LibJS: Implement tagged literals evaluation like the spec
We cache on the AST node side as this is easier to track a position, we
just have to take care to wrap the values in a handle to make sure they
are not garbage collected.
2022-08-17 23:56:24 +01:00
Luke Wilde
c0fadfb9b7 LibJS/Bytecode: Implement break/continue labels
This is done by keeping track of all the labels that apply to a given
break/continue scope alongside their bytecode target. When a
break/continue with a label is generated, we scan from the most inner
scope to the most outer scope looking for the label, performing any
necessary unwinds on the way. Once the label is found, it is then
jumped to.
2022-06-13 07:13:03 +04:30
Linus Groh
e815d3f9ce LibJS: De-duplicate ClassFieldDefinition Records
This was defined twice, despite being the very same thing:
- ClassElement::ClassFieldDefinition
- ECMAScriptFunctionObject::InstanceField

Move the former to a new header and use it everywhere. Also update the
define_field() AO to take a single field instead of separate name and
initializer arguments.
2022-04-20 00:08:32 +02:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
Luke Wilde
eac5534ce4 LibJS/Bytecode: Add support for new.target 2022-03-19 22:01:52 +01:00
Ali Mohammad Pur
8f7021faf7 LibJS: Implement bytecode generation for For-In/Of statements
This also implements the rather interesting behaviour that #12772 relies
on, so this fixes that bug in BC mode (the AST interp remains affected).
2022-03-19 12:51:29 +01:00
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
Ali Mohammad Pur
2000251333 LibJS: Implement bytecode generation for WithStatement 2022-03-13 17:50:21 +01:00
Andreas Kling
d5aed70dcf LibJS: Actually override is_private_identifier() in PrivateIdentifier
Regression from 72689ce7bd.
2022-03-06 00:49:35 +01:00
Idan Horowitz
7ebb421ee9 LibJS: Implement the object literal __proto__ property key special case 2022-03-06 01:38:25 +02:00
Andreas Kling
72689ce7bd LibJS: Add fast_is<PrivateIdentifier>() 2022-03-05 23:49:37 +01:00
Linus Groh
bfa4bc6f2d LibJS: Remove unused FunctionNode::set_name() 2022-02-20 15:40:41 +00:00
Ali Mohammad Pur
c7e6b65fd2 LibJS: Implement ClassExpression::generate_bytecode()
...and use that in ClassDeclaration::generate_bytecode().
2022-02-13 14:41:33 +00:00
Ali Mohammad Pur
75aa900b83 LibJS: Make ASTNode::generate_bytecode() fallible
Instead of crashing on the spot, return a descriptive error that will
eventually continue its days as a javascript "InternalError" exception.
This should make random crashes with BC less likely.
2022-02-13 14:41:33 +00:00
Andreas Kling
515594c667 LibJS: Add fast_is<T>() for things that were hot in RTTI
This gives a ~4% speedup when parsing the largest Discord JS file.
2022-02-13 14:44:36 +01:00
davidot
4136cbdb09 LibJS: Convert ScopeNode declaration functions to ThrowCompletionOr
This removes a number of vm.exception() checks which are now caught
directly by TRY. Make use of these checks in
{Global, Eval}DeclarationInstantiation and while we're here add spec
comments.
2022-02-08 09:12:42 +00:00
davidot
212c8dad5e LibJS: Keep handles on internal function while creating a class
It seems the stack search does not find all functions because they are
kept in variants and other structs. This meant some function could be
cleaned up while we were evaluating a class meaning it would fail/crash
when attempting to run the functions.
2022-02-05 11:52:51 +01:00
davidot
f568939568 LibJS: Implement the import assertions proposal
The hard part of parsing them in import statements and calls was already
done so this is just removing some check which threw before on
assertions. And filtering the assertions based on the result of a new
host hook.
2022-01-30 17:40:20 +00:00
davidot
e0e4ead2c8 LibJS: Follow the spec with storing im- and export entries
Because we can have arbitrary in- and export names with strings we can
have '*' and '' which means using '*' as an indicating namespace imports
failed / behaved incorrectly for string imports '*'.
We now use more specific types to indicate these special states instead
of these 'magic' string values.

Do note that 'default' is not actually a magic string value but one
specified by the spec. And you can in fact export the default value by
doing: `export { 1 as default }`.
2022-01-30 17:40:20 +00:00
davidot
023968a489 LibJS: Implement evaluation of im- and exports statements 2022-01-22 01:21:18 +00:00
davidot
99edf5b25a LibJS: Track whether a program has a top level await statement 2022-01-22 01:21:18 +00:00
davidot
aca427fc8c LibJS: Make parsing import and export entries follow the spec
The big changes are:
- Allow strings as Module{Export, Import}Name
- Properly track declarations in default export statements

However, the spec is a little strange in that it allows function and
class declarations without a name in default export statements.
This is quite hard to fully implement without rewriting more of the
parser so for now this behavior is emulated by faking things with
function and class expressions. See the comments in
parse_export_statement for details on the hacks and where it goes wrong.
2022-01-22 01:21:18 +00:00
davidot
232a8432b7 LibJS: Move binding_class_declaration_evaluation out of ClassDeclaration 2022-01-22 01:21:18 +00:00