Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/wasm-encoder-0.32.0/src/component/instances.rs
Line
Count
Source (jump to first uncovered line)
1
use super::CORE_INSTANCE_SORT;
2
use crate::{
3
    encode_section, ComponentExportKind, ComponentExternName, ComponentSection, ComponentSectionId,
4
    Encode, ExportKind,
5
};
6
7
/// Represents an argument to a module instantiation.
8
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9
pub enum ModuleArg {
10
    /// The argument is an instance.
11
    Instance(u32),
12
}
13
14
impl Encode for ModuleArg {
15
0
    fn encode(&self, sink: &mut Vec<u8>) {
16
0
        let (sort, idx) = match self {
17
0
            Self::Instance(idx) => (CORE_INSTANCE_SORT, *idx),
18
0
        };
19
0
        sink.push(sort);
20
0
        idx.encode(sink);
21
0
    }
22
}
23
24
/// An encoder for the core instance section of WebAssembly components.
25
///
26
/// # Example
27
///
28
/// ```rust
29
/// use wasm_encoder::{Component, InstanceSection, ExportKind, ModuleArg};
30
///
31
/// let mut instances = InstanceSection::new();
32
/// instances.export_items([("foo", ExportKind::Func, 0)]);
33
/// instances.instantiate(1, [("foo", ModuleArg::Instance(0))]);
34
///
35
/// let mut component = Component::new();
36
/// component.section(&instances);
37
///
38
/// let bytes = component.finish();
39
/// ```
40
#[derive(Clone, Debug, Default)]
41
pub struct InstanceSection {
42
    bytes: Vec<u8>,
43
    num_added: u32,
44
}
45
46
impl InstanceSection {
47
    /// Create a new core instance section encoder.
48
0
    pub fn new() -> Self {
49
0
        Self::default()
50
0
    }
51
52
    /// The number of instances in the section.
53
0
    pub fn len(&self) -> u32 {
54
0
        self.num_added
55
0
    }
56
57
    /// Determines if the section is empty.
58
0
    pub fn is_empty(&self) -> bool {
59
0
        self.num_added == 0
60
0
    }
61
62
    /// Define an instance by instantiating a core module.
63
0
    pub fn instantiate<A, S>(&mut self, module_index: u32, args: A) -> &mut Self
64
0
    where
65
0
        A: IntoIterator<Item = (S, ModuleArg)>,
66
0
        A::IntoIter: ExactSizeIterator,
67
0
        S: AsRef<str>,
68
0
    {
69
0
        let args = args.into_iter();
70
0
        self.bytes.push(0x00);
71
0
        module_index.encode(&mut self.bytes);
72
0
        args.len().encode(&mut self.bytes);
73
0
        for (name, arg) in args {
74
0
            name.as_ref().encode(&mut self.bytes);
75
0
            arg.encode(&mut self.bytes);
76
0
        }
77
0
        self.num_added += 1;
78
0
        self
79
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::instantiate::<core::iter::adapters::map::Map<core::slice::iter::Iter<wast::component::instance::CoreInstantiationArg>, <wast::component::binary::Encoder>::encode_core_instance::{closure#0}>, &str>
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::instantiate::<_, _>
80
81
    /// Define an instance by exporting core WebAssembly items.
82
0
    pub fn export_items<E, S>(&mut self, exports: E) -> &mut Self
83
0
    where
84
0
        E: IntoIterator<Item = (S, ExportKind, u32)>,
85
0
        E::IntoIter: ExactSizeIterator,
86
0
        S: AsRef<str>,
87
0
    {
88
0
        let exports = exports.into_iter();
89
0
        self.bytes.push(0x01);
90
0
        exports.len().encode(&mut self.bytes);
91
0
        for (name, kind, index) in exports {
92
0
            name.as_ref().encode(&mut self.bytes);
93
0
            kind.encode(&mut self.bytes);
94
0
            index.encode(&mut self.bytes);
95
0
        }
96
0
        self.num_added += 1;
97
0
        self
98
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<core::iter::adapters::map::Map<core::slice::iter::Iter<wast::component::instance::CoreInstanceExport>, <wast::component::binary::Encoder>::encode_core_instance::{closure#1}>, &str>
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection>::export_items::<_, _>
99
}
100
101
impl Encode for InstanceSection {
102
0
    fn encode(&self, sink: &mut Vec<u8>) {
103
0
        encode_section(sink, self.num_added, &self.bytes);
104
0
    }
105
}
106
107
impl ComponentSection for InstanceSection {
108
0
    fn id(&self) -> u8 {
109
0
        ComponentSectionId::CoreInstance.into()
110
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection as wasm_encoder::component::ComponentSection>::id
Unexecuted instantiation: <wasm_encoder::component::instances::InstanceSection as wasm_encoder::component::ComponentSection>::id
111
}
112
113
/// An encoder for the instance section of WebAssembly components.
114
///
115
/// # Example
116
///
117
/// ```rust
118
/// use wasm_encoder::{Component, ComponentInstanceSection, ComponentExportKind, ComponentExternName};
119
///
120
/// let mut instances = ComponentInstanceSection::new();
121
/// instances.export_items([(ComponentExternName::Kebab("foo"), ComponentExportKind::Func, 0)]);
122
/// instances.instantiate(1, [("foo", ComponentExportKind::Instance, 0)]);
123
///
124
/// let mut component = Component::new();
125
/// component.section(&instances);
126
///
127
/// let bytes = component.finish();
128
/// ```
129
#[derive(Clone, Debug, Default)]
130
pub struct ComponentInstanceSection {
131
    bytes: Vec<u8>,
132
    num_added: u32,
133
}
134
135
impl ComponentInstanceSection {
136
    /// Create a new instance section encoder.
137
0
    pub fn new() -> Self {
138
0
        Self::default()
139
0
    }
140
141
    /// The number of instances in the section.
142
0
    pub fn len(&self) -> u32 {
143
0
        self.num_added
144
0
    }
145
146
    /// Determines if the section is empty.
147
0
    pub fn is_empty(&self) -> bool {
148
0
        self.num_added == 0
149
0
    }
150
151
    /// Define an instance by instantiating a component.
152
0
    pub fn instantiate<A, S>(&mut self, component_index: u32, args: A) -> &mut Self
153
0
    where
154
0
        A: IntoIterator<Item = (S, ComponentExportKind, u32)>,
155
0
        A::IntoIter: ExactSizeIterator,
156
0
        S: AsRef<str>,
157
0
    {
158
0
        let args = args.into_iter();
159
0
        self.bytes.push(0x00);
160
0
        component_index.encode(&mut self.bytes);
161
0
        args.len().encode(&mut self.bytes);
162
0
        for (name, kind, index) in args {
163
0
            name.as_ref().encode(&mut self.bytes);
164
0
            kind.encode(&mut self.bytes);
165
0
            index.encode(&mut self.bytes);
166
0
        }
167
0
        self.num_added += 1;
168
0
        self
169
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<core::iter::adapters::map::Map<core::slice::iter::Iter<wast::component::instance::InstantiationArg>, <wast::component::binary::Encoder>::encode_instance::{closure#0}>, &str>
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::instantiate::<_, _>
170
171
    /// Define an instance by exporting items.
172
0
    pub fn export_items<'a, E>(&mut self, exports: E) -> &mut Self
173
0
    where
174
0
        E: IntoIterator<Item = (ComponentExternName<'a>, ComponentExportKind, u32)>,
175
0
        E::IntoIter: ExactSizeIterator,
176
0
    {
177
0
        let exports = exports.into_iter();
178
0
        self.bytes.push(0x01);
179
0
        exports.len().encode(&mut self.bytes);
180
0
        for (name, kind, index) in exports {
181
0
            name.encode(&mut self.bytes);
182
0
            kind.encode(&mut self.bytes);
183
0
            index.encode(&mut self.bytes);
184
0
        }
185
0
        self.num_added += 1;
186
0
        self
187
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::export_items::<core::iter::adapters::map::Map<core::slice::iter::Iter<wast::component::export::ComponentExport>, <wast::component::binary::Encoder>::encode_instance::{closure#1}>>
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection>::export_items::<_>
188
}
189
190
impl Encode for ComponentInstanceSection {
191
0
    fn encode(&self, sink: &mut Vec<u8>) {
192
0
        encode_section(sink, self.num_added, &self.bytes);
193
0
    }
194
}
195
196
impl ComponentSection for ComponentInstanceSection {
197
0
    fn id(&self) -> u8 {
198
0
        ComponentSectionId::Instance.into()
199
0
    }
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection as wasm_encoder::component::ComponentSection>::id
Unexecuted instantiation: <wasm_encoder::component::instances::ComponentInstanceSection as wasm_encoder::component::ComponentSection>::id
200
}