Commit graph

96 commits

Author SHA1 Message Date
Ben Wiederhake
32e98d0924 Libraries: Use AK::Variant default initialization where appropriate 2021-09-21 04:22:52 +04:30
Andreas Kling
33f038ba7a LibJS: Add fast failure path to try_parse_labelled_statement()
If the next token isn't a TokenType::Colon (:), this can't possibly be a
labelled statement, so we can fail before having to save_state().

This improves parsing time on a large chunk of JS by ~12.5%.
2021-09-18 19:54:24 +02:00
Ali Mohammad Pur
c7a99aafac LibJS: Use ScopePusher to correctly push the scope in for statements
We were previously pushing a scope but forgetting to actually set it as
the current scope.
2021-09-16 21:51:45 +02:00
Ali Mohammad Pur
72ddaa31e3 LibJS: Implement parsing and execution of optional chains 2021-09-14 20:03:27 +01:00
Andreas Kling
910de95e7a LibJS: Add a fast failure path to try_parse_arrow_function_expression()
The save/load of parser state performed by lookahead parsing is quite
expensive so let's try to avoid it in the most common case.

This is a 15-20% speedup on various chunks of JS I've tested. :^)
2021-09-14 02:51:16 +02:00
davidot
3fee7b0d0b LibJS: Fix that windows style line endings were not ignored or converted
These are tested by test262 but the current test262-runner reads the
files in python which automatically converts \r\n to \n.
This meant that we passed the tests while we should not have.
2021-09-06 08:43:38 +01:00
Daniel Bertalan
d7b6cc6421 Everywhere: Prevent risky implicit casts of (Nonnull)RefPtr
Our existing implementation did not check the element type of the other
pointer in the constructors and move assignment operators. This meant
that some operations that would require explicit casting on raw pointers
were done implicitly, such as:
- downcasting a base class to a derived class (e.g. `Kernel::Inode` =>
  `Kernel::ProcFSDirectoryInode` in Kernel/ProcFS.cpp),
- casting to an unrelated type (e.g. `Promise<bool>` => `Promise<Empty>`
  in LibIMAP/Client.cpp)

This, of course, allows gross violations of the type system, and makes
the need to type-check less obvious before downcasting. Luckily, while
adding the `static_ptr_cast`s, only two truly incorrect usages were
found; in the other instances, our casts just needed to be made
explicit.
2021-09-03 23:20:23 +02:00
Andreas Kling
eaf88cc78a AK: Rename create<T> => make_ref_counted<T>
And also try_create<T> => try_make_ref_counted<T>.

A global "create" was a bit much. The new name matches make<T> better,
which we've used for making single-owner objects since forever.
2021-09-03 02:36:09 +02:00
davidot
def8b44c40 LibJS: Add support for public fields in classes 2021-09-01 13:39:14 +01:00
davidot
3b6a8d1d53 LibJS: Fix small issues in parser
- Fix some places where escaped keywords are (not) allowed.
- Be more strict about parameters for functions with 'use strict'.
- Fix that expressions statements allowed functions and classes.
- Fix that class expressions were not allowed.
- Added a new next_token() method for checking the look ahead.
- Fix that continue labels could jump to non iterating targets.
- Fix that generator functions cannot be declared in if statements.
2021-09-01 13:39:14 +01:00
davidot
c108c8ff24 LibJS: Disallow yield expression correctly in formal parameters
And add ZERO WIDTH NO BREAK SPACE to valid whitespace.
2021-08-24 07:42:37 +01:00
davidot
7bcffd1b6a LibJS: Fix some small remaining issues with parsing unicode escapes
Added a test to ensure the behavior stays the same.
We now throw on a direct usage of an escaped keywords with a specific
error to make it more clear to the user.
2021-08-24 07:42:37 +01:00
Timothy Flynn
1259dc3623 LibJS: Allow Unicode escape sequences in identifiers
For example, "property.br\u{64}wn" should resolve to "property.brown".

To support this behavior, this commit changes the Token class to hold
both the evaluated identifier name and a view into the original source
for the unevaluated name. There are some contexts in which identifiers
are not allowed to contain Unicode escape sequences; for example, export
statements of the form "export {} from foo.js" forbid escapes in the
identifier "from".

The test file is added to .prettierignore because prettier will replace
all escaped Unicode sequences with their unescaped value.
2021-08-19 23:49:25 +02:00
davidot
4d6502de42 LibJS: Disallow standalone super expression 2021-08-16 23:20:04 +01:00
davidot
a8b25d6c36 LibJS: Handle '++' and '--' more correctly within expression 2021-08-16 23:20:04 +01:00
davidot
5f344f7ca3 LibJS: Check that 'let' is followed by declaration before matching it
Since 'let' is a valid variable name (in non-strict mode) let may not be
the start of a declaration but just an identifier.
2021-08-16 23:20:04 +01:00
davidot
179c48e1a4 LibJS: Add optional extra strict checks in parse_binding_pattern 2021-08-16 23:20:04 +01:00
davidot
f1f338edcd LibJS: Tighten default values in formal parameter parsing
Disallow default parameter for rest parameters.
Disallow yield expressions as default values.
2021-08-16 23:20:04 +01:00
davidot
4989e79c45 LibJS: Allow yielding a class 2021-08-16 23:20:04 +01:00
davidot
19582ccad8 LibJS: Treat yield as an identifier in more non-generator contexts
And disallow some cases where we are in a generator context.
2021-08-16 23:20:04 +01:00
davidot
26177b1826 LibJS: Add more duplicated declarations detection
This is a small step in the right direction although the amount of
different checks is becoming unsustainable. In the future we probably
want to have the current_scope handle all declarations.
2021-08-16 23:20:04 +01:00
davidot
085c7df895 LibJS: Be more strict about the lhs of a for in/of loop
This is not entirely correct as really Object- and ArrayExpressions are
not allowed but that requires a bigger refactoring of for statement
parsing.
2021-08-16 23:20:04 +01:00
davidot
106f9e30d7 LibJS: Force the lexer to parse a regex when expecting a statement 2021-08-16 23:20:04 +01:00
davidot
05444103e3 LibJS: Treat arrow expression as function and stop parsing after 2021-08-16 23:20:04 +01:00
davidot
e31b715808 LibJS: Make functions reset break and continue context 2021-08-16 23:20:04 +01:00
davidot
be3b4a68d2 LibJS: Allow class methods named "get", "set" or "static" 2021-08-16 23:20:04 +01:00
davidot
b16c02d6b4 LibJS: Allow labelled functions in certain contexts
Also disallow duplicated labels.
2021-08-16 23:20:04 +01:00
davidot
020bfc9d93 LibJS: Parse and partially execute import and export statements
We produce the import and export entries as per the spec. However we do
not yet verify that named things that are exported are declared
somewhere.
2021-08-15 23:51:47 +01:00
davidot
7613c22b06 LibJS: Add a mode to parse JS as a module
In a module strict mode should be enabled at the start of parsing and we
allow import and export statements.
2021-08-15 23:51:47 +01:00
Lenny Maiorani
a0d7640e03 Userland: Make use of container version of any_of
Problem:
- New `any_of` implementation takes the entire container so the user
  does not need to pass explicit begin/end iterators. This is unused
  except is in tests.

Solution:
- Make use of the new and more user-friendly version where possible.
2021-08-02 00:37:18 +02:00
Timothy Flynn
f1dd770a8a LibJS: Parse RegExp literals at AST creation time, not execution time
The spec requires that invalid RegExp literals must cause a Syntax Error
before the JavaScript is executed. See:
https://tc39.es/ecma262/#sec-patterns-static-semantics-early-errors

This is explicitly tested in the RegExp/property-escapes test262 tests.
For example, see unsupported-property-Line_Break.js:

    $DONOTEVALUATE();
    /\p{Line_Break}/u;

That RegExp literal is invalid because Line_Break is not a supported
Unicode property. $DONOTEVALUATE() just throws an exception when it is
executed. The test expects that this file will fail to be parsed.

Note that RegExp patterns can still be parsed at execution time by way
of "new RegExp(...)".
2021-07-30 21:26:31 +01:00
davidot
a6263150be LibJS: Disallow unqualified deletes in strict mode 2021-07-20 23:45:28 +02:00
davidot
697882a7ad LibJS: Disallow multiple __proto__ keys in object expression 2021-07-20 23:45:28 +02:00
davidot
93b57e6d8c LibJS: Disallow static methods named prototype in classes 2021-07-20 23:45:28 +02:00
davidot
40b8689f9b LibJS: Disallow duplicated variable declarations 2021-07-20 23:45:28 +02:00
davidot
5cc518f07a LibJS: Handle strict mode for functions more correctly
If a function is strict (has 'use strict' directive) it cannot have
bindings, cannot have duplicated parameter names and cannot have some
reserved keywords and identifiers as parameter names.
The logic partly applies depending on whether we are already in strict
mode or the function contains 'use strict';
2021-07-20 23:45:28 +02:00
davidot
4485df1405 LibJS: Be more strict about reserved and special identifiers 2021-07-20 23:45:28 +02:00
davidot
2a61b90fef LibJS: Be less strict about 'use strict'
It is allowed to have (bogus) directives e.g.
'does nothing'; 'same'; 'use strict';
Should still trigger strict mode.
2021-07-20 23:45:28 +02:00
Linus Groh
08a303172d LibJS: Extend class 'extends' RHS expression parsing
Instead of only parsing a primary expression, we should also allow
member expressions, call expressions, and tagged template literals (and
optional chains, which we don't have yet).
In the spec, all of this is covered by `LeftHandSideExpression`
(https://tc39.es/ecma262/#prod-LeftHandSideExpression).
2021-07-18 00:17:57 +01:00
Ali Mohammad Pur
dfb7e716f7 LibJS: Use expected() instead of syntax_error("Expected ...") 2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
049e210cfa LibJS: Rework Identifier parsing to match the spec more closely 2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
77a5144264 LibJS: Add support for binding patterns in catch clauses
`try { ... } catch({a=foo}) {}` is valid, and now we parse and evaluate
it correctly :^)
2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
1a9518ebe3 LibJS: Implement parsing and evaluation for AssignmentPatterns
e.g. `[...foo] = bar` can now be evaluated :^)
2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
7fc6cd6b20 LibJS: Allow 'name = value' in object literals as the spec does
Currently, these are _always_ a syntax error, future commits will make
it valid in certain contexts.
2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
7dae25eceb LibJS: Fix computed property ending token in binding pattern parsing
The syntax is supposed to be '[expression]', not '[expression['.
2021-07-11 21:41:54 +01:00
Ali Mohammad Pur
b5b84029ab LibJS: Treat default parameter values as being in function context
e.g. `new.target` should be permitted in
`function foo(x = new.target) {}`.
2021-07-11 21:41:54 +01:00
Timothy Flynn
d1e06b00e3 LibJS: Parse the RegExp.prototype.hasIndices flag 2021-07-10 16:49:35 +01:00
Hendi
37c4fbb6ca LibJS: Don't hoist functions under certain circumstances
When a lexical declaration with the same name as a function exists,
the function is not hoisted (annex B).
2021-07-06 22:55:16 +01:00
Linus Groh
3faeabf1dc Revert "LibJS: Don't hoist functions under certain circumstances"
This reverts commit 3411d50737.

It was causing LeakSanitizer on CI to fail, possibly due to a circular
reference.
2021-07-06 13:25:37 +01:00
Hendi
3411d50737 LibJS: Don't hoist functions under certain circumstances
When a lexical declaration with the same name as a function exists,
the function is not hoisted (annex B).
2021-07-06 00:15:37 +01:00