1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:27:43 +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:
Andrew Kaster 2023-01-08 10:42:26 -07:00 committed by Linus Groh
parent 0ea697ace5
commit f40094d014
6 changed files with 25 additions and 4 deletions

View file

@ -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()));
}
}