| 1 |
use crate::{ |
| 2 |
encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, ComponentValType, |
| 3 |
Encode, |
| 4 |
}; |
| 5 |
|
| 6 |
|
| 7 |
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] |
| 8 |
pub enum TypeBounds { |
| 9 |
|
| 10 |
Eq(u32), |
| 11 |
|
| 12 |
SubResource, |
| 13 |
} |
| 14 |
|
| 15 |
impl Encode for TypeBounds { |
| 16 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 17 |
match self { |
| 18 |
Self::Eq(i) => { |
| 19 |
sink.push(0x00); |
| 20 |
i.encode(sink); |
| 21 |
} |
| 22 |
Self::SubResource => sink.push(0x01), |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] |
| 29 |
pub enum ComponentTypeRef { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
Module(u32), |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
Func(u32), |
| 38 |
|
| 39 |
Value(ComponentValType), |
| 40 |
|
| 41 |
Type(TypeBounds), |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
Instance(u32), |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
Component(u32), |
| 50 |
} |
| 51 |
|
| 52 |
impl ComponentTypeRef { |
| 53 |
|
| 54 |
pub fn kind(&self) -> ComponentExportKind { |
| 55 |
match self { |
| 56 |
Self::Module(_) => ComponentExportKind::Module, |
| 57 |
Self::Func(_) => ComponentExportKind::Func, |
| 58 |
Self::Value(_) => ComponentExportKind::Value, |
| 59 |
Self::Type(..) => ComponentExportKind::Type, |
| 60 |
Self::Instance(_) => ComponentExportKind::Instance, |
| 61 |
Self::Component(_) => ComponentExportKind::Component, |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl Encode for ComponentTypeRef { |
| 67 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 68 |
self.kind().encode(sink); |
| 69 |
|
| 70 |
match self { |
| 71 |
Self::Module(idx) | Self::Func(idx) | Self::Instance(idx) | Self::Component(idx) => { |
| 72 |
idx.encode(sink); |
| 73 |
} |
| 74 |
Self::Value(ty) => ty.encode(sink), |
| 75 |
Self::Type(bounds) => bounds.encode(sink), |
| 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 |
#[derive(Clone, Debug, Default)] |
| 111 |
pub struct ComponentImportSection { |
| 112 |
bytes: Vec<u8>, |
| 113 |
num_added: u32, |
| 114 |
} |
| 115 |
|
| 116 |
impl ComponentImportSection { |
| 117 |
|
| 118 |
pub fn new() -> Self { |
| 119 |
Self::default() |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
pub fn len(&self) -> u32 { |
| 124 |
self.num_added |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
pub fn is_empty(&self) -> bool { |
| 129 |
self.num_added == 0 |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
pub fn import(&mut self, name: &str, ty: ComponentTypeRef) -> &mut Self { |
| 134 |
encode_component_import_name(&mut self.bytes, name); |
| 135 |
ty.encode(&mut self.bytes); |
| 136 |
self.num_added += 1; |
| 137 |
self |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl Encode for ComponentImportSection { |
| 142 |
fn encode(&self, sink: &mut Vec<u8>) { |
| 143 |
encode_section(sink, self.num_added, &self.bytes); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl ComponentSection for ComponentImportSection { |
| 148 |
fn id(&self) -> u8 { |
| 149 |
ComponentSectionId::Import.into() |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
pub(crate) fn encode_component_import_name(bytes: &mut Vec<u8>, name: &str) { |
| 167 |
bytes.push(0x00); |
| 168 |
name.encode(bytes); |
| 169 |
} |