diff options
author | Andrew Kaster <akaster@serenityos.org> | 2023-01-08 10:42:26 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-21 10:57:44 +0100 |
commit | f40094d0144a37ac20eb13abaf61e11735451053 (patch) | |
tree | 8365ac90da246839ab2c1f44b6cad7331093a35b /Userland/Libraries | |
parent | 0ea697ace57fbc1de834402077d6f0bde1b82f43 (diff) | |
download | serenity-f40094d0144a37ac20eb13abaf61e11735451053.zip |
LibWeb+LibJS: Format Console arguments with JS::Print
Instead of just calling JS::Value::to_string_without_side_effects() when
printing values to the console, have all the console clients use
the same JS::Print that the REPL does to print values.
This method leaves some things to be desired as far as OOM hardening
goes, however. We should be able to create a String in a way that
doesn't OOM on failure so hard.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibJS/Console.cpp | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Console.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/HTML/WorkerDebugConsoleClient.cpp | 2 |
3 files changed, 22 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Console.cpp b/Userland/Libraries/LibJS/Console.cpp index 37f1e80e74..99da170119 100644 --- a/Userland/Libraries/LibJS/Console.cpp +++ b/Userland/Libraries/LibJS/Console.cpp @@ -6,8 +6,11 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <AK/MemoryStream.h> #include <LibJS/Console.h> +#include <LibJS/Print.h> #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/StringConstructor.h> #include <LibJS/Runtime/Temporal/Duration.h> #include <LibJS/Runtime/ThrowableStringBuilder.h> @@ -669,4 +672,20 @@ ThrowCompletionOr<MarkedVector<Value>> ConsoleClient::formatter(MarkedVector<Val return formatter(result); } +ThrowCompletionOr<String> ConsoleClient::generically_format_values(MarkedVector<Value> const& values) +{ + AllocatingMemoryStream stream; + auto& vm = m_console.realm().vm(); + PrintContext ctx { vm, stream, true }; + bool first = true; + for (auto const& value : values) { + if (!first) + TRY_OR_THROW_OOM(vm, stream.write(" "sv.bytes())); + TRY_OR_THROW_OOM(vm, JS::print(value, ctx)); + first = false; + } + // FIXME: Is it possible we could end up serializing objects to invalid UTF-8? + return TRY_OR_THROW_OOM(vm, String::from_stream(stream, stream.used_buffer_size())); +} + } diff --git a/Userland/Libraries/LibJS/Console.h b/Userland/Libraries/LibJS/Console.h index 34f45a71b5..6394c14e50 100644 --- a/Userland/Libraries/LibJS/Console.h +++ b/Userland/Libraries/LibJS/Console.h @@ -116,6 +116,8 @@ public: virtual void clear() = 0; virtual void end_group() = 0; + ThrowCompletionOr<String> generically_format_values(MarkedVector<Value> const&); + protected: virtual ~ConsoleClient() = default; diff --git a/Userland/Libraries/LibWeb/HTML/WorkerDebugConsoleClient.cpp b/Userland/Libraries/LibWeb/HTML/WorkerDebugConsoleClient.cpp index f2a490cc63..659d2cd6f0 100644 --- a/Userland/Libraries/LibWeb/HTML/WorkerDebugConsoleClient.cpp +++ b/Userland/Libraries/LibWeb/HTML/WorkerDebugConsoleClient.cpp @@ -58,7 +58,7 @@ JS::ThrowCompletionOr<JS::Value> WorkerDebugConsoleClient::printer(JS::Console:: return JS::js_undefined(); } - auto output = TRY_OR_THROW_OOM(vm, String::join(' ', arguments.get<JS::MarkedVector<JS::Value>>())); + auto output = TRY(generically_format_values(arguments.get<JS::MarkedVector<JS::Value>>())); m_console.output_debug_message(log_level, output); switch (log_level) { |