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
use crate::emit::{Emit, EmitContext, Section};
use crate::map::IdHashSet;
use crate::parse::IndicesToIds;
use crate::{FunctionId, GlobalId, MemoryId, Module, Result, TableId};
use id_arena::{Arena, Id};
pub type ExportId = Id<Export>;
#[derive(Clone, Debug)]
pub struct Export {
id: ExportId,
pub name: String,
pub item: ExportItem,
}
impl Export {
pub fn id(&self) -> ExportId {
self.id
}
}
#[derive(Copy, Clone, Debug)]
pub enum ExportItem {
Function(FunctionId),
Table(TableId),
Memory(MemoryId),
Global(GlobalId),
}
#[derive(Debug, Default)]
pub struct ModuleExports {
arena: Arena<Export>,
blacklist: IdHashSet<Export>,
}
impl ModuleExports {
pub fn get(&self, id: ExportId) -> &Export {
&self.arena[id]
}
pub fn get_mut(&mut self, id: ExportId) -> &mut Export {
&mut self.arena[id]
}
pub fn iter(&self) -> impl Iterator<Item = &Export> {
self.arena.iter().map(|(_, f)| f)
}
pub fn add(&mut self, name: &str, item: impl Into<ExportItem>) -> ExportId {
self.arena.alloc_with_id(|id| Export {
id,
name: name.to_string(),
item: item.into(),
})
}
pub fn iter_roots(&self) -> impl Iterator<Item = &Export> {
self.iter().filter(move |e| !self.blacklist.contains(&e.id))
}
pub fn remove_root(&mut self, id: ExportId) {
self.blacklist.insert(id);
}
}
impl Module {
pub(crate) fn parse_exports(
&mut self,
section: wasmparser::ExportSectionReader,
ids: &IndicesToIds,
) -> Result<()> {
log::debug!("parse export section");
use wasmparser::ExternalKind::*;
for entry in section {
let entry = entry?;
let item = match entry.kind {
Function => ExportItem::Function(ids.get_func(entry.index)?),
Table => ExportItem::Table(ids.get_table(entry.index)?),
Memory => ExportItem::Memory(ids.get_memory(entry.index)?),
Global => ExportItem::Global(ids.get_global(entry.index)?),
};
let id = self.exports.arena.next_id();
self.exports.arena.alloc(Export {
id,
name: entry.field.to_string(),
item,
});
}
Ok(())
}
}
impl Emit for ModuleExports {
fn emit(&self, cx: &mut EmitContext) {
log::debug!("emit export section");
let count = self.iter_roots().count();
if count == 0 {
return;
}
let mut cx = cx.start_section(Section::Export);
cx.encoder.usize(count);
for export in self.iter_roots() {
cx.encoder.str(&export.name);
match export.item {
ExportItem::Function(id) => {
let index = cx.indices.get_func_index(id);
cx.encoder.byte(0x00);
cx.encoder.u32(index);
}
ExportItem::Table(id) => {
let index = cx.indices.get_table_index(id);
cx.encoder.byte(0x01);
cx.encoder.u32(index);
}
ExportItem::Memory(id) => {
let index = cx.indices.get_memory_index(id);
cx.encoder.byte(0x02);
cx.encoder.u32(index);
}
ExportItem::Global(id) => {
let index = cx.indices.get_global_index(id);
cx.encoder.byte(0x03);
cx.encoder.u32(index);
}
}
}
}
}
impl From<MemoryId> for ExportItem {
fn from(id: MemoryId) -> ExportItem {
ExportItem::Memory(id)
}
}
impl From<FunctionId> for ExportItem {
fn from(id: FunctionId) -> ExportItem {
ExportItem::Function(id)
}
}
impl From<GlobalId> for ExportItem {
fn from(id: GlobalId) -> ExportItem {
ExportItem::Global(id)
}
}
impl From<TableId> for ExportItem {
fn from(id: TableId) -> ExportItem {
ExportItem::Table(id)
}
}