Commit Graph

2889 Commits

Author SHA1 Message Date
Linus Groh
599b2bee4a LibJS: Tweak Interpreter::create() for more spec-likeness
Store a reference to the newly created execution context in an aptly
named variable, rename global_this_value to just this_value, and only
call set_global_object() in a single place.
2022-03-18 01:12:12 +01:00
Linus Groh
29964dc152 LibJS: Use TRY(push_execution_context()) in places where we can recover 2022-03-18 01:12:12 +01:00
Linus Groh
9422ae9bb2 LibJS: Add infallible variant of VM::push_execution_context()
It makes no sense to require passing a global object and doing a stack
space check in some cases where running out of stack is highly unlikely,
we can't recover from errors, and currently ignore the result anyway.

This is most commonly in constructors and when setting things up, rather
than regular function calls.
2022-03-18 01:12:12 +01:00
Timothy Flynn
157d16f049 LibJS: Update specification steps for RegExp Match Indices
This proposal was implemented in Stage 3 in commit:
6c67de8186

It is now Stage 4 and has been merged into the main ECMA-262 spec:
https://github.com/tc39/ecma262/commit/0209d85
2022-03-17 11:29:51 -04:00
Linus Groh
619794dfa7 LibJS: Fix fraction substring in ParseTimeZoneOffsetString
This is a normative change in the Temporal spec.

See:
- https://github.com/tc39/proposal-temporal/commit/97d553c
- https://github.com/tc39/proposal-temporal/commit/d53af7f

Note that we already implemented this correctly, so the only change is
updating the spec comment.
2022-03-16 21:49:38 +00:00
Linus Groh
356fa2dca1 LibJS: Remove unused code in DifferenceISODate
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/056f695
2022-03-16 21:49:38 +00:00
Linus Groh
2ad69d789b LibJS: Remove the ConstrainToRange AO
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/537b3e6
2022-03-16 21:49:38 +00:00
Linus Groh
1521c5f55c LibJS: Remove argument type assertion from ParseTemporalDurationString
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/8615b41
2022-03-16 21:49:38 +00:00
Linus Groh
0c22a7f822 LibJS: Assume Get() on the result of PrepareTemporalFields can't fail
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/980e168
2022-03-16 21:49:38 +00:00
Simon Wanner
cc77bb5067 LibJS: Relax line and column number restrictions in Error stack tests 2022-03-16 18:55:55 +00:00
Lenny Maiorani
d00b79568f Libraries: Use default constructors/destructors in LibJS
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-16 16:19:40 +00:00
Ali Mohammad Pur
8b50009e9b LibTest: Provide detailed per-file JSON output with --per-file
This makes test-js style runners dump out output in the same format as
libjs-test262's per-file output.
2022-03-16 15:44:52 +00:00
Linus Groh
fb60ada6ce LibJS: Handle non-Error this object in Error.prototype.stack getter
This is taken from the abandoned error stacks proposal, which
already serves as the source of truth for the setter. It only requires
the this value to be an object - if it's not an Error object, the getter
returns undefined.
I have not compared this behavior to the non-standard implementations of
the stack property in other engines, but presumably the spec authors
already did that work.

This change gets the Sentry browser SDK working to a point where it can
actually send uncaught exceptions via the API :^)
2022-03-15 17:32:48 +01:00
Linus Groh
37e988675f LibJS/Tests: Consolidate Error.prototype.stack tests
We don't usually separate tests for the getter and setter of a property,
and that error-stack.js (getter) test belongs in builtins/.
2022-03-15 17:32:48 +01:00
Linus Groh
ecba29c158 LibJS: Set internal function name of NativeError constructors
By using the same NativeFunction constructor as plain ErrorConstructor
and passing the name, TypeError & co. will now include their name in
backtraces and such.
Eventually we should probably rely on [[InitialName]] for this, but for
now that's how it works.
2022-03-15 17:32:48 +01:00
Timothy Flynn
04bb17ca94 LibJS: Reorganize spec steps for Intl.RelativeTimeFormat
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/a3ae343
2022-03-15 17:30:58 +01:00
Timothy Flynn
8941d0b49e LibJS: Reorganize spec steps for Intl.PluralRules
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/d7c7157
2022-03-15 17:30:58 +01:00
Timothy Flynn
812d3a7ef8 LibJS: Reorganize spec steps for Intl.NumberFormat
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/110cb1f
2022-03-15 17:30:58 +01:00
Timothy Flynn
d6868d1e9d LibJS: Reorganize spec steps for Intl.Locale
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/31f6003
2022-03-15 17:30:58 +01:00
Timothy Flynn
6677e03a52 LibJS: Reorganize spec steps for Intl.ListFormat
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/61bc370
2022-03-15 17:30:58 +01:00
Timothy Flynn
72462bed2f LibJS: Reorganize spec steps for Intl.DisplayNames
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/5b51804
2022-03-15 17:30:58 +01:00
Timothy Flynn
1f9c5c186f LibJS: Reorganize spec steps for Intl.DateTimeFormat
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/97e1ecc
2022-03-15 17:30:58 +01:00
Timothy Flynn
194ca06f78 LibJS: Reorganize spec steps for Intl.Collator
This is an editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/7c13db4

This also normalizes the spelling of the "Internal slots" heading in
Intl.Collator, which is another editorial change in the Intl spec:
https://github.com/tc39/ecma402/commit/ec064bd
2022-03-15 17:30:58 +01:00
Luke Wilde
db1236b336 LibJS/Bytecode: Fix typo in object binding an entry with no alias
In object binding, we would attempt to get NonnullRefPtr<Identifier>
from alias on the alias.has<Empty>() code path. In this case, we need
to get it from name instead.
2022-03-15 11:57:51 +03:30
Luke Wilde
515f3e0b85 LibJS/Bytecode: End for's variable scope after update block generation
The update block can generate bytecode that refers to the lexical
environment, so we have to end the scope after it has been generated.
Previously the Jump to the update block would terminate the block,
causing us to leave the lexical environment just before jumping to the
update block.
2022-03-15 11:57:51 +03:30
Luke Wilde
1fc6bbcdc3 LibJS: Stop generating switch case statements on block termination
After we terminate a block (e.g. break, continue), we cannot generate
anymore bytecode for the block. This caused us to crash with this
example code:
```
a = 0;
switch (a) {
    case 0:
        break;
        console.log("hello world");
}
```
Anything after a block terminating instruction is considered
unreachable code, so we can safely skip any statements after it.
2022-03-15 11:57:51 +03:30
Ali Mohammad Pur
d75cf27e02 LibJS: Implement default values for function parameters in BC 2022-03-14 22:28:53 +01:00
Ali Mohammad Pur
0b8d2fb62f LibJS/Bytecode: Replace merged block references before copying them 2022-03-14 22:28:53 +01:00
Ali Mohammad Pur
a37bee919a LibJS: Use ranges instead of specifying all registers for NewArray
Listing all the registers will lead to the inability to allocate enough
space in one basic block (as there can be an arbitrary number of
registers used), instead switch to specifying the range of registers
used and save a lot of space in the process.
2022-03-14 21:15:27 +03:30
Luke Wilde
e517cb505a LibJS/Bytecode: Make NewArray write directly to indexed properties
This follows how the regular AST interpreter creates arrays, as using
Array::create_from uses create_data_property_or_throw, which will crash
when it encounters an empty value. We require empty values to represent
array holes.
2022-03-14 21:15:27 +03:30
Luke Wilde
750b69540e LibJS/Bytecode: Setup declarative environment for lexical for statements 2022-03-14 21:15:27 +03:30
Luke Wilde
97af7654dd LibJS/Bytecode: Setup declarative environment for catch with variable 2022-03-14 21:15:27 +03:30
Luke Wilde
04774f923f LibJS/Bytecode: Setup lexical environment boundary for with statements
This allows us to properly unwind the object environment for `with` on
a block terminating instruction, e.g. an unconditional throw.
2022-03-14 21:15:27 +03:30
Luke Wilde
9f4cc6435d LibJS/Bytecode: Unwind to closest unwind boundary on Throw
This will leave any lexical/variable environments on the way to the
closest unwind context boundary.

This will not leave the closest unwind context, as we still need the
unwind context to perform the Throw instruction correctly.
2022-03-14 21:15:27 +03:30
Luke Wilde
858bcac4c7 LibJS/Bytecode: Unwind environments before block terminating instruction
When we reach a block terminating instruction (e.g. Break, Throw),
we cannot generate anymore instructions after it. This would not allow
us to leave any lexical/variable environments.

This uses the mechanism introduced in ba9c49 to unwind environments
when we encounter these instructions.
2022-03-14 21:15:27 +03:30
Luke Wilde
ada8880f58 LibJS: Leave unwind context if it has no finalizer when using handler
For example, a try/catch block with no finally. The try block and catch
block do not need to unwind to a finally block, so the unwind context
is no longer needed when we jump to the catch block.

If we threw an exception in a catch block of a try/catch, there will be
no handler or finalizer and the unit would continue on as if nothing
happened.

This would subsequently crash with the  `m_saved_exception.is_null()`
assertion failure when we next call a non-native function.
2022-03-14 21:15:27 +03:30
Luke Wilde
0356239f3e LibJS/Bytecode: Unconditionally end break/continuable scopes
Previously we would only end these scopes if the block was not
terminated. If the block was generated, we would not end the scope
and would generate other bytecode with these scopes still open.

These functions do not generate any code, so they can be used even if
the current block is terminated. The enter and end scope functions are
only used to track where to unwind to when break/continue are used.
2022-03-14 21:15:27 +03:30
Timothy Flynn
27904b1060 LibJS: Add a fast path for creating per-iteration DeclarativeEnvironment
The steps for creating a DeclarativeEnvironment for each iteration of a
for-loop can be done equivalently to the spec without following the spec
directly. For each binding creating in the loop's init expression, we:

    1. Create a new binding in the new environment.
    2. Grab the current value of the binding in the old environment.
    3. Set the value in the new environment to the old value.

This can be replaced by initializing the bindings vector in the new
environment directly with the bindings in the old environment (but only
copying the bindings of the init statement).
2022-03-14 16:33:15 +01:00
Timothy Flynn
f37fbcf516 LibJS: Preallocate the list returned from CreateListFromArrayLike
This list has up to 10,000 elements in some test262 tests, so let's
avoid frequent allocation bumps.
2022-03-14 16:33:15 +01:00
Idan Horowitz
c575710e5e LibWeb: Use inline script tag source line as javascript line offset
This makes JS exception line numbers meaningful for inline script tags.
2022-03-14 00:25:33 +01:00
Ali Mohammad Pur
2000251333 LibJS: Implement bytecode generation for WithStatement 2022-03-13 17:50:21 +01:00
Ali Mohammad Pur
57386ca839 LibJS: Initialize 'var' bindings to undefined on declaration
This is what CreateGlobalVarBinding does, so do the same thing in BC.
2022-03-13 17:50:21 +01:00
Ali Mohammad Pur
ba9c4959d6 LibJS: Leave the unwind context on break/continue/return in bytecode
Otherwise we'd keep the old unwind context, and end up never invoking
the other handlers up the stack.
2022-03-13 17:50:21 +01:00
Ali Mohammad Pur
41184c960d LibJS: Propagate abrupt completions in Bytecode::Op::Call
This was not handling the nullary call case correctly, remove the whole
nullary check as there's nothing particularly expensive in the catch-all
case anyway.
2022-03-13 17:50:21 +01:00
mjz19910
fd8a56cdde LibJS: Add some tests for TypedArray.prototype.set 2022-03-13 16:49:25 +01:00
Luke Wilde
a54fdd5212 LibJS: Apply source's byte offset in TA#set when both TAs have same type
On the code path where we are setting a TypedArray from another
TypedArray of the same type, we forgo the spec text and simply do a
memmove between the two ArrayBuffers. However, we forgot to apply
source's byte offset on this code path.

This meant if we tried setting a TypedArray from a TypedArray we got
from .subarray(), we would still copy from the start of the subarray's
ArrayBuffer.

This is because .subarray() returns a new TypedArray with the same
ArrayBuffer but the new TypedArray has a smaller length and a byte
offset that the rest of the codebase is responsible for applying.

This affected pako when it was decompressing a zlib stream that has
multiple zlib chunks in it. To read from the second chunk, it would
set the zlib window TypedArray from the .subarray() of the chunk offset
in the stream's TypedArray. This effectively made the decompressed data
from the second chunk a mis-mash of old data that looked completely
scrambled. It would also cause all future decompression using the same
pako Inflate instance to also appear scrambled.

As a pako comment aptly puts it:
> Call updatewindow() to create and/or update the window state.
> Note: a memory error from inflate() is non-recoverable.

This allows us to properly decompress the large compressed payloads
that Discord Gateway sends down to the Discord client. For example,
for an account that's only in the Serenity Discord, one of the payloads
is a 20 KB zlib compressed blob that has two chunks in it.

Surprisingly, this is not covered by test262! I imagine this would have
been caught earlier if there was such a test :^)
2022-03-11 22:20:23 +01:00
Linus Groh
251f5e8885 LibJS: Use CreateTemporalCalendar in GetBuiltinCalendar
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/f6b3a10
2022-03-10 23:20:39 +01:00
Linus Groh
7f5b4842f2 LibJS: Replace dead code in ParseTimeZoneOffsetString with an assertion
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/a29c40b
2022-03-10 23:20:39 +01:00
Linus Groh
55f9733316 LibJS: Add missing check in ParseTemporalInstant
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/baead4d
2022-03-10 23:20:39 +01:00
Linus Groh
f75052ff7c LibJS: Assert CreateTemporalDuration does not throw given correct input
This is an editorial change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/881dd22
2022-03-10 23:20:39 +01:00