Commit graph

251 commits

Author SHA1 Message Date
Linus Groh
9ea6ef4ed1 LibJS: Make Interpreter::throw_exception() a void function
The motivation for this change is twofold:

- Returning a JS::Value is misleading as one would expect it to carry
  some meaningful information, like maybe the error object that's being
  created, but in fact it is always empty. Supposedly to serve as a
  shortcut for the common case of "throw and return empty value", but
  that's just leading us to my second point.
- Inconsistent usage / coding style: as of this commit there are 114
  uses of throw_exception() discarding its return value and 55 uses
  directly returning the call result (in LibJS, not counting LibWeb);
  with the first style often having a more explicit empty value (or
  nullptr in some cases) return anyway.
  One more line to always make the return value obvious is should be
  worth it.

So now it's basically always these steps, which is already being used in
the majority of cases (as outlined above):

- Throw an exception. This mutates interpreter state by updating
  m_exception and unwinding, but doesn't return anything.
- Let the caller explicitly return an empty value, nullptr or anything
  else itself.
2020-08-25 18:30:31 +02:00
Ben Wiederhake
801058e514 LibJS: Soothe gcc about printf-%s on (non-)nullptr 2020-07-28 19:10:10 +02:00
Andreas Kling
aaf6014ae1 LibJS: Simplify Cell::initialize()
Remove the Interpreter& argument and pass only GlobalObject&. We can
find everything we need via the global object anyway.
2020-07-23 17:31:08 +02:00
Matthew Olsson
a51b2393f2 LibJS: Integrate iterator protocol into language features
Finally use Symbol.iterator protocol in language features :) currently
only used in for-of loops and spread expressions, but will have more
uses later (Maps, Sets, Array.from, etc).
2020-07-14 17:58:42 +02:00
Matthew Olsson
51bfc6c6b3 LibJS: Renamed Object::GetOwnPropertyReturnMode to Object::PropertyKind
This enum will be used by iterators, so it makes sense to use a more
general name.
2020-07-11 18:54:13 +02:00
Linus Groh
7241b9ca0c LibJS: Remove a few superfluous exception checks
We don't need to check for exceptions when defining properties on an
array we literally created ourselves a few lines earlier.
2020-07-11 18:38:51 +02:00
Matthew Olsson
7a1d485b19 LibJS: Integrate Symbols into objects as valid keys
This allows objects properties to be created for symbol keys in addition
to just plain strings/numbers
2020-07-09 23:33:00 +02:00
Matthew Olsson
5e971c91e3 LibJS: Hide some debug output behind flags
This hides some Object.cpp output, as well as removing the "debugger"
debug output.
2020-07-06 23:40:35 +02:00
Jack Karamanian
7533fd8b02 LibJS: Initial class implementation; allow super expressions in object
literal methods; add EnvrionmentRecord fields and methods to
LexicalEnvironment

Adding EnvrionmentRecord's fields and methods lets us throw an exception
when |this| is not initialized, which occurs when the super constructor
in a derived class has not yet been called, or when |this| has already
been initialized (the super constructor was already called).
2020-06-29 17:54:54 +02:00
Jack Karamanian
a535d58cac LibJS: Add Object::define_accessor()
This is a helper function based on the getter/setter definition logic from
ObjectExpression::execute() to look up an Accessor property if it already
exists, define a new Accessor property if it doesn't exist, and set the getter or
setter function on the Accessor.
2020-06-29 17:54:54 +02:00
Andreas Kling
32c121a8f7 LibJS: Pass GlobalObject& to Reference get/put 2020-06-20 17:50:48 +02:00
Andreas Kling
8d56e6103e LibJS: Make Value::to_object() take a GlobalObject& 2020-06-20 17:50:48 +02:00
Andreas Kling
a9e4babdaf LibJS: Pass GlobalObject& when constructing an Accessor 2020-06-20 17:50:48 +02:00
Andreas Kling
4aa98052ca LibJS: Remove some more use of Interpreter::global_object()
Let's do some more work towards supporting multiple global objects.
2020-06-20 15:45:07 +02:00
Matthew Olsson
78155a6668 LibJS: Consolidate error messages into ErrorTypes.h
Now, exceptions can be thrown with
interpreter.throw_exception<T>(ErrorType:TYPE, "format", "args",
"here").
2020-06-11 07:46:20 +02:00
Andreas Kling
5042e560ef LibJS: Make more Interpreter functions take a GlobalObject& 2020-06-08 21:25:16 +02:00
Andreas Kling
053863f35e LibJS: Interpreter::this_value() => this_value(GlobalObject&)
Once the Interpreter has no global object attached to it, we have to
provide it everywhere.
2020-06-08 21:12:20 +02:00
Andreas Kling
25f2a29d84 LibJS: Pass GlobalObject& to AST node execute() functions
More work towards supporting multiple global objects.
2020-06-08 21:12:20 +02:00
Matthew Olsson
4e33fbdb67 LibJS: Add interpreter exception checks 2020-06-08 09:57:29 +02:00
Linus Groh
0ff9d7e189 LibJS: Add BigInt 2020-06-07 19:29:40 +02:00
Matthew Olsson
61ac1d3ffa LibJS: Lex and parse regex literals, add RegExp objects
This adds regex parsing/lexing, as well as a relatively empty
RegExpObject. The purpose of this patch is to allow the engine to not
get hung up on parsing regexes. This will aid in finding new syntax
errors (say, from google or twitter) without having to replace all of
their regexes first!
2020-06-07 19:06:55 +02:00
Matthew Olsson
39ad42defd LibJS: Add Proxy objects
Includes all traps except the following: [[Call]], [[Construct]],
[[OwnPropertyKeys]].

An important implication of this commit is that any call to any virtual
Object method has the potential to throw an exception. These methods
were not checked in this commit -- a future commit will have to protect
these various method calls throughout the codebase.
2020-06-06 22:13:01 +02:00
Matthew Olsson
5ad5322f6a LibJS: Distinguish between omitted descriptor attributes and false ones
When calling Object.defineProperty, there is now a difference between
omitting a descriptor attribute and specifying that it is false. For
example, "{}" and "{ configurable: false }" will have different
attribute values.
2020-06-06 22:13:01 +02:00
Marcin Gasperowicz
2579d0bf55 LibJS: Hoist function declarations
This patch adds function declaration hoisting. The mechanism
is similar to var hoisting. Hoisted function declarations are to be put
before the hoisted var declarations, hence they have to be treated
separately.
2020-06-06 10:53:06 +02:00
Marcin Gasperowicz
0b74ea3d6a LibJS: Make typeof return undefined for undefined variables
This makes `typeof i_dont_exist` return `undefined` instead of 
throwing an error.
2020-06-03 19:31:44 +02:00
Sergey Bugaev
53a94b8bbd LibJS: Fix casting a value to ScriptFunction without checking it's one 2020-06-01 17:37:44 +02:00
Jack Karamanian
c12125fa81 LibJS: Track whether ScriptFunctions and FunctionExpressions are arrow
functions
2020-05-30 10:33:24 +02:00
Matthew Olsson
d52ea37717 LibJS: Integrate labels into the Interpreter
The interpreter now considers a statement or block's label when
considering whether or not to break. All statements can be labelled.
2020-05-29 16:20:32 +02:00
Linus Groh
8ff4587f65 LibJS: Throw in strict mode when assigning property to primitive value 2020-05-29 07:45:22 +02:00
Matthew Olsson
5ae9419a06 LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.

The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).

General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).

Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-28 17:17:13 +02:00
Matthew Olsson
dd08c992e8 LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.

Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".

This implies two things about put operations:
    - They will search the prototype chain for setters and call them, if
      necessary.
    - If no property exists with a particular key, the put operation
      should create a new property with the default attributes
      (configurable, writable, and enumerable).

In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).

Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".

Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-27 13:17:35 +02:00
Linus Groh
07af2e6b2c LibJS: Implement basic for..in and for..of loops 2020-05-25 18:45:36 +02:00
Linus Groh
9c8d390682 LibJS: Refactor Accessor
This changes Accessor's m_{getter,setter} from Value to Function* which
seems like a better API to me - a getter/setter must either be a
function or missing, and the creation of an accessor with other values
must be prevented by the parser and Object.defineProperty() anyway.

Also add Accessor::set_{getter,setter}() so we can reuse an already
created accessor when evaluating an ObjectExpression with getter/setter
shorthand syntax.
2020-05-24 18:49:58 +02:00
Matthew Olsson
c35732c011 LibJS: Add object literal getter/setter shorthand
Adds support for the following syntax:

let foo = {
    get x() {
        // ...
    },
    set x(value) {
        // ...
    }
}
2020-05-22 10:59:05 +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
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
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
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
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
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
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
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
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
Linus Groh
72d2bd56ce LibJS: Implement modulo assignment operator (%=) 2020-05-05 11:12:27 +02:00
Linus Groh
a2e1f1a872 LibJS: Implement exponentiation assignment operator (**=) 2020-05-05 11:12:27 +02:00
Linus Groh
3e754a15d4 LibJS: Implement bitwise assignment operators (&=, |=, ^=) 2020-05-05 11:12:27 +02:00
Linus Groh
454c1e6bbe LibJS: Implement rest parameters 2020-05-04 23:30:52 +02:00
Linus Groh
d4bfcea570 LibJS: Add indentation to sections in SwitchCase::dump()
This now matches the output of

Program
  (Variables)
    ...
  (Children)
    ...

or

FunctionDeclaration 'foo'
  (Parameters)
    ...
  (Body)
    ...

etc.

Also don't print each consequent statement index, it doesn't add any
value.
2020-05-04 21:42:47 +02:00
mattco98
adb4accab3 LibJS: Add template literals
Adds fully functioning template literals. Because template literals
contain expressions, most of the work has to be done in the Lexer rather
than the Parser. And because of the complexity of template literals
(expressions, nesting, escapes, etc), the Lexer needs to have some
template-related state.

When entering a new template literal, a TemplateLiteralStart token is
emitted. When inside a literal, all text will be parsed up until a '${'
or '`' (or EOF, but that's a syntax error) is seen, and then a
TemplateLiteralExprStart token is emitted. At this point, the Lexer
proceeds as normal, however it keeps track of the number of opening
and closing curly braces it has seen in order to determine the close
of the expression. Once it finds a matching curly brace for the '${',
a TemplateLiteralExprEnd token is emitted and the state is updated
accordingly.

When the Lexer is inside of a template literal, but not an expression,
and sees a '`', this must be the closing grave: a TemplateLiteralEnd
token is emitted.

The state required to correctly parse template strings consists of a
vector (for nesting) of two pieces of information: whether or not we
are in a template expression (as opposed to a template string); and
the count of the number of unmatched open curly braces we have seen
(only applicable if the Lexer is currently in a template expression).

TODO: Add support for template literal newlines in the JS REPL (this will
cause a syntax error currently):

    > `foo
    > bar`
    'foo
    bar'
2020-05-04 16:46:31 +02:00
Linus Groh
25cf0da2fb LibJS: Set name of anonymous functions during assignment 2020-05-03 11:41:56 +02:00
Matthew Olsson
5e66f1900b LibJS: Add function default arguments
Adds the ability for function arguments to have default values. This
works for standard functions as well as arrow functions. Default values
are not printed in a <function>.toString() call, as nodes cannot print
their source string representation.
2020-05-03 00:44:57 +02:00
Linus Groh
43c1fa9965 LibJS: Implement (no-op) debugger statement 2020-05-01 22:07:13 +02:00
Linus Groh
79b829637e LibJS: Implement most of the Reflect object 2020-05-01 16:54:01 +02:00
Linus Groh
fc34123a54 LibJS: Fix ConditionalExpression::dump()
Let's not print m_test three times :^)
2020-04-30 08:43:38 +02:00
mattco98
95abcc3722 LibJS: Implement correct object property ordering
This commit introduces a way to get an object's own properties in the
correct order. The "correct order" for JS object properties is first all
array-like index properties (numeric keys) sorted by insertion order,
followed by all string properties sorted by insertion order.

Objects also now print correctly in the repl! Before this commit:

courage ~/js-tests $ js
> ({ foo: 1, bar: 2, baz: 3 })
{ bar: 2, foo: 1, baz: 3 }

After:

courage ~/js-tests $ js
> ({ foo: 1, bar: 2, baz: 3 })
{ foo: 1, bar: 2, baz: 3 }
2020-04-29 18:47:03 +02:00
Andreas Kling
698652a548 LibJS: Make Value::as_string() return a PrimitiveString reference 2020-04-29 12:35:39 +02:00
Andreas Kling
75f246dde8 LibJS: Make StringObject::primitive_string() return a reference 2020-04-29 12:33:28 +02:00
Linus Groh
8ad11df89f LibJS: Handle exception in for loop test execution 2020-04-28 23:11:18 +02:00
mattco98
104969a9f5 LibJS: Add spreading in object literals
Supports spreading strings, arrays, and other objects within object
literals.
2020-04-28 20:37:21 +02:00
Linus Groh
30fe1b5d58 LibJS: Support spreading of strings and string objects 2020-04-28 15:35:24 +02:00
Andreas Kling
24cce3674b LibJS: Support o.f++ :^)
This patch teaches UpdateExpression how to use a Reference. Some other
changes were necessary to keep tests working:
A Reference can now also refer to a local or global variable. This is
not fully aligned with the spec since we don't have a Record concept.
2020-04-28 15:07:08 +02:00
Andreas Kling
ee0bf55127 LibJS: Make AssignmentExpression assign through a Reference
Reference now has assign(Interpreter&, Value) which is used to write
transparently through a Reference into whatever location it refers to.
2020-04-28 15:07:08 +02:00
Andreas Kling
3c4a9e421f LibJS: Allow "delete someGlobalVariable"
This is solved by allowing Identifier nodes to produce a Reference with
the global object as base.
2020-04-28 15:07:08 +02:00
Andreas Kling
67b8e6fc5b LibJS: Add Reference class to represent a base.property reference
Expression nodes can now be asked to produce a Reference. We then use
this to implement the "delete" operator without downcasting the child
node to a MemberExpression manually.
2020-04-28 15:07:08 +02:00
mattco98
80fecc615a LibJS: Add spreading in array literals
Implement the syntax and behavor necessary to support array literals
such as [...[1, 2, 3]]. A type error is thrown if the target of the
spread operator does not evaluate to an array (though it should
eventually just check for an iterable).

Note that the spread token's name is TripleDot, since the '...' token is
used for two features: spread and rest. Calling it anything involving
'spread' or 'rest' would be a bit confusing.
2020-04-27 11:32:18 +02:00
Andreas Kling
f897c41092 LibJS: Implement basic support for the "delete" operator
It turns out "delete" is actually a unary op :)
This patch implements deletion of object properties, it doesn't yet
work for casually deleting properties from the global object.

When deleting a property from an object, we switch that object to
having a unique shape, no longer sharing shapes with others.
Once an object has a unique shape, it no longer needs to care about
shape transitions.
2020-04-26 15:51:07 +02:00
Andreas Kling
35aea2e454 LibJS: Stop using Optional<Value> in favor of Value's empty state
JS::Value already has the empty state ({} or Value() gives you one.)
Use this instead of wrapping Value in Optional in some places.
I've also added Value::value_or(Value) so you can easily provide a
fallback value when one is not present.
2020-04-25 18:45:22 +02:00
Linus Groh
57caca3171 LibJS: Fix ObjectExpression::execute()
Change from code review changed key() and value() getters - forgot to
update this...
2020-04-24 00:02:07 +02:00
Linus Groh
746dd5b190 LibJS: Implement computed properties in object expressions 2020-04-23 23:56:04 +02:00
Linus Groh
11728b7db5 LibJS: Implement 'in' operator 2020-04-23 19:38:13 +02:00
Linus Groh
396ecfa2d7 LibJS: Implement bitwise unsigned right shift operator (>>>) 2020-04-23 19:38:13 +02:00
Linus Groh
502d1f5165 LibJS: Implement bitwise right shift operator (>>) 2020-04-23 19:38:13 +02:00
Linus Groh
f0e7404480 LibJS: Implement bitwise left shift operator (<<) 2020-04-23 19:38:13 +02:00
Linus Groh
a1b820b11c LibJS: Improve UpdateExpression::execute()
- Let undefined variables throw a ReferenceError by using
  Identifier::execute() rather than doing variable lookup manually and
  ASSERT()ing
- Coerce value to number rather than ASSERT()ing
- Make code DRY
- Add tests
2020-04-22 13:09:59 +02:00
Jack Karamanian
1fa0c7304d LibJS: Implement Function.prototype.bind() 2020-04-21 12:23:07 +02:00
Jack Karamanian
b3800829da LibJS: Prepend callee's bound arguments to the CallFrame and set the
this_value to the callee's bound |this| in CallExpression::execute()
2020-04-21 12:23:07 +02:00
Andreas Kling
f7a1696087 LibJS: Add MarkedValueList and use it for argument passing
A MarkedValueList is basically a Vector<JS::Value> that registers with
the Heap and makes sure that the stored values don't get GC'd.

Before this change, we were unsafely keeping Vector<JS::Value> in some
places, which is out-of-reach for the live reference finding logic
since Vector puts its elements on the heap by default.

We now pass all the JavaScript tests even when running with "js -g",
which does a GC on every heap allocation.
2020-04-19 17:34:33 +02:00
Andreas Kling
b251091a6a LibJS: CallExpression shouldn't throw TypeError or non-constructor call
This unbreaks the entire test suite. :^)
2020-04-19 14:51:47 +02:00
Linus Groh
03f2024b6e LibJS: Improve CallExpression::execute()'s error messages 2020-04-19 11:00:39 +02:00
Linus Groh
d14ddb6461 LibJS: Implement nullish coalescing operator (??) 2020-04-18 12:34:13 +02:00
Andreas Kling
bc1ece7f37 LibJS+LibWeb: Pass prototype to Object constructor
Everyone who constructs an Object must now pass a prototype object when
applicable. There's still a fair amount of code that passes something
fetched from the Interpreter, but this brings us closer to being able
to detach prototypes from Interpreter eventually.
2020-04-18 11:00:55 +02:00
Andreas Kling
f6d57c82f6 LibJS: Pass prototype to Function constructors 2020-04-18 10:28:22 +02:00
Andreas Kling
2d7b495244 LibJS: Make Array constructor take its prototype
Let's start moving towards native JS objects taking their prototype as
a constructor argument.

This will eventually allow us to move prototypes off of Interpreter and
into GlobalObject.
2020-04-18 10:28:22 +02:00
Andreas Kling
ed80952cb6 LibJS: Introduce LexicalEnvironment
This patch replaces the old variable lookup logic with a new one based
on lexical environments.

This brings us closer to the way JavaScript is actually specced, and
also gives us some basic support for closures.

The interpreter's call stack frames now have a pointer to the lexical
environment for that frame. Each lexical environment can have a chain
of parent environments.

Before calling a Function, we first ask it to create_environment().
This gives us a new LexicalEnvironment for that function, which has the
function's lexical parent's environment as its parent. This allows
inner functions to access variables in their outer function:

    function foo() { <-- LexicalEnvironment A
        var x = 1;
        function() { <-- LexicalEnvironment B (parent: A)
            console.log(x);
        }
    }

If we return the result of a function expression from a function, that
new function object will keep a reference to its parent environment,
which is how we get closures. :^)

I'm pretty sure I didn't get everything right here, but it's a pretty
good start. This is quite a bit slower than before, but also correcter!
2020-04-15 22:07:20 +02:00
Linus Groh
cea950fd70 LibJS: Support empty values in array expression 2020-04-15 21:23:06 +02:00
Linus Groh
d30db07048 LibJS: Implement void operator 2020-04-15 19:12:51 +02:00
Andreas Kling
63499c2c9f LibJS: Pass the Interpreter& to binary/logical/unary helpers 2020-04-15 09:28:41 +02:00
Linus Groh
9fab52a390 LibJS: Remove shift, pop, push functions from Array object
This abstraction isn't really that useful, as we can access the
underlying Vector<Value> using elements() and operate on it directly.
2020-04-14 13:40:04 +02:00
Andreas Kling
062d6af16e LibJS: Remove Interpreter::declare_variable()
Since declarations are now hoisted and handled on scope entry, the job
of a VariableDeclaration becomes to actually initialize variables.

As such, we can remove the part where we insert variables into the
nearest relevant scope. Less work == more speed! :^)
2020-04-13 17:22:24 +02:00
Andreas Kling
ac7459cb40 LibJS: Hoist variable declarations to the nearest relevant scope
"var" declarations are hoisted to the nearest function scope, while
"let" and "const" are hoisted to the nearest block scope.

This is done by the parser, which keeps two scope stacks, one stack
for the current var scope and one for the current let/const scope.

When the interpreter enters a scope, we walk all of the declarations
and insert them into the variable environment.

We don't support the temporal dead zone for let/const yet.
2020-04-13 17:22:23 +02:00
Stephan Unverwerth
f8f65053bd LibJS: Parse "this" as ThisExpression 2020-04-13 00:45:25 +02:00
Linus Groh
eece424694 LibJS: Make Function and CallFrame aware of their function name 2020-04-11 14:10:42 +02:00
Andreas Kling
e5da1cc566 LibJS: Throw real TypeError, ReferenceError, etc objects
Instead of just throwing Error objects with a name string, we now throw
the real Error subclass types. :^)
2020-04-10 13:09:35 +02:00
Barney Wilks
56474bab15 LibJS: Throw exception if LHS of assignment is of unexpected type 2020-04-10 11:27:10 +02:00
Emanuele Torre
38dfd04633 LibJS: rename JS::DeclarationType => JS::DeclarationKind
Many other parsers call it with this name.

Also Type can be confusing in this context since the DeclarationType is
not the type (number, string, etc.) of the variables that are being
declared by the VariableDeclaration.
2020-04-08 14:50:14 +02:00
Andreas Kling
f07f8d5a44 LibJS: Add "constructor" property to constructor prototypes 2020-04-08 11:08:07 +02:00