1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::emit::{Emit, EmitContext, Section};
use crate::ir::Value;
use crate::parse::IndicesToIds;
use crate::tombstone_arena::{Id, Tombstone, TombstoneArena};
use crate::{FunctionId, InitExpr, Module, Result, TableKind, ValType};
use failure::{bail, ResultExt};
pub type ElementId = Id<Element>;
#[derive(Debug)]
pub struct Element {
id: Id<Element>,
members: Vec<FunctionId>,
}
impl Element {
pub fn id(&self) -> Id<Element> {
self.id
}
}
impl Tombstone for Element {
fn on_delete(&mut self) {
self.members = Vec::new();
}
}
#[derive(Debug, Default)]
pub struct ModuleElements {
arena: TombstoneArena<Element>,
}
impl ModuleElements {
pub fn get(&self, id: ElementId) -> &Element {
&self.arena[id]
}
pub fn get_mut(&mut self, id: ElementId) -> &mut Element {
&mut self.arena[id]
}
pub fn delete(&mut self, id: ElementId) {
self.arena.delete(id);
}
pub fn iter(&self) -> impl Iterator<Item = &Element> {
self.arena.iter().map(|(_, f)| f)
}
}
impl Module {
pub(crate) fn parse_elements(
&mut self,
section: wasmparser::ElementSectionReader,
ids: &mut IndicesToIds,
) -> Result<()> {
log::debug!("parse element section");
for (i, segment) in section.into_iter().enumerate() {
let segment = segment?;
match segment.kind {
wasmparser::ElementKind::Passive(ty) => {
drop(ty);
bail!("passive element segments not supported yet");
}
wasmparser::ElementKind::Active {
table_index,
init_expr,
} => {
let table = ids.get_table(table_index)?;
let table = match &mut self.tables.get_mut(table).kind {
TableKind::Function(t) => t,
TableKind::Anyref(_) => {
bail!("active anyref segments not supported yet");
}
};
let offset = InitExpr::eval(&init_expr, ids)
.with_context(|_e| format!("in segment {}", i))?;
let functions = segment.items.get_items_reader()?.into_iter().map(|func| {
let func = func?;
ids.get_func(func)
});
match offset {
InitExpr::Value(Value::I32(n)) => {
let offset = n as usize;
for (i, id) in functions.enumerate() {
while i + offset + 1 > table.elements.len() {
table.elements.push(None);
}
table.elements[i + offset] = Some(id?);
}
}
InitExpr::Global(global) if self.globals.get(global).ty == ValType::I32 => {
let list = functions.collect::<Result<_>>()?;
table.relative_elements.push((global, list));
}
_ => bail!("non-i32 constant in segment {}", i),
}
}
}
}
Ok(())
}
}
impl Emit for ModuleElements {
fn emit(&self, cx: &mut EmitContext) {
log::debug!("emit element section");
let mut active = cx
.module
.tables
.iter()
.filter_map(|t| match &t.kind {
TableKind::Function(list) => Some((t.id(), list)),
TableKind::Anyref(_) => None,
})
.collect::<Vec<_>>();
active.sort_by_key(|pair| pair.0);
let mut chunks = Vec::new();
for (table_id, table) in active.iter() {
let mut offset = 0;
let mut len = 0;
for (i, item) in table.elements.iter().enumerate() {
if item.is_some() {
if len == 0 {
offset = i;
}
len += 1;
} else {
if len > 0 {
chunks.push((table_id, table, offset, len));
}
len = 0;
}
}
if len > 0 {
chunks.push((table_id, table, offset, len));
}
}
let passive = self.iter().count();
let relative = active
.iter()
.map(|(_, table)| table.relative_elements.len())
.sum::<usize>();
let total = passive + relative + chunks.len();
if total == 0 {
return;
}
let mut cx = cx.start_section(Section::Element);
cx.encoder.usize(total);
let active_table_header = |cx: &mut EmitContext, index: u32| {
if index == 0 {
cx.encoder.byte(0x00);
} else {
cx.encoder.byte(0x02);
cx.encoder.u32(index);
}
};
for (&id, table, offset, len) in chunks {
let table_index = cx.indices.get_table_index(id);
active_table_header(&mut cx, table_index);
InitExpr::Value(Value::I32(offset as i32)).emit(&mut cx);
cx.encoder.usize(len);
for item in table.elements[offset..][..len].iter() {
let index = cx.indices.get_func_index(item.unwrap());
cx.encoder.u32(index);
}
}
for (id, table) in active.iter() {
let table_index = cx.indices.get_table_index(*id);
for (global, list) in table.relative_elements.iter() {
active_table_header(&mut cx, table_index);
InitExpr::Global(*global).emit(&mut cx);
cx.encoder.usize(list.len());
for func in list {
let index = cx.indices.get_func_index(*func);
cx.encoder.u32(index);
}
}
}
for (id, segment) in self.arena.iter() {
cx.indices.push_element(id);
drop((id, segment));
panic!(
"encoding a passive element segment requires either \
`ref.null` or `ref.func` encodings, which aren't \
currently implemented"
);
}
}
}