Commit graph

494 commits

Author SHA1 Message Date
Gal Horowitz
b47d117bd2 LibJS: Add bytecode generation for simple ObjectExpressions
Bytecode is now generated for object literals which only contain simple
key-value pairs (i.e. no spread, getters or setters)
2021-06-11 22:44:17 +02:00
Linus Groh
862ba64037 LibJS: Implement the Error Cause proposal
Currently stage 3. https://github.com/tc39/proposal-error-cause
2021-06-11 21:34:05 +01:00
Linus Groh
8d77a3297a LibJS: Improve Error/NativeError tests
Some of this stuff is already tested properly in the name and message
prototype tests, so let's focus on covering all error types here as well
instead.
2021-06-11 21:34:05 +01:00
Linus Groh
cbd7437d40 LibJS: Implement AggregateError 2021-06-11 18:49:50 +01:00
Linus Groh
2f03eb8628 LibJS: Only initialize in add_constructor() if not already done 2021-06-11 18:49:50 +01:00
Linus Groh
ad3242bab7 LibJS: Rename JS_ENUMERATE_{ERROR_SUBCLASSES => NATIVE_ERRORS}
The fact that they *are* subclasses is an implementation detail and
should not be highlighted. The spec calls these NativeErrors, so let's
use that.
Also added a comment explaining *why* they inherit from Error - I was
about to change that :^)
2021-06-11 18:49:50 +01:00
Gal Horowitz
0e10dec324 LibJS: Parse only AssignmentExpressions in ComputedPropertyNames
The property name in an object literal can either be a literal or a
computed name, in which case any AssignmentExpression can be used, we
now only parse AssignmentExpression instead of the previous incorrect
behaviour which allowed any Expression (Specifically, comma
expressions).
2021-06-11 17:25:14 +01:00
Ali Mohammad Pur
8b3f8879c1 LibJS: Use an enum class instead of 'bool is_generator'
This avoid confusion in the order of the multiple boolean parameters
that exist.
2021-06-11 19:42:58 +04:30
Idan Horowitz
f5a978c1aa LibWeb: Set a detach key for ArrayBuffers returned from WASM
As required by the specification:
`Set buffer.[[ArrayBufferDetachKey]] to "WebAssembly.Memory".`
2021-06-11 13:38:25 +01:00
Marcin Gasperowicz
a64089092f LibJS: Implement bytecode generation for switch 2021-06-11 13:12:26 +02:00
Andreas Kling
af48a066c6 LibJS: Add bytecode generation for FunctionExpression :^) 2021-06-11 10:46:46 +02:00
Andreas Kling
749a3b9245 LibJS: Move is_arrow_function() from FunctionExpression to FunctionNode
This will make it easier to write bytecode generation for function
expressions in just a moment.
2021-06-11 10:45:49 +02:00
Linus Groh
17da54d49c LibJS: Fix two accidentally incorrect ScriptFunction constructions
The addition of an is_generator parameter broke this, as is_strict was
being passed in, causing an assertion.
This is being addressed by changing it to an enum in #7981, but in the
meantime let's just fix these two cases.
2021-06-11 01:21:46 +01:00
Linus Groh
f218a4b548 LibJS: Set Error message attributes to writable and configurable only
20.5.1.1 Error ( message )

    When the Error function is called with argument message, the
    following steps are taken:

    [...]
    3b. Let msgDesc be the PropertyDescriptor {
        [[Value]]: msg,
        [[Writable]]: true,
        [[Enumerable]]: false,
        [[Configurable]]: true
    }.
    3c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc).
2021-06-11 00:23:13 +01:00
Linus Groh
0e38c9b2f7 LibJS: Set NativeError constructors' prototype to Error constructor
The FunctionPrototype is correct for ErrorConstructor itself:

    20.5.2 Properties of the Error Constructor

    The Error constructor:
    - has a [[Prototype]] internal slot whose value is
      %Function.prototype%.

However, not for all the other "NativeError" constructors:

    20.5.6.2 Properties of the NativeError Constructors

    Each NativeError constructor:
    - has a [[Prototype]] internal slot whose value is %Error%.
2021-06-11 00:23:13 +01:00
Andreas Kling
9ee5029bc5 LibJS: Basic bytecode support for computed member expressions
Expressions like foo[1 + 2] now work, and you can assign to them
as well! :^)
2021-06-11 00:36:18 +02:00
Ali Mohammad Pur
b47246ec70 LibJS: Switch AST.{h,cpp} to east const 2021-06-11 00:30:09 +02:00
Ali Mohammad Pur
3234697eca LibJS: Implement generator functions (only in bytecode mode) 2021-06-11 00:30:09 +02:00
Ali Mohammad Pur
c53a86a3fe LibJS: Resolve the `this' value in call expression bytecode 2021-06-11 00:30:09 +02:00
Ali Mohammad Pur
4cfdfb6a88 LibJS: Automatically split linear bytecode into multiple blocks
...instead of crashing :^)
2021-06-11 00:30:09 +02:00
Idan Horowitz
8527f00065 LibJS: Allow and check for detached ArrayBuffers
This is required by the specification and will be used for the
$262.detachArrayBuffer method in test262.
2021-06-10 22:44:26 +01:00
Idan Horowitz
7d6db3f09b LibJS: Throw TypeError on non-object this value in ArrayBuffer methods
`1. If Type(O) is not Object, throw a TypeError exception.`
2021-06-10 22:44:26 +01:00
Idan Horowitz
bc95201aaf LibJS: Dont mask non-RangeError exceptions in ArrayBuffer construction
Non-RangeError exceptions can be thrown by user implementations of
valueOf (which are called by to_index), and the specification disallows
changing the type of the thrown error.
2021-06-10 22:44:26 +01:00
Idan Horowitz
8dc86c6aad LibJS: Bring ArrayBuffer.prototype.slice closer to spec
The exception order was incorrect in the old implementation, and it
did not use the Symbol.species constructor as required by the spec.
2021-06-10 22:44:26 +01:00
Idan Horowitz
b041108a1e LibJS: Explicitly return and accept a Function* in species_constructor
The second argument (the default constructor) and the return value have
to be constructors (as a result functions), so we can require that
explicitly by using appropriate types.
2021-06-10 22:44:26 +01:00
Andreas Kling
93a07ba962 LibJS: Remove GlobalObject& argument from VM::construct()
We can just get the global object from the constructor function.
2021-06-10 23:17:29 +02:00
Andreas Kling
f5feb1d2cd LibJS: Very basic support for "new" construction in bytecode VM
This patch adds a CallType to the Bytecode::Op::Call instruction,
which can be either Call or Construct. We then generate Construct
calls for the NewExpression AST node.

When executed, these get fed into VM::construct().
2021-06-10 23:01:49 +02:00
Andreas Kling
c3c68399b5 LibJS: Generate bytecode for entering nested lexical environments
This adds a new PushLexicalEnvironment instruction that creates a new
LexicalEnvironment and pushes it on the VM's scope stack.

There is no corresponding PopLexicalEnvironment instruction yet,
so this will behave incorrectly with let/const scopes for example.
2021-06-10 22:20:23 +02:00
Andreas Kling
d560ee118d LibJS: Allocate 4 KiB for Bytecode::BasicBlock
There's no reason not to, since we're using mmap() for these anyway
and that gives us memory in 4 KiB increments. :^)
2021-06-10 21:59:49 +02:00
Andreas Kling
22c803d8e5 LibJS: Always keep the global object in bytecode VM register $1 2021-06-10 21:59:49 +02:00
Andreas Kling
b3e6a6c1cd LibJS: Perform function instantiation in bytecode
This replaces Bytecode::Op::EnterScope with a new NewFunction op that
instantiates a ScriptFunction from a given FunctionNode (AST).

This is then used to instantiate the local functions directly from
bytecode when entering a ScopeNode. :^)
2021-06-10 21:59:49 +02:00
Andreas Kling
941be2dcc2 LibJS: Add empty bytecode generation for VariableDeclaration
These will be partly handled by the relevant ScopeNode due to
hoisting, same basic idea as function declarations.

VariableDeclaration needs to do some work, but let's stub it out
first and start empty.
2021-06-10 21:59:49 +02:00
Gunnar Beutner
67cc31a74f LibJS: Implement bytecode generation for try..catch..finally
EnterUnwindContext pushes an unwind context (exception handler and/or
finalizer) onto a stack.

LeaveUnwindContext pops the unwind context from that stack.

Upon return to the interpreter loop we check whether the VM has an
exception pending. If no unwind context is available we return from the
loop. If an exception handler is available we clear the VM's exception,
put the exception value into the accumulator register, clear the unwind
context's handler and jump to the handler. If no handler is available
but a finalizer is available we save the exception value + metadata (for
 later use by ContinuePendingUnwind), clear the VM's exception, pop the
unwind context and jump to the finalizer.

ContinuePendingUnwind checks whether a saved exception is available. If
no saved exception is available it jumps to the resume label. Otherwise
it stores the exception into the VM.

The Jump after LeaveUnwindContext could be integrated into the
LeaveUnwindContext instruction. I've kept them separate for now to make
the bytecode more readable.

> try { 1; throw "x" } catch (e) { 2 } finally { 3 }; 4
1:
[   0] EnterScope
[  10] EnterUnwindContext handler:@4 finalizer:@3
[  38] EnterScope
[  48] LoadImmediate 1
[  60] NewString 1 ("x")
[  70] Throw
<for non-terminated blocks: insert LeaveUnwindContext + Jump @3 here>
2:
[   0] LoadImmediate 4
3:
[   0] EnterScope
[  10] LoadImmediate 3
[  28] ContinuePendingUnwind resume:@2
4:
[   0] SetVariable 0 (e)
[  10] EnterScope
[  20] LoadImmediate 2
[  38] LeaveUnwindContext
[  3c] Jump @3

String Table:
0: e
1: x
2021-06-10 21:59:46 +02:00
Gunnar Beutner
319a60043b LibJS: Let the bytecode interpreter set the VM's last value 2021-06-10 21:47:25 +02:00
xyanrch
a0412e0d5e LibJS: Implement bytecode generation for BreakStatement 2021-06-10 21:48:20 +04:30
Luke
00a83a2957 LibJS: Make removed elements in Array.prototype.splice spec compliant
It wasn't using has_property, was directly appending to indexed
properties and wasn't setting the length.
2021-06-10 10:04:06 +01:00
Luke
bc540de0af LibJS: Pass in actual_delete_count to removed array creation in splice
More specifically, Array.prototype.splice. Additionally adds a missing
exception check to the array creation and a link to the spec.

Fixes create-non-array-invalid-len.js in the splice tests in test262.
This test timed out instead of throwing an "Invalid array length"
exception.
2021-06-10 10:04:06 +01:00
Idan Horowitz
f63ef4f196 LibJS: Stop asserting in {Set,Test}IntegrityLevel on missing descriptor
As per the specification (7.3.15 SetIntegrityLevel):
i. Let currentDesc be ? O.[[GetOwnProperty]](k).
ii. If currentDesc is not undefined, then...
2021-06-10 09:20:05 +01:00
Linus Groh
b639bf64f2 LibJS: Use create_list_from_array_like() in Function.prototype.apply() 2021-06-09 23:46:37 +01:00
Linus Groh
f932da095e LibJS: Use create_list_from_array_like() in Reflect.{apply,construct}() 2021-06-09 23:46:37 +01:00
Linus Groh
ad7aa05cc6 LibJS: Implement the CreateListFromArrayLike() abstract operation
We already have two separate implementations of this, so let's do it
properly. The optional value type check is done by a callback function
that returns Result<void, ErrorType> - value type accepted or message
for TypeError, that is.
2021-06-09 23:46:37 +01:00
Luke
5da94b30eb LibJS: Add logical assignment bytecode generation 2021-06-10 00:28:34 +02:00
Andreas Kling
4bc98fd39f LibJS: Only "var" declarations go in the global object at program level
"let" and "const" go in the lexical environment.

This fixes one part of #4001 (Lexically declared variables are mixed up
with global object properties)
2021-06-09 23:25:16 +02:00
Idan Horowitz
a00d154522 LibJS: Notify WeakSets when heap cells are sweeped
This is an implementation of the following optional optimization:
https://tc39.es/ecma262/#sec-weakref-execution
2021-06-09 21:52:25 +01:00
Idan Horowitz
fb63aeae4d LibJS: Add all of the WeakSet.prototype methods (add, delete, has) 2021-06-09 21:52:25 +01:00
Idan Horowitz
8b6beac5ce LibJS: Add the WeakSet built-in object 2021-06-09 21:52:25 +01:00
Gunnar Beutner
9dc9ddbf82 LibJS: Fix evaluation order for tagged template literals
This ensures that the tag function is evaluated first.
2021-06-09 22:26:27 +02:00
Gunnar Beutner
2d48fe894f LibJS: Generate bytecode for tagged template literals 2021-06-09 21:54:31 +02:00
Gunnar Beutner
25ab31219c LibJS: Fix the return value for TemplateLiteral
This ensures that the bytecode generated for TemplateLiterals returns
the correct value.
2021-06-09 21:50:39 +02:00
Andreas Kling
d69cd3f5bf LibJS: Don't create lexical environment for native (C++) function calls
This was creating a ton of pointless busywork for the garbage collector
and can be avoided simply by tolerating that the current call frame has
a null scope object for the duration of a NativeFunction activation.
2021-06-09 20:54:01 +02:00