mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-02 22:24:26 +00:00
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.
This commit is contained in:
parent
0ea697ace5
commit
f40094d014
|
@ -168,7 +168,7 @@ JS::ThrowCompletionOr<JS::Value> ConsoleClient::printer(JS::Console::LogLevel lo
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
auto output = DeprecatedString::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);
|
||||
|
||||
StringBuilder html;
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -158,7 +158,7 @@ JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::L
|
|||
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);
|
||||
|
||||
JS::ThrowableStringBuilder html(vm);
|
||||
|
|
|
@ -558,7 +558,7 @@ public:
|
|||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
auto output = TRY_OR_THROW_OOM(*g_vm, String::join(' ', arguments.get<JS::MarkedVector<JS::Value>>()));
|
||||
auto output = TRY(generically_format_values(arguments.get<JS::MarkedVector<JS::Value>>()));
|
||||
#ifdef AK_OS_SERENITY
|
||||
m_console.output_debug_message(log_level, output);
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue