wasmparser_nostd/readers/core/
producers.rs

1/* Copyright 2019 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16use crate::{BinaryReader, FromReader, Result, SectionLimited};
17
18/// A reader for the producers custom section of a WebAssembly module.
19///
20/// # Examples
21///
22/// ```
23/// # let data: &[u8] = &[0x01, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,
24/// #     0x02, 0x03, 0x77, 0x61, 0x74, 0x01, 0x31, 0x01, 0x43, 0x03, 0x39, 0x2e, 0x30];
25/// use wasmparser_nostd::{ProducersSectionReader, ProducersFieldValue, Result};
26/// let reader = ProducersSectionReader::new(data, 0).expect("producers reader");
27/// let field = reader.into_iter().next().unwrap().expect("producers field");
28/// assert!(field.name == "language");
29/// let value = field.values.into_iter().collect::<Result<Vec<_>>>().expect("values");
30/// assert!(value.len() == 2);
31/// assert!(value[0].name == "wat" && value[0].version == "1");
32/// assert!(value[1].name == "C" && value[1].version == "9.0");
33/// ```
34pub type ProducersSectionReader<'a> = SectionLimited<'a, ProducersField<'a>>;
35
36/// A field from the producers custom section.
37#[derive(Debug, Clone)]
38pub struct ProducersField<'a> {
39    /// The name of the field.
40    pub name: &'a str,
41    /// The values specified for this field
42    pub values: SectionLimited<'a, ProducersFieldValue<'a>>,
43}
44
45impl<'a> FromReader<'a> for ProducersField<'a> {
46    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
47        let name = reader.read_string()?;
48        let values = reader.skip(|reader| {
49            // FIXME(#188) ideally shouldn't need to skip here
50            for _ in 0..reader.read_var_u32()? {
51                reader.skip_string()?;
52                reader.skip_string()?;
53            }
54            Ok(())
55        })?;
56        Ok(ProducersField {
57            name,
58            values: SectionLimited::new(values.remaining_buffer(), values.original_position())?,
59        })
60    }
61}
62
63/// Represents a field value in the producers custom section.
64#[derive(Debug, Copy, Clone)]
65pub struct ProducersFieldValue<'a> {
66    /// The field name.
67    pub name: &'a str,
68    /// The field version.
69    pub version: &'a str,
70}
71
72impl<'a> FromReader<'a> for ProducersFieldValue<'a> {
73    fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> {
74        let name = reader.read_string()?;
75        let version = reader.read_string()?;
76        Ok(ProducersFieldValue { name, version })
77    }
78}