Browse Source

LibWasm: Implement Element section segment type 4

Ali Mohammad Pur 2 years ago
parent
commit
59e8f713db
2 changed files with 18 additions and 6 deletions
  1. 15 4
      Userland/Libraries/LibWasm/Parser/Parser.cpp
  2. 3 2
      Userland/Libraries/LibWasm/Types.h

+ 15 - 4
Userland/Libraries/LibWasm/Parser/Parser.cpp

@@ -1047,9 +1047,20 @@ ParseResult<ElementSection::SegmentType3> ElementSection::SegmentType3::parse(St
 
 ParseResult<ElementSection::SegmentType4> ElementSection::SegmentType4::parse(Stream& stream)
 {
-    dbgln("Type 4");
-    (void)stream;
-    return ParseError::NotImplemented;
+    auto expression = Expression::parse(stream);
+    if (expression.is_error())
+        return expression.error();
+    auto initializers = parse_vector<Expression>(stream);
+    if (initializers.is_error())
+        return initializers.error();
+
+    return SegmentType4 {
+        .mode = Active {
+            .index = 0,
+            .expression = expression.release_value(),
+        },
+        .initializer = initializers.release_value(),
+    };
 }
 
 ParseResult<ElementSection::SegmentType5> ElementSection::SegmentType5::parse(Stream& stream)
@@ -1117,7 +1128,7 @@ ParseResult<ElementSection::Element> ElementSection::Element::parse(Stream& stre
         if (auto result = SegmentType4::parse(stream); result.is_error()) {
             return result.error();
         } else {
-            return ParseError::NotImplemented;
+            return Element { ValueType(ValueType::FunctionReference), move(result.value().initializer), move(result.value().mode) };
         }
     case 0x05:
         if (auto result = SegmentType5::parse(stream); result.is_error()) {

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

@@ -737,7 +737,6 @@ public:
     };
 
     struct SegmentType0 {
-        // FIXME: Implement me!
         static ParseResult<SegmentType0> parse(Stream& stream);
 
         Vector<FunctionIndex> function_indices;
@@ -757,8 +756,10 @@ public:
         static ParseResult<SegmentType3> parse(Stream& stream);
     };
     struct SegmentType4 {
-        // FIXME: Implement me!
         static ParseResult<SegmentType4> parse(Stream& stream);
+
+        Active mode;
+        Vector<Expression> initializer;
     };
     struct SegmentType5 {
         // FIXME: Implement me!