Coverage Report

Created: 2021-03-22 08:29

/src/wasm-tools/crates/wasm-encoder/src/instances.rs
Line
Count
Source (jump to first uncovered line)
1
use super::*;
2
3
/// An encoder for the instance section.
4
///
5
/// Note that this is part of the [module linking proposal][proposal] and is not
6
/// currently part of stable WebAssembly.
7
///
8
/// [proposal]: https://github.com/webassembly/module-linking
9
///
10
/// # Example
11
///
12
/// ```
13
/// use wasm_encoder::{Module, InstanceSection, Export};
14
///
15
/// let mut instances = InstanceSection::new();
16
/// instances.instantiate(0, vec![
17
///     ("x", Export::Function(0)),
18
///     ("", Export::Module(2)),
19
///     ("foo", Export::Global(0)),
20
/// ]);
21
///
22
/// let mut module = Module::new();
23
/// module.section(&instances);
24
///
25
/// let wasm_bytes = module.finish();
26
/// ```
27
0
#[derive(Clone, Debug)]
28
pub struct InstanceSection {
29
    bytes: Vec<u8>,
30
    num_added: u32,
31
}
32
33
impl InstanceSection {
34
    /// Construct a new instance section encoder.
35
2.01M
    pub fn new() -> InstanceSection {
36
2.01M
        InstanceSection {
37
2.01M
            bytes: vec![],
38
2.01M
            num_added: 0,
39
2.01M
        }
40
2.01M
    }
41
42
    /// Define an instantiation of the given module with the given items as
43
    /// arguments to the instantiation.
44
106k
    pub fn instantiate<'a, I>(&mut self, module: u32, args: I) -> &mut Self
45
106k
    where
46
106k
        I: IntoIterator<Item = (&'a str, Export)>,
47
106k
        I::IntoIter: ExactSizeIterator,
48
106k
    {
49
106k
        let args = args.into_iter();
50
106k
51
106k
        self.bytes.push(0x00);
52
106k
        self.bytes.extend(encoders::u32(module));
53
106k
        self.bytes
54
106k
            .extend(encoders::u32(u32::try_from(args.len()).unwrap()));
55
175k
        for (name, export) in args {
56
68.5k
            self.bytes.extend(encoders::str(name));
57
68.5k
            export.encode(&mut self.bytes);
58
68.5k
        }
59
106k
        self.num_added += 1;
60
106k
        self
61
106k
    }
<wasm_encoder::instances::InstanceSection>::instantiate::<core::iter::adapters::map::Map<core::slice::iter::Iter<(alloc::string::String, wasm_smith::Export)>, <wasm_smith::ConfiguredModule<wasm_smith::config::SwarmConfig>>::encode_instances::{closure#0}>>
Line
Count
Source
44
106k
    pub fn instantiate<'a, I>(&mut self, module: u32, args: I) -> &mut Self
45
106k
    where
46
106k
        I: IntoIterator<Item = (&'a str, Export)>,
47
106k
        I::IntoIter: ExactSizeIterator,
48
106k
    {
49
106k
        let args = args.into_iter();
50
106k
51
106k
        self.bytes.push(0x00);
52
106k
        self.bytes.extend(encoders::u32(module));
53
106k
        self.bytes
54
106k
            .extend(encoders::u32(u32::try_from(args.len()).unwrap()));
55
175k
        for (name, export) in args {
56
68.5k
            self.bytes.extend(encoders::str(name));
57
68.5k
            export.encode(&mut self.bytes);
58
68.5k
        }
59
106k
        self.num_added += 1;
60
106k
        self
61
106k
    }
Unexecuted instantiation: <wasm_encoder::instances::InstanceSection>::instantiate::<core::iter::adapters::map::Map<core::slice::iter::Iter<(alloc::string::String, wasm_smith::Export)>, <wasm_smith::ConfiguredModule<wasm_smith::config::DefaultConfig>>::encode_instances::{closure#0}>>
Unexecuted instantiation: <wasm_encoder::instances::InstanceSection>::instantiate::<core::iter::adapters::map::Map<core::slice::iter::Iter<(alloc::string::String, wasm_smith::Export)>, <wasm_smith::ConfiguredModule<wasm_smith::config::SwarmConfig>>::encode_instances::{closure#0}>>
62
}
63
64
impl Section for InstanceSection {
65
2.01M
    fn id(&self) -> u8 {
66
2.01M
        SectionId::Instance.into()
67
2.01M
    }
68
69
2.01M
    fn encode<S>(&self, sink: &mut S)
70
2.01M
    where
71
2.01M
        S: Extend<u8>,
72
2.01M
    {
73
2.01M
        let num_added = encoders::u32(self.num_added);
74
2.01M
        let n = num_added.len();
75
2.01M
        sink.extend(
76
2.01M
            encoders::u32(u32::try_from(n + self.bytes.len()).unwrap())
77
2.01M
                .chain(num_added)
78
2.01M
                .chain(self.bytes.iter().copied()),
79
2.01M
        );
80
2.01M
    }
81
}