Commit graph

1619 commits

Author SHA1 Message Date
Leon Albrecht 6f5e92a279
AK/Tests: Install test fixtures required for TestJSON 2021-04-19 18:16:46 +02:00
DexesTTP 8e44a0bd46 AK: Add default ports for Websockets to the URL class 2021-04-18 22:42:10 +02:00
Lenny Maiorani c1971df4c7 AK/Hex: Cleanup implementation
Problem:
- Post-increment of loop index.
- `const` variables are not marked `const`.
- Incorrect type for loop index.

Solution:
- Pre-increment loop index.
- Mark all possible variables `const`.
- Corret type for loop index.
2021-04-18 22:09:25 +02:00
Lenny Maiorani d462a56163 AK/Hex: Decode hex digit in constexpr context
Problem:
- Hex digit decoding is not `constexpr`, but can be.

Solution:
- Move the body of the function to the header and decorate with
  `constexpr`.
- Provide tests for run-time and compile-time evaluation.
2021-04-18 22:09:25 +02:00
Lenny Maiorani 0ac02b9084 AK/Complex: C++20-compatible comparison operators
Problem:
- Clang correctly reports non-`const` member function comparison
  operators as ambiguous.

Solution:
- Make them `const`.
2021-04-18 22:09:25 +02:00
Marco Biscaro 7dc95721ee Tests: Fix test-math expected values
Some of the expected values in test-math were wrong, which caused some
tests to fail.

The updated values were generated by Python's math library, and rounded
to 6 decimals places:

>>> import math
>>> round(math.exp(20.99), 6)
1305693298.670892

Examples of failure outputs:

FAIL: ../Userland/Tests/LibM/test-math.cpp:98:
EXPECT_APPROXIMATE(exp(v.x), v.exp) failed with
lhs=1305693298.670892, rhs=1304956710.432034, (lhs-rhs)=736588.238857

FAIL: ../Userland/Tests/LibM/test-math.cpp:99:
EXPECT_APPROXIMATE(sinh(v.x), v.sinh) failed with
lhs=652846649.335446, rhs=652478355.216017, (lhs-rhs)=368294.119428

FAIL: ../Userland/Tests/LibM/test-math.cpp💯
EXPECT_APPROXIMATE(cosh(v.x), v.cosh) failed with
lhs=652846649.335446, rhs=652478355.216017, (lhs-rhs)=368294.119429
2021-04-18 21:41:17 +02:00
Linus Groh 2b0c361d04 Everywhere: Fix a bunch of typos 2021-04-18 10:30:03 +02:00
Andreas Kling 873da38d0e AK: Remove String-from-StringView optimization
We had an unusual optimization in AK::StringView where constructing
a StringView from a String would cause it to remember the internal
StringImpl pointer of the String.

This was used to make constructing a String from a StringView fast
and copy-free.

I tried removing this optimization and indeed we started seeing a
ton of allocation traffic. However, all of it was due to a silly
pattern where functions would take a StringView and then go on
to create a String from it.

I've gone through most of the code and updated those functions to
simply take a String directly instead, which now makes this
optimization unnecessary, and indeed a source of bloat instead.

So, let's get rid of it and make StringView a little smaller. :^)
2021-04-17 01:27:31 +02:00
Andreas Kling 1f684c8123 AK: Implement case-insensitive StringUtils::matches() without allocs
Previously this would create new to_lowercase()'d strings from the
needle and the haystack. This generated a huge amount of malloc
traffic in some programs.
2021-04-17 01:27:30 +02:00
Andreas Kling f7139d9422 AK: Make LexicalPath take String instead of StringView 2021-04-17 01:27:30 +02:00
AnotherTest e4412f1f59 AK+Kernel: Make IntrusiveList capable of holding non-raw pointers
This should allow creating intrusive lists that have smart pointers,
while remaining free (compared to the impl before this commit) when
holding raw pointers :^)
As a sidenote, this also adds a `RawPtr<T>` type, which is just
equivalent to `T*`.
Note that this does not actually use such functionality, but is only
expected to pave the way for #6369, to replace NonnullRefPtrVector<T>
with intrusive lists.

As it is with zero-cost things, this makes the interface a bit less nice
by requiring the type name of what an `IntrusiveListNode` holds (and
optionally its container, if not RawPtr), and also requiring the type of
the container (normally `RawPtr`) on the `IntrusiveList` instance.
2021-04-16 22:26:52 +02:00
AnotherTest fb814ee720 AK: Avoid the unnecessarily confusing bunch of casts in IntrusiveList
And use bit_cast instead.
Also explain what it does, because it's not at all trivial
to understand :^)
2021-04-16 22:26:52 +02:00
Hendiadyoin1 2e11b2d35a AK: Add outln_if and warnln_if
This uses the same gating mechanism as dbgln_if and should be equivalent
to #define flag etc
2021-04-16 20:03:35 +02:00
Timothy Flynn 99e1d8a359 AK: Add type alias for AK::Optional 2021-04-16 19:19:31 +02:00
Nicholas-Baron 73dd293ec4 Everywhere: Add -Wdouble-promotion warning
This warning informs of float-to-double conversions. The best solution
seems to be to do math *either* in 32-bit *or* in 64-bit, and only to
cross over when absolutely necessary.
2021-04-16 19:01:54 +02:00
Matthew Olsson d719e745fb AK: Fix incorrect formatter signing of numbers between -1.0 and 0.0
Floating point numbers are casted to i64 and passed to the integer
formatting logic, and the floating point portion of the number is
handled separately. However, casting to i64 when the number is between
-1.0 and 0.0 produces 0, so the sign would be lost. This commit fixes
that by using put_u64 instead, which allows us to manually provide the
is_negative flag.
2021-04-16 17:42:42 +02:00
Idan Horowitz 2ab292f381 AK: Expose the decode_hex_digit helper
This helper is useful on its own for things like uri encoding/decoding,
so this commit exposes it via the AK/Hex header
2021-04-14 13:30:10 +02:00
Timothy Flynn 3f4e90f32b AK: Fix StringView::find_last_of for one-character views
The find_last_of implementations were breaking out of the search loop
too early for single-character string views. This caused a crash in
CookieJar setting a cookie on google.com - CookieJar::default_path knew
there was at least one "/" in a string view, but find_last_of returned
nullopt, so dereferencing the optional caused a crash.

Fixes #6273
2021-04-12 22:37:00 +02:00
Timothy Flynn 2370efbea6 AK: Add a predicate variant of StringView::split_view 2021-04-12 22:37:00 +02:00
Idan Horowitz f8a3da46fd AK: Implement IntrusiveRedBlackTree container
This container is similar to the RedBlackTree container, but instead of
transparently allocating tree nodes on insertion and freeing on removal
this container piggybacks on intrusive node fields in the stored class
2021-04-12 18:03:44 +02:00
Idan Horowitz e962254eb2 AK: Implement RedBlackTree container
This container is based on a balanced binary search tree, and as such
allows for O(logn) worst-case insertion, removal, and search, as well
as O(n) sorted iteration.
2021-04-12 18:03:44 +02:00
Tim Waterhouse 4d81d868c7 AK: Update LexicalPath::relative_path to work for '/' prefix
If the prefix path is just a slash the LexicalPath was removing too many
characters. Now only remove an extra character if the prefix is not just
the root path.
2021-04-11 19:19:49 +02:00
Brian Gianforcaro 7db74a6b3e AK: Annotate StringBuilder functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 23454cab46 AK: Annotate WeakPtr functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 56ab0da6a4 AK: Annotate Trie functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 75e7c780e7 AK: Annotate StringView functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 28e40e22c0 AK: Annotate IntrusiveList functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 0593fa4dcb AK: Annotate HashTable functions as [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 15b94ec1fd AK: Make HashTable with capacity constructor explicit 2021-04-11 12:50:33 +02:00
Brian Gianforcaro a8671ec166 AK: Annotate HashMap functions with [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro eaa9ec9b5d AK: Annotate DoublyLinkedList functions with [[nodiscard]] 2021-04-11 12:50:33 +02:00
Brian Gianforcaro 5165661799 AK: Annotate Checked functions with [[nodiscard]] 2021-04-11 12:50:33 +02:00
AnotherTest a6e4482080 AK+Everywhere: Make StdLibExtras templates less wrapper-y
This commit makes the user-facing StdLibExtras templates and utilities
arguably more nice-looking by removing the need to reach into the
wrapper structs generated by them to get the value/type needed.
The C++ standard library had to invent `_v` and `_t` variants (likely
because of backwards compat), but we don't need to cater to any codebase
except our own, so might as well have good things for free. :^)
2021-04-10 21:01:31 +02:00
Andreas Kling c268df79e6 AK: Add BitmapView::set_range_and_verify_that_all_bits_flip()
This function sets a range of bits to the same value while also
verifying that all bits in the range get flipped in the process.
2021-04-09 17:08:49 +02:00
Jelle Raaijmakers c4e19250a1 Format: Strip trailing zeroes from floating point values
This is a pretty naive implementation that works well. The precision
parameter is interpreted as "maximum precision" instead of "minimum
precision", which in my opinion is the most useful interpretation.
2021-04-08 20:30:56 +02:00
Jelle Raaijmakers 4bfd394384 Tests: Merge duplicate TestFormat test into AK directory 2021-04-08 20:30:56 +02:00
thislooksfun e55b8712d4 AK: Inline HashTable writing bucket lookup
The old approach was more complex and also had a very bad edge case
with lots of collisions. This approach eliminates that possiblility.
It also makes both reading and writing lookups a little bit faster.
2021-04-02 12:54:54 +02:00
thislooksfun 509eb10df4 AK: Inline the bucket index calculation
The result of the modulo is only used in the array index, so why
make the code more complex by calculating it in two different places?
2021-04-02 12:54:54 +02:00
thislooksfun 8fb7739cfb AK: Add tests for HashTable 2021-04-02 12:54:54 +02:00
Linus Groh f418115f1b LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)

The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.

Implemented functions are:

- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()

For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].

Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.

This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.

I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.

[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-02 10:47:40 +02:00
vcollette 804ab79995
AK: Fix bogus return type of Result::release_error() (#6054)
The function was not used anywhere so the error was unnoticed.
2021-04-01 21:03:57 +02:00
Michel Hermier 7d591432c4 AK: Add Signed/Unsigned concepts. 2021-03-28 11:45:51 +02:00
Michel Hermier 4f1c72c174 AK: Add IsSigned conterpart to IsUnsigned. 2021-03-28 11:45:51 +02:00
Michel Hermier d77cbc0fa8 AK: Remove IsSigned<char> test as it is platform/compiler dependant. 2021-03-28 11:45:51 +02:00
Michel Hermier b8fac0a8b3 AK: Make Concepts.h and StdLibExtras.h properly alias their own sumbols. 2021-03-28 11:45:51 +02:00
Cesar Torres f4f5a1c0e7 AK: Add complex number library
Useful for diverse algorithms.
Also added some tests for it.
2021-03-27 10:20:55 +01:00
Timothy Flynn 4babf6e4e1 AK: Allow assigning a value to a specific index in a JsonArray 2021-03-26 22:58:31 +01:00
Idan Horowitz edecf8f6a3 AK: Add starts_with to Utf8View
Unlike String/StringView::starts_with this compares utf8 code points
instead of "characters" (bytes), which is important when handling
aribtary utf-8 input that could include overlong characters.
2021-03-25 10:59:34 +01:00
David Isaksson 17c0349d23 AK: Add longer human readable size to string helper function
Wraps the existing AK::human_readable_size function but will always
display the bytes in the base unit as well as the shorter string with
one decimal. E.g. "14 KiB (14396 bytes)".
2021-03-25 10:59:11 +01:00
Andreas Kling d20e3dbe8b LibGUI: Remove one ancient GMENU_DEBUG debug log 2021-03-23 20:37:39 +01:00
Oleg Sikorskiy e99dabc729 AK: Implement bit_cast 2021-03-23 08:22:15 +01:00
Hendiadyoin1 0d934fc991 Kernel::CPU: Move headers into common directory
Alot of code is shared between i386/i686/x86 and x86_64
and a lot probably will be used for compatability modes.
So we start by moving the headers into one Directory.
We will probalby be able to move some cpp files aswell.
2021-03-21 09:35:23 +01:00
Hendiadyoin1 60caffb113 AK+LibC: Make %p specifier Architecture Independent
We were using u32 as a pointer representation and not FlatPtr
2021-03-21 09:35:23 +01:00
Hendiadyoin1 f673f08bec AK: Fix integer_sequence_generate_array
It was for some reason Architecture dependent.
Now we use size_t instead of unsinged long
2021-03-21 09:35:23 +01:00
Hendiadyoin1 7ba3c22931 AK: Add a Ptr32 type to handle fixed sized pointers
This will be used to fix the UHCI Controller, which relies on
the pointer-size being 32-bit
2021-03-21 09:35:23 +01:00
Jean-Baptiste Boric 607fac662d AK: Implement terabytes, petabytes, exabytes 2021-03-17 23:22:42 +01:00
Jean-Baptiste Boric 7ffc6c371a AK: Add 64 bit methods to JsonValue 2021-03-17 23:22:42 +01:00
Andreas Kling ea81dc13cf AK: Move move() into the "std" namespace
This makes GCC emit warnings about redundant and pessimizing moves.
It also allows static analyzers like clang-tidy to detect common bugs
like use-after-move.
2021-03-17 16:31:17 +01:00
Andreas Kling f59ad2dc57 Everywhere: Remove pessimizing and redundant move() 2021-03-17 16:30:15 +01:00
Brendan Coles fa28cc85e6 AK: Tests: TestURL: Add port_int_overflow_wrap test 2021-03-17 12:39:03 +01:00
Idan Horowitz ea5f83616e LibCompress+AK: Dont short-circuit error handling propagation
In the case that both the stream and the wrapped substream had errors
to be handled only one of the two would be resolved due to boolean
short circuiting. this commit ensures both are handled irregardless
of one another.
2021-03-16 14:56:50 +01:00
Idan Horowitz a955fd4156 LibCompress+AK: Propagate error handling to wrapped streams
This ensures that when a DeflateCompressor stream is cleared of any
errors its underlying wrapped streams (InputBitStream/InputMemoryStream)
will be cleared as well and wont fail a VERIFY on destruction.
2021-03-15 21:35:48 +01:00
Andreas Kling 64cb04996f AK: Make ByteBuffer::slice(0, size()) a freebie
If you want the whole buffer, we can just give you the buffer itself.
2021-03-15 18:01:11 +01:00
Idan Horowitz 1b7b503bae AK: Add fast paths for aligned bit writes in BitOutputStream
If the bit write is aligned (or has been aligned during the write) we can
write in multiples of 32/16/8 bits for increased performance.
2021-03-13 23:50:07 +01:00
Idan Horowitz 3c7aa56ae8 AK: Store BinaryHeap key-value pairs together for efficient swaps
The 2 seperate key and value arrays are replaced with a single struct pair
array that allows for a 2x reduction in loads/stores during element swaps
in the common case of same-sized keys and values.
2021-03-13 23:50:07 +01:00
Idan Horowitz 168cf6fac9 AK: Implement minimum BinaryHeap
This enables efficient implementations of priority queues,
and will also be used in LibCompress for efficient huffman
tree generation.
2021-03-13 20:07:25 +01:00
Idan Horowitz 0ddc0e45ae AK: Add OutputBitStream class
This will be used in the deflate compressor.
2021-03-13 20:07:25 +01:00
Ben Wiederhake 81079ae616 AK: Fix some overflows/underflows that weren't properly handled
Based on #5699. Closes #5699.
2021-03-13 10:17:28 +01:00
Tom 8a8bdb2cd7 AK: Add decrement operator to Checked 2021-03-13 10:17:28 +01:00
Andreas Kling d3f112f9a2 AK+LibCore: Remove empty files 2021-03-12 17:38:17 +01:00
Andreas Kling ef1e5db1d0 Everywhere: Remove klog(), dbg() and purge all LogStream usage :^)
Good-bye LogStream. Long live AK::Format!
2021-03-12 17:29:37 +01:00
Andreas Kling 1b2ea12062 AK: Add basic const iteration to IntrusiveList 2021-03-11 14:21:49 +01:00
Andreas Kling 4d30166d61 AK: Add Formatter for RefPtr 2021-03-09 22:10:41 +01:00
Mițca Dumitru 8f7aa1e03a AK: Include Assertions.h in StdLibExtras.h 2021-03-09 07:28:06 +01:00
Brian Gianforcaro c825159f01 AK: Add AK_ENUM_BITWISE_OPERATORS(..) to enable type-safe enum bitwise operations
This change introduces `AK_ENUM_BITWISE_OPERATORS(..)` which when
enabled for an enum, will automatically declare all the necessary
bitwise operators for that enum.

This allows bit masks enums to be used as first class, type safe, citizens.
2021-03-08 18:47:40 +01:00
Tom 183b2e71ba AK: Take advantage of constexpr in Time and add time conversion methods
By making the Time constructor constexpr we can optimize creating a
Time instance from hardcoded values.

Also add more functions to convert between Time and various time units.
2021-03-08 15:29:11 +01:00
Tom 42bc229500 AK: Initialize the AK::Time members
Since we tell the compiler to provide a default constructor we
need to initialize the member variables.
2021-03-08 15:29:11 +01:00
Andreas Kling 483b45db3f AK: JsonObject::value_or() fallback value should be a const reference 2021-03-08 11:50:36 +01:00
Ben Wiederhake b6472204e5 AK+Tests: Also test Checked<> with unsigned
As expected, Checked<unsigned> works as intended. However, we didn't have a
test for that, so I added one.

See also 90c070cb1d.
2021-03-07 17:31:25 +01:00
speles 50de653cc9 AK: Add optional fragment parameter to create_with_file_protocol()
Now that we use fragment for specifying starting selection in
FileManager we would benefit from providing it as argument instead of
setting it each time separately.
2021-03-07 11:00:36 +01:00
Brian Gianforcaro fc5b252010 AK: Implement IsEnum<T> and UnderlyingType<T> type traits
I needed these meta-programming type traits while working on something else.
Add basic support for these two type traits as well as some tests.
2021-03-05 11:30:13 +01:00
Liav A 4a5cf8c789 AK: Add a full memory barrier function based on atomic operations
We use atomic_signal_fence and atomic_thread_fence together to prevent
reordering of memory accesses by the CPU and the compiler.

The usage of these functions was suggested by @tomuta so we can be sure
that important memory accesses happen in the expected order :)
2021-03-05 11:29:34 +01:00
Andreas Kling 1208fc7d37 AK: Simplify Bitmap and implement in terms of BitmapView
Add Bitmap::view() and forward most of the calls to BitmapView since
the code was identical.

Bitmap is now primarily concerned with its dynamically allocated
backing store and BitmapView deals with the rest.
2021-03-04 11:25:45 +01:00
Andreas Kling 40552bb5fb AK: Add BitmapView class (like StringView but for bitmaps)
AK::Bitmap is an awkwardly modal class which can either own or wrap
the underlying data. To get ourselves out of this unpleasant situation,
this patch adds BitmapView to replace the wrapped mode.

A BitmapView is simply a { data pointer, bit count } tuple internally
and provides all the convenient functionality of a bitmap class.
2021-03-04 11:25:45 +01:00
AnotherTest e59a631511 HackStudio/LanguageServers: Move some components out of Cpp
This makes them available for use by other language servers.
Also as a bonus, update the Shell language server to discover some
symbols and add go-to-definition functionality :^)
2021-03-04 11:21:43 +01:00
William McPherson 2479ead718 Everywhere: Remove unnecessary clang-format offs
Mostly due to the fact that clang-format allows aligned comments via
AlignTrailingComments.

We could also use raw string literals in inline asm, which clang-format
deals with properly (and would be nicer in a lot of places).
2021-03-04 11:01:48 +01:00
Andreas Kling ddaeb294dc AK: Add Bitmap::is_null()
This class has a null state but there was no API to check for it.
2021-03-04 10:11:37 +01:00
Linus Groh a5d6962148 AK+Kernel: Remove NO_DISCARD macro hack
This was added as clang-format would mess up the formatting when using
[[nodiscard]] on a class, which is no longer the case.
2021-03-03 23:08:42 +01:00
Ben Wiederhake cf32f29af6 AK+LibM: Rename EXPECT_CLOSE to EXPECT_APPROXIMATE 2021-03-03 20:19:24 +01:00
Ben Wiederhake 5df014b8ff AK+LibM: Make EXPECT_CLOSE more useful during debugging 2021-03-03 20:19:24 +01:00
ihsinme 69f82ede7a AK: Fix OOO mistake in StackInfo.cpp
"!=" has higher priority than "=".
2021-03-03 11:09:44 +01:00
Ben Wiederhake 340813e087 AK: Make Time more usable 2021-03-02 08:36:08 +01:00
Ben Wiederhake e510c41fd2 Kernel: Prevent using copy_from_user() for timespec/timeval
These structs can be inconsistent, for example if the amount of microseconds is
negative or larger than 1'000'000. Therefore, they should not be copied as-is.
Use copy_time_from_user instead.
2021-03-02 08:36:08 +01:00
Ben Wiederhake bd6be910e5 AK: Implement C++ 'Time' type for easier time-calculations
This adds a bunch of code in the hope that other, wrong code can be deleted.

Related to #5315.
2021-03-02 08:36:08 +01:00
Ben Wiederhake b7c5d977c7 AK: Remove unused template 2021-03-02 08:36:08 +01:00
Ben Wiederhake 90c070cb1d AK+Tests: Test Checked for main functionality 2021-03-02 08:36:08 +01:00
Andrew Kaster 669b6c43aa AK/Lagom: Modify TestSuite to return how many tests failed from main
This allows us to remove the FAIL_REGEX logic from the CTest invocation
of AK and LibRegex tests, as they will return a non-zero exit code on
failure :^).

Also means that running a failing TestSuite-enabled test with the
run-test-and-shutdown script will actually print that the test failed.
2021-03-01 11:12:36 +01:00
Andrew Kaster e787738c24 Meta: Build AK and LibRegex tests in Lagom and for Serenity
These tests were never built for the serenity target. Move their Lagom
build steps to the Lagom CMakeLists.txt, and add serenity build steps
for them. Also, fix the build errors when building them with the
serenity cross-compiler :^)
2021-02-28 18:19:37 +01:00
Sviatoslav Peleshko 183ebaee91 LibWeb: Add actual document loading for the CSS (at)import rule 2021-02-28 10:27:32 +01:00
Andreas Kling daf18e7777 AK: Use Array iterator instead of indexing outside Array bounds 2021-02-27 11:48:25 +01:00