/src/wasm-tools/crates/wasm-metadata/src/names/module.rs
Line | Count | Source |
1 | | use std::fmt::{self, Debug}; |
2 | | |
3 | | use anyhow::Result; |
4 | | use wasm_encoder::Encode; |
5 | | use wasm_encoder::reencode::{Reencode, RoundtripReencoder}; |
6 | | use wasmparser::{BinaryReader, NameSectionReader}; |
7 | | |
8 | | /// Helper for rewriting a module's name section with a new module name. |
9 | | pub struct ModuleNames<'a> { |
10 | | module_name: Option<String>, |
11 | | names: Vec<wasmparser::Name<'a>>, |
12 | | } |
13 | | |
14 | | impl<'a> Debug for ModuleNames<'a> { |
15 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
16 | 0 | f.debug_struct("ModuleNames") |
17 | 0 | .field("module_name", &self.module_name) |
18 | 0 | .finish_non_exhaustive() |
19 | 0 | } |
20 | | } |
21 | | |
22 | | impl<'a> ModuleNames<'a> { |
23 | | /// Create an empty name section. |
24 | 0 | pub fn empty() -> Self { |
25 | 0 | ModuleNames { |
26 | 0 | module_name: None, |
27 | 0 | names: Vec::new(), |
28 | 0 | } |
29 | 0 | } |
30 | | /// Read a name section from a WebAssembly binary. Records the module name, and all other |
31 | | /// contents of name section, for later serialization. |
32 | 0 | pub fn from_bytes(bytes: &'a [u8], offset: u64) -> Result<ModuleNames<'a>> { |
33 | 0 | let reader = BinaryReader::new(bytes, offset); |
34 | 0 | let section = NameSectionReader::new(reader); |
35 | 0 | let mut s = Self::empty(); |
36 | 0 | for name in section.into_iter() { |
37 | 0 | let name = name?; |
38 | 0 | match name { |
39 | 0 | wasmparser::Name::Module { name, .. } => s.module_name = Some(name.to_owned()), |
40 | 0 | _ => s.names.push(name), |
41 | | } |
42 | | } |
43 | 0 | Ok(s) |
44 | 0 | } |
45 | | /// Update module section according to [`AddMetadata`] |
46 | 0 | pub(crate) fn from_name(name: &Option<String>) -> Self { |
47 | 0 | let mut s = Self::empty(); |
48 | 0 | s.module_name = name.clone(); |
49 | 0 | s |
50 | 0 | } |
51 | | |
52 | | /// Merge with another section |
53 | 0 | pub(crate) fn merge(&mut self, other: &Self) { |
54 | 0 | if other.module_name.is_some() { |
55 | 0 | self.module_name = other.module_name.clone(); |
56 | 0 | } |
57 | 0 | self.names.extend_from_slice(&other.names); |
58 | 0 | } |
59 | | |
60 | | /// Set module name |
61 | 0 | pub fn set_name(&mut self, name: &str) { |
62 | 0 | self.module_name = Some(name.to_owned()) |
63 | 0 | } |
64 | | /// Get module name |
65 | 0 | pub fn get_name(&self) -> Option<&String> { |
66 | 0 | self.module_name.as_ref() |
67 | 0 | } |
68 | | /// Serialize into [`wasm_encoder::NameSection`]. |
69 | 0 | pub(crate) fn section(&self) -> Result<wasm_encoder::NameSection> { |
70 | 0 | let mut section = wasm_encoder::NameSection::new(); |
71 | 0 | if let Some(module_name) = &self.module_name { |
72 | 0 | section.module(&module_name); |
73 | 0 | } |
74 | 0 | for n in self.names.iter() { |
75 | 0 | RoundtripReencoder.parse_custom_name_subsection(&mut section, n.clone())?; |
76 | | } |
77 | 0 | Ok(section) |
78 | 0 | } |
79 | | |
80 | | /// Serialize into the raw bytes of a wasm custom section. |
81 | 0 | pub fn raw_custom_section(&self) -> Result<Vec<u8>> { |
82 | 0 | let mut ret = Vec::new(); |
83 | 0 | self.section()?.encode(&mut ret); |
84 | 0 | Ok(ret) |
85 | 0 | } |
86 | | } |