/src/wasm-tools/crates/wasm-encoder/src/component/exports.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::{ |
2 | | COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, FUNCTION_SORT, INSTANCE_SORT, TYPE_SORT, |
3 | | VALUE_SORT, |
4 | | }; |
5 | | use crate::{encode_section, ComponentSection, ComponentSectionId, ComponentTypeRef, Encode}; |
6 | | |
7 | | /// Represents the kind of an export from a WebAssembly component. |
8 | 0 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
9 | | pub enum ComponentExportKind { |
10 | | /// The export is a core module. |
11 | | Module, |
12 | | /// The export is a function. |
13 | | Func, |
14 | | /// The export is a value. |
15 | | Value, |
16 | | /// The export is a type. |
17 | | Type, |
18 | | /// The export is an instance. |
19 | | Instance, |
20 | | /// The export is a component. |
21 | | Component, |
22 | | } |
23 | | |
24 | | impl Encode for ComponentExportKind { |
25 | 13.0M | fn encode(&self, sink: &mut Vec<u8>) { |
26 | 13.0M | match self { |
27 | 0 | Self::Module => { |
28 | 0 | sink.push(CORE_SORT); |
29 | 0 | sink.push(CORE_MODULE_SORT); |
30 | 0 | } |
31 | 3.15M | Self::Func => { |
32 | 3.15M | sink.push(FUNCTION_SORT); |
33 | 3.15M | } |
34 | 0 | Self::Value => { |
35 | 0 | sink.push(VALUE_SORT); |
36 | 0 | } |
37 | 7.14M | Self::Type => { |
38 | 7.14M | sink.push(TYPE_SORT); |
39 | 7.14M | } |
40 | 1.72M | Self::Instance => { |
41 | 1.72M | sink.push(INSTANCE_SORT); |
42 | 1.72M | } |
43 | 1.01M | Self::Component => { |
44 | 1.01M | sink.push(COMPONENT_SORT); |
45 | 1.01M | } |
46 | | } |
47 | 13.0M | } |
48 | | } |
49 | | |
50 | | /// An encoder for the export section of WebAssembly component. |
51 | | /// |
52 | | /// # Example |
53 | | /// |
54 | | /// ```rust |
55 | | /// use wasm_encoder::{Component, ComponentExportSection, ComponentExportKind}; |
56 | | /// |
57 | | /// // This exports a function named "foo" |
58 | | /// let mut exports = ComponentExportSection::new(); |
59 | | /// exports.export("foo", "", ComponentExportKind::Func, 0, None); |
60 | | /// |
61 | | /// let mut component = Component::new(); |
62 | | /// component.section(&exports); |
63 | | /// |
64 | | /// let bytes = component.finish(); |
65 | | /// ``` |
66 | 300k | #[derive(Clone, Debug, Default)] <wasm_encoder::component::exports::ComponentExportSection as core::default::Default>::default Line | Count | Source | 66 | 299k | #[derive(Clone, Debug, Default)] |
<wasm_encoder::component::exports::ComponentExportSection as core::default::Default>::default Line | Count | Source | 66 | 261 | #[derive(Clone, Debug, Default)] |
|
67 | | pub struct ComponentExportSection { |
68 | | bytes: Vec<u8>, |
69 | | num_added: u32, |
70 | | } |
71 | | |
72 | | impl ComponentExportSection { |
73 | | /// Create a new component export section encoder. |
74 | 299k | pub fn new() -> Self { |
75 | 299k | Self::default() |
76 | 299k | } |
77 | | |
78 | | /// The number of exports in the section. |
79 | 0 | pub fn len(&self) -> u32 { |
80 | 0 | self.num_added |
81 | 0 | } |
82 | | |
83 | | /// Determines if the section is empty. |
84 | 0 | pub fn is_empty(&self) -> bool { |
85 | 0 | self.num_added == 0 |
86 | 0 | } |
87 | | |
88 | | /// Define an export in the export section. |
89 | 306k | pub fn export( |
90 | 306k | &mut self, |
91 | 306k | name: &str, |
92 | 306k | url: &str, |
93 | 306k | kind: ComponentExportKind, |
94 | 306k | index: u32, |
95 | 306k | ty: Option<ComponentTypeRef>, |
96 | 306k | ) -> &mut Self { |
97 | 306k | name.encode(&mut self.bytes); |
98 | 306k | url.encode(&mut self.bytes); |
99 | 306k | kind.encode(&mut self.bytes); |
100 | 306k | index.encode(&mut self.bytes); |
101 | 306k | match ty { |
102 | 21.8k | Some(ty) => { |
103 | 21.8k | self.bytes.push(0x01); |
104 | 21.8k | ty.encode(&mut self.bytes); |
105 | 21.8k | } |
106 | 284k | None => { |
107 | 284k | self.bytes.push(0x00); |
108 | 284k | } |
109 | | } |
110 | 306k | self.num_added += 1; |
111 | 306k | self |
112 | 306k | } |
113 | | } |
114 | | |
115 | | impl Encode for ComponentExportSection { |
116 | 299k | fn encode(&self, sink: &mut Vec<u8>) { |
117 | 299k | encode_section(sink, self.num_added, &self.bytes); |
118 | 299k | } |
119 | | } |
120 | | |
121 | | impl ComponentSection for ComponentExportSection { |
122 | 299k | fn id(&self) -> u8 { |
123 | 299k | ComponentSectionId::Export.into() |
124 | 299k | } |
125 | | } |