From: Brendan Hansen Date: Fri, 2 Jul 2021 20:20:41 +0000 (-0500) Subject: moved parse_section_locations to parser X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=7f27d70f4ece7e3ee6e9ac13e8c131348efe2478;p=onyx.git moved parse_section_locations to parser --- diff --git a/modules/wasm_utils/parser.onyx b/modules/wasm_utils/parser.onyx index a9b2d128..8f3b5a9a 100644 --- a/modules/wasm_utils/parser.onyx +++ b/modules/wasm_utils/parser.onyx @@ -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; +} diff --git a/modules/wasm_utils/types.onyx b/modules/wasm_utils/types.onyx index 2577d122..d9cc4279 100644 --- a/modules/wasm_utils/types.onyx +++ b/modules/wasm_utils/types.onyx @@ -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; -}