wasm_encoder/component/
exports.rs1use super::{
2 COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, FUNCTION_SORT, INSTANCE_SORT, TYPE_SORT,
3 VALUE_SORT,
4};
5use crate::{encode_section, ComponentSection, ComponentSectionId, ComponentTypeRef, Encode};
6use alloc::vec::Vec;
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub enum ComponentExportKind {
11 Module,
13 Func,
15 Value,
17 Type,
19 Instance,
21 Component,
23}
24
25impl Encode for ComponentExportKind {
26 fn encode(&self, sink: &mut Vec<u8>) {
27 match self {
28 Self::Module => {
29 sink.push(CORE_SORT);
30 sink.push(CORE_MODULE_SORT);
31 }
32 Self::Func => {
33 sink.push(FUNCTION_SORT);
34 }
35 Self::Value => {
36 sink.push(VALUE_SORT);
37 }
38 Self::Type => {
39 sink.push(TYPE_SORT);
40 }
41 Self::Instance => {
42 sink.push(INSTANCE_SORT);
43 }
44 Self::Component => {
45 sink.push(COMPONENT_SORT);
46 }
47 }
48 }
49}
50
51#[derive(Clone, Debug, Default)]
68pub struct ComponentExportSection {
69 bytes: Vec<u8>,
70 num_added: u32,
71}
72
73impl ComponentExportSection {
74 pub fn new() -> Self {
76 Self::default()
77 }
78
79 pub fn len(&self) -> u32 {
81 self.num_added
82 }
83
84 pub fn is_empty(&self) -> bool {
86 self.num_added == 0
87 }
88
89 pub fn export(
91 &mut self,
92 name: &str,
93 kind: ComponentExportKind,
94 index: u32,
95 ty: Option<ComponentTypeRef>,
96 ) -> &mut Self {
97 crate::encode_component_export_name(&mut self.bytes, name);
98 kind.encode(&mut self.bytes);
99 index.encode(&mut self.bytes);
100 match ty {
101 Some(ty) => {
102 self.bytes.push(0x01);
103 ty.encode(&mut self.bytes);
104 }
105 None => {
106 self.bytes.push(0x00);
107 }
108 }
109 self.num_added += 1;
110 self
111 }
112}
113
114impl Encode for ComponentExportSection {
115 fn encode(&self, sink: &mut Vec<u8>) {
116 encode_section(sink, self.num_added, &self.bytes);
117 }
118}
119
120impl ComponentSection for ComponentExportSection {
121 fn id(&self) -> u8 {
122 ComponentSectionId::Export.into()
123 }
124}
125
126pub(crate) fn encode_component_export_name(bytes: &mut Vec<u8>, name: &str) {
128 bytes.push(0x00);
129 name.encode(bytes);
130}