wasm_encoder/core/
exports.rs1use super::{
2 CORE_FUNCTION_SORT, CORE_GLOBAL_SORT, CORE_MEMORY_SORT, CORE_TABLE_SORT, CORE_TAG_SORT,
3};
4use crate::{encode_section, Encode, Section, SectionId};
5use alloc::vec::Vec;
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
9#[repr(u8)]
10pub enum ExportKind {
11 Func = CORE_FUNCTION_SORT,
13 Table = CORE_TABLE_SORT,
15 Memory = CORE_MEMORY_SORT,
17 Global = CORE_GLOBAL_SORT,
19 Tag = CORE_TAG_SORT,
21}
22
23impl Encode for ExportKind {
24 fn encode(&self, sink: &mut Vec<u8>) {
25 sink.push(*self as u8);
26 }
27}
28
29#[derive(Clone, Debug, Default)]
45pub struct ExportSection {
46 bytes: Vec<u8>,
47 num_added: u32,
48}
49
50impl ExportSection {
51 pub fn new() -> Self {
53 Self::default()
54 }
55
56 pub fn len(&self) -> u32 {
58 self.num_added
59 }
60
61 pub fn is_empty(&self) -> bool {
63 self.num_added == 0
64 }
65
66 pub fn export(&mut self, name: &str, kind: ExportKind, index: u32) -> &mut Self {
68 name.encode(&mut self.bytes);
69 kind.encode(&mut self.bytes);
70 index.encode(&mut self.bytes);
71 self.num_added += 1;
72 self
73 }
74}
75
76impl Encode for ExportSection {
77 fn encode(&self, sink: &mut Vec<u8>) {
78 encode_section(sink, self.num_added, &self.bytes);
79 }
80}
81
82impl Section for ExportSection {
83 fn id(&self) -> u8 {
84 SectionId::Export.into()
85 }
86}