| 1 |
use super::{COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, CORE_TYPE_SORT, TYPE_SORT}; |
| 2 |
use crate::{ |
| 3 |
encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, Encode, ExportKind, |
| 4 |
}; |
| 5 |
|
| 6 |
|
| 7 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 8 |
pub enum ComponentOuterAliasKind { |
| 9 |
|
| 10 |
CoreModule, |
| 11 |
|
| 12 |
CoreType, |
| 13 |
|
| 14 |
Type, |
| 15 |
|
| 16 |
Component, |
| 17 |
} |
| 18 |
|
| 19 |
impl Encode for ComponentOuterAliasKind { |
| 20 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 21 |
match self { |
| 22 |
Self::CoreModule => { |
| 23 |
sink.push(CORE_SORT); |
| 24 |
sink.push(CORE_MODULE_SORT); |
| 25 |
} |
| 26 |
Self::CoreType => { |
| 27 |
sink.push(CORE_SORT); |
| 28 |
sink.push(CORE_TYPE_SORT); |
| 29 |
} |
| 30 |
Self::Type => sink.push(TYPE_SORT), |
| 31 |
Self::Component => sink.push(COMPONENT_SORT), |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#[derive(Clone, Debug, Default)] |
| 53 |
pub struct ComponentAliasSection { |
| 54 |
bytes: Vec<u8>, |
| 55 |
num_added: u32, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[derive(Copy, Clone, Debug)] |
| 61 |
pub enum Alias<'a> { |
| 62 |
|
| 63 |
InstanceExport { |
| 64 |
|
| 65 |
instance: u32, |
| 66 |
|
| 67 |
|
| 68 |
kind: ComponentExportKind, |
| 69 |
|
| 70 |
name: &'a str, |
| 71 |
}, |
| 72 |
|
| 73 |
#[allow(missing_docs)] |
| 74 |
CoreInstanceExport { |
| 75 |
instance: u32, |
| 76 |
kind: ExportKind, |
| 77 |
name: &'a str, |
| 78 |
}, |
| 79 |
|
| 80 |
Outer { |
| 81 |
|
| 82 |
kind: ComponentOuterAliasKind, |
| 83 |
|
| 84 |
|
| 85 |
count: u32, |
| 86 |
|
| 87 |
|
| 88 |
index: u32, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
impl ComponentAliasSection { |
| 93 |
|
| 94 |
pub fn new() -> Self { |
| 95 |
Self::default() |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
pub fn len(&self) -> u32 { |
| 100 |
self.num_added |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
pub fn is_empty(&self) -> bool { |
| 105 |
self.num_added == 0 |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
pub fn alias(&mut self, alias: Alias<'_>) -> &mut Self { |
| 110 |
alias.encode(&mut self.bytes); |
| 111 |
self.num_added += 1; |
| 112 |
self |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
impl Encode for ComponentAliasSection { |
| 117 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 118 |
encode_section(sink, self.num_added, &self.bytes); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl ComponentSection for ComponentAliasSection { |
| 123 |
fn id(&self) -> u8 { |
| 124 |
ComponentSectionId::Alias.into() |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
impl Encode for Alias<'_> { |
| 129 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 130 |
match self { |
| 131 |
Alias::InstanceExport { |
| 132 |
instance, |
| 133 |
kind, |
| 134 |
name, |
| 135 |
} => { |
| 136 |
kind.encode(sink); |
| 137 |
sink.push(0x00); |
| 138 |
instance.encode(sink); |
| 139 |
name.encode(sink); |
| 140 |
} |
| 141 |
Alias::CoreInstanceExport { |
| 142 |
instance, |
| 143 |
kind, |
| 144 |
name, |
| 145 |
} => { |
| 146 |
sink.push(CORE_SORT); |
| 147 |
kind.encode(sink); |
| 148 |
sink.push(0x01); |
| 149 |
instance.encode(sink); |
| 150 |
name.encode(sink); |
| 151 |
} |
| 152 |
Alias::Outer { kind, count, index } => { |
| 153 |
kind.encode(sink); |
| 154 |
sink.push(0x02); |
| 155 |
count.encode(sink); |
| 156 |
index.encode(sink); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |