Bladeren bron

LibWasm: Port `Wasm::Printer` to `Core::Stream`

Tim Schumacher 2 jaren geleden
bovenliggende
commit
409fb0fe79

+ 1 - 2
Userland/Libraries/LibWasm/AbstractMachine/Configuration.cpp

@@ -87,8 +87,7 @@ void Configuration::dump_stack()
 {
     auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> 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());

+ 1 - 1
Userland/Libraries/LibWasm/Printer/Printer.cpp

@@ -34,7 +34,7 @@ Optional<OpCode> 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)

+ 4 - 3
Userland/Libraries/LibWasm/Printer/Printer.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <LibCore/Stream.h>
 #include <LibWasm/Types.h>
 
 namespace Wasm {
@@ -17,7 +18,7 @@ DeprecatedString instruction_name(OpCode const& opcode);
 Optional<OpCode> 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>(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 };
 };
 

+ 11 - 13
Userland/Utilities/wasm.cpp

@@ -19,7 +19,7 @@
 #include <unistd.h>
 
 RefPtr<Line::Editor> g_line_editor;
-static OwnPtr<AK::OutputStream> g_stdout {};
+static OwnPtr<Core::Stream::Stream> g_stdout {};
 static OwnPtr<Wasm::Printer> 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<int> 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<Core::Stream::WrapInAKOutputStream>(*standard_output));
+    g_stdout = TRY(Core::Stream::File::standard_output());
     g_printer = TRY(try_make<Wasm::Printer>(*g_stdout));
 
     if (print && !attempt_instantiate) {
@@ -400,8 +399,7 @@ ErrorOr<int> 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<int> 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<Wasm::WasmFunction>()).bytes());
+                g_stdout->write(DeprecatedString::formatted("    wasm function? {}\n", fn->has<Wasm::WasmFunction>()).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<int> 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);
                 }
             }