Commit Graph

191 Commits

Author SHA1 Message Date
Andreas Kling
5599d22744 LibJS: Support calling result of a computed MemberExpression in bytecode
This patch adds support for calls of the form o.f[expr]()
2021-10-25 16:05:04 +02:00
Andreas Kling
a2a8b97c11 LibJS: Simplify MemberExpression::generate_bytecode()
Use the get-from-reference helper in BytecodeGenerator.
2021-10-25 16:04:31 +02:00
Andreas Kling
cfbb69a9cd LibJS: Support more assignment expressions in the bytecode VM
Use the new reference get/put helpers in BytecodeGenerator to support
assignment expressions other than just plain assignment.
2021-10-25 15:29:52 +02:00
Andreas Kling
72736f9e93 LibJS: Generate bytecode for UpdateExpression with MemberExpression arg 2021-10-25 15:17:41 +02:00
Andreas Kling
1dc60b028f LibJS: Add BytecodeGenerator helpers for reference get/put
This allows code sharing between all AST nodes that want to get and/or
put through a reference.
2021-10-25 15:16:22 +02:00
Andreas Kling
d203a86900 LibJS: Always inline the bytecode instruction iterator's operator++ 2021-10-25 13:37:02 +02:00
Andreas Kling
3618ca2420 LibJS: Propagate exceptions across bytecode executable boundaries
To support situations like this:

    function foo() { throw 1; }

    try {
        foo();
    } catch (e) {
    }

Each unwind context now keeps track of its origin executable.

When an exception is thrown, we return from run() immediately if the
nearest unwind context isn't in the current executable.

This causes a natural unwind to the point where we find the
catch/finally block(s) to jump into.
2021-10-25 12:57:21 +02:00
Andreas Kling
049b755123 LibJS: Make bytecode interpreter leave unwind context immediately
We were missing some "break" statements, causing us to actually finish
executing everything within "try" blocks before actually jumping to the
"catch" and/or "finally" blocks.
2021-10-25 12:57:21 +02:00
Andreas Kling
a831cd9cc7 LibJS: Make bytecode VM throw TypeError on attempt to call non-callable
This isn't perfect, but allows us to progress instead of crashing in
the TODO().
2021-10-25 12:57:21 +02:00
Andreas Kling
6fc3c14b7a LibJS: Fix bogus bytecode codegen for "catch" parameters
Add a missing '!' so that catch clauses with a named parameter actually
generate a SetVariable opcode.
2021-10-25 12:57:21 +02:00
Andreas Kling
398c181c79 LibJS: Rename PropertyName to PropertyKey
Let's use the same name as the spec. :^)
2021-10-24 17:18:07 +02:00
Andreas Kling
715e7fada8 LibJS: Add the "fast non-local access" optimization to the bytecode VM
The GetVariable bytecode op now caches environment coordinates for fast
cross-scope variable lookup.
2021-10-24 17:18:07 +02:00
Andreas Kling
da98212001 LibJS: Add a separate "identifier table" to bytecode executables
This is a specialized string table for storing identifiers only.
Identifiers are always FlyStrings, which makes many common operations
faster by allowing O(1) comparison.
2021-10-24 17:18:07 +02:00
Andreas Kling
13f04e37e5 LibJS: Use String and move semantics in Bytecode::StringTable
Avoid creating new AK::String objects when we already have one.
2021-10-24 17:18:07 +02:00
Andreas Kling
3117182c2e LibJS: Implement 'this' in the bytecode VM
ThisExpression now emits a "ResolveThisBinding" bytecode op, which
simply loads the VM's current 'this' binding into the accumulator.
2021-10-24 17:18:06 +02:00
Andreas Kling
7c7bc4f44a LibJS: Alphabetize the bytecode opcode list 2021-10-24 17:18:06 +02:00
Andreas Kling
f75d78f56a LibJS: Include executable name in bytecode dumps 2021-10-24 17:18:06 +02:00
Andreas Kling
c95dde971b LibJS: Move global "should dump bytecode" flag into LibJS
This will allow us to trigger bytecode executable dumps when generating
bytecode inside LibJS as well, not just in clients like js and test-js.
2021-10-24 17:18:06 +02:00
Andreas Kling
da77e2aa4f LibJS: Add Bytecode::Executable::dump()
Let's have a helper for producing a consistent executable dump instead
of repeating the logic in multiple places.
2021-10-24 17:18:05 +02:00
Idan Horowitz
844be7a0a5 LibJS: Convert the RegExpCreate AO to ThrowCompletionOr 2021-10-23 18:01:51 +02:00
Idan Horowitz
e26d9f419b LibJS: Remove vm.construct and it's usages 2021-10-23 02:49:41 +03:00
Idan Horowitz
db5df26841 LibJS: Convert Array AOs to ThrowCompletionOr 2021-10-22 15:07:04 +03:00
Timothy Flynn
7b4814f74c LibJS: Convert IteratorValue AO to ThrowCompletionOr 2021-10-21 00:26:45 +01:00
Timothy Flynn
a64752cd34 LibJS: Convert IteratorComplete AO to ThrowCompletionOr 2021-10-21 00:26:45 +01:00
Timothy Flynn
c981d7b9bd LibJS: Convert IteratorNext AO to ThrowCompletionOr 2021-10-21 00:26:45 +01:00
Timothy Flynn
860a37640b LibJS: Convert GetIterator AO to ThrowCompletionOr 2021-10-21 00:26:45 +01:00
davidot
13ead80ee6 LibJS: Add PrivateEnvironment 2021-10-20 23:19:17 +01:00
Idan Horowitz
545d403f6b LibJS: Convert Value operator AOs to ThrowCompletionOr 2021-10-18 23:06:11 +01:00
Idan Horowitz
48ac15758e LibJS: Convert is_loosely_equal() to ThrowCompletionOr 2021-10-18 23:06:11 +01:00
Idan Horowitz
c488f5a59d LibJS: Convert to_property_key() to ThrowCompletionOr 2021-10-17 12:12:35 +01:00
Idan Horowitz
b8f101888b LibJS: Convert to_numeric() to ThrowCompletionOr 2021-10-17 12:12:35 +01:00
Linus Groh
52976bfac6 LibJS: Convert to_object() to ThrowCompletionOr 2021-10-13 09:55:10 +01:00
Linus Groh
4fa5748093 LibJS: Add an optimization to avoid needless arguments object creation
This gives FunctionNode a "might need arguments object" boolean flag and
sets it based on the simplest possible heuristic for this: if we
encounter an identifier called "arguments" or "eval" up to the next
(nested) function declaration or expression, we won't need an arguments
object. Otherwise, we *might* need one - the final decision is made in
the FunctionDeclarationInstantiation AO.

Now, this is obviously not perfect. Even if you avoid eval, something
like `foo.arguments` will still trigger a false positive - but it's a
start and already massively cuts down on needlessly allocated objects,
especially in real-world code that is often minified, and so a full
"arguments" identifier will be an actual arguments object more often
than not.

To illustrate the actual impact of this change, here's the number of
allocated arguments objects during a full test-js run:

Before:
- Unmapped arguments objects: 78765
- Mapped arguments objects: 2455

After:
- Unmapped arguments objects: 18
- Mapped arguments objects: 37

This results in a ~5% speedup of test-js on my Linux host machine, and
about 3.5% on i686 Serenity in QEMU (warm runs, average of 5).

The following microbenchmark (calling an empty function 1M times) runs
25% faster on Linux and 45% on Serenity:

    function foo() {}
    for (var i = 0; i < 1_000_000; ++i)
        foo();

test262 reports no changes in either direction, apart from a speedup :^)
2021-10-05 10:15:14 +01:00
Linus Groh
364dd42fc8 LibJS: Convert create_data_property_or_throw() to ThrowCompletionOr 2021-10-03 20:14:03 +01:00
Linus Groh
1d45541278 LibJS: Convert Object::set() to ThrowCompletionOr 2021-10-03 20:14:03 +01:00
Linus Groh
b7e5f08e56 LibJS: Convert Object::get() to ThrowCompletionOr
To no one's surprise, this patch is pretty big - this is possibly the
most used AO of all of them. Definitely worth it though.
2021-10-03 20:14:03 +01:00
davidot
715f9666f2 LibJS: Fix that in Bytecode mode functions where not created anymore
This is not a proper fix as we should follow the spec here but it gets
us back to a slightly more working state.
2021-09-30 15:37:56 +01:00
davidot
830ea0414c LibJS: Make scoping follow the spec
Before this we used an ad-hoc combination of references and 'variables'
stored in a hashmap. This worked in most cases but is not spec like.
Additionally hoisting, dynamically naming functions and scope analysis
was not done properly.

This patch fixes all of that by:
  - Implement BindingInitialization for destructuring assignment.
  - Implementing a new ScopePusher which tracks the lexical and var
    scoped declarations. This hoists functions to the top level if no
    lexical declaration name overlaps. Furthermore we do checking of
    redeclarations in the ScopePusher now requiring less checks all over
    the place.
  - Add methods for parsing the directives and statement lists instead
    of having that code duplicated in multiple places. This allows
    declarations to pushed to the appropriate scope more easily.
  - Remove the non spec way of storing 'variables' in
    DeclarativeEnvironment and make Reference follow the spec instead of
    checking both the bindings and 'variables'.
  - Remove all scoping related things from the Interpreter. And instead
    use environments as specified by the spec. This also includes fixing
    that NativeFunctions did not produce a valid FunctionEnvironment
    which could cause issues with callbacks and eval. All
    FunctionObjects now have a valid NewFunctionEnvironment
    implementation.
  - Remove execute_statements from Interpreter and instead use
    ASTNode::execute everywhere this simplifies AST.cpp as you no longer
    need to worry about which method to call.
  - Make ScopeNodes setup their own environment. This uses four
    different methods specified by the spec
    {Block, Function, Eval, Global}DeclarationInstantiation with the
    annexB extensions.
  - Implement and use NamedEvaluation where specified.

Additionally there are fixes to things exposed by these changes to eval,
{for, for-in, for-of} loops and assignment.

Finally it also fixes some tests in test-js which where passing before
but not now that we have correct behavior :^).
2021-09-30 08:16:32 +01:00
davidot
bfc1b4ba61 LibJS: Allow member expressions in binding patterns
Also allows literal string and numbers as property names in object
binding patterns.
2021-09-30 08:16:32 +01:00
Linus Groh
ee8380edea LibJS: Convert internal_own_property_keys() to ThrowCompletionOr 2021-09-29 23:49:53 +01:00
Linus Groh
e37cf73300 LibJS: Rename OrdinaryFunctionObject to ECMAScriptFunctionObject
The old name is the result of the perhaps somewhat confusingly named
abstract operation OrdinaryFunctionCreate(), which creates an "ordinary
object" (https://tc39.es/ecma262/#ordinary-object) in contrast to an
"exotic object" (https://tc39.es/ecma262/#exotic-object).

However, the term "Ordinary Function" is not used anywhere in the spec,
instead the created object is referred to as an "ECMAScript Function
Object" (https://tc39.es/ecma262/#sec-ecmascript-function-objects), so
let's call it that.

The "ordinary" vs. "exotic" distinction is important because there are
also "Built-in Function Objects", which can be either implemented as
ordinary ECMAScript function objects, or as exotic objects (our
NativeFunction).

More work needs to be done to move a lot of infrastructure to
ECMAScriptFunctionObject in order to make FunctionObject nothing more
than an interface for objects that implement [[Call]] and optionally
[[Construct]].
2021-09-25 17:51:30 +02:00
Linus Groh
32932f83be LibJS: Rename {Abstract,Typed => Loosely,Strictly}{Equals,Inequals}
This affects the AST's BinaryOp enum as well as the Bytecode's
ENUMERATE_BYTECODE_OPS and JS_ENUMERATE_COMMON_BINARY_OPS macros.
2021-09-24 09:13:57 +02:00
Linus Groh
580a7e0f7c LibJS: Rename abstract_eq() to is_loosely_equal()
This got turned into a proper AO with a new name recently.

See: https://github.com/tc39/ecma262/commit/c7d6d1c
2021-09-24 09:13:57 +02:00
Linus Groh
c7ff89891c LibJS: Rename strict_eq() to is_strictly_equal()
This got turned into a proper AO with a new name recently.

See: https://github.com/tc39/ecma262/commit/19d7ca4
2021-09-24 09:13:57 +02:00
Idan Horowitz
ab594e5f2f LibJS: Convert Value::invoke and VM::call to ThrowCompletionOr 2021-09-23 23:59:13 +03:00
Linus Groh
35cc579264 LibJS: Also set ExecutionContext::realm in Bytecode::Interpreter::run()
I forgot to consider the bytecode Interpreter when adding a Realm to the
ExecutionContext. This should make it a lot less crashy again :^)
2021-09-13 21:06:18 +01:00
Linus Groh
f29a82dd84 LibJS: Move the GlobalEnvironment from GlobalObject to Realm
This is where the spec wants to have it. Requires a couple of hacks as
currently everything that needs a Realm actually has a GlobalObject, so
we need to go via the Interpreter.
2021-09-12 11:10:20 +01:00
Linus Groh
2b8d5696ab LibJS: Allocate a Realm next to GlobalObject in Interpreter::create()
Also pass a Realm reference to the Bytecode::Interpreter constructor,
just like we pass the GlobalObject.
2021-09-12 11:10:20 +01:00
Timothy Flynn
66264f7c2a LibJS: Change ExecutionContext's arguments list to a MarkedValueList
The test262 tests under RegExp/property-escapes/generated will invoke
Reflect.apply with up to 10,000 arguments at a time. In LibJS, when the
call stack reached VM::call_internal, we transfer those arguments from
a MarkedValueList to the execution context's arguments Vector.

Because these types differ (MarkedValueList is a Vector<Value, 32>), the
arguments are copied rather than moved. By changing the arguments vector
to a MarkedValueList, we can properly move the passed arguments over.

This shaves about 2 seconds off the following test262 test (from 15sec):
  RegExp/property-escapes/generated/General_Category_-_Decimal_Number.js
2021-08-10 23:07:50 +02:00
Brian Gianforcaro
53166c10ca LibJS: Remove unused header includes 2021-08-01 08:10:16 +02:00