moved parse_section_locations to parser
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 2 Jul 2021 20:20:41 +0000 (15:20 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 2 Jul 2021 20:20:41 +0000 (15:20 -0500)
modules/wasm_utils/parser.onyx
modules/wasm_utils/types.onyx

index a9b2d1283d9434e08ce61ed4361a3899acc41330..8f3b5a9ab9653bb939868da878aa750c3a68f349 100644 (file)
@@ -342,3 +342,48 @@ parse_const_uint32 :: (reader: ^io.Reader, binary: ^WasmBinary) -> u32 {
 
     return ~~value;
 }
+
+#private
+parse_section_locations :: (use bin: ^WasmBinary) -> bool {
+    stream := io.string_stream_make(data);
+    reader := io.reader_make(^stream);
+
+    {
+        // Checking the magic string
+        magic_buffer: [4] u8; 
+
+        @Bug // If these are string literals, then the null byte messes up the compiler and it thinks its a 0-character string.
+        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 0, #char "a", #char "s", #char "m" ]) do return false;
+        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 1, 0, 0, 0 ]) do return false; // This may not be necessary
+    }
+    while !io.stream_end_of_file(^stream) {
+        section_number := cast(WasmSection) io.read_byte(^reader);
+        section_size   := read_uleb128(^reader);
+        _, pos := io.stream_tell(^stream);
+
+        switch section_number {
+            @Incomplete
+            case .Custom ---
+
+            case .Type, .Import, .Function, .Table, .Memory, .Global,
+                .Export, .Start, .Element, .Code, .Data, .DataCount  {
+                map.put(^sections, section_number, .{
+                    offset = pos,
+                    size   = ~~section_size,
+                });
+            }
+
+            case #default {
+                buffer: [128] u8;
+                conv :: package core.conv
+                assert(false, conv.str_format("Bad section number {}", ~~buffer, cast(i32) section_number));
+            }
+        }
+
+        err := io.stream_seek(^stream, pos + ~~section_size, .Start);
+        if err == .OutOfBounds do break;
+    }
+
+    return true;
+}
index 2577d122a69eb5ed6860ec02918128978c01cf36..d9cc42795d29d1596a0d03738efa230d73ac9616 100644 (file)
@@ -175,48 +175,16 @@ parse_sections :: (use bin: ^WasmBinary, allocator := context.allocator) -> Wasm
     return ws;
 }
 
+free_sections :: (use sections: ^WasmSections) {
+    raw_free(allocator, type_section.data);
+    raw_free(allocator, import_section.data);
+    raw_free(allocator, export_section.data);
+    raw_free(allocator, function_section.data);
+    raw_free(allocator, memory_section.data);
+    raw_free(allocator, table_section.data);
+    raw_free(allocator, global_section.data);
+    raw_free(allocator, code_section.data);
+    raw_free(allocator, data_section.data);
+}
 
-#private_file
-parse_section_locations :: (use bin: ^WasmBinary) -> bool {
-    stream := io.string_stream_make(data);
-    reader := io.reader_make(^stream);
-
-    {
-        // Checking the magic string
-        magic_buffer: [4] u8; 
-
-        @Bug // If these are string literals, then the null byte messes up the compiler and it thinks its a 0-character string.
-        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 0, #char "a", #char "s", #char "m" ]) do return false;
-        if !(io.read_bytes(^reader, cast([] u8) magic_buffer) == ~~u8.[ 1, 0, 0, 0 ]) do return false; // This may not be necessary
-    }
-    while !io.stream_end_of_file(^stream) {
-        section_number := cast(WasmSection) io.read_byte(^reader);
-        section_size   := read_uleb128(^reader);
-        _, pos := io.stream_tell(^stream);
-
-        switch section_number {
-            @Incomplete
-            case .Custom ---
-
-            case .Type, .Import, .Function, .Table, .Memory, .Global,
-                .Export, .Start, .Element, .Code, .Data, .DataCount  {
-                map.put(^sections, section_number, .{
-                    offset = pos,
-                    size   = ~~section_size,
-                });
-            }
-
-            case #default {
-                buffer: [128] u8;
-                conv :: package core.conv
-                assert(false, conv.str_format("Bad section number {}", ~~buffer, cast(i32) section_number));
-            }
-        }
-
-        err := io.stream_seek(^stream, pos + ~~section_size, .Start);
-        if err == .OutOfBounds do break;
-    }
 
-    return true;
-}