Commit graph

543 commits

Author SHA1 Message Date
Matthew Olsson
e415dd4e9c LibJS: Handle hex and unicode escape sequences in string literals
Introduces the following syntax:

'\x55'
'\u26a0'
'\u{1f41e}'
2020-05-18 17:58:17 +02:00
Linus Groh
b3090678a9 LibJS: Add Math.clz32() 2020-05-18 17:57:28 +02:00
Linus Groh
452dbbc463 LibJS: Add Math.expm1() 2020-05-18 17:57:28 +02:00
Linus Groh
e375766f98 LibJS: Add Math.exp() 2020-05-18 17:57:28 +02:00
Linus Groh
b27834cf16 LibJS: Add Math.sign() 2020-05-18 17:57:28 +02:00
Linus Groh
f06c12173c LibJS: Return early from parseFloat() if argument is a number
This saves us both a bit of time and accuracy, as Serenity's strtod()
still is a little bit off sometimes - and stringifying the result and
parsing it again just increases that offset.
2020-05-18 14:33:53 +02:00
Linus Groh
eb72ba2466 LibJS: Remove is_nan() check in as_size_t() and fix to_size_t()
We need to call as_double() on the freshly converted number, not the
value itself.
2020-05-18 11:39:11 +02:00
Linus Groh
36996bd720 LibJS: Rename to_{i32,size_t}() to as_{i32,size_t}() for clarity
As these parameter-less overloads don't change the value's type and
just assume Type::Number, naming them as_i32() and as_size_t() is more
appropriate.
2020-05-18 10:21:51 +02:00
Linus Groh
56502b0e84 LibJS: Check for exception after converting object to string primitive 2020-05-18 09:39:55 +02:00
Linus Groh
4569e88bea LibJS: Throw TypeError when coercing symbol to number 2020-05-18 09:39:55 +02:00
Linus Groh
476094922b LibJS: Pass Interpreter& to Value::to_number() et al.
This patch is unfortunately rather large and might make some things feel
bloated, but it is necessary to fix a few flaws in LibJS, primarily
blindly coercing values to numbers without exception checks - i.e.

interpreter.argument(0).to_i32();  // can fail!!!

Some examples where the interpreter would actually crash:

var o = { toString: () => { throw Error() } };
+o;
o - 1;
"foo".charAt(o);
"bar".repeat(o);

To fix this, we now have the following...

to_double(Interpreter&)
to_i32()
to_i32(Interpreter&)
to_size_t()
to_size_t(Interpreter&)

...and a whole lot of exception checking.

There's intentionally no to_double(), use as_double() directly instead.

This way we still can use these convenient utility functions but don't
need to check for exceptions if we are sure the value already is a
number.

Fixes #2267.
2020-05-18 09:39:55 +02:00
Linus Groh
1a1394f7a2 LibJS: Change Value::to_object(Heap& -> Interpreter&)
Passing a Heap& to it only to then call interpreter() on that is weird.
Let's just give it the Interpreter& directly, like some of the other
to_something() functions.
2020-05-18 09:39:55 +02:00
Linus Groh
b8b7f84547 LibJS: Remove no-op SymbolPrototype::description_setter()
We can just give put_native_property() a nullptr for the setter.
2020-05-18 09:36:14 +02:00
mattco98
4ced126704 LibJS: Add symbol objects
This commit adds the following classes: SymbolObject, SymbolConstructor,
SymbolPrototype, and Symbol. This commit does not introduce any
new functionality to the Object class, so they cannot be used as
property keys in objects.
2020-05-17 18:05:15 +02:00
Linus Groh
71ec72bddf LibJS: Simplify various StringPrototype functions 2020-05-17 16:23:48 +02:00
Linus Groh
73eef31f49 LibJS: Add Number.parseFloat() 2020-05-17 16:21:38 +02:00
Linus Groh
6f6b089aa0 LibJS: Add parseFloat() 2020-05-17 16:21:33 +02:00
Linus Groh
b299c75d45 LibJS: Make Object.prototype.constructor non-enumerable 2020-05-16 21:22:34 +02:00
Andreas Kling
de7827faf7 LibM: Fix floor() and floorf() for negative numbers
And add a LibJS test to exercise the code. :^)
2020-05-15 22:05:59 +02:00
Andreas Kling
c6ddbd1f3e LibJS: Add side-effect-free version of Value::to_string()
There are now two API's on Value:

- Value::to_string(Interpreter&) -- may throw.
- Value::to_string_without_side_effects() -- will never throw.

These are some pretty big sweeping changes, so it's possible that I did
some part the wrong way. We'll work it out as we go. :^)

Fixes #2123.
2020-05-15 13:50:42 +02:00
Linus Groh
33defef267 LibJS: Let parser keep track of errors
Rather than printing them to stderr directly the parser now keeps a
Vector<Error>, which allows the "owner" of the parser to consume them
individually after parsing.

The Error struct has a message, line number, column number and a
to_string() helper function to format this information into a meaningful
error message.

The Function() constructor will now include an error message when
throwing a SyntaxError.
2020-05-15 09:53:52 +02:00
Linus Groh
00b61a212f LibJS: Remove syntax errors from lexer
Giving the lexer the ability to generate errors adds unnecessary
complexity - also it only calls its syntax_error() function in one place
anyway ("unterminated string literal"). But since the lexer *also* emits
tokens like Eof or UnterminatedStringLiteral, it should be up to the
consumer of these tokens to decide what to do.

Also remove the option to not print errors to stderr as that's not
relevant anymore.
2020-05-15 09:53:52 +02:00
Sergey Bugaev
450a2a0f9c Build: Switch to CMake :^)
Closes https://github.com/SerenityOS/serenity/issues/2080
2020-05-14 20:15:18 +02:00
Linus Groh
de42ddfd93 LibJS: Trim whitespace from string before coercing to number 2020-05-13 09:36:20 +02:00
Linus Groh
f39c7c2978 LibJS: Use String::trim_whitespace() for String.prototype.trim*() 2020-05-13 09:36:20 +02:00
Linus Groh
0c14ee035c LibJS: Make string to number coercion work for doubles 2020-05-13 09:36:20 +02:00
Linus Groh
7b8765c311 LibJS: Make the Function() constructor throw a SyntaxError, not return 2020-05-13 09:34:25 +02:00
Linus Groh
d69ed91790 LibJS: Check AssignmentExpression LHS in parser
There are many cases which shouldn't even parse, like

null = ...
true = ...
false = ...
123 = ...
"foo" = ...

However this *is* valid syntax:

foo() = ...

So we still have to keep the current code doing a runtime check if the
LHS value is a resolvable reference. I believe this was declared valid
syntax to *in theory* allow functions returning references - though in
practice that isn't a thing.

Fixes #2204.
2020-05-13 01:15:29 +02:00
Linus Groh
063228c02e LibJS: Handle empty values in operator<<()
Otherwise something like dbg() << Value(); chokes on
ASSERT_NOT_REACHED() in Value::to_string()
2020-05-13 01:14:22 +02:00
Linus Groh
1383cd23bc LibJS: Add missing keywords/tokens
Some of these are required for syntax we have not implemented yet, some
are future reserved words in strict mode.
2020-05-12 18:47:38 +02:00
Linus Groh
8ffdcce0d0 LibJS: Parse comma operator into SequenceExpression 2020-05-11 18:36:28 +02:00
AnotherTest
bd12f132f3 LibJS: Correct tiny issue with passing a String to String::format
I'm not sure how this even _builds_
2020-05-08 21:02:47 +02:00
AnotherTest
0901b17126 LibJS: Be a bit more explicit about sizeof(buf) / sizeof(FlatPtr)
This (seemingly) no-op cast communicates our intention to clang
2020-05-08 21:02:47 +02:00
Linus Groh
e333b60064 LibJS: Add Array.of() 2020-05-08 20:06:49 +02:00
Linus Groh
ca22476d9d LibJS: Add Array.isArray() 2020-05-08 20:06:49 +02:00
Linus Groh
01fd6ce045 LibJS: Support multiple arguments in Array constructor 2020-05-08 20:06:49 +02:00
Linus Groh
8137f40b73 LibJS: Add Value::is_integer() 2020-05-08 20:06:49 +02:00
Matthew Olsson
532d4bc0ab LibJS: Spec-compliant equality comparisons
The ECMAScript spec defines multiple equality operations which are used
all over the spec; this patch introduces them. Of course, the two
primary equality operations are AbtractEquals ('==') and StrictEquals
('==='), which have been renamed to 'abstract_eq' and 'strict_eq' in
this patch.

In support of the two operations mentioned above, the following have
also been added: SameValue, SameValueZero, and SameValueNonNumeric.
These are important to have, because they are used elsewhere in the spec
aside from the two primary equality comparisons.
2020-05-08 09:49:20 +02:00
Yonatan Goldschmidt
b184f12aaf LibJS: Limit scope of 'for' loop variables
This required 2 changes:
1. In the parser, create a new variable scope, so the variable is
   declared in it instead of the scope in which the 'for' is found.
2. On execute, push the variable into the newly created block. Existing
   code created an empty block (no variables, no arguments) which
   allows Interpreter::enter_scope() to skip the creation of a new
   environment, therefore when the variable initializer is executed, it
   sets the variable to the outer scope. By attaching the variable to
   the new block, the block gets a new environment.

This is only needed for 'let' / 'const' declarations, since 'var'
declarations are expected to leak.

Fixes: #2103
2020-05-07 23:31:49 +02:00
Matthew Olsson
ab652fa1ee LibJS: Add String.raw 2020-05-07 23:05:55 +02:00
Matthew Olsson
b5f1df57ed LibJS: Add raw strings to tagged template literals
When calling a function with a tagged template, the first array that is
passed in now contains a "raw" property with the raw, escaped strings.
2020-05-07 23:05:55 +02:00
Emanuele Torre
6bbd0a18a1 LibJS: Fix shellcheck warnings in Tests/run-tests 2020-05-07 12:20:41 +02:00
Matthew Olsson
107ca2e4ba LibJS: Add function call spreading
Adds support for the following syntax:

    myFunction(...x, ...[1, 2, 3], ...o.foo, ...'abcd')
2020-05-06 19:19:02 +02:00
Matthew Olsson
838390171c LibJS: Function.length respects default and rest parameters
"[Function.length is] the number of formal parameters. This number
excludes the rest parameter and only includes parameters before
the first one with a default value." - MDN
2020-05-06 17:40:56 +02:00
Andreas Kling
025cdfdce8 LibJS: Simplify a Value type check in Object::to_string() 2020-05-06 16:40:08 +02:00
Linus Groh
4d20cf57db LibJS: Implement tagged template literals (foobar)
To make processing tagged template literals easier, template literals
will now add one empty StringLiteral before and after each template
expression *if* there's no other string - e.g.:

`${foo}` -> "", foo, ""
`test${foo}${bar}test` -> "test", foo, "", bar, "test"

This also matches the behaviour of many other parsers.
2020-05-06 14:49:53 +02:00
Linus Groh
eea62dd365 LibJS: Add Value::{is, as}_function() 2020-05-06 14:49:53 +02:00
Matthew Olsson
419bce6915 LibJS: Fix syntax error for arrow function non-decl variable assignment
A regression was introduced in dc9b4da where the parser would
incorrectly parse the assignment of arrow functions to (non-declaration)
variables. For example, consider:

    a = () => {}

Because the parser was aware of default parameters, in
try_parse_arrow_function, the equals sign would be interpreted as a
default argument, leading to incorrect parsing of the overall
expression. Also resulted in some funny behavior
(a = () => {} => {} worked just fine!).

The simple fix is to only look for default parameters if the arrow
function is required to have parenthesis.
2020-05-06 12:21:29 +02:00
Andreas Kling
fc5d0a1bd2 LibJS: Switch objects to unique shape after 100 property additions
At that point, it seems unlikely that the shape is gonna be shared with
other objects, and we avoid getting stuck holding a big bag of shapes.
2020-05-05 18:49:45 +02:00
Linus Groh
72d2bd56ce LibJS: Implement modulo assignment operator (%=) 2020-05-05 11:12:27 +02:00