wasm/core/reader/
section_header.rs1use crate::core::reader::span::Span;
2use crate::core::reader::{WasmReadable, WasmReader};
3use crate::execution::assert_validated::UnwrapValidatedExt;
4use crate::{unreachable_validated, Error, Result};
5
6#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
7pub enum SectionTy {
8 Custom = 0,
9 Type = 1,
10 Import = 2,
11 Function = 3,
12 Table = 4,
13 Memory = 5,
14 Global = 6,
15 Export = 7,
16 Start = 8,
17 Element = 9,
18 Code = 10,
19 Data = 11,
20 DataCount = 12,
21}
22
23impl WasmReadable for SectionTy {
24 fn read(wasm: &mut WasmReader) -> Result<Self> {
25 use SectionTy::*;
26 let ty = match wasm.read_u8()? {
27 0 => Custom,
28 1 => Type,
29 2 => Import,
30 3 => Function,
31 4 => Table,
32 5 => Memory,
33 6 => Global,
34 7 => Export,
35 8 => Start,
36 9 => Element,
37 10 => Code,
38 11 => Data,
39 12 => DataCount,
40 other => return Err(Error::InvalidSectionType(other)),
41 };
42
43 Ok(ty)
44 }
45
46 fn read_unvalidated(wasm: &mut WasmReader) -> Self {
47 use SectionTy::*;
48 match wasm.read_u8().unwrap_validated() {
49 0 => Custom,
50 1 => Type,
51 2 => Import,
52 3 => Function,
53 4 => Table,
54 5 => Memory,
55 6 => Global,
56 7 => Export,
57 8 => Start,
58 9 => Element,
59 10 => Code,
60 11 => Data,
61 12 => DataCount,
62 _ => unreachable_validated!(),
63 }
64 }
65}
66
67#[derive(Debug)]
68pub(crate) struct SectionHeader {
69 pub ty: SectionTy,
70 pub contents: Span,
71}
72
73impl WasmReadable for SectionHeader {
74 fn read(wasm: &mut WasmReader) -> Result<Self> {
75 let ty = SectionTy::read(wasm)?;
76 let size: u32 = wasm.read_var_u32()?;
77 let contents_span = wasm.make_span(size as usize)?;
78
79 Ok(SectionHeader {
80 ty,
81 contents: contents_span,
82 })
83 }
84
85 fn read_unvalidated(wasm: &mut WasmReader) -> Self {
86 let ty = SectionTy::read_unvalidated(wasm);
87 let size: u32 = wasm.read_var_u32().unwrap_validated();
88 let contents_span = wasm
89 .make_span(size as usize)
90 .expect("TODO remove this expect");
91
92 SectionHeader {
93 ty,
94 contents: contents_span,
95 }
96 }
97}