1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use std::borrow::Cow;
use super::*;
use crate::{encoding_size, ExportKind, NameMap, SectionId};
/// Encoding for the `component-name` custom section which assigns
/// human-readable names to items within a component.
#[derive(Clone, Debug, Default)]
pub struct ComponentNameSection {
bytes: Vec<u8>,
}
enum Subsection {
Component,
Decls,
}
impl ComponentNameSection {
/// Creates a new blank `name` custom section.
pub fn new() -> Self {
Self::default()
}
/// Appends a component name subsection to this section.
///
/// This will indicate that the name of the entire component should be the
/// `name` specified. Note that this should be encoded first before other
/// subsections.
pub fn component(&mut self, name: &str) {
let len = encoding_size(u32::try_from(name.len()).unwrap());
self.subsection_header(Subsection::Component, len + name.len());
name.encode(&mut self.bytes);
}
/// Appends a decls name subsection to name core functions within the
/// component.
pub fn core_funcs(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Func as u8, names)
}
/// Appends a decls name subsection to name core tables within the
/// component.
pub fn core_tables(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Table as u8, names)
}
/// Appends a decls name subsection to name core memories within the
/// component.
pub fn core_memories(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Memory as u8, names)
}
/// Appends a decls name subsection to name core globals within the
/// component.
pub fn core_globals(&mut self, names: &NameMap) {
self.core_decls(ExportKind::Global as u8, names)
}
/// Appends a decls name subsection to name core types within the
/// component.
pub fn core_types(&mut self, names: &NameMap) {
self.core_decls(CORE_TYPE_SORT, names)
}
/// Appends a decls name subsection to name core modules within the
/// component.
pub fn core_modules(&mut self, names: &NameMap) {
self.core_decls(CORE_MODULE_SORT, names)
}
/// Appends a decls name subsection to name core instances within the
/// component.
pub fn core_instances(&mut self, names: &NameMap) {
self.core_decls(CORE_INSTANCE_SORT, names)
}
/// Appends a decls name subsection to name component functions within the
/// component.
pub fn funcs(&mut self, names: &NameMap) {
self.component_decls(FUNCTION_SORT, names)
}
/// Appends a decls name subsection to name component values within the
/// component.
pub fn values(&mut self, names: &NameMap) {
self.component_decls(VALUE_SORT, names)
}
/// Appends a decls name subsection to name component type within the
/// component.
pub fn types(&mut self, names: &NameMap) {
self.component_decls(TYPE_SORT, names)
}
/// Appends a decls name subsection to name components within the
/// component.
pub fn components(&mut self, names: &NameMap) {
self.component_decls(COMPONENT_SORT, names)
}
/// Appends a decls name subsection to name component instances within the
/// component.
pub fn instances(&mut self, names: &NameMap) {
self.component_decls(INSTANCE_SORT, names)
}
/// Appends a raw subsection with the given id and data.
pub fn raw(&mut self, id: u8, data: &[u8]) {
self.bytes.push(id);
data.encode(&mut self.bytes);
}
fn component_decls(&mut self, kind: u8, names: &NameMap) {
self.subsection_header(Subsection::Decls, 1 + names.size());
self.bytes.push(kind);
names.encode(&mut self.bytes);
}
fn core_decls(&mut self, kind: u8, names: &NameMap) {
self.subsection_header(Subsection::Decls, 2 + names.size());
self.bytes.push(CORE_SORT);
self.bytes.push(kind);
names.encode(&mut self.bytes);
}
fn subsection_header(&mut self, id: Subsection, len: usize) {
self.bytes.push(id as u8);
len.encode(&mut self.bytes);
}
/// Returns whether this section is empty, or nothing has been encoded.
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
/// View the encoded section as a CustomSection.
pub fn as_custom<'a>(&'a self) -> CustomSection<'a> {
CustomSection {
name: "component-name".into(),
data: Cow::Borrowed(&self.bytes),
}
}
}
impl Encode for ComponentNameSection {
fn encode(&self, sink: &mut Vec<u8>) {
self.as_custom().encode(sink);
}
}
impl ComponentSection for ComponentNameSection {
fn id(&self) -> u8 {
SectionId::Custom.into()
}
}