| 1 |
use crate::parser::{Parse, Parser, Result}; |
| 2 |
use crate::token::{self, Span}; |
| 3 |
use crate::{annotation, kw}; |
| 4 |
|
| 5 |
/// A custom section within a wasm module. |
| 6 |
#[derive(Debug)] |
| 7 |
pub enum Custom<'a> { |
| 8 |
/// A raw custom section with the manual placement and bytes specified. |
| 9 |
Raw(RawCustomSection<'a>), |
| 10 |
/// A producers custom section. |
| 11 |
Producers(Producers<'a>), |
| 12 |
/// The `dylink.0` custom section |
| 13 |
Dylink0(Dylink0<'a>), |
| 14 |
} |
| 15 |
|
| 16 |
impl Custom<'_> { |
| 17 |
/// Where this custom section is placed. |
| 18 |
pub fn place(&self) -> CustomPlace { |
| 19 |
match self { |
| 20 |
Custom::Raw(s) => s.place, |
| 21 |
Custom::Producers(_) => CustomPlace::AfterLast, |
| 22 |
Custom::Dylink0(_) => CustomPlace::BeforeFirst, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/// The name of this custom section |
| 27 |
pub fn name(&self) -> &str { |
| 28 |
match self { |
| 29 |
Custom::Raw(s) => s.name, |
| 30 |
Custom::Producers(_) => "producers", |
| 31 |
Custom::Dylink0(_) => "dylink.0", |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl<'a> Parse<'a> for Custom<'a> { |
| 37 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 38 |
if parser.peek::<annotation::producers>()? { |
| 39 |
Ok(Custom::Producers(parser.parse()?)) |
| 40 |
} else if parser.peek::<annotation::dylink_0>()? { |
| 41 |
Ok(Custom::Dylink0(parser.parse()?)) |
| 42 |
} else { |
| 43 |
Ok(Custom::Raw(parser.parse()?)) |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/// A wasm custom section within a module. |
| 49 |
#[derive(Debug)] |
| 50 |
pub struct RawCustomSection<'a> { |
| 51 |
/// Where this `@custom` was defined. |
| 52 |
pub span: Span, |
| 53 |
|
| 54 |
/// Name of the custom section. |
| 55 |
pub name: &'a str, |
| 56 |
|
| 57 |
/// Where the custom section is being placed, |
| 58 |
pub place: CustomPlace, |
| 59 |
|
| 60 |
/// Payload of this custom section. |
| 61 |
pub data: Vec<&'a [u8]>, |
| 62 |
} |
| 63 |
|
| 64 |
/// Possible locations to place a custom section within a module. |
| 65 |
#[derive(Debug, PartialEq, Copy, Clone)] |
| 66 |
pub enum CustomPlace { |
| 67 |
/// This custom section will appear before the first section in the module. |
| 68 |
BeforeFirst, |
| 69 |
/// This custom section will be placed just before a known section. |
| 70 |
Before(CustomPlaceAnchor), |
| 71 |
/// This custom section will be placed just after a known section. |
| 72 |
After(CustomPlaceAnchor), |
| 73 |
/// This custom section will appear after the last section in the module. |
| 74 |
AfterLast, |
| 75 |
} |
| 76 |
|
| 77 |
/// Known sections that custom sections can be placed relative to. |
| 78 |
#[derive(Debug, PartialEq, Eq, Copy, Clone)] |
| 79 |
#[allow(missing_docs)] |
| 80 |
pub enum CustomPlaceAnchor { |
| 81 |
Type, |
| 82 |
Import, |
| 83 |
Func, |
| 84 |
Table, |
| 85 |
Memory, |
| 86 |
Global, |
| 87 |
Export, |
| 88 |
Start, |
| 89 |
Elem, |
| 90 |
Code, |
| 91 |
Data, |
| 92 |
Tag, |
| 93 |
} |
| 94 |
|
| 95 |
impl<'a> Parse<'a> for RawCustomSection<'a> { |
| 96 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 97 |
let span = parser.parse::<annotation::custom>()?.0; |
| 98 |
let name = parser.parse()?; |
| 99 |
let place = if parser.peek::<token::LParen>()? { |
| 100 |
parser.parens(|p| p.parse())? |
| 101 |
} else { |
| 102 |
CustomPlace::AfterLast |
| 103 |
}; |
| 104 |
let mut data = Vec::new(); |
| 105 |
while !parser.is_empty() { |
| 106 |
data.push(parser.parse()?); |
| 107 |
} |
| 108 |
Ok(RawCustomSection { |
| 109 |
span, |
| 110 |
name, |
| 111 |
place, |
| 112 |
data, |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl<'a> Parse<'a> for CustomPlace { |
| 118 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 119 |
let mut l = parser.lookahead1(); |
| 120 |
let ctor = if l.peek::<kw::before>()? { |
| 121 |
parser.parse::<kw::before>()?; |
| 122 |
if l.peek::<kw::first>()? { |
| 123 |
parser.parse::<kw::first>()?; |
| 124 |
return Ok(CustomPlace::BeforeFirst); |
| 125 |
} |
| 126 |
CustomPlace::Before as fn(CustomPlaceAnchor) -> _ |
| 127 |
} else if l.peek::<kw::after>()? { |
| 128 |
parser.parse::<kw::after>()?; |
| 129 |
if l.peek::<kw::last>()? { |
| 130 |
parser.parse::<kw::last>()?; |
| 131 |
return Ok(CustomPlace::AfterLast); |
| 132 |
} |
| 133 |
CustomPlace::After |
| 134 |
} else { |
| 135 |
return Err(l.error()); |
| 136 |
}; |
| 137 |
Ok(ctor(parser.parse()?)) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
impl<'a> Parse<'a> for CustomPlaceAnchor { |
| 142 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 143 |
if parser.peek::<kw::r#type>()? { |
| 144 |
parser.parse::<kw::r#type>()?; |
| 145 |
return Ok(CustomPlaceAnchor::Type); |
| 146 |
} |
| 147 |
if parser.peek::<kw::import>()? { |
| 148 |
parser.parse::<kw::import>()?; |
| 149 |
return Ok(CustomPlaceAnchor::Import); |
| 150 |
} |
| 151 |
if parser.peek::<kw::func>()? { |
| 152 |
parser.parse::<kw::func>()?; |
| 153 |
return Ok(CustomPlaceAnchor::Func); |
| 154 |
} |
| 155 |
if parser.peek::<kw::table>()? { |
| 156 |
parser.parse::<kw::table>()?; |
| 157 |
return Ok(CustomPlaceAnchor::Table); |
| 158 |
} |
| 159 |
if parser.peek::<kw::memory>()? { |
| 160 |
parser.parse::<kw::memory>()?; |
| 161 |
return Ok(CustomPlaceAnchor::Memory); |
| 162 |
} |
| 163 |
if parser.peek::<kw::global>()? { |
| 164 |
parser.parse::<kw::global>()?; |
| 165 |
return Ok(CustomPlaceAnchor::Global); |
| 166 |
} |
| 167 |
if parser.peek::<kw::export>()? { |
| 168 |
parser.parse::<kw::export>()?; |
| 169 |
return Ok(CustomPlaceAnchor::Export); |
| 170 |
} |
| 171 |
if parser.peek::<kw::start>()? { |
| 172 |
parser.parse::<kw::start>()?; |
| 173 |
return Ok(CustomPlaceAnchor::Start); |
| 174 |
} |
| 175 |
if parser.peek::<kw::elem>()? { |
| 176 |
parser.parse::<kw::elem>()?; |
| 177 |
return Ok(CustomPlaceAnchor::Elem); |
| 178 |
} |
| 179 |
if parser.peek::<kw::code>()? { |
| 180 |
parser.parse::<kw::code>()?; |
| 181 |
return Ok(CustomPlaceAnchor::Code); |
| 182 |
} |
| 183 |
if parser.peek::<kw::data>()? { |
| 184 |
parser.parse::<kw::data>()?; |
| 185 |
return Ok(CustomPlaceAnchor::Data); |
| 186 |
} |
| 187 |
if parser.peek::<kw::tag>()? { |
| 188 |
parser.parse::<kw::tag>()?; |
| 189 |
return Ok(CustomPlaceAnchor::Tag); |
| 190 |
} |
| 191 |
|
| 192 |
Err(parser.error("expected a valid section name")) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/// A producers custom section |
| 197 |
#[allow(missing_docs)] |
| 198 |
#[derive(Debug)] |
| 199 |
pub struct Producers<'a> { |
| 200 |
pub fields: Vec<(&'a str, Vec<(&'a str, &'a str)>)>, |
| 201 |
} |
| 202 |
|
| 203 |
impl<'a> Parse<'a> for Producers<'a> { |
| 204 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 205 |
parser.parse::<annotation::producers>()?.0; |
| 206 |
let mut languages = Vec::new(); |
| 207 |
let mut sdks = Vec::new(); |
| 208 |
let mut processed_by = Vec::new(); |
| 209 |
while !parser.is_empty() { |
| 210 |
parser.parens(|parser| { |
| 211 |
let mut l = parser.lookahead1(); |
| 212 |
let dst = if l.peek::<kw::language>()? { |
| 213 |
parser.parse::<kw::language>()?; |
| 214 |
&mut languages |
| 215 |
} else if l.peek::<kw::sdk>()? { |
| 216 |
parser.parse::<kw::sdk>()?; |
| 217 |
&mut sdks |
| 218 |
} else if l.peek::<kw::processed_by>()? { |
| 219 |
parser.parse::<kw::processed_by>()?; |
| 220 |
&mut processed_by |
| 221 |
} else { |
| 222 |
return Err(l.error()); |
| 223 |
}; |
| 224 |
|
| 225 |
dst.push((parser.parse()?, parser.parse()?)); |
| 226 |
Ok(()) |
| 227 |
})?; |
| 228 |
} |
| 229 |
|
| 230 |
let mut fields = Vec::new(); |
| 231 |
if !languages.is_empty() { |
| 232 |
fields.push(("language", languages)); |
| 233 |
} |
| 234 |
if !sdks.is_empty() { |
| 235 |
fields.push(("sdk", sdks)); |
| 236 |
} |
| 237 |
if !processed_by.is_empty() { |
| 238 |
fields.push(("processed-by", processed_by)); |
| 239 |
} |
| 240 |
Ok(Producers { fields }) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/// A `dylink.0` custom section |
| 245 |
#[allow(missing_docs)] |
| 246 |
#[derive(Debug)] |
| 247 |
pub struct Dylink0<'a> { |
| 248 |
pub subsections: Vec<Dylink0Subsection<'a>>, |
| 249 |
} |
| 250 |
|
| 251 |
/// Possible subsections of the `dylink.0` custom section |
| 252 |
#[derive(Debug)] |
| 253 |
#[allow(missing_docs)] |
| 254 |
pub enum Dylink0Subsection<'a> { |
| 255 |
MemInfo { |
| 256 |
memory_size: u32, |
| 257 |
memory_align: u32, |
| 258 |
table_size: u32, |
| 259 |
table_align: u32, |
| 260 |
}, |
| 261 |
Needed(Vec<&'a str>), |
| 262 |
ExportInfo(Vec<(&'a str, u32)>), |
| 263 |
ImportInfo(Vec<(&'a str, &'a str, u32)>), |
| 264 |
RuntimePath(Vec<&'a str>), |
| 265 |
} |
| 266 |
|
| 267 |
impl<'a> Parse<'a> for Dylink0<'a> { |
| 268 |
fn parse(parser: Parser<'a>) -> Result<Self> { |
| 269 |
parser.parse::<annotation::dylink_0>()?.0; |
| 270 |
let mut ret = Dylink0 { |
| 271 |
subsections: Vec::new(), |
| 272 |
}; |
| 273 |
while !parser.is_empty() { |
| 274 |
parser.parens(|p| ret.parse_next(p))?; |
| 275 |
} |
| 276 |
Ok(ret) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
impl<'a> Dylink0<'a> { |
| 281 |
fn parse_next(&mut self, parser: Parser<'a>) -> Result<()> { |
| 282 |
let mut l = parser.lookahead1(); |
| 283 |
if l.peek::<kw::mem_info>()? { |
| 284 |
parser.parse::<kw::mem_info>()?; |
| 285 |
let mut memory_size = 0; |
| 286 |
let mut memory_align = 0; |
| 287 |
let mut table_size = 0; |
| 288 |
let mut table_align = 0; |
| 289 |
if parser.peek2::<kw::memory>()? { |
| 290 |
parser.parens(|p| { |
| 291 |
p.parse::<kw::memory>()?; |
| 292 |
memory_size = p.parse()?; |
| 293 |
memory_align = p.parse()?; |
| 294 |
Ok(()) |
| 295 |
})?; |
| 296 |
} |
| 297 |
if parser.peek2::<kw::table>()? { |
| 298 |
parser.parens(|p| { |
| 299 |
p.parse::<kw::table>()?; |
| 300 |
table_size = p.parse()?; |
| 301 |
table_align = p.parse()?; |
| 302 |
Ok(()) |
| 303 |
})?; |
| 304 |
} |
| 305 |
self.subsections.push(Dylink0Subsection::MemInfo { |
| 306 |
memory_size, |
| 307 |
memory_align, |
| 308 |
table_size, |
| 309 |
table_align, |
| 310 |
}); |
| 311 |
} else if l.peek::<kw::needed>()? { |
| 312 |
parser.parse::<kw::needed>()?; |
| 313 |
let mut names = Vec::new(); |
| 314 |
while !parser.is_empty() { |
| 315 |
names.push(parser.parse()?); |
| 316 |
} |
| 317 |
self.subsections.push(Dylink0Subsection::Needed(names)); |
| 318 |
} else if l.peek::<kw::export_info>()? { |
| 319 |
parser.parse::<kw::export_info>()?; |
| 320 |
let name = parser.parse()?; |
| 321 |
let flags = parse_sym_flags(parser)?; |
| 322 |
match self.subsections.last_mut() { |
| 323 |
Some(Dylink0Subsection::ExportInfo(list)) => list.push((name, flags)), |
| 324 |
_ => self |
| 325 |
.subsections |
| 326 |
.push(Dylink0Subsection::ExportInfo(vec![(name, flags)])), |
| 327 |
} |
| 328 |
} else if l.peek::<kw::import_info>()? { |
| 329 |
parser.parse::<kw::import_info>()?; |
| 330 |
let module = parser.parse()?; |
| 331 |
let name = parser.parse()?; |
| 332 |
let flags = parse_sym_flags(parser)?; |
| 333 |
match self.subsections.last_mut() { |
| 334 |
Some(Dylink0Subsection::ImportInfo(list)) => list.push((module, name, flags)), |
| 335 |
_ => self |
| 336 |
.subsections |
| 337 |
.push(Dylink0Subsection::ImportInfo(vec![(module, name, flags)])), |
| 338 |
} |
| 339 |
} else if l.peek::<kw::runtime_path>()? { |
| 340 |
parser.parse::<kw::runtime_path>()?; |
| 341 |
let mut names = Vec::new(); |
| 342 |
while !parser.is_empty() { |
| 343 |
names.push(parser.parse()?); |
| 344 |
} |
| 345 |
self.subsections.push(Dylink0Subsection::RuntimePath(names)); |
| 346 |
} else { |
| 347 |
return Err(l.error()); |
| 348 |
} |
| 349 |
Ok(()) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
fn parse_sym_flags(parser: Parser<'_>) -> Result<u32> { |
| 354 |
let mut flags = 0; |
| 355 |
while !parser.is_empty() { |
| 356 |
let mut l = parser.lookahead1(); |
| 357 |
if l.peek::<u32>()? { |
| 358 |
flags |= parser.parse::<u32>()?; |
| 359 |
continue; |
| 360 |
} |
| 361 |
macro_rules! parse_flags { |
| 362 |
($($kw:tt = $val:expr,)*) => {$({ |
| 363 |
custom_keyword!(flag = $kw); |
| 364 |
if l.peek::<flag>()? { |
| 365 |
parser.parse::<flag>()?; |
| 366 |
flags |= $val; |
| 367 |
continue; |
| 368 |
} |
| 369 |
})*}; |
| 370 |
} |
| 371 |
// N.B.: Keep in sync with `print_dylink0_flags` in `crates/wasmprinter/src/lib.rs`. |
| 372 |
parse_flags! { |
| 373 |
"binding-weak" = 1 << 0, |
| 374 |
"binding-local" = 1 << 1, |
| 375 |
"visibility-hidden" = 1 << 2, |
| 376 |
"undefined" = 1 << 4, |
| 377 |
"exported" = 1 << 5, |
| 378 |
"explicit-name" = 1 << 6, |
| 379 |
"no-strip" = 1 << 7, |
| 380 |
"tls" = 1 << 8, |
| 381 |
"absolute" = 1 << 9, |
| 382 |
} |
| 383 |
return Err(l.error()); |
| 384 |
} |
| 385 |
Ok(flags) |
| 386 |
} |
| 387 |
|
| 388 |
mod flag {} |
| 389 |
|
| 390 |
impl Dylink0Subsection<'_> { |
| 391 |
/// Returns the byte id of this subsection used to identify it. |
| 392 |
pub fn id(&self) -> u8 { |
| 393 |
use Dylink0Subsection::*; |
| 394 |
match self { |
| 395 |
MemInfo { .. } => 1, |
| 396 |
Needed(..) => 2, |
| 397 |
ExportInfo(..) => 3, |
| 398 |
ImportInfo(..) => 4, |
| 399 |
RuntimePath(..) => 5, |
| 400 |
} |
| 401 |
} |
| 402 |
} |