LibJS+LibWeb+WebContent: Stop using ThrowableStringBuilder

This commit is contained in:
Timothy Flynn 2023-09-06 08:29:01 -04:00 committed by Tim Flynn
parent 54d1f4e234
commit 573cbb5ca0
6 changed files with 43 additions and 46 deletions

View file

@ -7,13 +7,13 @@
*/
#include <AK/MemoryStream.h>
#include <AK/StringBuilder.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>
namespace JS {
@ -501,16 +501,16 @@ void Console::report_exception(JS::Error const& exception, bool in_promise) cons
ThrowCompletionOr<String> Console::value_vector_to_string(MarkedVector<Value> const& values)
{
auto& vm = realm().vm();
ThrowableStringBuilder builder(vm);
StringBuilder builder;
for (auto const& item : values) {
if (!builder.is_empty())
MUST_OR_THROW_OOM(builder.append(' '));
builder.append(' ');
MUST_OR_THROW_OOM(builder.append(TRY(item.to_string(vm))));
builder.append(TRY(item.to_string(vm)));
}
return builder.to_string();
return MUST(builder.to_string());
}
ThrowCompletionOr<String> Console::format_time_since(Core::ElapsedTimer timer)
@ -520,27 +520,26 @@ ThrowCompletionOr<String> Console::format_time_since(Core::ElapsedTimer timer)
auto elapsed_ms = timer.elapsed_time().to_milliseconds();
auto duration = TRY(Temporal::balance_duration(vm, 0, 0, 0, 0, elapsed_ms, 0, "0"_sbigint, "year"sv));
auto append = [&](ThrowableStringBuilder& builder, auto format, auto number) -> ThrowCompletionOr<void> {
auto append = [&](auto& builder, auto format, auto number) {
if (!builder.is_empty())
MUST_OR_THROW_OOM(builder.append(' '));
MUST_OR_THROW_OOM(builder.appendff(format, number));
return {};
builder.append(' ');
builder.appendff(format, number);
};
ThrowableStringBuilder builder(vm);
StringBuilder builder;
if (duration.days > 0)
MUST_OR_THROW_OOM(append(builder, "{:.0} day(s)"sv, duration.days));
append(builder, "{:.0} day(s)"sv, duration.days);
if (duration.hours > 0)
MUST_OR_THROW_OOM(append(builder, "{:.0} hour(s)"sv, duration.hours));
append(builder, "{:.0} hour(s)"sv, duration.hours);
if (duration.minutes > 0)
MUST_OR_THROW_OOM(append(builder, "{:.0} minute(s)"sv, duration.minutes));
append(builder, "{:.0} minute(s)"sv, duration.minutes);
if (duration.seconds > 0 || duration.milliseconds > 0) {
double combined_seconds = duration.seconds + (0.001 * duration.milliseconds);
MUST_OR_THROW_OOM(append(builder, "{:.3} seconds"sv, combined_seconds));
append(builder, "{:.3} seconds"sv, combined_seconds);
}
return builder.to_string();
return MUST(builder.to_string());
}
// 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger

View file

@ -7,13 +7,13 @@
#include <AK/CharacterTypes.h>
#include <AK/FlyString.h>
#include <AK/StringBuilder.h>
#include <AK/Utf16View.h>
#include <AK/Utf8View.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>

View file

@ -4,10 +4,10 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringBuilder.h>
#include <LibJS/Heap/MarkedVector.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/HTML/WorkerDebugConsoleClient.h>
@ -40,12 +40,12 @@ JS::ThrowCompletionOr<JS::Value> WorkerDebugConsoleClient::printer(JS::Console::
if (log_level == JS::Console::LogLevel::Trace) {
auto trace = arguments.get<JS::Console::Trace>();
JS::ThrowableStringBuilder builder(vm);
StringBuilder builder;
if (!trace.label.is_empty())
MUST_OR_THROW_OOM(builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label));
builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label);
for (auto& function_name : trace.stack)
MUST_OR_THROW_OOM(builder.appendff("{}-> {}\n", indent, function_name));
builder.appendff("{}-> {}\n", indent, function_name);
dbgln("{}", builder.string_view());
return JS::js_undefined();

View file

@ -7,6 +7,7 @@
#include <AK/MemoryStream.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/BigInt.h>
@ -15,7 +16,6 @@
#include <LibJS/Runtime/NativeFunction.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Promise.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/VM.h>
#include <LibWasm/AbstractMachine/Validator.h>
@ -283,10 +283,10 @@ JS::ThrowCompletionOr<size_t> instantiate_module(JS::VM& vm, Wasm::Module const&
auto link_result = linker.finish();
if (link_result.is_error()) {
// FIXME: Throw a LinkError.
JS::ThrowableStringBuilder builder(vm);
MUST_OR_THROW_OOM(builder.append("LinkError: Missing "sv));
MUST_OR_THROW_OOM(builder.join(' ', link_result.error().missing_imports));
return vm.throw_completion<JS::TypeError>(MUST_OR_THROW_OOM(builder.to_string()));
StringBuilder builder;
builder.append("LinkError: Missing "sv);
builder.join(' ', link_result.error().missing_imports);
return vm.throw_completion<JS::TypeError>(MUST(builder.to_string()));
}
auto instance_result = s_abstract_machine.instantiate(module, link_result.release_value());

View file

@ -7,13 +7,13 @@
*/
#include "WebContentConsoleClient.h"
#include <AK/StringBuilder.h>
#include <AK/TemporaryChange.h>
#include <LibJS/MarkupGenerator.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/ObjectEnvironment.h>
#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/HTML/PolicyContainers.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
@ -132,21 +132,19 @@ void WebContentConsoleClient::clear()
// 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer
JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::LogLevel log_level, PrinterArguments arguments)
{
auto& vm = m_console.realm().vm();
auto styling = escape_html_entities(m_current_message_style.string_view());
m_current_message_style.clear();
if (log_level == JS::Console::LogLevel::Trace) {
auto trace = arguments.get<JS::Console::Trace>();
JS::ThrowableStringBuilder html(vm);
StringBuilder html;
if (!trace.label.is_empty())
MUST_OR_THROW_OOM(html.appendff("<span class='title' style='{}'>{}</span><br>", styling, escape_html_entities(trace.label)));
html.appendff("<span class='title' style='{}'>{}</span><br>", styling, escape_html_entities(trace.label));
MUST_OR_THROW_OOM(html.append("<span class='trace'>"sv));
html.append("<span class='trace'>"sv);
for (auto& function_name : trace.stack)
MUST_OR_THROW_OOM(html.appendff("-> {}<br>", escape_html_entities(function_name)));
MUST_OR_THROW_OOM(html.append("</span>"sv));
html.appendff("-> {}<br>", escape_html_entities(function_name));
html.append("</span>"sv);
print_html(html.string_view());
return JS::js_undefined();
@ -161,31 +159,31 @@ JS::ThrowCompletionOr<JS::Value> WebContentConsoleClient::printer(JS::Console::L
auto output = TRY(generically_format_values(arguments.get<JS::MarkedVector<JS::Value>>()));
m_console.output_debug_message(log_level, output);
JS::ThrowableStringBuilder html(vm);
StringBuilder html;
switch (log_level) {
case JS::Console::LogLevel::Debug:
MUST_OR_THROW_OOM(html.appendff("<span class=\"debug\" style=\"{}\">(d) "sv, styling));
html.appendff("<span class=\"debug\" style=\"{}\">(d) "sv, styling);
break;
case JS::Console::LogLevel::Error:
MUST_OR_THROW_OOM(html.appendff("<span class=\"error\" style=\"{}\">(e) "sv, styling));
html.appendff("<span class=\"error\" style=\"{}\">(e) "sv, styling);
break;
case JS::Console::LogLevel::Info:
MUST_OR_THROW_OOM(html.appendff("<span class=\"info\" style=\"{}\">(i) "sv, styling));
html.appendff("<span class=\"info\" style=\"{}\">(i) "sv, styling);
break;
case JS::Console::LogLevel::Log:
MUST_OR_THROW_OOM(html.appendff("<span class=\"log\" style=\"{}\"> "sv, styling));
html.appendff("<span class=\"log\" style=\"{}\"> "sv, styling);
break;
case JS::Console::LogLevel::Warn:
case JS::Console::LogLevel::CountReset:
MUST_OR_THROW_OOM(html.appendff("<span class=\"warn\" style=\"{}\">(w) "sv, styling));
html.appendff("<span class=\"warn\" style=\"{}\">(w) "sv, styling);
break;
default:
MUST_OR_THROW_OOM(html.appendff("<span style=\"{}\">"sv, styling));
html.appendff("<span style=\"{}\">"sv, styling);
break;
}
MUST_OR_THROW_OOM(html.append(escape_html_entities(output)));
MUST_OR_THROW_OOM(html.append("</span>"sv));
html.append(escape_html_entities(output));
html.append("</span>"sv);
print_html(html.string_view());
return JS::js_undefined();

View file

@ -7,6 +7,7 @@
*/
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/StandardPaths.h>
@ -23,7 +24,6 @@
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/JSONObject.h>
#include <LibJS/Runtime/StringPrototype.h>
#include <LibJS/Runtime/ThrowableStringBuilder.h>
#include <LibJS/SourceTextModule.h>
#include <LibLine/Editor.h>
#include <LibMain/Main.h>
@ -503,12 +503,12 @@ public:
if (log_level == JS::Console::LogLevel::Trace) {
auto trace = arguments.get<JS::Console::Trace>();
JS::ThrowableStringBuilder builder(*g_vm);
StringBuilder builder;
if (!trace.label.is_empty())
MUST_OR_THROW_OOM(builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label));
builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label);
for (auto& function_name : trace.stack)
MUST_OR_THROW_OOM(builder.appendff("{}-> {}\n", indent, function_name));
builder.appendff("{}-> {}\n", indent, function_name);
outln("{}", builder.string_view());
return JS::js_undefined();