Przeglądaj źródła

LibWasm: Implement parsing of the DataCount section

With this, the parser should technically be able to parse all wasm
modules. Any parse failure on correct modules should henceforth be
labelled a bug :^)
Ali Mohammad Pur 4 lat temu
rodzic
commit
6e891822c5

+ 18 - 2
Userland/Libraries/LibWasm/Parser/Parser.cpp

@@ -1066,8 +1066,16 @@ ParseResult<DataSection> DataSection::parse(InputStream& stream)
 ParseResult<DataCountSection> DataCountSection::parse([[maybe_unused]] InputStream& stream)
 {
     ScopeLogger<WASM_BINPARSER_DEBUG> logger("DataCountSection");
-    // FIXME: Implement parsing optional values!
-    return with_eof_check(stream, ParseError::NotImplemented);
+    u32 value;
+    if (!LEB128::read_unsigned(stream, value)) {
+        if (stream.unreliable_eof()) {
+            // The section simply didn't contain anything.
+            return DataCountSection { {} };
+        }
+        return ParseError::ExpectedSize;
+    }
+
+    return DataCountSection { value };
 }
 
 ParseResult<Module> Module::parse(InputStream& stream)
@@ -1203,6 +1211,14 @@ ParseResult<Module> Module::parse(InputStream& stream)
                 return section.error();
             }
         }
+        case DataCountSection::section_id: {
+            if (auto section = DataCountSection::parse(section_stream); !section.is_error()) {
+                sections.append(section.release_value());
+                continue;
+            } else {
+                return section.error();
+            }
+        }
         default:
             return with_eof_check(stream, ParseError::InvalidIndex);
         }

+ 2 - 0
Userland/Libraries/LibWasm/Printer/Printer.cpp

@@ -783,4 +783,6 @@ HashMap<Wasm::OpCode, String> Wasm::Names::instruction_names {
     { Instructions::table_grow, "table.grow" },
     { Instructions::table_size, "table.size" },
     { Instructions::table_fill, "table.fill" },
+    { Instructions::structured_else, "synthetic:else" },
+    { Instructions::structured_end, "synthetic:end" },
 };

+ 2 - 1
Userland/Libraries/LibWasm/Types.h

@@ -945,7 +945,8 @@ public:
         StartSection,
         ElementSection,
         CodeSection,
-        DataSection>;
+        DataSection,
+        DataCountSection>;
 
     static constexpr Array<u8, 4> wasm_magic { 0, 'a', 's', 'm' };
     static constexpr Array<u8, 4> wasm_version { 1, 0, 0, 0 };