wasm_metadata/names/
component.rs1use std::fmt::{self, Debug};
2
3use anyhow::Result;
4use wasm_encoder::Encode;
5use wasmparser::{BinaryReader, ComponentNameSectionReader};
6
7use crate::utils::name_map;
8
9pub struct ComponentNames<'a> {
11 component_name: Option<String>,
12 names: Vec<wasmparser::ComponentName<'a>>,
13}
14
15impl<'a> Debug for ComponentNames<'a> {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 f.debug_struct("ComponentNames")
18 .field("component_name", &self.component_name)
19 .finish_non_exhaustive()
20 }
21}
22
23impl<'a> ComponentNames<'a> {
24 pub fn empty() -> Self {
26 ComponentNames {
27 component_name: None,
28 names: Vec::new(),
29 }
30 }
31 pub fn from_bytes(bytes: &'a [u8], offset: usize) -> Result<ComponentNames<'a>> {
34 let reader = BinaryReader::new(bytes, offset);
35 let section = ComponentNameSectionReader::new(reader);
36 let mut s = Self::empty();
37 for name in section.into_iter() {
38 let name = name?;
39 match name {
40 wasmparser::ComponentName::Component { name, .. } => {
41 s.component_name = Some(name.to_owned())
42 }
43 _ => s.names.push(name),
44 }
45 }
46 Ok(s)
47 }
48 pub(crate) fn from_name(name: &Option<String>) -> Self {
50 let mut s = Self::empty();
51 s.component_name = name.clone();
52 s
53 }
54
55 pub(crate) fn merge(&mut self, other: &Self) {
57 if other.component_name.is_some() {
58 self.component_name = other.component_name.clone();
59 }
60 self.names.extend_from_slice(&other.names);
61 }
62
63 pub fn set_name(&mut self, name: &str) {
65 self.component_name = Some(name.to_owned())
66 }
67 pub fn get_name(&self) -> Option<&String> {
69 self.component_name.as_ref()
70 }
71 pub(crate) fn section(&self) -> Result<wasm_encoder::ComponentNameSection> {
73 let mut section = wasm_encoder::ComponentNameSection::new();
74 if let Some(component_name) = &self.component_name {
75 section.component(&component_name);
76 }
77 for n in self.names.iter() {
78 match n {
79 wasmparser::ComponentName::Component { .. } => unreachable!(),
80 wasmparser::ComponentName::CoreFuncs(m) => section.core_funcs(&name_map(&m)?),
81 wasmparser::ComponentName::CoreGlobals(m) => section.core_globals(&name_map(&m)?),
82 wasmparser::ComponentName::CoreMemories(m) => section.core_memories(&name_map(&m)?),
83 wasmparser::ComponentName::CoreTables(m) => section.core_tables(&name_map(&m)?),
84 wasmparser::ComponentName::CoreModules(m) => section.core_modules(&name_map(&m)?),
85 wasmparser::ComponentName::CoreInstances(m) => {
86 section.core_instances(&name_map(&m)?)
87 }
88 wasmparser::ComponentName::CoreTypes(m) => section.core_types(&name_map(&m)?),
89 wasmparser::ComponentName::Types(m) => section.types(&name_map(&m)?),
90 wasmparser::ComponentName::Instances(m) => section.instances(&name_map(&m)?),
91 wasmparser::ComponentName::Components(m) => section.components(&name_map(&m)?),
92 wasmparser::ComponentName::Funcs(m) => section.funcs(&name_map(&m)?),
93 wasmparser::ComponentName::Values(m) => section.values(&name_map(&m)?),
94 wasmparser::ComponentName::Unknown { .. } => {} }
96 }
97 Ok(section)
98 }
99
100 pub fn raw_custom_section(&self) -> Result<Vec<u8>> {
102 let mut ret = Vec::new();
103 self.section()?.encode(&mut ret);
104 Ok(ret)
105 }
106}