| 1 |
use crate::reencode::{Error, Reencode, RoundtripReencoder}; |
| 2 |
|
| 3 |
#[allow(missing_docs)] // FIXME |
| 4 |
pub trait ReencodeComponent: Reencode { |
| 5 |
fn component_type_index(&mut self, ty: u32) -> u32 { |
| 6 |
ty |
| 7 |
} |
| 8 |
|
| 9 |
fn component_instance_index(&mut self, ty: u32) -> u32 { |
| 10 |
ty |
| 11 |
} |
| 12 |
|
| 13 |
fn component_func_index(&mut self, ty: u32) -> u32 { |
| 14 |
ty |
| 15 |
} |
| 16 |
|
| 17 |
fn component_index(&mut self, ty: u32) -> u32 { |
| 18 |
ty |
| 19 |
} |
| 20 |
|
| 21 |
fn module_index(&mut self, ty: u32) -> u32 { |
| 22 |
ty |
| 23 |
} |
| 24 |
|
| 25 |
fn instance_index(&mut self, ty: u32) -> u32 { |
| 26 |
ty |
| 27 |
} |
| 28 |
|
| 29 |
fn component_value_index(&mut self, ty: u32) -> u32 { |
| 30 |
ty |
| 31 |
} |
| 32 |
|
| 33 |
fn outer_type_index(&mut self, count: u32, ty: u32) -> u32 { |
| 34 |
let _ = count; |
| 35 |
self.type_index(ty) |
| 36 |
} |
| 37 |
|
| 38 |
fn outer_component_type_index(&mut self, count: u32, ty: u32) -> u32 { |
| 39 |
let _ = count; |
| 40 |
self.component_type_index(ty) |
| 41 |
} |
| 42 |
|
| 43 |
fn outer_component_index(&mut self, count: u32, component: u32) -> u32 { |
| 44 |
let _ = count; |
| 45 |
self.component_index(component) |
| 46 |
} |
| 47 |
|
| 48 |
fn outer_module_index(&mut self, count: u32, module: u32) -> u32 { |
| 49 |
let _ = count; |
| 50 |
self.module_index(module) |
| 51 |
} |
| 52 |
|
| 53 |
fn push_depth(&mut self) {} |
| 54 |
|
| 55 |
fn pop_depth(&mut self) {} |
| 56 |
|
| 57 |
fn component_external_index( |
| 58 |
&mut self, |
| 59 |
kind: wasmparser::ComponentExternalKind, |
| 60 |
index: u32, |
| 61 |
) -> u32 { |
| 62 |
match kind { |
| 63 |
wasmparser::ComponentExternalKind::Func => self.component_func_index(index), |
| 64 |
wasmparser::ComponentExternalKind::Module => self.module_index(index), |
| 65 |
wasmparser::ComponentExternalKind::Component => self.component_index(index), |
| 66 |
wasmparser::ComponentExternalKind::Type => self.component_type_index(index), |
| 67 |
wasmparser::ComponentExternalKind::Instance => self.component_instance_index(index), |
| 68 |
wasmparser::ComponentExternalKind::Value => self.component_value_index(index), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn parse_component( |
| 73 |
&mut self, |
| 74 |
component: &mut crate::Component, |
| 75 |
parser: wasmparser::Parser, |
| 76 |
data: &[u8], |
| 77 |
) -> Result<(), Error<Self::Error>> { |
| 78 |
component_utils::parse_component(self, component, parser, data, data) |
| 79 |
} |
| 80 |
|
| 81 |
fn parse_component_payload( |
| 82 |
&mut self, |
| 83 |
component: &mut crate::Component, |
| 84 |
payload: wasmparser::Payload<'_>, |
| 85 |
whole_component: &[u8], |
| 86 |
) -> Result<(), Error<Self::Error>> { |
| 87 |
component_utils::parse_component_payload(self, component, payload, whole_component) |
| 88 |
} |
| 89 |
|
| 90 |
fn parse_component_submodule( |
| 91 |
&mut self, |
| 92 |
component: &mut crate::Component, |
| 93 |
parser: wasmparser::Parser, |
| 94 |
module: &[u8], |
| 95 |
) -> Result<(), Error<Self::Error>> { |
| 96 |
component_utils::parse_component_submodule(self, component, parser, module) |
| 97 |
} |
| 98 |
|
| 99 |
fn parse_component_subcomponent( |
| 100 |
&mut self, |
| 101 |
component: &mut crate::Component, |
| 102 |
parser: wasmparser::Parser, |
| 103 |
subcomponent: &[u8], |
| 104 |
whole_component: &[u8], |
| 105 |
) -> Result<(), Error<Self::Error>> { |
| 106 |
component_utils::parse_component_subcomponent( |
| 107 |
self, |
| 108 |
component, |
| 109 |
parser, |
| 110 |
subcomponent, |
| 111 |
whole_component, |
| 112 |
) |
| 113 |
} |
| 114 |
|
| 115 |
fn parse_unknown_component_section( |
| 116 |
&mut self, |
| 117 |
component: &mut crate::Component, |
| 118 |
id: u8, |
| 119 |
contents: &[u8], |
| 120 |
) -> Result<(), Error<Self::Error>> { |
| 121 |
component_utils::parse_unknown_component_section(self, component, id, contents) |
| 122 |
} |
| 123 |
|
| 124 |
fn parse_component_custom_section( |
| 125 |
&mut self, |
| 126 |
component: &mut crate::Component, |
| 127 |
section: wasmparser::CustomSectionReader<'_>, |
| 128 |
) -> Result<(), Error<Self::Error>> { |
| 129 |
component_utils::parse_component_custom_section(self, component, section) |
| 130 |
} |
| 131 |
|
| 132 |
fn parse_component_type_section( |
| 133 |
&mut self, |
| 134 |
types: &mut crate::ComponentTypeSection, |
| 135 |
section: wasmparser::ComponentTypeSectionReader<'_>, |
| 136 |
) -> Result<(), Error<Self::Error>> { |
| 137 |
component_utils::parse_component_type_section(self, types, section) |
| 138 |
} |
| 139 |
|
| 140 |
fn parse_component_type( |
| 141 |
&mut self, |
| 142 |
dst: crate::ComponentTypeEncoder<'_>, |
| 143 |
ty: wasmparser::ComponentType<'_>, |
| 144 |
) -> Result<(), Error<Self::Error>> { |
| 145 |
component_utils::parse_component_type(self, dst, ty) |
| 146 |
} |
| 147 |
|
| 148 |
fn component_instance_type( |
| 149 |
&mut self, |
| 150 |
ty: Box<[wasmparser::InstanceTypeDeclaration<'_>]>, |
| 151 |
) -> Result<crate::InstanceType, Error<Self::Error>> { |
| 152 |
component_utils::component_instance_type(self, ty) |
| 153 |
} |
| 154 |
|
| 155 |
fn parse_component_instance_type_declaration( |
| 156 |
&mut self, |
| 157 |
ty: &mut crate::InstanceType, |
| 158 |
decl: wasmparser::InstanceTypeDeclaration<'_>, |
| 159 |
) -> Result<(), Error<Self::Error>> { |
| 160 |
component_utils::parse_component_instance_type_declaration(self, ty, decl) |
| 161 |
} |
| 162 |
|
| 163 |
fn parse_component_core_type( |
| 164 |
&mut self, |
| 165 |
ty: crate::ComponentCoreTypeEncoder<'_>, |
| 166 |
core: wasmparser::CoreType<'_>, |
| 167 |
) -> Result<(), Error<Self::Error>> { |
| 168 |
component_utils::parse_component_core_type(self, ty, core) |
| 169 |
} |
| 170 |
|
| 171 |
fn component_type( |
| 172 |
&mut self, |
| 173 |
ty: Box<[wasmparser::ComponentTypeDeclaration<'_>]>, |
| 174 |
) -> Result<crate::ComponentType, Error<Self::Error>> { |
| 175 |
component_utils::component_type(self, ty) |
| 176 |
} |
| 177 |
|
| 178 |
fn parse_component_type_declaration( |
| 179 |
&mut self, |
| 180 |
component: &mut crate::ComponentType, |
| 181 |
decl: wasmparser::ComponentTypeDeclaration<'_>, |
| 182 |
) -> Result<(), Error<Self::Error>> { |
| 183 |
component_utils::parse_component_type_declaration(self, component, decl) |
| 184 |
} |
| 185 |
|
| 186 |
fn parse_component_func_type( |
| 187 |
&mut self, |
| 188 |
func: crate::ComponentFuncTypeEncoder<'_>, |
| 189 |
ty: wasmparser::ComponentFuncType<'_>, |
| 190 |
) -> Result<(), Error<Self::Error>> { |
| 191 |
component_utils::parse_component_func_type(self, func, ty) |
| 192 |
} |
| 193 |
|
| 194 |
fn parse_component_defined_type( |
| 195 |
&mut self, |
| 196 |
defined: crate::ComponentDefinedTypeEncoder<'_>, |
| 197 |
ty: wasmparser::ComponentDefinedType<'_>, |
| 198 |
) -> Result<(), Error<Self::Error>> { |
| 199 |
component_utils::parse_component_defined_type(self, defined, ty) |
| 200 |
} |
| 201 |
|
| 202 |
fn component_module_type( |
| 203 |
&mut self, |
| 204 |
ty: Box<[wasmparser::ModuleTypeDeclaration<'_>]>, |
| 205 |
) -> Result<crate::ModuleType, Error<Self::Error>> { |
| 206 |
component_utils::component_module_type(self, ty) |
| 207 |
} |
| 208 |
|
| 209 |
fn parse_component_module_type_declaration( |
| 210 |
&mut self, |
| 211 |
module: &mut crate::ModuleType, |
| 212 |
decl: wasmparser::ModuleTypeDeclaration<'_>, |
| 213 |
) -> Result<(), Error<Self::Error>> { |
| 214 |
component_utils::parse_component_module_type_declaration(self, module, decl) |
| 215 |
} |
| 216 |
|
| 217 |
fn component_alias<'a>( |
| 218 |
&mut self, |
| 219 |
alias: wasmparser::ComponentAlias<'a>, |
| 220 |
) -> Result<crate::Alias<'a>, Error<Self::Error>> { |
| 221 |
component_utils::component_alias(self, alias) |
| 222 |
} |
| 223 |
|
| 224 |
fn parse_component_import_section( |
| 225 |
&mut self, |
| 226 |
imports: &mut crate::ComponentImportSection, |
| 227 |
section: wasmparser::ComponentImportSectionReader<'_>, |
| 228 |
) -> Result<(), Error<Self::Error>> { |
| 229 |
component_utils::parse_component_import_section(self, imports, section) |
| 230 |
} |
| 231 |
|
| 232 |
fn parse_component_canonical_section( |
| 233 |
&mut self, |
| 234 |
canonical: &mut crate::CanonicalFunctionSection, |
| 235 |
section: wasmparser::ComponentCanonicalSectionReader<'_>, |
| 236 |
) -> Result<(), Error<Self::Error>> { |
| 237 |
component_utils::parse_component_canonical_section(self, canonical, section) |
| 238 |
} |
| 239 |
|
| 240 |
fn parse_component_canonical( |
| 241 |
&mut self, |
| 242 |
section: &mut crate::CanonicalFunctionSection, |
| 243 |
func: wasmparser::CanonicalFunction, |
| 244 |
) -> Result<(), Error<Self::Error>> { |
| 245 |
component_utils::parse_component_canonical(self, section, func) |
| 246 |
} |
| 247 |
|
| 248 |
fn parse_component_alias_section( |
| 249 |
&mut self, |
| 250 |
aliases: &mut crate::ComponentAliasSection, |
| 251 |
section: wasmparser::ComponentAliasSectionReader<'_>, |
| 252 |
) -> Result<(), Error<Self::Error>> { |
| 253 |
component_utils::parse_component_alias_section(self, aliases, section) |
| 254 |
} |
| 255 |
|
| 256 |
fn parse_component_instance_section( |
| 257 |
&mut self, |
| 258 |
instances: &mut crate::ComponentInstanceSection, |
| 259 |
section: wasmparser::ComponentInstanceSectionReader<'_>, |
| 260 |
) -> Result<(), Error<Self::Error>> { |
| 261 |
component_utils::parse_component_instance_section(self, instances, section) |
| 262 |
} |
| 263 |
|
| 264 |
fn parse_component_instance( |
| 265 |
&mut self, |
| 266 |
instances: &mut crate::ComponentInstanceSection, |
| 267 |
instance: wasmparser::ComponentInstance<'_>, |
| 268 |
) -> Result<(), Error<Self::Error>> { |
| 269 |
component_utils::parse_component_instance(self, instances, instance) |
| 270 |
} |
| 271 |
|
| 272 |
fn parse_instance_section( |
| 273 |
&mut self, |
| 274 |
instances: &mut crate::InstanceSection, |
| 275 |
section: wasmparser::InstanceSectionReader<'_>, |
| 276 |
) -> Result<(), Error<Self::Error>> { |
| 277 |
component_utils::parse_instance_section(self, instances, section) |
| 278 |
} |
| 279 |
|
| 280 |
fn parse_instance( |
| 281 |
&mut self, |
| 282 |
instances: &mut crate::InstanceSection, |
| 283 |
instance: wasmparser::Instance<'_>, |
| 284 |
) -> Result<(), Error<Self::Error>> { |
| 285 |
component_utils::parse_instance(self, instances, instance) |
| 286 |
} |
| 287 |
|
| 288 |
fn parse_core_type_section( |
| 289 |
&mut self, |
| 290 |
types: &mut crate::CoreTypeSection, |
| 291 |
section: wasmparser::CoreTypeSectionReader<'_>, |
| 292 |
) -> Result<(), Error<Self::Error>> { |
| 293 |
component_utils::parse_core_type_section(self, types, section) |
| 294 |
} |
| 295 |
|
| 296 |
fn parse_component_export_section( |
| 297 |
&mut self, |
| 298 |
exports: &mut crate::ComponentExportSection, |
| 299 |
section: wasmparser::ComponentExportSectionReader<'_>, |
| 300 |
) -> Result<(), Error<Self::Error>> { |
| 301 |
component_utils::parse_component_export_section(self, exports, section) |
| 302 |
} |
| 303 |
|
| 304 |
fn parse_component_export( |
| 305 |
&mut self, |
| 306 |
exports: &mut crate::ComponentExportSection, |
| 307 |
export: wasmparser::ComponentExport<'_>, |
| 308 |
) -> Result<(), Error<Self::Error>> { |
| 309 |
component_utils::parse_component_export(self, exports, export) |
| 310 |
} |
| 311 |
|
| 312 |
fn parse_component_start_section( |
| 313 |
&mut self, |
| 314 |
component: &mut crate::Component, |
| 315 |
func: wasmparser::ComponentStartFunction, |
| 316 |
) -> Result<(), Error<Self::Error>> { |
| 317 |
component_utils::parse_component_start_section(self, component, func) |
| 318 |
} |
| 319 |
|
| 320 |
fn component_type_ref( |
| 321 |
&mut self, |
| 322 |
ty: wasmparser::ComponentTypeRef, |
| 323 |
) -> crate::component::ComponentTypeRef { |
| 324 |
component_utils::component_type_ref(self, ty) |
| 325 |
} |
| 326 |
|
| 327 |
fn component_primitive_val_type( |
| 328 |
&mut self, |
| 329 |
ty: wasmparser::PrimitiveValType, |
| 330 |
) -> crate::component::PrimitiveValType { |
| 331 |
component_utils::component_primitive_val_type(self, ty) |
| 332 |
} |
| 333 |
|
| 334 |
fn component_export_kind( |
| 335 |
&mut self, |
| 336 |
ty: wasmparser::ComponentExternalKind, |
| 337 |
) -> crate::component::ComponentExportKind { |
| 338 |
component_utils::component_export_kind(self, ty) |
| 339 |
} |
| 340 |
|
| 341 |
fn component_outer_alias_kind( |
| 342 |
&mut self, |
| 343 |
kind: wasmparser::ComponentOuterAliasKind, |
| 344 |
) -> crate::component::ComponentOuterAliasKind { |
| 345 |
component_utils::component_outer_alias_kind(self, kind) |
| 346 |
} |
| 347 |
|
| 348 |
fn component_val_type( |
| 349 |
&mut self, |
| 350 |
ty: wasmparser::ComponentValType, |
| 351 |
) -> crate::component::ComponentValType { |
| 352 |
component_utils::component_val_type(self, ty) |
| 353 |
} |
| 354 |
|
| 355 |
fn type_bounds(&mut self, ty: wasmparser::TypeBounds) -> crate::component::TypeBounds { |
| 356 |
component_utils::type_bounds(self, ty) |
| 357 |
} |
| 358 |
|
| 359 |
fn canonical_option( |
| 360 |
&mut self, |
| 361 |
ty: wasmparser::CanonicalOption, |
| 362 |
) -> crate::component::CanonicalOption { |
| 363 |
component_utils::canonical_option(self, ty) |
| 364 |
} |
| 365 |
|
| 366 |
fn custom_component_name_section( |
| 367 |
&mut self, |
| 368 |
section: wasmparser::ComponentNameSectionReader<'_>, |
| 369 |
) -> Result<crate::ComponentNameSection, Error<Self::Error>> { |
| 370 |
component_utils::custom_component_name_section(self, section) |
| 371 |
} |
| 372 |
|
| 373 |
fn parse_custom_component_name_subsection( |
| 374 |
&mut self, |
| 375 |
names: &mut crate::ComponentNameSection, |
| 376 |
section: wasmparser::ComponentName<'_>, |
| 377 |
) -> Result<(), Error<Self::Error>> { |
| 378 |
component_utils::parse_custom_component_name_subsection(self, names, section) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
impl ReencodeComponent for RoundtripReencoder {} |
| 383 |
|
| 384 |
#[allow(missing_docs)] // FIXME |
| 385 |
pub mod component_utils { |
| 386 |
use super::super::utils::name_map; |
| 387 |
use super::ReencodeComponent; |
| 388 |
use crate::reencode::Error; |
| 389 |
|
| 390 |
pub fn parse_component<T: ?Sized + ReencodeComponent>( |
| 391 |
reencoder: &mut T, |
| 392 |
component: &mut crate::Component, |
| 393 |
mut parser: wasmparser::Parser, |
| 394 |
data: &[u8], |
| 395 |
whole_component: &[u8], |
| 396 |
) -> Result<(), Error<T::Error>> { |
| 397 |
let mut remaining = data; |
| 398 |
while !remaining.is_empty() { |
| 399 |
let section = match parser.parse(remaining, true)? { |
| 400 |
wasmparser::Chunk::Parsed { consumed, payload } => { |
| 401 |
remaining = &remaining[consumed..]; |
| 402 |
payload |
| 403 |
} |
| 404 |
wasmparser::Chunk::NeedMoreData(_) => unreachable!(), |
| 405 |
}; |
| 406 |
match §ion { |
| 407 |
wasmparser::Payload::ComponentSection { |
| 408 |
unchecked_range, .. |
| 409 |
} |
| 410 |
| wasmparser::Payload::ModuleSection { |
| 411 |
unchecked_range, .. |
| 412 |
} => { |
| 413 |
remaining = &remaining[unchecked_range.len()..]; |
| 414 |
} |
| 415 |
_ => {} |
| 416 |
} |
| 417 |
reencoder.parse_component_payload(component, section, whole_component)?; |
| 418 |
} |
| 419 |
|
| 420 |
Ok(()) |
| 421 |
} |
| 422 |
|
| 423 |
pub fn parse_component_payload<T: ?Sized + ReencodeComponent>( |
| 424 |
reencoder: &mut T, |
| 425 |
component: &mut crate::Component, |
| 426 |
payload: wasmparser::Payload<'_>, |
| 427 |
whole_component: &[u8], |
| 428 |
) -> Result<(), Error<T::Error>> { |
| 429 |
match payload { |
| 430 |
wasmparser::Payload::Version { |
| 431 |
encoding: wasmparser::Encoding::Component, |
| 432 |
.. |
| 433 |
} => (), |
| 434 |
wasmparser::Payload::Version { .. } => { |
| 435 |
return Err(Error::UnexpectedNonComponentSection) |
| 436 |
} |
| 437 |
wasmparser::Payload::TypeSection(_) |
| 438 |
| wasmparser::Payload::ImportSection(_) |
| 439 |
| wasmparser::Payload::FunctionSection(_) |
| 440 |
| wasmparser::Payload::TableSection(_) |
| 441 |
| wasmparser::Payload::MemorySection(_) |
| 442 |
| wasmparser::Payload::TagSection(_) |
| 443 |
| wasmparser::Payload::GlobalSection(_) |
| 444 |
| wasmparser::Payload::ExportSection(_) |
| 445 |
| wasmparser::Payload::StartSection { .. } |
| 446 |
| wasmparser::Payload::ElementSection(_) |
| 447 |
| wasmparser::Payload::DataCountSection { .. } |
| 448 |
| wasmparser::Payload::DataSection(_) |
| 449 |
| wasmparser::Payload::CodeSectionStart { .. } |
| 450 |
| wasmparser::Payload::CodeSectionEntry(_) => { |
| 451 |
return Err(Error::UnexpectedNonComponentSection) |
| 452 |
} |
| 453 |
wasmparser::Payload::ComponentTypeSection(section) => { |
| 454 |
let mut types = crate::ComponentTypeSection::new(); |
| 455 |
reencoder.parse_component_type_section(&mut types, section)?; |
| 456 |
component.section(&types); |
| 457 |
} |
| 458 |
wasmparser::Payload::ComponentImportSection(section) => { |
| 459 |
let mut imports = crate::ComponentImportSection::new(); |
| 460 |
reencoder.parse_component_import_section(&mut imports, section)?; |
| 461 |
component.section(&imports); |
| 462 |
} |
| 463 |
wasmparser::Payload::ComponentCanonicalSection(section) => { |
| 464 |
let mut canonical = crate::CanonicalFunctionSection::new(); |
| 465 |
reencoder.parse_component_canonical_section(&mut canonical, section)?; |
| 466 |
component.section(&canonical); |
| 467 |
} |
| 468 |
wasmparser::Payload::ComponentAliasSection(section) => { |
| 469 |
let mut aliases = crate::ComponentAliasSection::new(); |
| 470 |
reencoder.parse_component_alias_section(&mut aliases, section)?; |
| 471 |
component.section(&aliases); |
| 472 |
} |
| 473 |
wasmparser::Payload::ComponentInstanceSection(section) => { |
| 474 |
let mut instances = crate::ComponentInstanceSection::new(); |
| 475 |
reencoder.parse_component_instance_section(&mut instances, section)?; |
| 476 |
component.section(&instances); |
| 477 |
} |
| 478 |
wasmparser::Payload::InstanceSection(section) => { |
| 479 |
let mut instances = crate::InstanceSection::new(); |
| 480 |
reencoder.parse_instance_section(&mut instances, section)?; |
| 481 |
component.section(&instances); |
| 482 |
} |
| 483 |
wasmparser::Payload::CoreTypeSection(section) => { |
| 484 |
let mut types = crate::CoreTypeSection::new(); |
| 485 |
reencoder.parse_core_type_section(&mut types, section)?; |
| 486 |
component.section(&types); |
| 487 |
} |
| 488 |
wasmparser::Payload::ComponentExportSection(section) => { |
| 489 |
let mut exports = crate::ComponentExportSection::new(); |
| 490 |
reencoder.parse_component_export_section(&mut exports, section)?; |
| 491 |
component.section(&exports); |
| 492 |
} |
| 493 |
wasmparser::Payload::CustomSection(section) => { |
| 494 |
reencoder.parse_component_custom_section(component, section)?; |
| 495 |
} |
| 496 |
wasmparser::Payload::ModuleSection { |
| 497 |
parser, |
| 498 |
unchecked_range, |
| 499 |
} => { |
| 500 |
reencoder.parse_component_submodule( |
| 501 |
component, |
| 502 |
parser, |
| 503 |
&whole_component[unchecked_range], |
| 504 |
)?; |
| 505 |
} |
| 506 |
wasmparser::Payload::ComponentSection { |
| 507 |
parser, |
| 508 |
unchecked_range, |
| 509 |
} => { |
| 510 |
reencoder.parse_component_subcomponent( |
| 511 |
component, |
| 512 |
parser, |
| 513 |
&whole_component[unchecked_range], |
| 514 |
whole_component, |
| 515 |
)?; |
| 516 |
} |
| 517 |
wasmparser::Payload::ComponentStartSection { start, range: _ } => { |
| 518 |
reencoder.parse_component_start_section(component, start)?; |
| 519 |
} |
| 520 |
wasmparser::Payload::End(_) => {} |
| 521 |
|
| 522 |
other => match other.as_section() { |
| 523 |
Some((id, range)) => { |
| 524 |
let section = &whole_component[range]; |
| 525 |
reencoder.parse_unknown_component_section(component, id, section)?; |
| 526 |
} |
| 527 |
None => unreachable!(), |
| 528 |
}, |
| 529 |
} |
| 530 |
Ok(()) |
| 531 |
} |
| 532 |
|
| 533 |
pub fn parse_component_submodule<T: ?Sized + ReencodeComponent>( |
| 534 |
reencoder: &mut T, |
| 535 |
component: &mut crate::Component, |
| 536 |
parser: wasmparser::Parser, |
| 537 |
submodule: &[u8], |
| 538 |
) -> Result<(), Error<T::Error>> { |
| 539 |
reencoder.push_depth(); |
| 540 |
let mut module = crate::Module::new(); |
| 541 |
crate::reencode::utils::parse_core_module(reencoder, &mut module, parser, submodule)?; |
| 542 |
component.section(&crate::ModuleSection(&module)); |
| 543 |
reencoder.pop_depth(); |
| 544 |
Ok(()) |
| 545 |
} |
| 546 |
|
| 547 |
pub fn parse_component_subcomponent<T: ?Sized + ReencodeComponent>( |
| 548 |
reencoder: &mut T, |
| 549 |
component: &mut crate::Component, |
| 550 |
parser: wasmparser::Parser, |
| 551 |
data: &[u8], |
| 552 |
whole_component: &[u8], |
| 553 |
) -> Result<(), Error<T::Error>> { |
| 554 |
reencoder.push_depth(); |
| 555 |
let mut subcomponent = crate::Component::new(); |
| 556 |
parse_component(reencoder, &mut subcomponent, parser, data, whole_component)?; |
| 557 |
component.section(&crate::NestedComponentSection(&subcomponent)); |
| 558 |
reencoder.pop_depth(); |
| 559 |
Ok(()) |
| 560 |
} |
| 561 |
|
| 562 |
pub fn parse_unknown_component_section<T: ?Sized + ReencodeComponent>( |
| 563 |
_reencoder: &mut T, |
| 564 |
component: &mut crate::Component, |
| 565 |
id: u8, |
| 566 |
contents: &[u8], |
| 567 |
) -> Result<(), Error<T::Error>> { |
| 568 |
component.section(&crate::RawSection { id, data: contents }); |
| 569 |
Ok(()) |
| 570 |
} |
| 571 |
|
| 572 |
pub fn parse_component_custom_section<T: ?Sized + ReencodeComponent>( |
| 573 |
reencoder: &mut T, |
| 574 |
component: &mut crate::Component, |
| 575 |
section: wasmparser::CustomSectionReader<'_>, |
| 576 |
) -> Result<(), Error<T::Error>> { |
| 577 |
match section.as_known() { |
| 578 |
wasmparser::KnownCustom::ComponentName(name) => { |
| 579 |
component.section(&reencoder.custom_component_name_section(name)?); |
| 580 |
} |
| 581 |
_ => { |
| 582 |
component.section(&reencoder.custom_section(section)); |
| 583 |
} |
| 584 |
} |
| 585 |
Ok(()) |
| 586 |
} |
| 587 |
|
| 588 |
pub fn parse_component_type_section<T: ?Sized + ReencodeComponent>( |
| 589 |
reencoder: &mut T, |
| 590 |
types: &mut crate::ComponentTypeSection, |
| 591 |
section: wasmparser::ComponentTypeSectionReader<'_>, |
| 592 |
) -> Result<(), Error<T::Error>> { |
| 593 |
for ty in section { |
| 594 |
reencoder.parse_component_type(types.ty(), ty?)?; |
| 595 |
} |
| 596 |
Ok(()) |
| 597 |
} |
| 598 |
|
| 599 |
pub fn parse_component_type<T: ?Sized + ReencodeComponent>( |
| 600 |
reencoder: &mut T, |
| 601 |
dst: crate::ComponentTypeEncoder, |
| 602 |
ty: wasmparser::ComponentType<'_>, |
| 603 |
) -> Result<(), Error<T::Error>> { |
| 604 |
match ty { |
| 605 |
wasmparser::ComponentType::Defined(ty) => { |
| 606 |
reencoder.parse_component_defined_type(dst.defined_type(), ty)?; |
| 607 |
} |
| 608 |
wasmparser::ComponentType::Func(func) => { |
| 609 |
reencoder.parse_component_func_type(dst.function(), func)?; |
| 610 |
} |
| 611 |
wasmparser::ComponentType::Component(component) => { |
| 612 |
let ty = reencoder.component_type(component)?; |
| 613 |
dst.component(&ty); |
| 614 |
} |
| 615 |
wasmparser::ComponentType::Instance(instance) => { |
| 616 |
let ty = reencoder.component_instance_type(instance)?; |
| 617 |
dst.instance(&ty); |
| 618 |
} |
| 619 |
wasmparser::ComponentType::Resource { rep, dtor } => { |
| 620 |
let rep = reencoder.val_type(rep)?; |
| 621 |
let dtor = dtor.map(|i| reencoder.function_index(i)); |
| 622 |
dst.resource(rep, dtor); |
| 623 |
} |
| 624 |
} |
| 625 |
Ok(()) |
| 626 |
} |
| 627 |
|
| 628 |
pub fn component_instance_type<T: ?Sized + ReencodeComponent>( |
| 629 |
reencoder: &mut T, |
| 630 |
ty: Box<[wasmparser::InstanceTypeDeclaration<'_>]>, |
| 631 |
) -> Result<crate::InstanceType, Error<T::Error>> { |
| 632 |
reencoder.push_depth(); |
| 633 |
let mut ret = crate::InstanceType::new(); |
| 634 |
for decl in Vec::from(ty) { |
| 635 |
reencoder.parse_component_instance_type_declaration(&mut ret, decl)?; |
| 636 |
} |
| 637 |
reencoder.pop_depth(); |
| 638 |
Ok(ret) |
| 639 |
} |
| 640 |
|
| 641 |
pub fn parse_component_instance_type_declaration<T: ?Sized + ReencodeComponent>( |
| 642 |
reencoder: &mut T, |
| 643 |
instance: &mut crate::InstanceType, |
| 644 |
decl: wasmparser::InstanceTypeDeclaration<'_>, |
| 645 |
) -> Result<(), Error<T::Error>> { |
| 646 |
match decl { |
| 647 |
wasmparser::InstanceTypeDeclaration::CoreType(core) => { |
| 648 |
reencoder.parse_component_core_type(instance.core_type(), core) |
| 649 |
} |
| 650 |
wasmparser::InstanceTypeDeclaration::Type(t) => { |
| 651 |
reencoder.parse_component_type(instance.ty(), t) |
| 652 |
} |
| 653 |
wasmparser::InstanceTypeDeclaration::Alias(a) => { |
| 654 |
let a = reencoder.component_alias(a)?; |
| 655 |
instance.alias(a); |
| 656 |
Ok(()) |
| 657 |
} |
| 658 |
wasmparser::InstanceTypeDeclaration::Export { name, ty } => { |
| 659 |
let ty = reencoder.component_type_ref(ty); |
| 660 |
instance.export(name.0, ty); |
| 661 |
Ok(()) |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
pub fn parse_component_core_type<T: ?Sized + ReencodeComponent>( |
| 667 |
reencoder: &mut T, |
| 668 |
ty: crate::ComponentCoreTypeEncoder<'_>, |
| 669 |
decl: wasmparser::CoreType<'_>, |
| 670 |
) -> Result<(), Error<T::Error>> { |
| 671 |
match decl { |
| 672 |
wasmparser::CoreType::Rec(rec) => { |
| 673 |
reencoder.parse_recursive_type_group(ty.core(), rec)?; |
| 674 |
} |
| 675 |
wasmparser::CoreType::Module(decls) => { |
| 676 |
ty.module(&reencoder.component_module_type(decls)?); |
| 677 |
} |
| 678 |
} |
| 679 |
Ok(()) |
| 680 |
} |
| 681 |
|
| 682 |
pub fn component_type<T: ?Sized + ReencodeComponent>( |
| 683 |
reencoder: &mut T, |
| 684 |
ty: Box<[wasmparser::ComponentTypeDeclaration<'_>]>, |
| 685 |
) -> Result<crate::ComponentType, Error<T::Error>> { |
| 686 |
reencoder.push_depth(); |
| 687 |
let mut ret = crate::ComponentType::new(); |
| 688 |
for decl in Vec::from(ty) { |
| 689 |
reencoder.parse_component_type_declaration(&mut ret, decl)?; |
| 690 |
} |
| 691 |
reencoder.pop_depth(); |
| 692 |
Ok(ret) |
| 693 |
} |
| 694 |
|
| 695 |
pub fn parse_component_type_declaration<T: ?Sized + ReencodeComponent>( |
| 696 |
reencoder: &mut T, |
| 697 |
component: &mut crate::ComponentType, |
| 698 |
decl: wasmparser::ComponentTypeDeclaration<'_>, |
| 699 |
) -> Result<(), Error<T::Error>> { |
| 700 |
match decl { |
| 701 |
wasmparser::ComponentTypeDeclaration::CoreType(ty) => { |
| 702 |
reencoder.parse_component_core_type(component.core_type(), ty) |
| 703 |
} |
| 704 |
wasmparser::ComponentTypeDeclaration::Type(ty) => { |
| 705 |
reencoder.parse_component_type(component.ty(), ty) |
| 706 |
} |
| 707 |
wasmparser::ComponentTypeDeclaration::Alias(a) => { |
| 708 |
let a = reencoder.component_alias(a)?; |
| 709 |
component.alias(a); |
| 710 |
Ok(()) |
| 711 |
} |
| 712 |
wasmparser::ComponentTypeDeclaration::Export { name, ty } => { |
| 713 |
let ty = reencoder.component_type_ref(ty); |
| 714 |
component.export(name.0, ty); |
| 715 |
Ok(()) |
| 716 |
} |
| 717 |
wasmparser::ComponentTypeDeclaration::Import(import) => { |
| 718 |
let ty = reencoder.component_type_ref(import.ty); |
| 719 |
component.import(import.name.0, ty); |
| 720 |
Ok(()) |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
pub fn parse_component_func_type<T: ?Sized + ReencodeComponent>( |
| 726 |
reencoder: &mut T, |
| 727 |
mut func: crate::ComponentFuncTypeEncoder<'_>, |
| 728 |
ty: wasmparser::ComponentFuncType<'_>, |
| 729 |
) -> Result<(), Error<T::Error>> { |
| 730 |
func.params( |
| 731 |
Vec::from(ty.params) |
| 732 |
.into_iter() |
| 733 |
.map(|(name, ty)| (name, reencoder.component_val_type(ty))), |
| 734 |
); |
| 735 |
match ty.results { |
| 736 |
wasmparser::ComponentFuncResult::Unnamed(ty) => { |
| 737 |
func.result(reencoder.component_val_type(ty)); |
| 738 |
} |
| 739 |
wasmparser::ComponentFuncResult::Named(list) => { |
| 740 |
func.results( |
| 741 |
Vec::from(list) |
| 742 |
.into_iter() |
| 743 |
.map(|(name, ty)| (name, reencoder.component_val_type(ty))), |
| 744 |
); |
| 745 |
} |
| 746 |
} |
| 747 |
Ok(()) |
| 748 |
} |
| 749 |
|
| 750 |
pub fn parse_component_defined_type<T: ?Sized + ReencodeComponent>( |
| 751 |
reencoder: &mut T, |
| 752 |
defined: crate::ComponentDefinedTypeEncoder<'_>, |
| 753 |
ty: wasmparser::ComponentDefinedType<'_>, |
| 754 |
) -> Result<(), Error<T::Error>> { |
| 755 |
match ty { |
| 756 |
wasmparser::ComponentDefinedType::Primitive(p) => { |
| 757 |
defined.primitive(reencoder.component_primitive_val_type(p)); |
| 758 |
} |
| 759 |
wasmparser::ComponentDefinedType::Record(r) => { |
| 760 |
defined.record( |
| 761 |
r.iter() |
| 762 |
.map(|(name, ty)| (*name, reencoder.component_val_type(*ty))), |
| 763 |
); |
| 764 |
} |
| 765 |
wasmparser::ComponentDefinedType::Variant(v) => { |
| 766 |
defined.variant(v.iter().map(|case| { |
| 767 |
( |
| 768 |
case.name, |
| 769 |
case.ty.map(|t| reencoder.component_val_type(t)), |
| 770 |
case.refines, |
| 771 |
) |
| 772 |
})); |
| 773 |
} |
| 774 |
wasmparser::ComponentDefinedType::List(t) => { |
| 775 |
defined.list(reencoder.component_val_type(t)); |
| 776 |
} |
| 777 |
wasmparser::ComponentDefinedType::Tuple(t) => { |
| 778 |
defined.tuple(t.iter().map(|t| reencoder.component_val_type(*t))); |
| 779 |
} |
| 780 |
wasmparser::ComponentDefinedType::Flags(t) => { |
| 781 |
defined.flags(t.iter().copied()); |
| 782 |
} |
| 783 |
wasmparser::ComponentDefinedType::Enum(t) => { |
| 784 |
defined.enum_type(t.iter().copied()); |
| 785 |
} |
| 786 |
wasmparser::ComponentDefinedType::Option(t) => { |
| 787 |
defined.option(reencoder.component_val_type(t)); |
| 788 |
} |
| 789 |
wasmparser::ComponentDefinedType::Result { ok, err } => { |
| 790 |
let ok = ok.map(|t| reencoder.component_val_type(t)); |
| 791 |
let err = err.map(|t| reencoder.component_val_type(t)); |
| 792 |
defined.result(ok, err); |
| 793 |
} |
| 794 |
wasmparser::ComponentDefinedType::Own(i) => { |
| 795 |
defined.own(reencoder.component_type_index(i)); |
| 796 |
} |
| 797 |
wasmparser::ComponentDefinedType::Borrow(i) => { |
| 798 |
defined.borrow(reencoder.component_type_index(i)); |
| 799 |
} |
| 800 |
} |
| 801 |
Ok(()) |
| 802 |
} |
| 803 |
|
| 804 |
pub fn component_module_type<T: ?Sized + ReencodeComponent>( |
| 805 |
reencoder: &mut T, |
| 806 |
ty: Box<[wasmparser::ModuleTypeDeclaration<'_>]>, |
| 807 |
) -> Result<crate::ModuleType, Error<T::Error>> { |
| 808 |
reencoder.push_depth(); |
| 809 |
let mut ret = crate::ModuleType::new(); |
| 810 |
for decl in Vec::from(ty) { |
| 811 |
reencoder.parse_component_module_type_declaration(&mut ret, decl)?; |
| 812 |
} |
| 813 |
reencoder.pop_depth(); |
| 814 |
Ok(ret) |
| 815 |
} |
| 816 |
|
| 817 |
pub fn parse_component_module_type_declaration<T: ?Sized + ReencodeComponent>( |
| 818 |
reencoder: &mut T, |
| 819 |
module: &mut crate::ModuleType, |
| 820 |
decl: wasmparser::ModuleTypeDeclaration<'_>, |
| 821 |
) -> Result<(), Error<T::Error>> { |
| 822 |
match decl { |
| 823 |
wasmparser::ModuleTypeDeclaration::Type(rec) => { |
| 824 |
reencoder.parse_recursive_type_group(module.ty(), rec)?; |
| 825 |
} |
| 826 |
wasmparser::ModuleTypeDeclaration::Export { name, ty } => { |
| 827 |
module.export(name, reencoder.entity_type(ty)?); |
| 828 |
} |
| 829 |
wasmparser::ModuleTypeDeclaration::OuterAlias { |
| 830 |
kind: wasmparser::OuterAliasKind::Type, |
| 831 |
count, |
| 832 |
index, |
| 833 |
} => { |
| 834 |
let index = reencoder.outer_type_index(count, index); |
| 835 |
module.alias_outer_core_type(count, index); |
| 836 |
} |
| 837 |
wasmparser::ModuleTypeDeclaration::Import(import) => { |
| 838 |
module.import( |
| 839 |
import.module, |
| 840 |
import.name, |
| 841 |
reencoder.entity_type(import.ty)?, |
| 842 |
); |
| 843 |
} |
| 844 |
} |
| 845 |
Ok(()) |
| 846 |
} |
| 847 |
|
| 848 |
pub fn component_alias<'a, T: ?Sized + ReencodeComponent>( |
| 849 |
reencoder: &mut T, |
| 850 |
alias: wasmparser::ComponentAlias<'a>, |
| 851 |
) -> Result<crate::Alias<'a>, Error<T::Error>> { |
| 852 |
match alias { |
| 853 |
wasmparser::ComponentAlias::InstanceExport { |
| 854 |
kind, |
| 855 |
instance_index, |
| 856 |
name, |
| 857 |
} => Ok(crate::Alias::InstanceExport { |
| 858 |
instance: reencoder.component_instance_index(instance_index), |
| 859 |
kind: kind.into(), |
| 860 |
name, |
| 861 |
}), |
| 862 |
wasmparser::ComponentAlias::CoreInstanceExport { |
| 863 |
kind, |
| 864 |
instance_index, |
| 865 |
name, |
| 866 |
} => Ok(crate::Alias::CoreInstanceExport { |
| 867 |
instance: reencoder.instance_index(instance_index), |
| 868 |
kind: kind.into(), |
| 869 |
name, |
| 870 |
}), |
| 871 |
wasmparser::ComponentAlias::Outer { kind, count, index } => Ok(crate::Alias::Outer { |
| 872 |
kind: kind.into(), |
| 873 |
count, |
| 874 |
index: match kind { |
| 875 |
wasmparser::ComponentOuterAliasKind::CoreModule => { |
| 876 |
reencoder.outer_module_index(count, index) |
| 877 |
} |
| 878 |
wasmparser::ComponentOuterAliasKind::CoreType => { |
| 879 |
reencoder.outer_type_index(count, index) |
| 880 |
} |
| 881 |
wasmparser::ComponentOuterAliasKind::Type => { |
| 882 |
reencoder.outer_component_type_index(count, index) |
| 883 |
} |
| 884 |
wasmparser::ComponentOuterAliasKind::Component => { |
| 885 |
reencoder.outer_component_index(count, index) |
| 886 |
} |
| 887 |
}, |
| 888 |
}), |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
pub fn parse_component_import_section<T: ?Sized + ReencodeComponent>( |
| 893 |
reencoder: &mut T, |
| 894 |
imports: &mut crate::ComponentImportSection, |
| 895 |
section: wasmparser::ComponentImportSectionReader<'_>, |
| 896 |
) -> Result<(), Error<T::Error>> { |
| 897 |
for import in section { |
| 898 |
let import = import?; |
| 899 |
imports.import(import.name.0, reencoder.component_type_ref(import.ty)); |
| 900 |
} |
| 901 |
Ok(()) |
| 902 |
} |
| 903 |
|
| 904 |
pub fn parse_component_canonical_section<T: ?Sized + ReencodeComponent>( |
| 905 |
reencoder: &mut T, |
| 906 |
canonical: &mut crate::CanonicalFunctionSection, |
| 907 |
section: wasmparser::ComponentCanonicalSectionReader<'_>, |
| 908 |
) -> Result<(), Error<T::Error>> { |
| 909 |
for c in section { |
| 910 |
reencoder.parse_component_canonical(canonical, c?)?; |
| 911 |
} |
| 912 |
Ok(()) |
| 913 |
} |
| 914 |
|
| 915 |
pub fn parse_component_canonical<T: ?Sized + ReencodeComponent>( |
| 916 |
reencoder: &mut T, |
| 917 |
section: &mut crate::CanonicalFunctionSection, |
| 918 |
func: wasmparser::CanonicalFunction, |
| 919 |
) -> Result<(), Error<T::Error>> { |
| 920 |
match func { |
| 921 |
wasmparser::CanonicalFunction::Lift { |
| 922 |
core_func_index, |
| 923 |
type_index, |
| 924 |
options, |
| 925 |
} => { |
| 926 |
let func = reencoder.function_index(core_func_index); |
| 927 |
let ty = reencoder.component_type_index(type_index); |
| 928 |
section.lift( |
| 929 |
func, |
| 930 |
ty, |
| 931 |
options.iter().map(|o| reencoder.canonical_option(*o)), |
| 932 |
); |
| 933 |
} |
| 934 |
wasmparser::CanonicalFunction::Lower { |
| 935 |
func_index, |
| 936 |
options, |
| 937 |
} => { |
| 938 |
let func = reencoder.component_func_index(func_index); |
| 939 |
section.lower(func, options.iter().map(|o| reencoder.canonical_option(*o))); |
| 940 |
} |
| 941 |
wasmparser::CanonicalFunction::ResourceNew { resource } => { |
| 942 |
let resource = reencoder.component_type_index(resource); |
| 943 |
section.resource_new(resource); |
| 944 |
} |
| 945 |
wasmparser::CanonicalFunction::ResourceDrop { resource } => { |
| 946 |
let resource = reencoder.component_type_index(resource); |
| 947 |
section.resource_drop(resource); |
| 948 |
} |
| 949 |
wasmparser::CanonicalFunction::ResourceRep { resource } => { |
| 950 |
let resource = reencoder.component_type_index(resource); |
| 951 |
section.resource_rep(resource); |
| 952 |
} |
| 953 |
wasmparser::CanonicalFunction::ThreadSpawn { func_ty_index } => { |
| 954 |
let func_ty = reencoder.type_index(func_ty_index); |
| 955 |
section.thread_spawn(func_ty); |
| 956 |
} |
| 957 |
wasmparser::CanonicalFunction::ThreadHwConcurrency => { |
| 958 |
section.thread_hw_concurrency(); |
| 959 |
} |
| 960 |
} |
| 961 |
Ok(()) |
| 962 |
} |
| 963 |
|
| 964 |
pub fn parse_component_alias_section<T: ?Sized + ReencodeComponent>( |
| 965 |
reencoder: &mut T, |
| 966 |
aliases: &mut crate::ComponentAliasSection, |
| 967 |
section: wasmparser::ComponentAliasSectionReader<'_>, |
| 968 |
) -> Result<(), Error<T::Error>> { |
| 969 |
for a in section { |
| 970 |
aliases.alias(reencoder.component_alias(a?)?); |
| 971 |
} |
| 972 |
Ok(()) |
| 973 |
} |
| 974 |
|
| 975 |
pub fn parse_component_instance_section<T: ?Sized + ReencodeComponent>( |
| 976 |
reencoder: &mut T, |
| 977 |
instances: &mut crate::ComponentInstanceSection, |
| 978 |
section: wasmparser::ComponentInstanceSectionReader<'_>, |
| 979 |
) -> Result<(), Error<T::Error>> { |
| 980 |
for i in section { |
| 981 |
reencoder.parse_component_instance(instances, i?)?; |
| 982 |
} |
| 983 |
Ok(()) |
| 984 |
} |
| 985 |
|
| 986 |
pub fn parse_component_instance<T: ?Sized + ReencodeComponent>( |
| 987 |
reencoder: &mut T, |
| 988 |
instances: &mut crate::ComponentInstanceSection, |
| 989 |
instance: wasmparser::ComponentInstance<'_>, |
| 990 |
) -> Result<(), Error<T::Error>> { |
| 991 |
match instance { |
| 992 |
wasmparser::ComponentInstance::Instantiate { |
| 993 |
component_index, |
| 994 |
args, |
| 995 |
} => { |
| 996 |
instances.instantiate( |
| 997 |
reencoder.component_index(component_index), |
| 998 |
args.iter().map(|arg| { |
| 999 |
( |
| 1000 |
arg.name, |
| 1001 |
arg.kind.into(), |
| 1002 |
reencoder.component_external_index(arg.kind, arg.index), |
| 1003 |
) |
| 1004 |
}), |
| 1005 |
); |
| 1006 |
} |
| 1007 |
wasmparser::ComponentInstance::FromExports(exports) => { |
| 1008 |
instances.export_items(exports.iter().map(|export| { |
| 1009 |
( |
| 1010 |
export.name.0, |
| 1011 |
export.kind.into(), |
| 1012 |
reencoder.component_external_index(export.kind, export.index), |
| 1013 |
) |
| 1014 |
})); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
Ok(()) |
| 1018 |
} |
| 1019 |
|
| 1020 |
pub fn parse_instance_section<T: ?Sized + ReencodeComponent>( |
| 1021 |
reencoder: &mut T, |
| 1022 |
instances: &mut crate::InstanceSection, |
| 1023 |
section: wasmparser::InstanceSectionReader<'_>, |
| 1024 |
) -> Result<(), Error<T::Error>> { |
| 1025 |
for i in section { |
| 1026 |
reencoder.parse_instance(instances, i?)?; |
| 1027 |
} |
| 1028 |
Ok(()) |
| 1029 |
} |
| 1030 |
|
| 1031 |
pub fn parse_instance<T: ?Sized + ReencodeComponent>( |
| 1032 |
reencoder: &mut T, |
| 1033 |
instances: &mut crate::InstanceSection, |
| 1034 |
instance: wasmparser::Instance<'_>, |
| 1035 |
) -> Result<(), Error<T::Error>> { |
| 1036 |
match instance { |
| 1037 |
wasmparser::Instance::Instantiate { module_index, args } => { |
| 1038 |
instances.instantiate( |
| 1039 |
reencoder.module_index(module_index), |
| 1040 |
args.iter().map(|arg| match arg.kind { |
| 1041 |
wasmparser::InstantiationArgKind::Instance => ( |
| 1042 |
arg.name, |
| 1043 |
crate::ModuleArg::Instance(reencoder.instance_index(arg.index)), |
| 1044 |
), |
| 1045 |
}), |
| 1046 |
); |
| 1047 |
} |
| 1048 |
wasmparser::Instance::FromExports(exports) => { |
| 1049 |
instances.export_items(exports.iter().map(|export| { |
| 1050 |
( |
| 1051 |
export.name, |
| 1052 |
reencoder.export_kind(export.kind), |
| 1053 |
reencoder.external_index(export.kind, export.index), |
| 1054 |
) |
| 1055 |
})); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
Ok(()) |
| 1059 |
} |
| 1060 |
|
| 1061 |
pub fn parse_core_type_section<T: ?Sized + ReencodeComponent>( |
| 1062 |
reencoder: &mut T, |
| 1063 |
types: &mut crate::CoreTypeSection, |
| 1064 |
section: wasmparser::CoreTypeSectionReader<'_>, |
| 1065 |
) -> Result<(), Error<T::Error>> { |
| 1066 |
for t in section { |
| 1067 |
reencoder.parse_component_core_type(types.ty(), t?)?; |
| 1068 |
} |
| 1069 |
Ok(()) |
| 1070 |
} |
| 1071 |
|
| 1072 |
pub fn parse_component_export_section<T: ?Sized + ReencodeComponent>( |
| 1073 |
reencoder: &mut T, |
| 1074 |
exports: &mut crate::ComponentExportSection, |
| 1075 |
section: wasmparser::ComponentExportSectionReader<'_>, |
| 1076 |
) -> Result<(), Error<T::Error>> { |
| 1077 |
for e in section { |
| 1078 |
reencoder.parse_component_export(exports, e?)?; |
| 1079 |
} |
| 1080 |
Ok(()) |
| 1081 |
} |
| 1082 |
|
| 1083 |
pub fn parse_component_export<T: ?Sized + ReencodeComponent>( |
| 1084 |
reencoder: &mut T, |
| 1085 |
exports: &mut crate::ComponentExportSection, |
| 1086 |
export: wasmparser::ComponentExport<'_>, |
| 1087 |
) -> Result<(), Error<T::Error>> { |
| 1088 |
exports.export( |
| 1089 |
export.name.0, |
| 1090 |
export.kind.into(), |
| 1091 |
reencoder.component_external_index(export.kind, export.index), |
| 1092 |
export.ty.map(|t| reencoder.component_type_ref(t)), |
| 1093 |
); |
| 1094 |
Ok(()) |
| 1095 |
} |
| 1096 |
|
| 1097 |
pub fn parse_component_start_section<T: ?Sized + ReencodeComponent>( |
| 1098 |
reencoder: &mut T, |
| 1099 |
component: &mut crate::Component, |
| 1100 |
func: wasmparser::ComponentStartFunction, |
| 1101 |
) -> Result<(), Error<T::Error>> { |
| 1102 |
component.section(&crate::ComponentStartSection { |
| 1103 |
function_index: reencoder.component_func_index(func.func_index), |
| 1104 |
args: func |
| 1105 |
.arguments |
| 1106 |
.iter() |
| 1107 |
.map(|i| reencoder.component_value_index(*i)) |
| 1108 |
.collect::<Vec<_>>(), |
| 1109 |
results: func.results, |
| 1110 |
}); |
| 1111 |
Ok(()) |
| 1112 |
} |
| 1113 |
|
| 1114 |
pub fn component_type_ref<T: ?Sized + ReencodeComponent>( |
| 1115 |
reencoder: &mut T, |
| 1116 |
ty: wasmparser::ComponentTypeRef, |
| 1117 |
) -> crate::component::ComponentTypeRef { |
| 1118 |
match ty { |
| 1119 |
wasmparser::ComponentTypeRef::Module(u) => { |
| 1120 |
crate::component::ComponentTypeRef::Module(reencoder.type_index(u)) |
| 1121 |
} |
| 1122 |
wasmparser::ComponentTypeRef::Func(u) => { |
| 1123 |
crate::component::ComponentTypeRef::Func(reencoder.component_type_index(u)) |
| 1124 |
} |
| 1125 |
wasmparser::ComponentTypeRef::Value(valty) => { |
| 1126 |
crate::component::ComponentTypeRef::Value(reencoder.component_val_type(valty)) |
| 1127 |
} |
| 1128 |
wasmparser::ComponentTypeRef::Type(bounds) => { |
| 1129 |
crate::component::ComponentTypeRef::Type(reencoder.type_bounds(bounds)) |
| 1130 |
} |
| 1131 |
wasmparser::ComponentTypeRef::Instance(u) => { |
| 1132 |
crate::component::ComponentTypeRef::Instance(reencoder.component_type_index(u)) |
| 1133 |
} |
| 1134 |
wasmparser::ComponentTypeRef::Component(u) => { |
| 1135 |
crate::component::ComponentTypeRef::Component(reencoder.component_type_index(u)) |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
pub fn component_primitive_val_type<T: ?Sized + ReencodeComponent>( |
| 1141 |
_reencoder: &mut T, |
| 1142 |
ty: wasmparser::PrimitiveValType, |
| 1143 |
) -> crate::component::PrimitiveValType { |
| 1144 |
match ty { |
| 1145 |
wasmparser::PrimitiveValType::Bool => crate::component::PrimitiveValType::Bool, |
| 1146 |
wasmparser::PrimitiveValType::S8 => crate::component::PrimitiveValType::S8, |
| 1147 |
wasmparser::PrimitiveValType::U8 => crate::component::PrimitiveValType::U8, |
| 1148 |
wasmparser::PrimitiveValType::S16 => crate::component::PrimitiveValType::S16, |
| 1149 |
wasmparser::PrimitiveValType::U16 => crate::component::PrimitiveValType::U16, |
| 1150 |
wasmparser::PrimitiveValType::S32 => crate::component::PrimitiveValType::S32, |
| 1151 |
wasmparser::PrimitiveValType::U32 => crate::component::PrimitiveValType::U32, |
| 1152 |
wasmparser::PrimitiveValType::S64 => crate::component::PrimitiveValType::S64, |
| 1153 |
wasmparser::PrimitiveValType::U64 => crate::component::PrimitiveValType::U64, |
| 1154 |
wasmparser::PrimitiveValType::F32 => crate::component::PrimitiveValType::F32, |
| 1155 |
wasmparser::PrimitiveValType::F64 => crate::component::PrimitiveValType::F64, |
| 1156 |
wasmparser::PrimitiveValType::Char => crate::component::PrimitiveValType::Char, |
| 1157 |
wasmparser::PrimitiveValType::String => crate::component::PrimitiveValType::String, |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
pub fn component_export_kind<T: ?Sized + ReencodeComponent>( |
| 1162 |
_reencoder: &mut T, |
| 1163 |
ty: wasmparser::ComponentExternalKind, |
| 1164 |
) -> crate::component::ComponentExportKind { |
| 1165 |
match ty { |
| 1166 |
wasmparser::ComponentExternalKind::Module => crate::ComponentExportKind::Module, |
| 1167 |
wasmparser::ComponentExternalKind::Func => crate::ComponentExportKind::Func, |
| 1168 |
wasmparser::ComponentExternalKind::Value => crate::ComponentExportKind::Value, |
| 1169 |
wasmparser::ComponentExternalKind::Type => crate::ComponentExportKind::Type, |
| 1170 |
wasmparser::ComponentExternalKind::Instance => crate::ComponentExportKind::Instance, |
| 1171 |
wasmparser::ComponentExternalKind::Component => crate::ComponentExportKind::Component, |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
pub fn component_outer_alias_kind<T: ?Sized + ReencodeComponent>( |
| 1176 |
_reencoder: &mut T, |
| 1177 |
ty: wasmparser::ComponentOuterAliasKind, |
| 1178 |
) -> crate::component::ComponentOuterAliasKind { |
| 1179 |
match ty { |
| 1180 |
wasmparser::ComponentOuterAliasKind::CoreModule => { |
| 1181 |
crate::component::ComponentOuterAliasKind::CoreModule |
| 1182 |
} |
| 1183 |
wasmparser::ComponentOuterAliasKind::CoreType => { |
| 1184 |
crate::component::ComponentOuterAliasKind::CoreType |
| 1185 |
} |
| 1186 |
wasmparser::ComponentOuterAliasKind::Type => { |
| 1187 |
crate::component::ComponentOuterAliasKind::Type |
| 1188 |
} |
| 1189 |
wasmparser::ComponentOuterAliasKind::Component => { |
| 1190 |
crate::ComponentOuterAliasKind::Component |
| 1191 |
} |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
pub fn component_val_type<T: ?Sized + ReencodeComponent>( |
| 1196 |
reencoder: &mut T, |
| 1197 |
ty: wasmparser::ComponentValType, |
| 1198 |
) -> crate::component::ComponentValType { |
| 1199 |
match ty { |
| 1200 |
wasmparser::ComponentValType::Type(u) => { |
| 1201 |
crate::component::ComponentValType::Type(reencoder.component_type_index(u)) |
| 1202 |
} |
| 1203 |
wasmparser::ComponentValType::Primitive(pty) => { |
| 1204 |
crate::component::ComponentValType::Primitive( |
| 1205 |
crate::component::PrimitiveValType::from(pty), |
| 1206 |
) |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
pub fn type_bounds<T: ?Sized + ReencodeComponent>( |
| 1212 |
reencoder: &mut T, |
| 1213 |
ty: wasmparser::TypeBounds, |
| 1214 |
) -> crate::component::TypeBounds { |
| 1215 |
match ty { |
| 1216 |
wasmparser::TypeBounds::Eq(u) => { |
| 1217 |
crate::component::TypeBounds::Eq(reencoder.component_type_index(u)) |
| 1218 |
} |
| 1219 |
wasmparser::TypeBounds::SubResource => crate::component::TypeBounds::SubResource, |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
pub fn canonical_option<T: ?Sized + ReencodeComponent>( |
| 1224 |
reencoder: &mut T, |
| 1225 |
ty: wasmparser::CanonicalOption, |
| 1226 |
) -> crate::component::CanonicalOption { |
| 1227 |
match ty { |
| 1228 |
wasmparser::CanonicalOption::UTF8 => crate::component::CanonicalOption::UTF8, |
| 1229 |
wasmparser::CanonicalOption::UTF16 => crate::component::CanonicalOption::UTF16, |
| 1230 |
wasmparser::CanonicalOption::CompactUTF16 => { |
| 1231 |
crate::component::CanonicalOption::CompactUTF16 |
| 1232 |
} |
| 1233 |
wasmparser::CanonicalOption::Memory(u) => { |
| 1234 |
crate::component::CanonicalOption::Memory(reencoder.memory_index(u)) |
| 1235 |
} |
| 1236 |
wasmparser::CanonicalOption::Realloc(u) => { |
| 1237 |
crate::component::CanonicalOption::Realloc(reencoder.function_index(u)) |
| 1238 |
} |
| 1239 |
wasmparser::CanonicalOption::PostReturn(u) => { |
| 1240 |
crate::component::CanonicalOption::PostReturn(reencoder.function_index(u)) |
| 1241 |
} |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
pub fn custom_component_name_section<T: ?Sized + ReencodeComponent>( |
| 1246 |
reencoder: &mut T, |
| 1247 |
section: wasmparser::ComponentNameSectionReader<'_>, |
| 1248 |
) -> Result<crate::ComponentNameSection, Error<T::Error>> { |
| 1249 |
let mut ret = crate::ComponentNameSection::new(); |
| 1250 |
for subsection in section { |
| 1251 |
reencoder.parse_custom_component_name_subsection(&mut ret, subsection?)?; |
| 1252 |
} |
| 1253 |
Ok(ret) |
| 1254 |
} |
| 1255 |
|
| 1256 |
pub fn parse_custom_component_name_subsection<T: ?Sized + ReencodeComponent>( |
| 1257 |
reencoder: &mut T, |
| 1258 |
names: &mut crate::ComponentNameSection, |
| 1259 |
section: wasmparser::ComponentName<'_>, |
| 1260 |
) -> Result<(), Error<T::Error>> { |
| 1261 |
match section { |
| 1262 |
wasmparser::ComponentName::Component { name, .. } => { |
| 1263 |
names.component(name); |
| 1264 |
} |
| 1265 |
wasmparser::ComponentName::CoreFuncs(map) => { |
| 1266 |
names.core_funcs(&name_map(map, |i| reencoder.function_index(i))?); |
| 1267 |
} |
| 1268 |
wasmparser::ComponentName::CoreGlobals(map) => { |
| 1269 |
names.core_globals(&name_map(map, |i| reencoder.global_index(i))?); |
| 1270 |
} |
| 1271 |
wasmparser::ComponentName::CoreMemories(map) => { |
| 1272 |
names.core_memories(&name_map(map, |i| reencoder.memory_index(i))?); |
| 1273 |
} |
| 1274 |
wasmparser::ComponentName::CoreTables(map) => { |
| 1275 |
names.core_tables(&name_map(map, |i| reencoder.table_index(i))?); |
| 1276 |
} |
| 1277 |
wasmparser::ComponentName::CoreModules(map) => { |
| 1278 |
names.core_modules(&name_map(map, |i| reencoder.module_index(i))?); |
| 1279 |
} |
| 1280 |
wasmparser::ComponentName::CoreInstances(map) => { |
| 1281 |
names.core_instances(&name_map(map, |i| reencoder.instance_index(i))?); |
| 1282 |
} |
| 1283 |
wasmparser::ComponentName::CoreTypes(map) => { |
| 1284 |
names.core_types(&name_map(map, |i| reencoder.type_index(i))?); |
| 1285 |
} |
| 1286 |
wasmparser::ComponentName::Types(map) => { |
| 1287 |
names.types(&name_map(map, |i| reencoder.component_type_index(i))?); |
| 1288 |
} |
| 1289 |
wasmparser::ComponentName::Instances(map) => { |
| 1290 |
names.instances(&name_map(map, |i| reencoder.component_instance_index(i))?); |
| 1291 |
} |
| 1292 |
wasmparser::ComponentName::Components(map) => { |
| 1293 |
names.components(&name_map(map, |i| reencoder.component_index(i))?); |
| 1294 |
} |
| 1295 |
wasmparser::ComponentName::Funcs(map) => { |
| 1296 |
names.funcs(&name_map(map, |i| reencoder.component_func_index(i))?); |
| 1297 |
} |
| 1298 |
wasmparser::ComponentName::Values(map) => { |
| 1299 |
names.values(&name_map(map, |i| reencoder.component_value_index(i))?); |
| 1300 |
} |
| 1301 |
wasmparser::ComponentName::Unknown { ty, data, .. } => { |
| 1302 |
names.raw(ty, data); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
Ok(()) |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
impl From<wasmparser::ComponentValType> for crate::ComponentValType { |
| 1310 |
fn from(ty: wasmparser::ComponentValType) -> Self { |
| 1311 |
RoundtripReencoder.component_val_type(ty) |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
impl From<wasmparser::TypeBounds> for crate::TypeBounds { |
| 1316 |
fn from(ty: wasmparser::TypeBounds) -> Self { |
| 1317 |
RoundtripReencoder.type_bounds(ty) |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
impl From<wasmparser::CanonicalOption> for crate::CanonicalOption { |
| 1322 |
fn from(opt: wasmparser::CanonicalOption) -> Self { |
| 1323 |
RoundtripReencoder.canonical_option(opt) |
| 1324 |
} |
| 1325 |
} |
| 1326 |
|
| 1327 |
impl From<wasmparser::ComponentExternalKind> for crate::ComponentExportKind { |
| 1328 |
fn from(kind: wasmparser::ComponentExternalKind) -> Self { |
| 1329 |
RoundtripReencoder.component_export_kind(kind) |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
impl From<wasmparser::ComponentOuterAliasKind> for crate::ComponentOuterAliasKind { |
| 1334 |
fn from(kind: wasmparser::ComponentOuterAliasKind) -> Self { |
| 1335 |
RoundtripReencoder.component_outer_alias_kind(kind) |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
impl From<wasmparser::ComponentTypeRef> for crate::ComponentTypeRef { |
| 1340 |
fn from(ty: wasmparser::ComponentTypeRef) -> Self { |
| 1341 |
RoundtripReencoder.component_type_ref(ty) |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
impl From<wasmparser::PrimitiveValType> for crate::PrimitiveValType { |
| 1346 |
fn from(ty: wasmparser::PrimitiveValType) -> Self { |
| 1347 |
RoundtripReencoder.component_primitive_val_type(ty) |
| 1348 |
} |
| 1349 |
} |