wasm_encoder/core/
imports.rs1use crate::{
2 CORE_FUNCTION_EXACT_SORT, CORE_FUNCTION_SORT, CORE_GLOBAL_SORT, CORE_MEMORY_SORT,
3 CORE_TABLE_SORT, CORE_TAG_SORT, Encode, GlobalType, MemoryType, Section, SectionId, TableType,
4 TagType, encode_section,
5};
6use alloc::vec::Vec;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum EntityType {
11 Function(u32),
15 Table(TableType),
17 Memory(MemoryType),
19 Global(GlobalType),
21 Tag(TagType),
25 FunctionExact(u32),
29}
30
31impl Encode for EntityType {
32 fn encode(&self, sink: &mut Vec<u8>) {
33 match self {
34 Self::Function(f) => {
35 sink.push(CORE_FUNCTION_SORT);
36 f.encode(sink);
37 }
38 Self::FunctionExact(f) => {
39 sink.push(CORE_FUNCTION_EXACT_SORT);
40 f.encode(sink);
41 }
42 Self::Table(t) => {
43 sink.push(CORE_TABLE_SORT);
44 t.encode(sink);
45 }
46 Self::Memory(t) => {
47 sink.push(CORE_MEMORY_SORT);
48 t.encode(sink);
49 }
50 Self::Global(t) => {
51 sink.push(CORE_GLOBAL_SORT);
52 t.encode(sink);
53 }
54 Self::Tag(t) => {
55 sink.push(CORE_TAG_SORT);
56 t.encode(sink);
57 }
58 }
59 }
60}
61
62impl From<TableType> for EntityType {
63 fn from(t: TableType) -> Self {
64 Self::Table(t)
65 }
66}
67
68impl From<MemoryType> for EntityType {
69 fn from(t: MemoryType) -> Self {
70 Self::Memory(t)
71 }
72}
73
74impl From<GlobalType> for EntityType {
75 fn from(t: GlobalType) -> Self {
76 Self::Global(t)
77 }
78}
79
80impl From<TagType> for EntityType {
81 fn from(t: TagType) -> Self {
82 Self::Tag(t)
83 }
84}
85
86#[derive(Clone, Debug, Default)]
112pub struct ImportSection {
113 bytes: Vec<u8>,
114 num_added: u32,
115}
116
117impl ImportSection {
118 pub fn new() -> Self {
120 Self::default()
121 }
122
123 pub fn len(&self) -> u32 {
125 self.num_added
126 }
127
128 pub fn is_empty(&self) -> bool {
130 self.num_added == 0
131 }
132
133 pub fn import(&mut self, module: &str, field: &str, ty: impl Into<EntityType>) -> &mut Self {
135 module.encode(&mut self.bytes);
136 field.encode(&mut self.bytes);
137 ty.into().encode(&mut self.bytes);
138 self.num_added += 1;
139 self
140 }
141}
142
143impl Encode for ImportSection {
144 fn encode(&self, sink: &mut Vec<u8>) {
145 encode_section(sink, self.num_added, &self.bytes);
146 }
147}
148
149impl Section for ImportSection {
150 fn id(&self) -> u8 {
151 SectionId::Import.into()
152 }
153}