| 1 |
use std::fmt::{self, Debug}; |
| 2 |
|
| 3 |
use anyhow::Result; |
| 4 |
use wasm_encoder::Encode; |
| 5 |
use wasmparser::{BinaryReader, ComponentNameSectionReader}; |
| 6 |
|
| 7 |
use crate::utils::name_map; |
| 8 |
|
| 9 |
/// Helper for rewriting a component's component-name section with a new component name. |
| 10 |
pub struct ComponentNames<'a> { |
| 11 |
component_name: Option<String>, |
| 12 |
names: Vec<wasmparser::ComponentName<'a>>, |
| 13 |
} |
| 14 |
|
| 15 |
impl<'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 |
|
| 23 |
impl<'a> ComponentNames<'a> { |
| 24 |
/// Create an empty component-name section. |
| 25 |
pub fn empty() -> Self { |
| 26 |
ComponentNames { |
| 27 |
component_name: None, |
| 28 |
names: Vec::new(), |
| 29 |
} |
| 30 |
} |
| 31 |
/// Read a component-name section from a WebAssembly binary. Records the component name, as |
| 32 |
/// well as all other component name fields for later serialization. |
| 33 |
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 |
/// Set component name according to [`AddMetadata`] |
| 49 |
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 |
/// Merge with another section |
| 56 |
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 |
/// Set component name |
| 64 |
pub fn set_name(&mut self, name: &str) { |
| 65 |
self.component_name = Some(name.to_owned()) |
| 66 |
} |
| 67 |
/// Get component name |
| 68 |
pub fn get_name(&self) -> Option<&String> { |
| 69 |
self.component_name.as_ref() |
| 70 |
} |
| 71 |
/// Serialize into [`wasm_encoder::ComponentNameSection`] |
| 72 |
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::CoreTags(m) => section.core_tags(&name_map(&m)?), |
| 85 |
wasmparser::ComponentName::CoreModules(m) => section.core_modules(&name_map(&m)?), |
| 86 |
wasmparser::ComponentName::CoreInstances(m) => { |
| 87 |
section.core_instances(&name_map(&m)?) |
| 88 |
} |
| 89 |
wasmparser::ComponentName::CoreTypes(m) => section.core_types(&name_map(&m)?), |
| 90 |
wasmparser::ComponentName::Types(m) => section.types(&name_map(&m)?), |
| 91 |
wasmparser::ComponentName::Instances(m) => section.instances(&name_map(&m)?), |
| 92 |
wasmparser::ComponentName::Components(m) => section.components(&name_map(&m)?), |
| 93 |
wasmparser::ComponentName::Funcs(m) => section.funcs(&name_map(&m)?), |
| 94 |
wasmparser::ComponentName::Values(m) => section.values(&name_map(&m)?), |
| 95 |
wasmparser::ComponentName::Unknown { .. } => {} // wasm-encoder doesn't support it |
| 96 |
} |
| 97 |
} |
| 98 |
Ok(section) |
| 99 |
} |
| 100 |
|
| 101 |
/// Serialize into the raw bytes of a wasm custom section. |
| 102 |
pub fn raw_custom_section(&self) -> Result<Vec<u8>> { |
| 103 |
let mut ret = Vec::new(); |
| 104 |
self.section()?.encode(&mut ret); |
| 105 |
Ok(ret) |
| 106 |
} |
| 107 |
} |