From 409fb0fe79071686590cfd1fdbc815193a15d795 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 21 Jan 2023 10:29:21 +0100 Subject: LibWasm: Port `Wasm::Printer` to `Core::Stream` --- .../LibWasm/AbstractMachine/Configuration.cpp | 3 +-- Userland/Libraries/LibWasm/Printer/Printer.cpp | 2 +- Userland/Libraries/LibWasm/Printer/Printer.h | 7 ++++--- Userland/Utilities/wasm.cpp | 24 ++++++++++------------ 4 files changed, 17 insertions(+), 19 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp b/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp index 88de3a1459..43cc915617 100644 --- a/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp +++ b/Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp @@ -87,8 +87,7 @@ void Configuration::dump_stack() { auto print_value = [](CheckedFormatString format, Ts... vs) { Core::Stream::AllocatingMemoryStream memory_stream; - Core::Stream::WrapInAKOutputStream wrapped_memory_stream { memory_stream }; - Printer { wrapped_memory_stream }.print(vs...); + Printer { memory_stream }.print(vs...); auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors(); memory_stream.read_entire_buffer(buffer).release_value_but_fixme_should_propagate_errors(); dbgln(format.view(), StringView(buffer).trim_whitespace()); diff --git a/Userland/Libraries/LibWasm/Printer/Printer.cpp b/Userland/Libraries/LibWasm/Printer/Printer.cpp index 19547499e1..24bef8505b 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.cpp +++ b/Userland/Libraries/LibWasm/Printer/Printer.cpp @@ -34,7 +34,7 @@ Optional instruction_from_name(StringView name) void Printer::print_indent() { for (size_t i = 0; i < m_indent; ++i) - m_stream.write_or_error(" "sv.bytes()); + m_stream.write_entire_buffer(" "sv.bytes()).release_value_but_fixme_should_propagate_errors(); } void Printer::print(Wasm::BlockType const& type) diff --git a/Userland/Libraries/LibWasm/Printer/Printer.h b/Userland/Libraries/LibWasm/Printer/Printer.h index cf060674ad..a3aa735a62 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.h +++ b/Userland/Libraries/LibWasm/Printer/Printer.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Wasm { @@ -17,7 +18,7 @@ DeprecatedString instruction_name(OpCode const& opcode); Optional instruction_from_name(StringView name); struct Printer { - explicit Printer(OutputStream& stream, size_t initial_indent = 0) + explicit Printer(Core::Stream::Stream& stream, size_t initial_indent = 0) : m_stream(stream) , m_indent(initial_indent) { @@ -68,10 +69,10 @@ private: { StringBuilder builder; builder.appendff(fmt.view(), forward(args)...); - m_stream.write_or_error(builder.string_view().bytes()); + m_stream.write_entire_buffer(builder.string_view().bytes()).release_value_but_fixme_should_propagate_errors(); } - OutputStream& m_stream; + Core::Stream::Stream& m_stream; size_t m_indent { 0 }; }; diff --git a/Userland/Utilities/wasm.cpp b/Userland/Utilities/wasm.cpp index 94337c98d9..0d21b98bfc 100644 --- a/Userland/Utilities/wasm.cpp +++ b/Userland/Utilities/wasm.cpp @@ -19,7 +19,7 @@ #include RefPtr g_line_editor; -static OwnPtr g_stdout {}; +static OwnPtr g_stdout {}; static OwnPtr g_printer {}; static bool g_continue { false }; static void (*old_signal)(int); @@ -53,12 +53,12 @@ static bool pre_interpret_hook(Wasm::Configuration& config, Wasm::InstructionPoi if (always_print_stack) config.dump_stack(); if (always_print_instruction) { - g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()); + g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors(); g_printer->print(instr); } if (g_continue) return true; - g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()); + g_stdout->write(DeprecatedString::formatted("{:0>4} ", ip.value()).bytes()).release_value_but_fixme_should_propagate_errors(); g_printer->print(instr); DeprecatedString last_command = ""; for (;;) { @@ -213,7 +213,7 @@ static bool pre_interpret_hook(Wasm::Configuration& config, Wasm::InstructionPoi if (!result.values().is_empty()) warnln("Returned:"); for (auto& value : result.values()) { - g_stdout->write(" -> "sv.bytes()); + g_stdout->write(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors(); g_printer->print(value); } continue; @@ -339,8 +339,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!parse_result.has_value()) return 1; - auto standard_output = TRY(Core::Stream::File::standard_output()); - g_stdout = TRY(try_make(*standard_output)); + g_stdout = TRY(Core::Stream::File::standard_output()); g_printer = TRY(try_make(*g_stdout)); if (print && !attempt_instantiate) { @@ -400,8 +399,7 @@ ErrorOr serenity_main(Main::Arguments arguments) bool first = true; for (auto& argument : arguments) { Core::Stream::AllocatingMemoryStream stream; - Core::Stream::WrapInAKOutputStream wrapped_stream { stream }; - Wasm::Printer { wrapped_stream }.print(argument); + Wasm::Printer { stream }.print(argument); if (first) first = false; else @@ -454,15 +452,15 @@ ErrorOr serenity_main(Main::Arguments arguments) auto print_func = [&](auto const& address) { Wasm::FunctionInstance* fn = machine.store().get(address); - g_stdout->write(DeprecatedString::formatted("- Function with address {}, ptr = {}\n", address.value(), fn).bytes()); + g_stdout->write(DeprecatedString::formatted("- Function with address {}, ptr = {}\n", address.value(), fn).bytes()).release_value_but_fixme_should_propagate_errors(); if (fn) { - g_stdout->write(DeprecatedString::formatted(" wasm function? {}\n", fn->has()).bytes()); + g_stdout->write(DeprecatedString::formatted(" wasm function? {}\n", fn->has()).bytes()).release_value_but_fixme_should_propagate_errors(); fn->visit( [&](Wasm::WasmFunction const& func) { Wasm::Printer printer { *g_stdout, 3 }; - g_stdout->write(" type:\n"sv.bytes()); + g_stdout->write(" type:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors(); printer.print(func.type()); - g_stdout->write(" code:\n"sv.bytes()); + g_stdout->write(" code:\n"sv.bytes()).release_value_but_fixme_should_propagate_errors(); printer.print(func.code()); }, [](Wasm::HostFunction const&) {}); @@ -526,7 +524,7 @@ ErrorOr serenity_main(Main::Arguments arguments) if (!result.values().is_empty()) warnln("Returned:"); for (auto& value : result.values()) { - g_stdout->write(" -> "sv.bytes()); + g_stdout->write(" -> "sv.bytes()).release_value_but_fixme_should_propagate_errors(); g_printer->print(value); } } -- cgit v1.2.3