Debian Codegraph @

third_party/rust/wast/tests/annotations.rs

raw
1 use wasmparser::*;
2
3 #[test]
4 fn name_annotations() -> anyhow::Result<()> {
5 assert_module_name("foo", r#"(module $foo)"#)?;
6 assert_module_name("foo", r#"(module (@name "foo"))"#)?;
7 assert_module_name("foo", r#"(module $bar (@name "foo"))"#)?;
8 assert_module_name("foo bar", r#"(module $bar (@name "foo bar"))"#)?;
9 Ok(())
10 }
11
12 fn assert_module_name(expected_name: &str, wat: &str) -> anyhow::Result<()> {
13 let wasm = wat::parse_str(wat)?;
14 let mut found = false;
15 for s in get_name_section(&wasm)? {
16 match s? {
17 Name::Module { name, .. } => {
18 assert_eq!(name, expected_name);
19 found = true;
20 }
21 _ => {}
22 }
23 }
24 assert!(found);
25 Ok(())
26 }
27
28 #[test]
29 fn func_annotations() -> anyhow::Result<()> {
30 assert_func_name("foo", r#"(module (func $foo))"#)?;
31 assert_func_name("foo", r#"(module (func (@name "foo")))"#)?;
32 assert_func_name("foo", r#"(module (func $bar (@name "foo")))"#)?;
33 assert_func_name("foo bar", r#"(module (func $bar (@name "foo bar")))"#)?;
34 Ok(())
35 }
36
37 fn assert_func_name(name: &str, wat: &str) -> anyhow::Result<()> {
38 let wasm = wat::parse_str(wat)?;
39 let mut found = false;
40 for s in get_name_section(&wasm)? {
41 match s? {
42 Name::Function(n) => {
43 let naming = n.into_iter().next().unwrap()?;
44 assert_eq!(naming.index, 0);
45 assert_eq!(naming.name, name);
46 found = true;
47 }
48 _ => {}
49 }
50 }
51 assert!(found);
52 Ok(())
53 }
54
55 #[test]
56 fn local_annotations() -> anyhow::Result<()> {
57 assert_local_name("foo", r#"(module (func (param $foo i32)))"#)?;
58 assert_local_name("foo", r#"(module (func (local $foo i32)))"#)?;
59 assert_local_name("foo", r#"(module (func (param (@name "foo") i32)))"#)?;
60 assert_local_name("foo", r#"(module (func (local (@name "foo") i32)))"#)?;
61 assert_local_name("foo", r#"(module (func (param $bar (@name "foo") i32)))"#)?;
62 assert_local_name("foo", r#"(module (func (local $bar (@name "foo") i32)))"#)?;
63 assert_local_name(
64 "foo bar",
65 r#"(module (func (param $bar (@name "foo bar") i32)))"#,
66 )?;
67 assert_local_name(
68 "foo bar",
69 r#"(module (func (local $bar (@name "foo bar") i32)))"#,
70 )?;
71 Ok(())
72 }
73
74 fn assert_local_name(name: &str, wat: &str) -> anyhow::Result<()> {
75 let wasm = wat::parse_str(wat)?;
76 let mut found = false;
77 for s in get_name_section(&wasm)? {
78 match s? {
79 Name::Local(n) => {
80 let naming = n
81 .into_iter()
82 .next()
83 .unwrap()?
84 .names
85 .into_iter()
86 .next()
87 .unwrap()?;
88 assert_eq!(naming.index, 0);
89 assert_eq!(naming.name, name);
90 found = true;
91 }
92 _ => {}
93 }
94 }
95 assert!(found);
96 Ok(())
97 }
98
99 fn get_name_section(wasm: &[u8]) -> anyhow::Result<NameSectionReader<'_>> {
100 for payload in Parser::new(0).parse_all(&wasm) {
101 if let Payload::CustomSection(c) = payload? {
102 if let KnownCustom::Name(s) = c.as_known() {
103 return Ok(s);
104 }
105 }
106 }
107 panic!("no name section found");
108 }
109
110 #[test]
111 fn custom_section_order() -> anyhow::Result<()> {
112 let bytes = wat::parse_str(
113 r#"
114 (module
115 (@custom "A" "aaa")
116 (type (func))
117 (@custom "B" (after func) "bbb")
118 (@custom "C" (before func) "ccc")
119 (@custom "D" (after last) "ddd")
120 (table 10 funcref)
121 (func (type 0))
122 (@custom "E" (after import) "eee")
123 (@custom "F" (before type) "fff")
124 (@custom "G" (after data) "ggg")
125 (@custom "H" (after code) "hhh")
126 (@custom "I" (after func) "iii")
127 (@custom "J" (before func) "jjj")
128 (@custom "K" (before first) "kkk")
129 )
130 "#,
131 )?;
132 macro_rules! assert_matches {
133 ($a:expr, $b:pat $(if $cond:expr)? $(,)?) => {
134 match &$a {
135 $b $(if $cond)? => {}
136 a => panic!("`{:?}` doesn't match `{}`", a, stringify!($b)),
137 }
138 };
139 }
140 let wasm = Parser::new(0)
141 .parse_all(&bytes)
142 .collect::<Result<Vec<_>>>()?;
143 assert_matches!(wasm[0], Payload::Version { .. });
144 assert_matches!(
145 wasm[1],
146 Payload::CustomSection(c) if c.name() == "K"
147 );
148 assert_matches!(
149 wasm[2],
150 Payload::CustomSection(c) if c.name() == "F"
151 );
152 assert_matches!(wasm[3], Payload::TypeSection(_));
153 assert_matches!(
154 wasm[4],
155 Payload::CustomSection(c) if c.name() == "E"
156 );
157 assert_matches!(
158 wasm[5],
159 Payload::CustomSection(c) if c.name() == "C"
160 );
161 assert_matches!(
162 wasm[6],
163 Payload::CustomSection(c) if c.name() == "J"
164 );
165 assert_matches!(wasm[7], Payload::FunctionSection(_));
166 assert_matches!(
167 wasm[8],
168 Payload::CustomSection(c) if c.name() == "B"
169 );
170 assert_matches!(
171 wasm[9],
172 Payload::CustomSection(c) if c.name() == "I"
173 );
174 assert_matches!(wasm[10], Payload::TableSection(_));
175 assert_matches!(wasm[11], Payload::CodeSectionStart { .. });
176 assert_matches!(wasm[12], Payload::CodeSectionEntry { .. });
177 assert_matches!(
178 wasm[13],
179 Payload::CustomSection(c) if c.name() == "H"
180 );
181 assert_matches!(
182 wasm[14],
183 Payload::CustomSection(c) if c.name() == "G"
184 );
185 assert_matches!(
186 wasm[15],
187 Payload::CustomSection(c) if c.name() == "A"
188 );
189 assert_matches!(
190 wasm[16],
191 Payload::CustomSection(c) if c.name() == "D"
192 );
193
194 match &wasm[17] {
195 Payload::End(x) if *x == bytes.len() => {}
196 p => panic!("`{:?}` doesn't match expected length of {}", p, bytes.len()),
197 }
198
199 Ok(())
200 }