| 1 |
use crate::{BinaryReader, BinaryReaderError, NameMap, Result, Subsection, Subsections}; |
| 2 |
use core::ops::Range; |
| 3 |
|
| 4 |
/// Type used to iterate and parse the contents of the `component-name` custom |
| 5 |
/// section in compnents, similar to the `name` section of core modules. |
| 6 |
pub type ComponentNameSectionReader<'a> = Subsections<'a, ComponentName<'a>>; |
| 7 |
|
| 8 |
/// Represents a name read from the names custom section. |
| 9 |
#[derive(Clone)] |
| 10 |
#[allow(missing_docs)] |
| 11 |
pub enum ComponentName<'a> { |
| 12 |
Component { |
| 13 |
name: &'a str, |
| 14 |
name_range: Range<usize>, |
| 15 |
}, |
| 16 |
CoreFuncs(NameMap<'a>), |
| 17 |
CoreGlobals(NameMap<'a>), |
| 18 |
CoreMemories(NameMap<'a>), |
| 19 |
CoreTables(NameMap<'a>), |
| 20 |
CoreModules(NameMap<'a>), |
| 21 |
CoreInstances(NameMap<'a>), |
| 22 |
CoreTypes(NameMap<'a>), |
| 23 |
Types(NameMap<'a>), |
| 24 |
Instances(NameMap<'a>), |
| 25 |
Components(NameMap<'a>), |
| 26 |
Funcs(NameMap<'a>), |
| 27 |
Values(NameMap<'a>), |
| 28 |
|
| 29 |
/// An unknown [name subsection](https://webassembly.github.io/spec/core/appendix/custom.html#subsections). |
| 30 |
Unknown { |
| 31 |
/// The identifier for this subsection. |
| 32 |
ty: u8, |
| 33 |
/// The contents of this subsection. |
| 34 |
data: &'a [u8], |
| 35 |
/// The range of bytes, relative to the start of the original data |
| 36 |
/// stream, that the contents of this subsection reside in. |
| 37 |
range: Range<usize>, |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
impl<'a> Subsection<'a> for ComponentName<'a> { |
| 42 |
fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> { |
| 43 |
let data = reader.remaining_buffer(); |
| 44 |
let offset = reader.original_position(); |
| 45 |
Ok(match id { |
| 46 |
0 => { |
| 47 |
let name = reader.read_string()?; |
| 48 |
if !reader.eof() { |
| 49 |
return Err(BinaryReaderError::new( |
| 50 |
"trailing data at the end of a name", |
| 51 |
reader.original_position(), |
| 52 |
)); |
| 53 |
} |
| 54 |
ComponentName::Component { |
| 55 |
name, |
| 56 |
name_range: offset..reader.original_position(), |
| 57 |
} |
| 58 |
} |
| 59 |
1 => { |
| 60 |
let ctor: fn(NameMap<'a>) -> ComponentName<'a> = match reader.read_u8()? { |
| 61 |
0x00 => match reader.read_u8()? { |
| 62 |
0x00 => ComponentName::CoreFuncs, |
| 63 |
0x01 => ComponentName::CoreTables, |
| 64 |
0x02 => ComponentName::CoreMemories, |
| 65 |
0x03 => ComponentName::CoreGlobals, |
| 66 |
0x10 => ComponentName::CoreTypes, |
| 67 |
0x11 => ComponentName::CoreModules, |
| 68 |
0x12 => ComponentName::CoreInstances, |
| 69 |
_ => { |
| 70 |
return Ok(ComponentName::Unknown { |
| 71 |
ty: 1, |
| 72 |
data, |
| 73 |
range: offset..offset + data.len(), |
| 74 |
}); |
| 75 |
} |
| 76 |
}, |
| 77 |
0x01 => ComponentName::Funcs, |
| 78 |
0x02 => ComponentName::Values, |
| 79 |
0x03 => ComponentName::Types, |
| 80 |
0x04 => ComponentName::Components, |
| 81 |
0x05 => ComponentName::Instances, |
| 82 |
_ => { |
| 83 |
return Ok(ComponentName::Unknown { |
| 84 |
ty: 1, |
| 85 |
data, |
| 86 |
range: offset..offset + data.len(), |
| 87 |
}); |
| 88 |
} |
| 89 |
}; |
| 90 |
ctor(NameMap::new(reader.shrink())?) |
| 91 |
} |
| 92 |
ty => ComponentName::Unknown { |
| 93 |
ty, |
| 94 |
data, |
| 95 |
range: offset..offset + data.len(), |
| 96 |
}, |
| 97 |
}) |
| 98 |
} |
| 99 |
} |