/rust/registry/src/github.com-1ecc6299db9ec823/wasmi-validation-0.3.0/src/context.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::Error; |
2 | | use alloc::vec::Vec; |
3 | | use parity_wasm::elements::{ |
4 | | BlockType, FunctionType, GlobalType, MemoryType, TableType, ValueType, |
5 | | }; |
6 | | |
7 | 0 | #[derive(Default, Debug)] |
8 | | pub struct ModuleContext { |
9 | | pub memories: Vec<MemoryType>, |
10 | | pub tables: Vec<TableType>, |
11 | | pub globals: Vec<GlobalType>, |
12 | | pub types: Vec<FunctionType>, |
13 | | pub func_type_indexes: Vec<u32>, |
14 | | } |
15 | | |
16 | | impl ModuleContext { |
17 | 125k | pub fn memories(&self) -> &[MemoryType] { |
18 | 125k | &self.memories |
19 | 125k | } |
20 | | |
21 | 54.4k | pub fn tables(&self) -> &[TableType] { |
22 | 54.4k | &self.tables |
23 | 54.4k | } |
24 | | |
25 | 746k | pub fn globals(&self) -> &[GlobalType] { |
26 | 746k | &self.globals |
27 | 746k | } |
28 | | |
29 | 651k | pub fn types(&self) -> &[FunctionType] { |
30 | 651k | &self.types |
31 | 651k | } |
32 | | |
33 | 618k | pub fn func_type_indexes(&self) -> &[u32] { |
34 | 618k | &self.func_type_indexes |
35 | 618k | } |
36 | | |
37 | 97.2k | pub fn require_memory(&self, idx: u32) -> Result<(), Error> { |
38 | 97.2k | if self.memories().get(idx as usize).is_none() { |
39 | 0 | return Err(Error(format!("Memory at index {} doesn't exists", idx))); |
40 | 97.2k | } |
41 | 97.2k | Ok(()) |
42 | 97.2k | } |
43 | | |
44 | 26.4k | pub fn require_table(&self, idx: u32) -> Result<&TableType, Error> { |
45 | 26.4k | self.tables() |
46 | 26.4k | .get(idx as usize) |
47 | 26.4k | .ok_or_else(|| Error(format!("Table at index {} doesn't exists", idx))) |
48 | 26.4k | } |
49 | | |
50 | 618k | pub fn require_function(&self, idx: u32) -> Result<(&[ValueType], BlockType), Error> { |
51 | 618k | let ty_idx = self |
52 | 618k | .func_type_indexes() |
53 | 618k | .get(idx as usize) |
54 | 618k | .ok_or_else(|| Error(format!("Function at index {} doesn't exists", idx)))?; |
55 | 618k | self.require_function_type(*ty_idx) |
56 | 618k | } |
57 | | |
58 | 651k | pub fn require_function_type(&self, idx: u32) -> Result<(&[ValueType], BlockType), Error> { |
59 | 651k | let ty = self |
60 | 651k | .types() |
61 | 651k | .get(idx as usize) |
62 | 651k | .ok_or_else(|| Error(format!("Type at index {} doesn't exists", idx)))?; |
63 | | |
64 | 651k | let params = ty.params(); |
65 | 651k | let return_ty = ty |
66 | 651k | .return_type() |
67 | 651k | .map(BlockType::Value) |
68 | 651k | .unwrap_or(BlockType::NoResult); |
69 | 651k | Ok((params, return_ty)) |
70 | 651k | } |
71 | | |
72 | 719k | pub fn require_global(&self, idx: u32, mutability: Option<bool>) -> Result<&GlobalType, Error> { |
73 | 719k | let global = self |
74 | 719k | .globals() |
75 | 719k | .get(idx as usize) |
76 | 719k | .ok_or_else(|| Error(format!("Global at index {} doesn't exists", idx)))?; |
77 | | |
78 | 719k | if let Some(expected_mutable) = mutability { |
79 | 238k | if expected_mutable && !global.is_mutable() { |
80 | 0 | return Err(Error(format!("Expected global {} to be mutable", idx))); |
81 | 238k | } |
82 | 238k | if !expected_mutable && global.is_mutable() { |
83 | 0 | return Err(Error(format!("Expected global {} to be immutable", idx))); |
84 | 238k | } |
85 | 481k | } |
86 | 719k | Ok(global) |
87 | 719k | } |
88 | | } |
89 | | |
90 | 28.2k | #[derive(Default)] |
91 | | pub struct ModuleContextBuilder { |
92 | | memories: Vec<MemoryType>, |
93 | | tables: Vec<TableType>, |
94 | | globals: Vec<GlobalType>, |
95 | | types: Vec<FunctionType>, |
96 | | func_type_indexes: Vec<u32>, |
97 | | } |
98 | | |
99 | | impl ModuleContextBuilder { |
100 | 28.2k | pub fn new() -> ModuleContextBuilder { |
101 | 28.2k | ModuleContextBuilder::default() |
102 | 28.2k | } |
103 | | |
104 | 28.2k | pub fn push_memory(&mut self, memory: MemoryType) { |
105 | 28.2k | self.memories.push(memory); |
106 | 28.2k | } |
107 | | |
108 | 2.73k | pub fn push_table(&mut self, table: TableType) { |
109 | 2.73k | self.tables.push(table); |
110 | 2.73k | } |
111 | | |
112 | 51.2k | pub fn push_global(&mut self, global: GlobalType) { |
113 | 51.2k | self.globals.push(global); |
114 | 51.2k | } |
115 | | |
116 | 28.2k | pub fn set_types(&mut self, types: Vec<FunctionType>) { |
117 | 28.2k | self.types = types; |
118 | 28.2k | } |
119 | | |
120 | 26.7k | pub fn push_func_type_index(&mut self, func_type_index: u32) { |
121 | 26.7k | self.func_type_indexes.push(func_type_index); |
122 | 26.7k | } |
123 | | |
124 | 27.9k | pub fn build(self) -> ModuleContext { |
125 | 27.9k | let ModuleContextBuilder { |
126 | 27.9k | memories, |
127 | 27.9k | tables, |
128 | 27.9k | globals, |
129 | 27.9k | types, |
130 | 27.9k | func_type_indexes, |
131 | 27.9k | } = self; |
132 | 27.9k | |
133 | 27.9k | ModuleContext { |
134 | 27.9k | memories, |
135 | 27.9k | tables, |
136 | 27.9k | globals, |
137 | 27.9k | types, |
138 | 27.9k | func_type_indexes, |
139 | 27.9k | } |
140 | 27.9k | } |
141 | | } |