Commit graph

273 commits

Author SHA1 Message Date
Tim Ledbetter
48a3a02238 LibCrypto: Make constructing a BigInteger from string fallible
Previously, constructing a `UnsignedBigInteger::from_base()` could
produce an incorrect result if the input string contained a valid
Base36 digit that was out of range of the given base. The same method
would also crash if the input string contained an invalid Base36 digit.
An error is now returned in both these cases.

Constructing a BigFraction from string is now also fallible, so that we
can handle the case where we are given an input string with invalid
digits.
2024-01-13 19:01:35 -07:00
Timothy Flynn
8de1db7b78 LibJS: Avoid needless allocation in the StringToNumber AO 2024-01-04 12:43:10 +01:00
Shannon Booth
e2e7c4d574 Everywhere: Use to_number<T> instead of to_{int,uint,float,double}
In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
2023-12-23 20:41:07 +01:00
Ali Mohammad Pur
5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30
Andreas Kling
ece961f882 LibJS: Pass call/construct argument lists as ReadonlySpan<Value>
(Instead of MarkedVector<Value>.) This is a step towards not storing
argument lists in MarkedVector<Value> at all. Note that they still end
up in MarkedVectors since that's what ExecutionContext has.
2023-11-29 09:48:18 +01:00
Andreas Kling
65717e3b75 LibJS: Inline fast case for Value::to_{boolean,number,numeric,primitive}
These functions all have a very common case that can be dealt with a
very simple inline check, often avoiding the need to call an out-of-line
function. This patch moves the common case to inline functions in a new
ValueInlines.h header (necessary due to header dependency issues..)

8% speed-up on the entire Kraken benchmark :^)
2023-10-07 07:13:52 +02:00
Andreas Kling
111622a164 LibJS: Add fast path for multiplying two Int32 values
We can avoid a lot of work here, as long as the result doesn't overflow
the Int32 range.

5% speed-up on Kraken/imaging-gaussian-blur.js :^)
2023-10-04 20:10:12 +02:00
Andreas Kling
008b9f4c9f LibJS: Add fast-path for Value::to_numeric() on number values
We can skip a whole bunch of checks in this case and just return the
value itself.

2% speed-up on Kraken/imaging-darkroom.js :^)
2023-09-03 18:59:00 +02:00
Andreas Kling
72c9f56c66 LibJS: Make Heap::allocate<T>() infallible
Stop worrying about tiny OOMs. Work towards #20449.

While going through these, I also changed the function signature in many
places where returning ThrowCompletionOr<T> is no longer necessary.
2023-08-13 15:38:42 +02:00
Andreas Kling
97ebfd9f0f LibJS: Make Value::to_string_without_side_effects() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
b8f78c0adc LibJS: Make JS::number_to_string() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
09547ec975 LibJS: Make PrimitiveString::deprecated_string() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
c084269e5f LibJS: Make PrimitiveString::utf8_string() infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
7849950383 LibJS: Make Utf16String & related APIs infallible
Work towards #20449.
2023-08-09 17:09:16 +02:00
Andreas Kling
34344120f2 AK: Make "foo"_string infallible
Stop worrying about tiny OOMs.

Work towards #20405.
2023-08-07 16:03:27 +02:00
Andreas Kling
0cb9c9e81e LibJS: Add fast paths for bitwise ops on 2x Int32 JS::Value
~9% speed-up on Kraken/stanford-crypto-aes.js :^)
2023-07-01 20:29:12 +01:00
Daniel Bertalan
5aef8f280f LibJS: Fix integer overflow in Number::exponentiate
The exponent might be larger than the range of values representable by
an i32, so we have to use the `fmod` function instead of the modulo
operator.

This fixes 3 test262 tests on AArch64. No changes on x86-64.
2023-06-26 19:25:12 +02:00
Linus Groh
b33b0d60e6 LibJS: Port Value::get_method() to GCPtr 2023-04-14 09:59:29 +02:00
Linus Groh
9279b0780d LibJS: Port Value::to_bigint() to NonnullGCPtr 2023-04-14 09:59:29 +02:00
Linus Groh
f345f72b55 LibJS: Port Value::to_object() to NonnullGCPtr 2023-04-14 09:59:29 +02:00
Linus Groh
e79f5b6e85 LibJS: Port Value::to_primitive_string() to NonnullGCPtr 2023-04-14 09:59:29 +02:00
Linus Groh
2555d7a36a LibJS: Make well-known symbol getters return NonnullGCPtr
None of these are ever null after the VM has been initialized, as proved
by virtually every caller immediately dereferencing the raw pointer.
2023-04-13 14:29:42 +02:00
Linus Groh
b84f8fb55b LibJS: Make intrinsics getters return NonnullGCPtr
Some of these are allocated upon initialization of the intrinsics, and
some lazily, but in neither case the getters actually return a nullptr.

This saves us a whole bunch of pointer dereferences (as NonnullGCPtr has
an `operator T&()`), and also has the interesting side effect of forcing
us to explicitly use the FunctionObject& overload of call(), as passing
a NonnullGCPtr is ambigous - it could implicitly be turned into a Value
_or_ a FunctionObject& (so we have to dereference manually).
2023-04-13 14:29:42 +02:00
Andreas Kling
b727f8113f LibJS: Add fast path to Value::to_u32() if Value is a positive i32
6.6% speed-up on Kraken's stanford-crypto-aes subtest. :^)
2023-03-30 19:13:35 +01:00
Linus Groh
e77503e49b LibJS: Make string_to_number() return double instead of Optional<Value>
This would never return an empty optional or non-numeric value, and in
fact every caller as_double()'d the value right away.
Let's make the type match reality instead :^)
2023-03-01 17:55:42 +00:00
Hendiadyoin1
09fa3a17c7 LibJS: Allow to stringify empty Values without side effects 2023-02-26 19:40:09 +01:00
Linus Groh
09d40bfbb2 Everywhere: Use _{short_,}string to create Strings from literals 2023-02-25 20:51:49 +01:00
Timothy Flynn
ba40ef1f3a LibJS: Remove Value::to_deprecated_string_without_side_effects 2023-02-16 14:32:22 +01:00
Timothy Flynn
a73b8292ed LbJS: Convert exception-related usages of Value::TDSWOSE to String
TDSWOSE being to_deprecated_string_without_side_effects.
2023-02-16 14:32:22 +01:00
Timothy Flynn
3ffb6d9b5a LibJS: Define Value::to_string_without_side_effects for String 2023-02-16 14:32:22 +01:00
Timothy Flynn
dc4207323b LibJS: Remove the VM parameter from the NumberToString AO
This will be needed by Value::to_string_without_side_effects, which can
be called in contexts without a VM (e.g. in AK::Format specializations).
So to_string_without_side_effects will need to be callable without a VM,
thus NumberToString must be as well.
2023-02-16 14:32:22 +01:00
Timothy Flynn
b245300ba1 LibJS+Everywhere: Deprecate Value::to_string_without_side_effects 2023-02-16 14:32:22 +01:00
Linus Groh
a8bf2f8e4c LibJS: Port Symbol to String
This includes the VM's global_symbol_registry HashMap, which can now
store String keys.
2023-02-11 21:47:57 +00:00
Linus Groh
89700a2101 LibJS: Rename Symbol::to_deprecated_string() to descriptive_string()
This implements the spec's SymbolDescriptiveString AO and should be
named accordingly.
2023-02-11 21:47:57 +00:00
Timothy Flynn
8414734a2d LibJS: Propagate the OOM error from setting the String length property 2023-01-29 00:02:45 +00:00
Linus Groh
2ff989914d LibJS: Make Value::typeof() return a StringView 2023-01-26 17:26:31 +00:00
Timothy Flynn
ceaec41726 LibJS: Port Value to-string methods to String 2023-01-15 01:00:20 +00:00
Timothy Flynn
c9056b824e LibJS: Define Value::to_string to convert a Value to a String 2023-01-15 01:00:20 +00:00
Timothy Flynn
afeb7273cc LibJS+Everywhere: Rename Value::to_string to to_deprecated_string 2023-01-15 01:00:20 +00:00
Timothy Flynn
0b58748156 LibJS: Define number_to_string to convert a double to a String 2023-01-15 01:00:20 +00:00
Timothy Flynn
8a88d4434f LibJS+LibWeb: Rename number_to_string to number_to_deprecated_string 2023-01-15 01:00:20 +00:00
Timothy Flynn
0ddc2e1f50 LibCrypto+Everywhere: Rename *BigInteger::to_base to to_base_deprecated 2023-01-15 01:00:20 +00:00
Timothy Flynn
115baa7e32 LibJS+Everywhere: Make PrimitiveString and Utf16String fallible
This makes construction of Utf16String fallible in OOM conditions. The
immediate impact is that PrimitiveString must then be fallible as well,
as it may either transcode UTF-8 to UTF-16, or create a UTF-16 string
from ropes.

There are a couple of places where it is very non-trivial to propagate
the error further. A FIXME has been added to those locations.
2023-01-08 12:13:15 +01:00
Timothy Flynn
49b24b0968 LibJS: Let Utf16String be forward-declared in Value.h
It's only used as a template parameter, so let it be forward-declared.
Otherwise, we aren't able to include Completion.h in Utf16String.h, as
there would be a Utf16String -> Completion -> Value -> Utf16String
include cycle.
2023-01-08 12:13:15 +01:00
Linus Groh
dd3d133968 LibJS: Convert SymbolObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
049e99b5f0 LibJS: Convert StringObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
f990095728 LibJS: Convert NumberObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
b48fa8756f LibJS: Convert BooleanObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
cf0a24ff0c LibJS: Convert BigIntObject::create() to NonnullGCPtr 2022-12-14 09:59:45 +00:00
Linus Groh
e4b388ea5a LibJS: Use modulo() for modulo operations in Value.cpp 2022-12-12 21:26:12 +00:00