Debian Codegraph @

third_party/rust/wasm-smith/src/component/encode.rs

raw
1 use std::borrow::Cow;
2
3 use crate::core::{CompositeInnerType, CompositeType};
4
5 use super::*;
6 use wasm_encoder::{ComponentExportKind, ComponentOuterAliasKind, ExportKind};
7
8 impl Component {
9 /// Encode this Wasm component into bytes.
10 pub fn to_bytes(&self) -> Vec<u8> {
11 self.encoded().finish()
12 }
13
14 fn encoded(&self) -> wasm_encoder::Component {
15 let mut component = wasm_encoder::Component::new();
16 for section in &self.sections {
17 section.encode(&mut component);
18 }
19 component
20 }
21 }
22
23 impl Section {
24 fn encode(&self, component: &mut wasm_encoder::Component) {
25 match self {
26 Self::Custom(sec) => sec.encode(component),
27 Self::CoreModule(module) => {
28 let bytes = module.to_bytes();
29 component.section(&wasm_encoder::RawSection {
30 id: wasm_encoder::ComponentSectionId::CoreModule as u8,
31 data: &bytes,
32 });
33 }
34 Self::CoreInstance(_) => todo!(),
35 Self::CoreType(sec) => sec.encode(component),
36 Self::Component(comp) => {
37 let bytes = comp.to_bytes();
38 component.section(&wasm_encoder::RawSection {
39 id: wasm_encoder::ComponentSectionId::Component as u8,
40 data: &bytes,
41 });
42 }
43 Self::Instance(_) => todo!(),
44 Self::Alias(_) => todo!(),
45 Self::Type(sec) => sec.encode(component),
46 Self::Canonical(sec) => sec.encode(component),
47 Self::Start(_) => todo!(),
48 Self::Import(sec) => sec.encode(component),
49 Self::Export(_) => todo!(),
50 }
51 }
52 }
53
54 impl CustomSection {
55 fn encode(&self, component: &mut wasm_encoder::Component) {
56 component.section(&wasm_encoder::CustomSection {
57 name: (&self.name).into(),
58 data: Cow::Borrowed(&self.data),
59 });
60 }
61 }
62
63 impl TypeSection {
64 fn encode(&self, component: &mut wasm_encoder::Component) {
65 let mut sec = wasm_encoder::ComponentTypeSection::new();
66 for ty in &self.types {
67 ty.encode(sec.ty());
68 }
69 component.section(&sec);
70 }
71 }
72
73 impl ImportSection {
74 fn encode(&self, component: &mut wasm_encoder::Component) {
75 let mut sec = wasm_encoder::ComponentImportSection::new();
76 for imp in &self.imports {
77 sec.import(&imp.name, imp.ty);
78 }
79 component.section(&sec);
80 }
81 }
82
83 impl CanonicalSection {
84 fn encode(&self, component: &mut wasm_encoder::Component) {
85 let mut sec = wasm_encoder::CanonicalFunctionSection::new();
86 for func in &self.funcs {
87 match func {
88 Func::CanonLift {
89 func_ty,
90 options,
91 core_func_index,
92 } => {
93 let options = translate_canon_opt(options);
94 sec.lift(*core_func_index, *func_ty, options);
95 }
96 Func::CanonLower {
97 options,
98 func_index,
99 } => {
100 let options = translate_canon_opt(options);
101 sec.lower(*func_index, options);
102 }
103 }
104 }
105 component.section(&sec);
106 }
107 }
108
109 impl CoreTypeSection {
110 fn encode(&self, component: &mut wasm_encoder::Component) {
111 let mut sec = wasm_encoder::CoreTypeSection::new();
112 for ty in &self.types {
113 ty.encode(sec.ty());
114 }
115 component.section(&sec);
116 }
117 }
118
119 impl CoreType {
120 fn encode(&self, enc: wasm_encoder::ComponentCoreTypeEncoder<'_>) {
121 match self {
122 Self::Func(ty) => {
123 enc.core()
124 .function(ty.params.iter().copied(), ty.results.iter().copied());
125 }
126 Self::Module(mod_ty) => {
127 let mut enc_mod_ty = wasm_encoder::ModuleType::new();
128 for def in &mod_ty.defs {
129 match def {
130 ModuleTypeDef::TypeDef(CompositeType {
131 inner: CompositeInnerType::Func(func_ty),
132 ..
133 }) => {
134 enc_mod_ty.ty().function(
135 func_ty.params.iter().copied(),
136 func_ty.results.iter().copied(),
137 );
138 }
139 ModuleTypeDef::TypeDef(_) => {
140 unimplemented!("non-func types in a component's module type")
141 }
142 ModuleTypeDef::OuterAlias { count, i, kind } => match kind {
143 CoreOuterAliasKind::Type(_) => {
144 enc_mod_ty.alias_outer_core_type(*count, *i);
145 }
146 },
147 ModuleTypeDef::Import(imp) => {
148 enc_mod_ty.import(
149 &imp.module,
150 &imp.field,
151 crate::core::encode::translate_entity_type(&imp.entity_type),
152 );
153 }
154 ModuleTypeDef::Export(name, ty) => {
155 enc_mod_ty.export(name, crate::core::encode::translate_entity_type(ty));
156 }
157 }
158 }
159 enc.module(&enc_mod_ty);
160 }
161 }
162 }
163 }
164
165 impl Type {
166 fn encode(&self, enc: wasm_encoder::ComponentTypeEncoder<'_>) {
167 match self {
168 Self::Defined(ty) => {
169 ty.encode(enc.defined_type());
170 }
171 Self::Func(func_ty) => {
172 let mut f = enc.function();
173
174 f.params(func_ty.params.iter().map(|(name, ty)| (name.as_str(), *ty)));
175
176 if let Some(ty) = func_ty.unnamed_result_ty() {
177 f.result(ty);
178 } else {
179 f.results(
180 func_ty
181 .results
182 .iter()
183 .map(|(name, ty)| (name.as_deref().unwrap(), *ty)),
184 );
185 }
186 }
187 Self::Component(comp_ty) => {
188 let mut enc_comp_ty = wasm_encoder::ComponentType::new();
189 for def in &comp_ty.defs {
190 match def {
191 ComponentTypeDef::Import(imp) => {
192 enc_comp_ty.import(&imp.name, imp.ty);
193 }
194 ComponentTypeDef::CoreType(ty) => {
195 ty.encode(enc_comp_ty.core_type());
196 }
197 ComponentTypeDef::Type(ty) => {
198 ty.encode(enc_comp_ty.ty());
199 }
200 ComponentTypeDef::Export { name, url: _, ty } => {
201 enc_comp_ty.export(name, *ty);
202 }
203 ComponentTypeDef::Alias(a) => {
204 enc_comp_ty.alias(translate_alias(a));
205 }
206 }
207 }
208 enc.component(&enc_comp_ty);
209 }
210 Self::Instance(inst_ty) => {
211 let mut enc_inst_ty = wasm_encoder::InstanceType::new();
212 for def in &inst_ty.defs {
213 match def {
214 InstanceTypeDecl::CoreType(ty) => {
215 ty.encode(enc_inst_ty.core_type());
216 }
217 InstanceTypeDecl::Type(ty) => {
218 ty.encode(enc_inst_ty.ty());
219 }
220 InstanceTypeDecl::Export { name, url: _, ty } => {
221 enc_inst_ty.export(name, *ty);
222 }
223 InstanceTypeDecl::Alias(a) => {
224 enc_inst_ty.alias(translate_alias(a));
225 }
226 }
227 }
228 enc.instance(&enc_inst_ty);
229 }
230 }
231 }
232 }
233
234 impl DefinedType {
235 fn encode(&self, enc: wasm_encoder::ComponentDefinedTypeEncoder<'_>) {
236 match self {
237 Self::Primitive(ty) => enc.primitive(*ty),
238 Self::Record(ty) => {
239 enc.record(ty.fields.iter().map(|(name, ty)| (name.as_str(), *ty)));
240 }
241 Self::Variant(ty) => {
242 enc.variant(
243 ty.cases
244 .iter()
245 .map(|(name, ty, refines)| (name.as_str(), *ty, *refines)),
246 );
247 }
248 Self::List(ty) => {
249 enc.list(ty.elem_ty);
250 }
251 Self::Tuple(ty) => {
252 enc.tuple(ty.fields.iter().copied());
253 }
254 Self::Flags(ty) => {
255 enc.flags(ty.fields.iter().map(|f| f.as_str()));
256 }
257 Self::Enum(ty) => {
258 enc.enum_type(ty.variants.iter().map(|v| v.as_str()));
259 }
260 Self::Option(ty) => {
261 enc.option(ty.inner_ty);
262 }
263 Self::Result(ty) => {
264 enc.result(ty.ok_ty, ty.err_ty);
265 }
266 }
267 }
268 }
269
270 fn translate_canon_opt(options: &[CanonOpt]) -> Vec<wasm_encoder::CanonicalOption> {
271 options
272 .iter()
273 .map(|o| match o {
274 CanonOpt::StringUtf8 => wasm_encoder::CanonicalOption::UTF8,
275 CanonOpt::StringUtf16 => wasm_encoder::CanonicalOption::UTF16,
276 CanonOpt::StringLatin1Utf16 => wasm_encoder::CanonicalOption::CompactUTF16,
277 CanonOpt::Memory(idx) => wasm_encoder::CanonicalOption::Memory(*idx),
278 CanonOpt::Realloc(idx) => wasm_encoder::CanonicalOption::Realloc(*idx),
279 CanonOpt::PostReturn(idx) => wasm_encoder::CanonicalOption::PostReturn(*idx),
280 })
281 .collect()
282 }
283
284 fn translate_alias(alias: &Alias) -> wasm_encoder::Alias<'_> {
285 match alias {
286 Alias::InstanceExport {
287 instance,
288 name,
289 kind,
290 } => wasm_encoder::Alias::InstanceExport {
291 instance: *instance,
292 name,
293 kind: match kind {
294 InstanceExportAliasKind::Module => ComponentExportKind::Module,
295 InstanceExportAliasKind::Component => ComponentExportKind::Component,
296 InstanceExportAliasKind::Instance => ComponentExportKind::Instance,
297 InstanceExportAliasKind::Func => ComponentExportKind::Func,
298 InstanceExportAliasKind::Value => ComponentExportKind::Value,
299 },
300 },
301 Alias::CoreInstanceExport {
302 instance,
303 name,
304 kind,
305 } => wasm_encoder::Alias::CoreInstanceExport {
306 instance: *instance,
307 name,
308 kind: match kind {
309 CoreInstanceExportAliasKind::Func => ExportKind::Func,
310 CoreInstanceExportAliasKind::Table => ExportKind::Table,
311 CoreInstanceExportAliasKind::Global => ExportKind::Global,
312 CoreInstanceExportAliasKind::Memory => ExportKind::Memory,
313 CoreInstanceExportAliasKind::Tag => ExportKind::Tag,
314 },
315 },
316 Alias::Outer { count, i, kind } => wasm_encoder::Alias::Outer {
317 count: *count,
318 index: *i,
319 kind: match kind {
320 OuterAliasKind::Module => ComponentOuterAliasKind::CoreModule,
321 OuterAliasKind::Component => ComponentOuterAliasKind::Component,
322 OuterAliasKind::Type(_) => ComponentOuterAliasKind::Type,
323 OuterAliasKind::CoreType(_) => ComponentOuterAliasKind::CoreType,
324 },
325 },
326 }
327 }