Coverage Report

Created: 2024-10-16 07:58

/rust/registry/src/index.crates.io-6f17d22bba15001f/wast-64.0.0/src/core/export.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::kw;
2
use crate::parser::{Cursor, Parse, Parser, Peek, Result};
3
use crate::token::{Index, Span};
4
5
/// A entry in a WebAssembly module's export section.
6
#[derive(Debug)]
7
pub struct Export<'a> {
8
    /// Where this export was defined.
9
    pub span: Span,
10
    /// The name of this export from the module.
11
    pub name: &'a str,
12
    /// The kind of item being exported.
13
    pub kind: ExportKind,
14
    /// What's being exported from the module.
15
    pub item: Index<'a>,
16
}
17
18
/// Different kinds of elements that can be exported from a WebAssembly module,
19
/// contained in an [`Export`].
20
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
21
#[allow(missing_docs)]
22
pub enum ExportKind {
23
    Func,
24
    Table,
25
    Memory,
26
    Global,
27
    Tag,
28
}
29
30
impl<'a> Parse<'a> for Export<'a> {
31
0
    fn parse(parser: Parser<'a>) -> Result<Self> {
32
0
        let span = parser.parse::<kw::export>()?.0;
33
0
        let name = parser.parse()?;
34
0
        let (kind, item) = parser.parens(|p| Ok((p.parse()?, p.parse()?)))?;
35
0
        Ok(Export {
36
0
            span,
37
0
            name,
38
0
            kind,
39
0
            item,
40
0
        })
41
0
    }
42
}
43
44
impl<'a> Parse<'a> for ExportKind {
45
0
    fn parse(parser: Parser<'a>) -> Result<Self> {
46
0
        let mut l = parser.lookahead1();
47
0
        if l.peek::<kw::func>()? {
48
0
            parser.parse::<kw::func>()?;
49
0
            Ok(ExportKind::Func)
50
0
        } else if l.peek::<kw::table>()? {
51
0
            parser.parse::<kw::table>()?;
52
0
            Ok(ExportKind::Table)
53
0
        } else if l.peek::<kw::memory>()? {
54
0
            parser.parse::<kw::memory>()?;
55
0
            Ok(ExportKind::Memory)
56
0
        } else if l.peek::<kw::global>()? {
57
0
            parser.parse::<kw::global>()?;
58
0
            Ok(ExportKind::Global)
59
0
        } else if l.peek::<kw::tag>()? {
60
0
            parser.parse::<kw::tag>()?;
61
0
            Ok(ExportKind::Tag)
62
        } else {
63
0
            Err(l.error())
64
        }
65
0
    }
66
}
67
68
impl Peek for ExportKind {
69
0
    fn peek(cursor: Cursor<'_>) -> Result<bool> {
70
0
        Ok(kw::func::peek(cursor)?
71
0
            || kw::table::peek(cursor)?
72
0
            || kw::memory::peek(cursor)?
73
0
            || kw::global::peek(cursor)?
74
0
            || kw::tag::peek(cursor)?)
75
0
    }
76
0
    fn display() -> &'static str {
77
0
        "export kind"
78
0
    }
79
}
80
81
macro_rules! kw_conversions {
82
    ($($kw:ident => $kind:ident)*) => ($(
83
        impl From<kw::$kw> for ExportKind {
84
0
            fn from(_: kw::$kw) -> ExportKind {
85
0
                ExportKind::$kind
86
0
            }
Unexecuted instantiation: <wast::core::export::ExportKind as core::convert::From<wast::kw::func>>::from
Unexecuted instantiation: <wast::core::export::ExportKind as core::convert::From<wast::kw::table>>::from
Unexecuted instantiation: <wast::core::export::ExportKind as core::convert::From<wast::kw::global>>::from
Unexecuted instantiation: <wast::core::export::ExportKind as core::convert::From<wast::kw::tag>>::from
Unexecuted instantiation: <wast::core::export::ExportKind as core::convert::From<wast::kw::memory>>::from
87
        }
88
89
        impl Default for kw::$kw {
90
0
            fn default() -> kw::$kw {
91
0
                kw::$kw(Span::from_offset(0))
92
0
            }
Unexecuted instantiation: <wast::kw::func as core::default::Default>::default
Unexecuted instantiation: <wast::kw::table as core::default::Default>::default
Unexecuted instantiation: <wast::kw::global as core::default::Default>::default
Unexecuted instantiation: <wast::kw::tag as core::default::Default>::default
Unexecuted instantiation: <wast::kw::memory as core::default::Default>::default
93
        }
94
    )*);
95
}
96
97
kw_conversions! {
98
    func => Func
99
    table => Table
100
    global => Global
101
    tag => Tag
102
    memory => Memory
103
}
104
105
/// A listing of inline `(export "foo")` statements on a WebAssembly item in
106
/// its textual format.
107
#[derive(Debug, Default)]
108
pub struct InlineExport<'a> {
109
    /// The extra names to export an item as, if any.
110
    pub names: Vec<&'a str>,
111
}
112
113
impl<'a> Parse<'a> for InlineExport<'a> {
114
0
    fn parse(parser: Parser<'a>) -> Result<Self> {
115
0
        let mut names = Vec::new();
116
0
        while parser.peek::<Self>()? {
117
0
            names.push(parser.parens(|p| {
118
0
                p.parse::<kw::export>()?;
119
0
                p.parse::<&str>()
120
0
            })?);
121
        }
122
0
        Ok(InlineExport { names })
123
0
    }
124
}
125
126
impl Peek for InlineExport<'_> {
127
0
    fn peek(cursor: Cursor<'_>) -> Result<bool> {
128
0
        let cursor = match cursor.lparen()? {
129
0
            Some(cursor) => cursor,
130
0
            None => return Ok(false),
131
        };
132
0
        let cursor = match cursor.keyword()? {
133
0
            Some(("export", cursor)) => cursor,
134
0
            _ => return Ok(false),
135
        };
136
0
        let cursor = match cursor.string()? {
137
0
            Some((_, cursor)) => cursor,
138
0
            None => return Ok(false),
139
        };
140
0
        Ok(cursor.rparen()?.is_some())
141
0
    }
142
143
0
    fn display() -> &'static str {
144
0
        "inline export"
145
0
    }
146
}