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
use super::*;
use crate::{encoding_size, CustomSection, Encode, ExportKind, NameMap, SectionId};
#[derive(Clone, Debug, Default)]
pub struct ComponentNameSection {
bytes: Vec<u8>,
}
enum Subsection {
Component,
Decls,
}
impl ComponentNameSection {
pub fn new() -> Self {
Self::default()
}
pub fn component(&mut self, name: &str) {
let len = encoding_size(u32::try_from(name.len()).unwrap());
self.subsection_header(Subsection::Component, len + name.len());
name.encode(&mut self.bytes);
}
pub fn core_funcs(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Func as u8, names)
}
pub fn core_tables(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Table as u8, names)
}
pub fn core_memories(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Memory as u8, names)
}
pub fn core_globals(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Global as u8, names)
}
pub fn core_types(&mut self, names: &NameMap) {
self.core_decls(CORE_TYPE_SORT, names)
}
pub fn core_modules(&mut self, names: &NameMap) {
self.core_decls(CORE_MODULE_SORT, names)
}
pub fn core_instances(&mut self, names: &NameMap) {
self.core_decls(CORE_INSTANCE_SORT, names)
}
pub fn funcs(&mut self, names: &NameMap) {
self.component_decls(FUNCTION_SORT, names)
}
pub fn values(&mut self, names: &NameMap) {
self.component_decls(VALUE_SORT, names)
}
pub fn types(&mut self, names: &NameMap) {
self.component_decls(TYPE_SORT, names)
}
pub fn components(&mut self, names: &NameMap) {
self.component_decls(COMPONENT_SORT, names)
}
pub fn instances(&mut self, names: &NameMap) {
self.component_decls(INSTANCE_SORT, names)
}
fn component_decls(&mut self, kind: u8, names: &NameMap) {
self.subsection_header(Subsection::Decls, 1 + names.size());
self.bytes.push(kind);
names.encode(&mut self.bytes);
}
fn core_decls(&mut self, kind: u8, names: &NameMap) {
self.subsection_header(Subsection::Decls, 2 + names.size());
self.bytes.push(CORE_SORT);
self.bytes.push(kind);
names.encode(&mut self.bytes);
}
fn subsection_header(&mut self, id: Subsection, len: usize) {
self.bytes.push(id as u8);
len.encode(&mut self.bytes);
}
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
pub fn as_custom<'a>(&'a self) -> CustomSection<'a> {
CustomSection {
name: "component-name",
data: &self.bytes,
}
}
}
impl Encode for ComponentNameSection {
fn encode(&self, sink: &mut Vec<u8>) {
self.as_custom().encode(sink);
}
}
impl ComponentSection for ComponentNameSection {
fn id(&self) -> u8 {
SectionId::Custom.into()
}
}