Commit graph

249 commits

Author SHA1 Message Date
Luke
4d34802f74 LibJS: Expose TypedArray.prototype.byteOffset 2021-05-21 22:52:35 +01:00
Luke
58afd71ad2 LibJS: Expose TypedArray.prototype.byteLength 2021-05-21 22:52:35 +01:00
Luke
8004a2dc77 LibJS: Expose TypedArray.prototype.buffer 2021-05-21 22:52:35 +01:00
Luke
6f1688279a LibJS: Expose BYTES_PER_ELEMENT on each TypedArray 2021-05-21 22:52:35 +01:00
Linus Groh
3a4cbbf01c LibJS: Fix indexed access of TypedArray with byte offset
By doing the offset calculation in {get,put}_by_index() we would
delegate these operations to Object for any index >= (array length -
byte offset). By doing the offset calculation in data() instead, we can
just use the unaltered property index for indexing the returned Span.
In other words: data()[0] now returns the same value as indexing the
TypedArray at index 0 in JS.

This also fixes a bug in the js REPL which would not consider the byte
offset and subsequently access the underlying ArrayBuffer data with a
wrong index.
2021-05-21 19:29:23 +01:00
Linus Groh
d60ebbbba6 Revert "Userland: static vs non-static constexpr variables"
This reverts commit 800ea8ea96.

Booting the system no longer worked after these changes.
2021-05-21 10:30:52 +01:00
Lenny Maiorani
800ea8ea96 Userland: static vs non-static constexpr variables
Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
2021-05-21 10:07:06 +01:00
Andreas Kling
3ee092cd0c LibJS: Implement Object.hasOwn() :^)
This is currently a TC39 Stage 2 proposal, but let's go for it!

https://github.com/tc39/proposal-accessible-object-hasownproperty

I wrote the C++, @linusg found bugs and wrote the test.
2021-05-18 11:18:19 +02:00
Linus Groh
63e8477a6b LibJS: Handle OOB access in GenericIndexedPropertyStorage::take_last()
We already do this for the SimpleIndexedPropertyStorage, so for indexed
properties with GenericIndexedPropertyStorage this would previously
crash. Since overwriting the array-like size with a larger value won't
magically insert values at previously unset indices, we need to handle
such an out of bounds access gracefully and just return an empty value.

Fixes #7043.
2021-05-17 23:20:29 +01:00
Linus Groh
c15121fef7 LibJS: Make length_setter_generic_storage_threshold a global constant
This was a bit hard to find as a local variable - rename it to uppercase
LENGTH_SETTER_GENERIC_STORAGE_THRESHOLD and move it to the top (next to
SPARSE_ARRAY_HOLE_THRESHOLD) for good visibility.
2021-05-17 23:15:18 +01:00
Andreas Kling
e0493c509e LibJS: Make the forward transition chain weakly cached
Before this patch, every shape would permanently remember every other
shape it had ever transitioned to. This could lead to pathological
accumulation of unused shape objects in some cases.

Fix this by using WeakPtr instead of a strongly visited Shape* in the
the forward transition chain map. This means that we will now miss out
on some shape sharing opportunities, but since this is not required
for correctness it doesn't matter.

Note that the backward transition chain is still strongly cached,
as it's necessary for the reification of property tables.

An interesting future optimization could be to allow property tables
to get garbage collected (by detaching them from the shape object)
and then reconstituted from the backwards transition chain (if needed.)
2021-05-17 21:40:18 +02:00
Andreas Kling
751ad19c86 LibJS: Don't consider cells in the lazy freelist in conservative scan
Cells after the lazy freelist bump index are guaranteed to not be
valid cell pointers, so ignore them during the conservative scan.
2021-05-17 19:57:40 +02:00
Andreas Kling
aa857bcdeb LibJS: Always prefer freelist over lazy freelist if possible
If we're able to allocate cells from a freelist, we should always
prefer that over the lazy freelist, since this may further defer
faulting in additional memory for the HeapBlock.

Thanks to @gunnarbeutner for pointing this out. :^)
2021-05-17 19:53:00 +02:00
Andreas Kling
6714cf3631 LibJS: Move Cell.{cpp,h} from Runtime/ to Heap/ 2021-05-17 19:53:00 +02:00
Andreas Kling
c2d9cd8d53 LibJS: Implement lazy freelist allocation for cells
HeapBlock now implements the same lazy freelist as LibC malloc() does,
where new blocks start out in a "bump allocator" mode that gets used
until we've bump-allocated all the way to the end of the block.

Then we fall back to the old freelist style as before.

This means we don't have to pre-initialize the freelist on HeapBlock
construction. This defers page faults and reduces memory usage for
blocks where all cells don't get used. :^)
2021-05-17 19:30:12 +02:00
Andreas Kling
a15c7b7944 Build: Stop using precompiled headers (PCH)
This had very bad interactions with ccache, often leading to rebuilds
with 100% cache misses, etc. Ali says it wasn't that big of a speedup
in the end anyway, so let's not bother with it.

We can always bring it back in the future if it seems like a good idea.
2021-05-17 19:30:12 +02:00
Linus Groh
b9d3df70e0 LibJS: Increase free stack space required for function calls to 32 kiB
The previous 16 kiB weren't sufficient with ASAN enabled and would
trigger stack overflow failures.
2021-05-17 18:03:10 +01:00
Ali Mohammad Pur
b1b0db946e LibJS: Default-initialize the current_node pointer member in CallFrame
Some parts of the code depend on this being nullptr without actually
initializing it, leading to odd random crashes.
e.g. `VM::call_internal`.
2021-05-17 09:41:26 +02:00
Jean-Baptiste Boric
090936e424 Userland: Replace arc4random() with get_random<u32>() 2021-05-14 22:24:02 +02:00
Andrew Kaster
f90a19ba4c LibJS: Make sure all allocators are 8-byte aligned
Absolutely massive allocations > 1024 bytes would go into the size
class which was 3172 bytes. 3172 happens to not be 8 byte aligned, and
so made UBSAN very sad on x86_64. Change the largest allocator to be
3072 bytes, which is in fact a multiple of 8 :^)
2021-05-14 08:34:00 +01:00
Linus Groh
a92dc4e30d LibJS: Ensure function declarations don't leak outside function scopes
When using VM::set_variable() to put the created ScriptFunction onto a
ScopeObject, we would previously unexpectedly reach the global object as
set_variable() checks each traversed scope for an existing Variable with
the given name - which would cause a leak of the inner function past the
outer function (we even had a test expecting that behaviour!). Now we
first declare functions (as DeclarationKind::Var) before setting them.
This will need some more work to make hoisting across non-lexical scopes
work, but it fixes this specific issue for now.

Fixes #6766.
2021-05-13 23:59:00 +01:00
Linus Groh
b221cad659 LibJS/Tests: Add details for toBeTrue() / toBeFalse() expectation error 2021-05-13 23:59:00 +01:00
Linus Groh
f28491dbe7 LibJS/Tests: Add details for toThrowWithMessage did-not-throw case 2021-05-13 23:59:00 +01:00
Linus Groh
5b18bce23c LibJS/Tests: Add prefix to toThrowWithMessage expectation error details
This way we get some more information about where things went wrong.
2021-05-13 23:59:00 +01:00
Linus Groh
0a329d2d70 LibJS: Make super() in catch block work
The TryStatement handler execution creates a new LexicalEnvironment
without a current function set, which we were not accounting for when
trying to get the super constructor while executing a SuperExpression.
This makes it work but isn't pretty - this needs some refactoring to be
close to the spec for that to happen.

Fixes #7045.
2021-05-11 23:31:30 +01:00
Linus Groh
d85b9fd5a0 LibJS: Bring back runtime validation of RegExp flags
This is a partial revert of commit 60064e2, which removed the validation
of RegExp flags during runtime and expected the parser to do that
exclusively - however this was not taking into account the RegExp()
constructor, which was subsequently crashing on invalid flags.

Also adds test for these constructor error cases, which were obviously
missing before.

Fixes #7042.
2021-05-11 22:47:14 +01:00
Linus Groh
431782bcd6 LibJS/Tests: Add details for toThrowWithMessage() expectation error 2021-05-11 22:41:35 +01:00
Luke
c5c9494f48 LibJS: Use u64 instead of u32 in NumberPrototype::to_string
Update to #7033
Partial fix for #7034 (just ups the range to about 2 ** 54 before
losing precision)
2021-05-11 18:29:55 +01:00
Luke
2ff03ecfa8 LibJS: Make number parts unsigned in NumberPrototype::to_string
Fixes #3931
2021-05-11 17:29:37 +01:00
Linus Groh
60064e2049 LibJS: Make invalid RegExp flags a SyntaxError at parse time
This patch changes the validation of RegExp flags (checking for
invalid and duplicate values) from a SyntaxError at runtime to a
SyntaxError at parse time - it's not something that's supposed to be
catchable.
As a nice side effect, this simplifies the RegExpObject constructor a
bit, as it can no longer throw an exception and doesn't have to validate
the flags itself.
2021-05-10 12:01:38 +01:00
Linus Groh
c93c2dc72c LibJS: Rename RegExpLiteral m_content to m_pattern
This is what we call it elsewhere, let's be consistent.
2021-05-10 11:57:35 +01:00
Linus Groh
d1a72dc6eb LibJS/Tests: Rename function parameter from 'arguments' to 'arguments_'
The former has a special meaning and should be avoided where possible.
2021-05-10 11:54:01 +01:00
Andreas Kling
72259d5cee LibJS: Convert StringBuilder::appendf() => AK::Format 2021-05-07 21:12:09 +02:00
Linus Groh
346560d7c8 LibJS/Tests: Use hasOwnProperty() for duplicate test check
The current way of doing this would also traverse the prototype chain,
and therefore yield false positive results for keys like "toString".
2021-05-05 15:58:53 +01:00
Andreas Kling
3d4afe7614 Everywhere: "indexes" => "indices"
I've wasted a silly amount of time in the past fretting over which
of these words to use. Let's just choose one and use it everywhere. :^)
2021-04-29 22:23:52 +02:00
Jean-Baptiste Boric
91def742a4 LibM: Fix INFITITY to float
POSIX mandates it.
2021-04-27 23:06:16 +02:00
Linus Groh
7b1ba4bd5c LibJS: Fallback to undefined if last value in eval() is empty
For something like eval(""), the VM's 'last value' is an empty value,
which we must not leak.

Fixes #6643.
2021-04-25 22:52:19 +02:00
Idan Horowitz
2b4c2301a9 LibJS: Stop rolling back parser state that is immediately replaced
This showed up on a profile (barely), so should help a tiny bit with
perf in parsing arrow functions.
2021-04-25 22:46:34 +02:00
Linus Groh
aef502e8e0 LibJS: Change PropertyName::as_number() return type to u32
This is how it's stored internally - even though we still only construct
from i32. I had the compiler yell at me while trying something with this
and didn't want to add yet another cast, so let's quickly fix this.
2021-04-25 22:42:48 +02:00
Linus Groh
c61de8e4be LibJS: Use Object::get_own_properties() for getOwnPropertyNames() 2021-04-25 22:40:21 +02:00
Linus Groh
af62678c31 LibJS: Don't assume call_frame->current_node in Exception constructor
It's a nullptr in promise reaction job functions, for example. Regressed
in 97d49cb.

Fixes #6641.
2021-04-25 21:45:23 +02:00
FalseHonesty
bee16bb83a LibJS: Don't suppress GlobalObject variable lookup exceptions
In HackStudio's Debugger a custom GlobalObject is used to reflect
debugger variables into the JS scope by overriding GlobalObject's
get method. However, when throwing a custom error during that lookup
it was replaced with the generic "not found" js exception. This patch
makes it instead pass along the custom error.
2021-04-25 19:03:57 +02:00
Linus Groh
b0faf2287a LibJS: Use linusg@serenityos.org for my new copyright headers, too
Whoops, I have a new email for these! :^)
2021-04-24 20:16:31 +02:00
Linus Groh
62c7608a25 LibJS+LibWeb: Move exception logging and remove should_log_exceptions
LibWeb is now responsible for logging unhandled exceptions itself,
which means set_should_log_exceptions() is no longer used and can be
removed. It turned out to be not the best option for web page exception
logging, as we would have no indication regarding whether the exception
was later handled of not.
2021-04-24 20:11:04 +02:00
Linus Groh
08373090ae LibJS: Add VM::on_call_stack_emptied callback
Instead of having to run queued promise jobs in LibWeb in various
places, this allows us to consolidate that into one function - this is
very close to how the spec describes it as well ("at some future point
in time, when there is no running execution context and the execution
context stack is empty, the implementation must [...]").

Eventually this will also be used to log unhandled exceptions, and
possibly other actions that require JS execution to have ended.
2021-04-24 20:11:04 +02:00
Linus Groh
97d49cb92b LibJS: Consolidate exception function names and source ranges
Instead of storing the function names (in a badly named Vector<String>)
and source ranges separately, consolidate them into a new struct:
TracebackFrame. This makes it both easier to use now and easier to
extend in the future.
Unlike before we now keep each call frame's current node source range
in the traceback frame next to the function name, meaning we can display
line and column numbers outside of the VM and after the call stack is
emptied.
2021-04-24 20:11:04 +02:00
Linus Groh
0cf04d07aa LibJS: Temporarily clear exception in Object::get_without_side_effects()
This would return an empty value once it hits an exception check
otherwise. Considering that this mostly is used in situations where we
already *do* have an exception (traceback printing, for example), let's
make this easier for ourselves to use.
2021-04-24 20:11:04 +02:00
Linus Groh
5caab0148c LibJS: Add TemporaryClearException helper class
This is very similar to AK::TemporaryChange (and in fact replaces one
use of it), but since we can't directly set VM's m_exception from
outside of the VM, we need something more sophisticated.
Sometimes we need to temporarily remove the stored exception for some
other operation to succeed (e.g. anything that uses call(), as well as
get_without_side_effects()) and later restore it - the boilerplate code
required for this is annoying enough to justify a helper.
2021-04-24 20:11:04 +02:00
Andreas Kling
3a4d42bbbb LibJS: Remove stray '%' from MemberExpression AST dump 2021-04-24 18:50:12 +02:00
Linus Groh
0053816e9d LibJS: Correctly handle mixing +0 and -0 in Math.{min,max}()
The native C++ < and > operators won't handle this correctly, so the
result was different depending on the order of arguments. This is now
fixed by explicitly checking for positive and negative zero values.

Fixes #6589.
2021-04-23 20:51:48 +02:00