wasm/core/decoding/modules/indices.rs
1use crate::{
2 core::{
3 decoding::reader::WasmDecoder,
4 structure::modules::indices::{
5 DataIdx, ElemIdx, FuncIdx, GlobalIdx, Idx, LocalIdx, MemIdx, TableIdx, TypeIdx,
6 },
7 },
8 DecodingError,
9};
10
11impl TypeIdx {
12 /// Reads a type index from Wasm code without validating it. Using the
13 /// returned type requires some other form of validation to be done.
14 ///
15 /// # Safety
16 ///
17 /// The caller must ensure that there is a valid type index in the [`WasmDecoder`].
18 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
19 let index = wasm.decode_var_u32().unwrap();
20 <Self as Idx>::new(index)
21 }
22}
23
24impl FuncIdx {
25 /// Reads a function index from Wasm code without validating it.
26 ///
27 /// # Safety
28 ///
29 /// The caller must ensure that there is a valid function index in the [`WasmDecoder`].
30 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
31 let index = wasm.decode_var_u32().unwrap();
32 <Self as Idx>::new(index)
33 }
34}
35
36impl TableIdx {
37 /// Reads a table index from Wasm code without validating it.
38 ///
39 /// # Safety
40 ///
41 /// The caller must ensure that there is a valid table index in the [`WasmDecoder`].
42 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
43 let index = wasm.decode_var_u32().unwrap();
44 <Self as Idx>::new(index)
45 }
46}
47
48impl MemIdx {
49 /// Reads a memory index from Wasm code without validating it.
50 ///
51 /// # Safety
52 ///
53 /// The caller must ensure that there is a valid memory index in the [`WasmDecoder`].
54 #[allow(unused)] // reason = "unused until multiple memories proposal is implemented"
55 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
56 let index = wasm.decode_var_u32().unwrap();
57 Self::new(index)
58 }
59}
60
61impl GlobalIdx {
62 /// Reads a global index from Wasm code without validating it.
63 ///
64 /// # Safety
65 ///
66 /// The caller must ensure that there is a valid global index in the [`WasmDecoder`].
67 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
68 let index = wasm.decode_var_u32().unwrap();
69 <Self as Idx>::new(index)
70 }
71}
72
73impl ElemIdx {
74 /// Reads an element index from Wasm code without validating it.
75 ///
76 /// # Safety
77 ///
78 /// The caller must ensure that there is a valid element index in the [`WasmDecoder`].
79 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
80 let index = wasm.decode_var_u32().unwrap();
81 <Self as Idx>::new(index)
82 }
83}
84
85impl DataIdx {
86 /// Reads a data index from Wasm code without validating it.
87 ///
88 /// # Safety
89 ///
90 /// The caller must ensure that there is a valid data index in the [`WasmDecoder`].
91 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
92 let index = wasm.decode_var_u32().unwrap();
93 <Self as Idx>::new(index)
94 }
95}
96
97impl LocalIdx {
98 /// Reads a local index from Wasm code without validating it.
99 ///
100 /// # Safety
101 ///
102 /// The caller must ensure that there is a valid local index in the [`WasmDecoder`].
103 pub unsafe fn decode_unchecked(wasm: &mut WasmDecoder) -> Self {
104 let index = wasm.decode_var_u32().unwrap();
105 Self(index)
106 }
107}
108
109/// Reads a label index from Wasm code without validating it.
110pub fn decode_label_idx(wasm: &mut WasmDecoder) -> Result<u32, DecodingError> {
111 wasm.decode_var_u32()
112}
113
114/// Reads a label index from Wasm code without validating it.
115///
116/// # Safety
117///
118/// The caller must ensure that there is a valid label index in the [`WasmDecoder`].
119pub unsafe fn decode_label_idx_unchecked(wasm: &mut WasmDecoder) -> u32 {
120 // TODO use `unwrap_unchecked` instead
121 wasm.decode_var_u32().unwrap()
122}