diff --git a/Userland/Libraries/LibWasm/Parser/Parser.cpp b/Userland/Libraries/LibWasm/Parser/Parser.cpp index d588d02d1d..8243c4ae01 100644 --- a/Userland/Libraries/LibWasm/Parser/Parser.cpp +++ b/Userland/Libraries/LibWasm/Parser/Parser.cpp @@ -970,7 +970,7 @@ ParseResult Locals::parse(InputStream& stream) return Locals { static_cast(count), type.release_value() }; } -ParseResult Func::parse(InputStream& stream) +ParseResult CodeSection::Func::parse(InputStream& stream) { ScopeLogger logger("Func"); auto locals = parse_vector(stream); @@ -1227,6 +1227,28 @@ ParseResult Module::parse(InputStream& stream) return Module { move(sections) }; } +void Module::populate_sections() +{ + const FunctionSection* function_section { nullptr }; + for_each_section_of_type([&](const FunctionSection& section) { function_section = §ion; }); + for_each_section_of_type([&](const CodeSection& section) { + // FIXME: This should be considered invalid once validation is implemented. + if (!function_section) + return; + size_t index = 0; + for (auto& entry : section.functions()) { + auto& type_index = function_section->types()[index]; + Vector locals; + for (auto& local : entry.func().locals()) { + for (size_t i = 0; i < local.n(); ++i) + locals.append(local.type()); + } + m_functions.empend(type_index, move(locals), entry.func().body()); + ++index; + } + }); +} + String parse_error_to_string(ParseError error) { switch (error) { diff --git a/Userland/Libraries/LibWasm/Printer/Printer.cpp b/Userland/Libraries/LibWasm/Printer/Printer.cpp index fa6ac369c9..17cf67a7fe 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.cpp +++ b/Userland/Libraries/LibWasm/Printer/Printer.cpp @@ -242,7 +242,7 @@ void Printer::print(const Wasm::Expression& expression) print(instr); } -void Printer::print(const Wasm::Func& func) +void Printer::print(const Wasm::CodeSection::Func& func) { print_indent(); print("(function\n"); diff --git a/Userland/Libraries/LibWasm/Printer/Printer.h b/Userland/Libraries/LibWasm/Printer/Printer.h index f39ed19a39..8226916aa5 100644 --- a/Userland/Libraries/LibWasm/Printer/Printer.h +++ b/Userland/Libraries/LibWasm/Printer/Printer.h @@ -11,14 +11,16 @@ namespace Wasm { struct Printer { - explicit Printer(OutputStream& stream) + explicit Printer(OutputStream& stream, size_t initial_indent = 0) : m_stream(stream) + , m_indent(initial_indent) { } void print(const Wasm::BlockType&); void print(const Wasm::CodeSection&); void print(const Wasm::CodeSection::Code&); + void print(const Wasm::CodeSection::Func&); void print(const Wasm::ConstrainedStream&); void print(const Wasm::CustomSection&); void print(const Wasm::DataCountSection&); @@ -29,7 +31,6 @@ struct Printer { void print(const Wasm::ExportSection&); void print(const Wasm::ExportSection::Export&); void print(const Wasm::Expression&); - void print(const Wasm::Func&); void print(const Wasm::FunctionSection&); void print(const Wasm::FunctionType&); void print(const Wasm::GlobalSection&); diff --git a/Userland/Libraries/LibWasm/Types.h b/Userland/Libraries/LibWasm/Types.h index 2954a06638..3fea1975da 100644 --- a/Userland/Libraries/LibWasm/Types.h +++ b/Userland/Libraries/LibWasm/Types.h @@ -799,27 +799,26 @@ private: ValueType m_type; }; -// https://webassembly.github.io/spec/core/bikeshed/#binary-func -class Func { -public: - explicit Func(Vector locals, Expression body) - : m_locals(move(locals)) - , m_body(move(body)) - { - } - - auto& locals() const { return m_locals; } - auto& body() const { return m_body; } - - static ParseResult parse(InputStream& stream); - -private: - Vector m_locals; - Expression m_body; -}; - class CodeSection { public: + // https://webassembly.github.io/spec/core/bikeshed/#binary-func + class Func { + public: + explicit Func(Vector locals, Expression body) + : m_locals(move(locals)) + , m_body(move(body)) + { + } + + auto& locals() const { return m_locals; } + auto& body() const { return m_body; } + + static ParseResult parse(InputStream& stream); + + private: + Vector m_locals; + Expression m_body; + }; class Code { public: explicit Code(u32 size, Func func) @@ -916,10 +915,10 @@ class Module { public: class Function { public: - explicit Function(TypeIndex type, Vector local_types, Expression body) + explicit Function(TypeIndex type, Vector local_types, const Expression& body) : m_type(type) , m_local_types(move(local_types)) - , m_body(move(body)) + , m_body(body) { } @@ -930,7 +929,7 @@ public: private: TypeIndex m_type; Vector m_local_types; - Expression m_body; + const Expression& m_body; }; using AnySection = Variant< @@ -954,13 +953,35 @@ public: explicit Module(Vector sections) : m_sections(move(sections)) { + populate_sections(); } auto& sections() const { return m_sections; } + auto& functions() const { return m_functions; } + + template + void for_each_section_of_type(Callback&& callback) const + { + for (auto& section : m_sections) { + if (auto ptr = section.get_pointer()) + callback(*ptr); + } + } + template + void for_each_section_of_type(Callback&& callback) + { + for (auto& section : m_sections) { + if (auto ptr = section.get_pointer()) + callback(*ptr); + } + } static ParseResult parse(InputStream& stream); private: + void populate_sections(); + Vector m_sections; + Vector m_functions; }; }