wasm_metadata/names/
module.rs1use std::fmt::{self, Debug};
2
3use anyhow::Result;
4use wasm_encoder::Encode;
5use wasmparser::{BinaryReader, NameSectionReader};
6
7use crate::utils::{indirect_name_map, name_map};
8
9pub struct ModuleNames<'a> {
11 module_name: Option<String>,
12 names: Vec<wasmparser::Name<'a>>,
13}
14
15impl<'a> Debug for ModuleNames<'a> {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 f.debug_struct("ModuleNames")
18 .field("module_name", &self.module_name)
19 .finish_non_exhaustive()
20 }
21}
22
23impl<'a> ModuleNames<'a> {
24 pub fn empty() -> Self {
26 ModuleNames {
27 module_name: None,
28 names: Vec::new(),
29 }
30 }
31 pub fn from_bytes(bytes: &'a [u8], offset: usize) -> Result<ModuleNames<'a>> {
34 let reader = BinaryReader::new(bytes, offset);
35 let section = NameSectionReader::new(reader);
36 let mut s = Self::empty();
37 for name in section.into_iter() {
38 let name = name?;
39 match name {
40 wasmparser::Name::Module { name, .. } => s.module_name = Some(name.to_owned()),
41 _ => s.names.push(name),
42 }
43 }
44 Ok(s)
45 }
46 pub(crate) fn from_name(name: &Option<String>) -> Self {
48 let mut s = Self::empty();
49 s.module_name = name.clone();
50 s
51 }
52
53 pub(crate) fn merge(&mut self, other: &Self) {
55 if other.module_name.is_some() {
56 self.module_name = other.module_name.clone();
57 }
58 self.names.extend_from_slice(&other.names);
59 }
60
61 pub fn set_name(&mut self, name: &str) {
63 self.module_name = Some(name.to_owned())
64 }
65 pub fn get_name(&self) -> Option<&String> {
67 self.module_name.as_ref()
68 }
69 pub(crate) fn section(&self) -> Result<wasm_encoder::NameSection> {
71 let mut section = wasm_encoder::NameSection::new();
72 if let Some(module_name) = &self.module_name {
73 section.module(&module_name);
74 }
75 for n in self.names.iter() {
76 match n {
77 wasmparser::Name::Module { .. } => unreachable!(),
78 wasmparser::Name::Function(m) => section.functions(&name_map(&m)?),
79 wasmparser::Name::Local(m) => section.locals(&indirect_name_map(&m)?),
80 wasmparser::Name::Label(m) => section.labels(&indirect_name_map(&m)?),
81 wasmparser::Name::Type(m) => section.types(&name_map(&m)?),
82 wasmparser::Name::Table(m) => section.tables(&name_map(&m)?),
83 wasmparser::Name::Memory(m) => section.memories(&name_map(&m)?),
84 wasmparser::Name::Global(m) => section.globals(&name_map(&m)?),
85 wasmparser::Name::Element(m) => section.elements(&name_map(&m)?),
86 wasmparser::Name::Data(m) => section.data(&name_map(&m)?),
87 wasmparser::Name::Field(m) => section.fields(&indirect_name_map(&m)?),
88 wasmparser::Name::Tag(m) => section.tags(&name_map(&m)?),
89 wasmparser::Name::Unknown { .. } => {} }
91 }
92 Ok(section)
93 }
94
95 pub fn raw_custom_section(&self) -> Result<Vec<u8>> {
97 let mut ret = Vec::new();
98 self.section()?.encode(&mut ret);
99 Ok(ret)
100 }
101}