Commit graph

251 commits

Author SHA1 Message Date
Andreas Kling
19cbfaee54 LibJS: Add SequenceExpression AST node (comma operator)
This patch only adds the AST node, the parser doesn't create them yet.
2020-04-07 15:56:26 +02:00
DexesTTP
4a9485f830 LibJS: Fix impossible member access for negative integers
The PropertyName class able to match a number or an array can only
accept positive numerical values. However, the computed_property_name
method sometimes returned negative values.

This commit also adds a basic object access test case.
2020-04-06 21:45:20 +02:00
Andreas Kling
bdffc9e7fb LibJS: Support array holes, encoded as empty JS::Value
This patch adds a new kind of JS::Value, the empty value.
It's what you get when you do JSValue() (or most commonly, {} in C++.)

An empty Value signifies the absence of a value, and should never be
visible to JavaScript itself. As of right now, it's used for array
holes and as a return value when an exception has been thrown and we
just want to unwind.

This patch is a bit of a mess as I had to fix a whole bunch of code
that was relying on JSValue() being undefined, etc.
2020-04-06 20:27:44 +02:00
Andreas Kling
5495f06af5 LibJS: Give argument vectors an inline capacity of 8
This avoids one malloc/free pair for every function call if there are
8 arguments or fewer.
2020-04-06 19:22:12 +02:00
Andreas Kling
be019f28ca LibJS: Add a PropertyName class that represents a string or a number
Now that we have two separate storages for Object properties depending
on what kind of index they have, it's nice to have an abstraction that
still allows us to say "here's a property name".

We use PropertyName to always choose the optimal storage path directly
while interpreting the AST. :^)
2020-04-06 18:09:26 +02:00
Andreas Kling
6e7713a5f4 LibJS: Remove unnecessary malloc+free in AssignmentExpression::execute
We were creating a temporary AK::Function for no good reason, and this
was dominating profiles. Reorganize the code so it's not necessary.
2020-04-06 08:26:26 +02:00
Linus Groh
0403845d3e LibJS: Implement exponentiation (** operator) 2020-04-05 15:32:06 +02:00
Linus Groh
eafd3dbaf8 LibJS: Rename BinaryOp::{Plus,Minus,Asterisk,Slash} 2020-04-05 15:31:12 +02:00
Andreas Kling
3c99c27db4 LibJS: Clean up the anonymous wrapper block in "for" using ScopeGuard
This ensures that we don't forget to pop the wrapper off of the scope
stack when returning early due to an exception or some such. :^)
2020-04-05 00:24:32 +02:00
Andreas Kling
9ebd066ac8 LibJS: Add support for "continue" inside "for" statements :^) 2020-04-05 00:22:42 +02:00
Andreas Kling
e3b92caa6d LibJS: Make "break" actually work inside "switch" 2020-04-05 00:09:48 +02:00
Andreas Kling
5e40aa182b LibJS: Support VariableDeclaration with multiple declarators
This patch adds support in the parser and interpreter for this:

    var a = 1, b = 2, c = a + b;

VariableDeclaration is now a sequence of VariableDeclarators. :^)
2020-04-04 21:47:12 +02:00
Andreas Kling
f8393b80e3 LibJS: Add support for do..while statements 2020-04-04 21:29:23 +02:00
Andreas Kling
da0715aba9 LibJS: Rename WhileStatement::predicate() => body()
This name matches other parsers.
2020-04-04 21:22:03 +02:00
Andreas Kling
644ff1bbfd LibJS: Add basic support for modulo (%) in binary expressions 2020-04-04 21:17:34 +02:00
Linus Groh
2944039d6b LibJS: Add Function() and Function.prototype 2020-04-04 15:58:49 +02:00
Andreas Kling
faac43597a LibJS: Add js_string(Interpreter&, String) 2020-04-04 12:58:05 +02:00
Stephan Unverwerth
520311eb8b LibJS: Add short circuit logical evaluation
When evaluating logical binop expressions, the rhs must not be
evaluated if the lhs leads to the whole expression not being
truthy.
2020-04-03 19:17:52 +02:00
Linus Groh
6e5f9e20eb LibJS: Fix logical expressions
The current implementation of logical AND (&&) and logical OR (||)
always returns a boolean - this is not correct.
2020-04-03 16:32:36 +02:00
Andreas Kling
0622181d1f LibJS: Implement ConditionalExpression (ternary "?:" operator) 2020-04-03 12:15:14 +02:00
Linus Groh
2636cac6e4 LibJS: Remove UndefinedLiteral, add undefined to global object
There is no such thing as a "undefined literal" in JS - undefined is
just a property on the global object with a value of undefined.
This is pretty similar to NaN.

var undefined = "foo"; is a perfectly fine AssignmentExpression :^)
2020-04-03 00:10:52 +02:00
Linus Groh
a62230770b LibJS: Implement unary plus / minus 2020-04-02 19:55:45 +02:00
Jack Karamanian
bb15b37228 LibJS: Evaluate CallExpression arguments before pushing a CallFrame 2020-04-02 08:50:28 +02:00
Andreas Kling
1549c5c48b LibJS: Make Value::as_object() return Object&
Let's move towards using references over pointers in LibJS as well.
I had originally steered away from it because that's how I've seen
things done in other engines. But this is not the other engines. :^)
2020-04-01 22:18:47 +02:00
Linus Groh
849e2c77e4 LibJS: Implement constructor/non-constructor function calls
This adds Function::construct() for constructor function calls via `new`
keyword. NativeFunction doesn't have constructor behaviour by default,
ScriptFunction simply calls call() in construct()
2020-04-01 20:18:36 +02:00
Andreas Kling
cd9379dca9 LibJS: Reorganize computing of |this| for CallExpressions
This avoids executing the LHS of the object expression twice when doing
a call on the result of an object expression.
2020-04-01 18:57:00 +02:00
Linus Groh
fb0401871c LibJS: Throw TypeError when calling non-function object
We now have "undefined is not a function" :^)
2020-03-30 14:43:58 +02:00
Andreas Kling
2285f84596 LibJS: Implement basic execution of "switch" statements
The "break" keyword now unwinds to the nearest ScopeType::Breakable.
There's no support for break labels yet, but we'll get there too.
2020-03-29 15:03:58 +02:00
Andreas Kling
1923051c5b LibJS: Lexer and parser support for "switch" statements 2020-03-29 15:03:58 +02:00
Andreas Kling
7c4e53f31e LibJS: Rework how native functions are called to improve |this| value
Native functions now only get the Interpreter& as an argument. They can
then extract |this| along with any indexed arguments it wants from it.

This forces functions that want |this| to actually deal with calling
interpreter.this_value().to_object(), and dealing with the possibility
of a non-object |this|.

This is still not great but let's keep massaging it forward.
2020-03-28 22:51:09 +01:00
Andreas Kling
a3d92b1210 LibJS: Implement the "instanceof" operator
This operator walks the prototype chain of the RHS value and looks for
a "prototype" property with the same value as the prototype of the LHS.

This is pretty cool. :^)
2020-03-28 16:56:54 +01:00
Andreas Kling
37fe16a99c LibJS: Add Function.prototype and make "new" Objects delegate to it 2020-03-28 16:40:36 +01:00
Andreas Kling
0593ce406b LibJS: Implement basic support for the "new" keyword
NewExpression mostly piggybacks on the existing CallExpression. The big
difference is that "new" creates a new Object and passes it as |this|
to the callee.
2020-03-28 16:33:52 +01:00
Andreas Kling
0d2fb306af LibJS: Check for exceptions in a lot more places 2020-03-27 15:36:00 +01:00
Andreas Kling
c60dc84a33 LibJS: Allow function calls with missing arguments
We were interpreting "undefined" as a variable lookup failure in some
cases and throwing a ReferenceError exception instead of treating it
as the valid value "undefined".

This patch wraps the result of variable lookup in Optional<>, which
allows us to only throw ReferenceError when lookup actually fails.
2020-03-27 12:56:05 +01:00
Andreas Kling
4802413f71 LibJS: Say "return {}" instead of "return js_undefined()" in AST nodes
This looks a bit nicer somehow.
2020-03-26 12:36:52 +01:00
Andreas Kling
ad6ede7ee4 LibJS: Make FunctionDeclaration return undefined
FunctionExpression returns a JS::Function, while FunctionDeclaration
declares one in the relevant scope.
2020-03-26 12:27:15 +01:00
Andreas Kling
6c9d2cfa5e LibJS: Handle "for" statements with empty initializer and updater 2020-03-25 16:14:51 +01:00
Andreas Kling
faddf3a1db LibJS: Implement "throw"
You can now throw an expression to the nearest catcher! :^)

To support throwing arbitrary values, I added an Exception class that
sits as a wrapper around whatever is thrown. In the future it will be
a logical place to store a call stack.
2020-03-24 22:21:58 +01:00
Andreas Kling
343e224aa8 LibJS: Implement basic exception throwing
You can now throw exceptions by calling Interpreter::throw_exception().
Anyone who calls ASTNode::execute() needs to check afterwards if the
Interpreter now has an exception(), and if so, stop what they're doing
and simply return.

When catching an exception, we'll first execute the CatchClause node
if present. After that, we'll execute the finalizer block if present.

This is unlikely to be completely correct, but it's a start! :^)
2020-03-24 16:14:10 +01:00
Andreas Kling
c33d4aefc3 LibJS: Parse "try", "catch" and "finally"
This is the first step towards support exceptions. :^)
2020-03-24 16:14:10 +01:00
Andreas Kling
494df52961 LibJS: Actually leave the current function scope on "return"
We now unwind until the nearest function-level scope on the scope stack
when executing a return statement.
2020-03-23 19:22:24 +01:00
Andreas Kling
cccbe43056 LibJS: Use FlyString for identifiers
This makes variable and property lookups a lot faster since comparing
two FlyStrings is O(1).
2020-03-22 13:03:43 +01:00
Andreas Kling
7c48c3c8e1 LibJS: Parse "if" statements
This patch implements basic parsing of "if" statements. We don't yet
support parsing "else", so I added a FIXME about that.
2020-03-21 18:40:17 +01:00
Andreas Kling
00feef8642 LibJS: Some optimizations for ObjectExpression
- move() the property map when constructing ObjectExpression instead of
  making a copy.
- Use key+value iterators to traverse the property map in the execute()
  and dump() functions.
2020-03-21 13:11:51 +01:00
0xtechnobabble
bc002f807a LibJS: Parse object expressions 2020-03-21 10:08:58 +01:00
Andreas Kling
bceabd7c4b LibJS: Add ArrayPrototype and implement Array.prototype.push()
This function is ultimately supposed to be generic and allow any |this|
that has a length property, but for now it only works on our own Array
object type.
2020-03-20 21:56:40 +01:00
Andreas Kling
a3d2e07446 LibJS: Parse computed MemberExpressions
MemberExpression comes in two flavors:

    computed: a[b]
non-computed: a.b

We can now parse both of the types. :^)
2020-03-20 21:56:40 +01:00
Andreas Kling
a82f64d3d6 LibJS: Parse ArrayExpression and start implementing Array objects
Note that property lookup is not functional yet.
2020-03-20 21:56:40 +01:00
Andreas Kling
5db8940c9e LibJS: Use StringBuilder::join() 2020-03-20 14:41:23 +01:00