| 1 |
//! Conversions from `wasmparser` to `wasm-encoder` to [`Reencode`] parsed wasm. |
| 2 |
//! |
| 3 |
//! The [`RoundtripReencoder`] allows encoding identical wasm to the parsed |
| 4 |
//! input. |
| 5 |
|
| 6 |
use crate::CoreTypeEncoder; |
| 7 |
use std::convert::Infallible; |
| 8 |
|
| 9 |
#[cfg(feature = "component-model")] |
| 10 |
mod component; |
| 11 |
|
| 12 |
#[cfg(feature = "component-model")] |
| 13 |
pub use self::component::*; |
| 14 |
|
| 15 |
#[allow(missing_docs)] // FIXME |
| 16 |
pub trait Reencode { |
| 17 |
type Error; |
| 18 |
|
| 19 |
fn data_index(&mut self, data: u32) -> u32 { |
| 20 |
utils::data_index(self, data) |
| 21 |
} |
| 22 |
|
| 23 |
fn element_index(&mut self, element: u32) -> u32 { |
| 24 |
utils::element_index(self, element) |
| 25 |
} |
| 26 |
|
| 27 |
fn function_index(&mut self, func: u32) -> u32 { |
| 28 |
utils::function_index(self, func) |
| 29 |
} |
| 30 |
|
| 31 |
fn global_index(&mut self, global: u32) -> u32 { |
| 32 |
utils::global_index(self, global) |
| 33 |
} |
| 34 |
|
| 35 |
fn memory_index(&mut self, memory: u32) -> u32 { |
| 36 |
utils::memory_index(self, memory) |
| 37 |
} |
| 38 |
|
| 39 |
fn table_index(&mut self, table: u32) -> u32 { |
| 40 |
utils::table_index(self, table) |
| 41 |
} |
| 42 |
|
| 43 |
fn tag_index(&mut self, tag: u32) -> u32 { |
| 44 |
utils::tag_index(self, tag) |
| 45 |
} |
| 46 |
|
| 47 |
fn type_index(&mut self, ty: u32) -> u32 { |
| 48 |
utils::type_index(self, ty) |
| 49 |
} |
| 50 |
|
| 51 |
fn type_index_unpacked( |
| 52 |
&mut self, |
| 53 |
ty: wasmparser::UnpackedIndex, |
| 54 |
) -> Result<u32, Error<Self::Error>> { |
| 55 |
utils::type_index_unpacked(self, ty) |
| 56 |
} |
| 57 |
|
| 58 |
fn external_index(&mut self, kind: wasmparser::ExternalKind, index: u32) -> u32 { |
| 59 |
match kind { |
| 60 |
wasmparser::ExternalKind::Func => self.function_index(index), |
| 61 |
wasmparser::ExternalKind::Table => self.table_index(index), |
| 62 |
wasmparser::ExternalKind::Memory => self.memory_index(index), |
| 63 |
wasmparser::ExternalKind::Global => self.global_index(index), |
| 64 |
wasmparser::ExternalKind::Tag => self.tag_index(index), |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
fn abstract_heap_type( |
| 69 |
&mut self, |
| 70 |
value: wasmparser::AbstractHeapType, |
| 71 |
) -> crate::AbstractHeapType { |
| 72 |
utils::abstract_heap_type(self, value) |
| 73 |
} |
| 74 |
|
| 75 |
fn array_type( |
| 76 |
&mut self, |
| 77 |
array_ty: wasmparser::ArrayType, |
| 78 |
) -> Result<crate::ArrayType, Error<Self::Error>> { |
| 79 |
utils::array_type(self, array_ty) |
| 80 |
} |
| 81 |
|
| 82 |
fn block_type( |
| 83 |
&mut self, |
| 84 |
arg: wasmparser::BlockType, |
| 85 |
) -> Result<crate::BlockType, Error<Self::Error>> { |
| 86 |
utils::block_type(self, arg) |
| 87 |
} |
| 88 |
|
| 89 |
fn const_expr( |
| 90 |
&mut self, |
| 91 |
const_expr: wasmparser::ConstExpr, |
| 92 |
) -> Result<crate::ConstExpr, Error<Self::Error>> { |
| 93 |
utils::const_expr(self, const_expr) |
| 94 |
} |
| 95 |
|
| 96 |
fn catch(&mut self, arg: wasmparser::Catch) -> crate::Catch { |
| 97 |
utils::catch(self, arg) |
| 98 |
} |
| 99 |
|
| 100 |
fn composite_type( |
| 101 |
&mut self, |
| 102 |
composite_ty: wasmparser::CompositeType, |
| 103 |
) -> Result<crate::CompositeType, Error<Self::Error>> { |
| 104 |
utils::composite_type(self, composite_ty) |
| 105 |
} |
| 106 |
|
| 107 |
fn entity_type( |
| 108 |
&mut self, |
| 109 |
type_ref: wasmparser::TypeRef, |
| 110 |
) -> Result<crate::EntityType, Error<Self::Error>> { |
| 111 |
utils::entity_type(self, type_ref) |
| 112 |
} |
| 113 |
|
| 114 |
fn export_kind(&mut self, external_kind: wasmparser::ExternalKind) -> crate::ExportKind { |
| 115 |
utils::export_kind(self, external_kind) |
| 116 |
} |
| 117 |
|
| 118 |
fn field_type( |
| 119 |
&mut self, |
| 120 |
field_ty: wasmparser::FieldType, |
| 121 |
) -> Result<crate::FieldType, Error<Self::Error>> { |
| 122 |
utils::field_type(self, field_ty) |
| 123 |
} |
| 124 |
|
| 125 |
fn func_type( |
| 126 |
&mut self, |
| 127 |
func_ty: wasmparser::FuncType, |
| 128 |
) -> Result<crate::FuncType, Error<Self::Error>> { |
| 129 |
utils::func_type(self, func_ty) |
| 130 |
} |
| 131 |
|
| 132 |
fn cont_type( |
| 133 |
&mut self, |
| 134 |
cont_ty: wasmparser::ContType, |
| 135 |
) -> Result<crate::ContType, Error<Self::Error>> { |
| 136 |
utils::cont_type(self, cont_ty) |
| 137 |
} |
| 138 |
|
| 139 |
fn global_type( |
| 140 |
&mut self, |
| 141 |
global_ty: wasmparser::GlobalType, |
| 142 |
) -> Result<crate::GlobalType, Error<Self::Error>> { |
| 143 |
utils::global_type(self, global_ty) |
| 144 |
} |
| 145 |
|
| 146 |
fn handle(&mut self, on: wasmparser::Handle) -> crate::Handle { |
| 147 |
utils::handle(self, on) |
| 148 |
} |
| 149 |
|
| 150 |
fn heap_type( |
| 151 |
&mut self, |
| 152 |
heap_type: wasmparser::HeapType, |
| 153 |
) -> Result<crate::HeapType, Error<Self::Error>> { |
| 154 |
utils::heap_type(self, heap_type) |
| 155 |
} |
| 156 |
|
| 157 |
fn instruction<'a>( |
| 158 |
&mut self, |
| 159 |
arg: wasmparser::Operator<'a>, |
| 160 |
) -> Result<crate::Instruction<'a>, Error<Self::Error>> { |
| 161 |
utils::instruction(self, arg) |
| 162 |
} |
| 163 |
|
| 164 |
fn memory_type(&mut self, memory_ty: wasmparser::MemoryType) -> crate::MemoryType { |
| 165 |
utils::memory_type(self, memory_ty) |
| 166 |
} |
| 167 |
|
| 168 |
fn mem_arg(&mut self, arg: wasmparser::MemArg) -> crate::MemArg { |
| 169 |
utils::mem_arg(self, arg) |
| 170 |
} |
| 171 |
|
| 172 |
fn ordering(&mut self, arg: wasmparser::Ordering) -> crate::Ordering { |
| 173 |
utils::ordering(self, arg) |
| 174 |
} |
| 175 |
|
| 176 |
fn ref_type( |
| 177 |
&mut self, |
| 178 |
ref_type: wasmparser::RefType, |
| 179 |
) -> Result<crate::RefType, Error<Self::Error>> { |
| 180 |
utils::ref_type(self, ref_type) |
| 181 |
} |
| 182 |
|
| 183 |
fn storage_type( |
| 184 |
&mut self, |
| 185 |
storage_ty: wasmparser::StorageType, |
| 186 |
) -> Result<crate::StorageType, Error<Self::Error>> { |
| 187 |
utils::storage_type(self, storage_ty) |
| 188 |
} |
| 189 |
|
| 190 |
fn struct_type( |
| 191 |
&mut self, |
| 192 |
struct_ty: wasmparser::StructType, |
| 193 |
) -> Result<crate::StructType, Error<Self::Error>> { |
| 194 |
utils::struct_type(self, struct_ty) |
| 195 |
} |
| 196 |
|
| 197 |
fn sub_type( |
| 198 |
&mut self, |
| 199 |
sub_ty: wasmparser::SubType, |
| 200 |
) -> Result<crate::SubType, Error<Self::Error>> { |
| 201 |
utils::sub_type(self, sub_ty) |
| 202 |
} |
| 203 |
|
| 204 |
fn table_type( |
| 205 |
&mut self, |
| 206 |
table_ty: wasmparser::TableType, |
| 207 |
) -> Result<crate::TableType, Error<Self::Error>> { |
| 208 |
utils::table_type(self, table_ty) |
| 209 |
} |
| 210 |
|
| 211 |
fn tag_kind(&mut self, kind: wasmparser::TagKind) -> crate::TagKind { |
| 212 |
utils::tag_kind(self, kind) |
| 213 |
} |
| 214 |
|
| 215 |
fn tag_type(&mut self, tag_ty: wasmparser::TagType) -> crate::TagType { |
| 216 |
utils::tag_type(self, tag_ty) |
| 217 |
} |
| 218 |
|
| 219 |
fn val_type( |
| 220 |
&mut self, |
| 221 |
val_ty: wasmparser::ValType, |
| 222 |
) -> Result<crate::ValType, Error<Self::Error>> { |
| 223 |
utils::val_type(self, val_ty) |
| 224 |
} |
| 225 |
|
| 226 |
/// Parses the input `section` given from the `wasmparser` crate and |
| 227 |
/// adds the custom section to the `module`. |
| 228 |
fn parse_custom_section( |
| 229 |
&mut self, |
| 230 |
module: &mut crate::Module, |
| 231 |
section: wasmparser::CustomSectionReader<'_>, |
| 232 |
) -> Result<(), Error<Self::Error>> { |
| 233 |
utils::parse_custom_section(self, module, section) |
| 234 |
} |
| 235 |
|
| 236 |
/// Converts the input `section` given from the `wasmparser` crate into an |
| 237 |
/// encoded custom section. |
| 238 |
fn custom_section<'a>( |
| 239 |
&mut self, |
| 240 |
section: wasmparser::CustomSectionReader<'a>, |
| 241 |
) -> crate::CustomSection<'a> { |
| 242 |
utils::custom_section(self, section) |
| 243 |
} |
| 244 |
|
| 245 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 246 |
/// all the code to the `code` section. |
| 247 |
fn parse_code_section( |
| 248 |
&mut self, |
| 249 |
code: &mut crate::CodeSection, |
| 250 |
section: wasmparser::CodeSectionReader<'_>, |
| 251 |
) -> Result<(), Error<Self::Error>> { |
| 252 |
utils::parse_code_section(self, code, section) |
| 253 |
} |
| 254 |
|
| 255 |
/// Parses a single [`wasmparser::FunctionBody`] and adds it to the `code` section. |
| 256 |
fn parse_function_body( |
| 257 |
&mut self, |
| 258 |
code: &mut crate::CodeSection, |
| 259 |
func: wasmparser::FunctionBody<'_>, |
| 260 |
) -> Result<(), Error<Self::Error>> { |
| 261 |
utils::parse_function_body(self, code, func) |
| 262 |
} |
| 263 |
|
| 264 |
/// Create a new [`crate::Function`] by parsing the locals declarations from the |
| 265 |
/// provided [`wasmparser::FunctionBody`]. |
| 266 |
fn new_function_with_parsed_locals( |
| 267 |
&mut self, |
| 268 |
func: &wasmparser::FunctionBody<'_>, |
| 269 |
) -> Result<crate::Function, Error<Self::Error>> { |
| 270 |
utils::new_function_with_parsed_locals(self, func) |
| 271 |
} |
| 272 |
|
| 273 |
/// Parses a single instruction from `reader` and adds it to `function`. |
| 274 |
fn parse_instruction<'a>( |
| 275 |
&mut self, |
| 276 |
reader: &mut wasmparser::OperatorsReader<'a>, |
| 277 |
) -> Result<crate::Instruction<'a>, Error<Self::Error>> { |
| 278 |
utils::parse_instruction(self, reader) |
| 279 |
} |
| 280 |
|
| 281 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 282 |
/// all the data to the `data` section. |
| 283 |
fn parse_data_section( |
| 284 |
&mut self, |
| 285 |
data: &mut crate::DataSection, |
| 286 |
section: wasmparser::DataSectionReader<'_>, |
| 287 |
) -> Result<(), Error<Self::Error>> { |
| 288 |
utils::parse_data_section(self, data, section) |
| 289 |
} |
| 290 |
|
| 291 |
/// Parses a single [`wasmparser::Data`] and adds it to the `data` section. |
| 292 |
fn parse_data( |
| 293 |
&mut self, |
| 294 |
data: &mut crate::DataSection, |
| 295 |
datum: wasmparser::Data<'_>, |
| 296 |
) -> Result<(), Error<Self::Error>> { |
| 297 |
utils::parse_data(self, data, datum) |
| 298 |
} |
| 299 |
|
| 300 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 301 |
/// all the elements to the `element` section. |
| 302 |
fn parse_element_section( |
| 303 |
&mut self, |
| 304 |
elements: &mut crate::ElementSection, |
| 305 |
section: wasmparser::ElementSectionReader<'_>, |
| 306 |
) -> Result<(), Error<Self::Error>> { |
| 307 |
utils::parse_element_section(self, elements, section) |
| 308 |
} |
| 309 |
|
| 310 |
/// Parses the single [`wasmparser::Element`] provided and adds it to the |
| 311 |
/// `element` section. |
| 312 |
fn parse_element( |
| 313 |
&mut self, |
| 314 |
elements: &mut crate::ElementSection, |
| 315 |
element: wasmparser::Element<'_>, |
| 316 |
) -> Result<(), Error<Self::Error>> { |
| 317 |
utils::parse_element(self, elements, element) |
| 318 |
} |
| 319 |
|
| 320 |
fn element_items<'a>( |
| 321 |
&mut self, |
| 322 |
items: wasmparser::ElementItems<'a>, |
| 323 |
) -> Result<crate::Elements<'a>, Error<Self::Error>> { |
| 324 |
utils::element_items(self, items) |
| 325 |
} |
| 326 |
|
| 327 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 328 |
/// all the exports to the `exports` section. |
| 329 |
fn parse_export_section( |
| 330 |
&mut self, |
| 331 |
exports: &mut crate::ExportSection, |
| 332 |
section: wasmparser::ExportSectionReader<'_>, |
| 333 |
) -> Result<(), Error<Self::Error>> { |
| 334 |
utils::parse_export_section(self, exports, section) |
| 335 |
} |
| 336 |
|
| 337 |
/// Parses the single [`wasmparser::Export`] provided and adds it to the |
| 338 |
/// `exports` section. |
| 339 |
fn parse_export(&mut self, exports: &mut crate::ExportSection, export: wasmparser::Export<'_>) { |
| 340 |
utils::parse_export(self, exports, export) |
| 341 |
} |
| 342 |
|
| 343 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 344 |
/// all the functions to the `functions` section. |
| 345 |
fn parse_function_section( |
| 346 |
&mut self, |
| 347 |
functions: &mut crate::FunctionSection, |
| 348 |
section: wasmparser::FunctionSectionReader<'_>, |
| 349 |
) -> Result<(), Error<Self::Error>> { |
| 350 |
utils::parse_function_section(self, functions, section) |
| 351 |
} |
| 352 |
|
| 353 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 354 |
/// all the globals to the `globals` section. |
| 355 |
fn parse_global_section( |
| 356 |
&mut self, |
| 357 |
globals: &mut crate::GlobalSection, |
| 358 |
section: wasmparser::GlobalSectionReader<'_>, |
| 359 |
) -> Result<(), Error<Self::Error>> { |
| 360 |
utils::parse_global_section(self, globals, section) |
| 361 |
} |
| 362 |
|
| 363 |
/// Parses the single [`wasmparser::Global`] provided and adds it to the |
| 364 |
/// `globals` section. |
| 365 |
fn parse_global( |
| 366 |
&mut self, |
| 367 |
globals: &mut crate::GlobalSection, |
| 368 |
global: wasmparser::Global<'_>, |
| 369 |
) -> Result<(), Error<Self::Error>> { |
| 370 |
utils::parse_global(self, globals, global) |
| 371 |
} |
| 372 |
|
| 373 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 374 |
/// all the imports to the `import` section. |
| 375 |
fn parse_import_section( |
| 376 |
&mut self, |
| 377 |
imports: &mut crate::ImportSection, |
| 378 |
section: wasmparser::ImportSectionReader<'_>, |
| 379 |
) -> Result<(), Error<Self::Error>> { |
| 380 |
utils::parse_import_section(self, imports, section) |
| 381 |
} |
| 382 |
|
| 383 |
/// Parses the single [`wasmparser::Import`] provided and adds it to the |
| 384 |
/// `import` section. |
| 385 |
fn parse_import( |
| 386 |
&mut self, |
| 387 |
imports: &mut crate::ImportSection, |
| 388 |
import: wasmparser::Import<'_>, |
| 389 |
) -> Result<(), Error<Self::Error>> { |
| 390 |
utils::parse_import(self, imports, import) |
| 391 |
} |
| 392 |
|
| 393 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 394 |
/// all the memories to the `memories` section. |
| 395 |
fn parse_memory_section( |
| 396 |
&mut self, |
| 397 |
memories: &mut crate::MemorySection, |
| 398 |
section: wasmparser::MemorySectionReader<'_>, |
| 399 |
) -> Result<(), Error<Self::Error>> { |
| 400 |
utils::parse_memory_section(self, memories, section) |
| 401 |
} |
| 402 |
|
| 403 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 404 |
/// all the tables to the `tables` section. |
| 405 |
fn parse_table_section( |
| 406 |
&mut self, |
| 407 |
tables: &mut crate::TableSection, |
| 408 |
section: wasmparser::TableSectionReader<'_>, |
| 409 |
) -> Result<(), Error<Self::Error>> { |
| 410 |
utils::parse_table_section(self, tables, section) |
| 411 |
} |
| 412 |
|
| 413 |
/// Parses a single [`wasmparser::Table`] and adds it to the `tables` section. |
| 414 |
fn parse_table( |
| 415 |
&mut self, |
| 416 |
tables: &mut crate::TableSection, |
| 417 |
table: wasmparser::Table<'_>, |
| 418 |
) -> Result<(), Error<Self::Error>> { |
| 419 |
utils::parse_table(self, tables, table) |
| 420 |
} |
| 421 |
|
| 422 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 423 |
/// all the tags to the `tags` section. |
| 424 |
fn parse_tag_section( |
| 425 |
&mut self, |
| 426 |
tags: &mut crate::TagSection, |
| 427 |
section: wasmparser::TagSectionReader<'_>, |
| 428 |
) -> Result<(), Error<Self::Error>> { |
| 429 |
utils::parse_tag_section(self, tags, section) |
| 430 |
} |
| 431 |
|
| 432 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 433 |
/// all the types to the `types` section. |
| 434 |
fn parse_type_section( |
| 435 |
&mut self, |
| 436 |
types: &mut crate::TypeSection, |
| 437 |
section: wasmparser::TypeSectionReader<'_>, |
| 438 |
) -> Result<(), Error<Self::Error>> { |
| 439 |
utils::parse_type_section(self, types, section) |
| 440 |
} |
| 441 |
|
| 442 |
/// Parses a single [`wasmparser::RecGroup`] and adds it to the `types` section. |
| 443 |
fn parse_recursive_type_group( |
| 444 |
&mut self, |
| 445 |
encoder: CoreTypeEncoder, |
| 446 |
rec_group: wasmparser::RecGroup, |
| 447 |
) -> Result<(), Error<Self::Error>> { |
| 448 |
utils::parse_recursive_type_group(self, encoder, rec_group) |
| 449 |
} |
| 450 |
|
| 451 |
fn parse_unknown_section( |
| 452 |
&mut self, |
| 453 |
module: &mut crate::Module, |
| 454 |
id: u8, |
| 455 |
contents: &[u8], |
| 456 |
) -> Result<(), Error<Self::Error>> { |
| 457 |
utils::parse_unknown_section(self, module, id, contents) |
| 458 |
} |
| 459 |
|
| 460 |
/// A hook method that is called inside [`Reencode::parse_core_module`] |
| 461 |
/// before and after every non-custom core wasm section. |
| 462 |
/// |
| 463 |
/// This method can be used to insert new custom sections in between those |
| 464 |
/// sections, or to detect when a non-custom section is missing and insert |
| 465 |
/// it in the [proper order]. |
| 466 |
/// |
| 467 |
/// The `after` parameter is `None` iff the hook is called before the first |
| 468 |
/// non-custom section, and `Some(s)` afterwards, where `s` is the |
| 469 |
/// [`SectionId`] of the previous non-custom section. |
| 470 |
/// |
| 471 |
/// The `before` parameter is `None` iff the hook is called after the last |
| 472 |
/// non-custom section, and `Some(s)` beforehand, where `s` is the |
| 473 |
/// [`SectionId`] of the following non-custom section. |
| 474 |
/// |
| 475 |
/// [proper order]: https://webassembly.github.io/spec/core/binary/modules.html#binary-module |
| 476 |
/// [`SectionId`]: crate::SectionId |
| 477 |
fn intersperse_section_hook( |
| 478 |
&mut self, |
| 479 |
module: &mut crate::Module, |
| 480 |
after: Option<crate::SectionId>, |
| 481 |
before: Option<crate::SectionId>, |
| 482 |
) -> Result<(), Error<Self::Error>> { |
| 483 |
utils::intersperse_section_hook(self, module, after, before) |
| 484 |
} |
| 485 |
|
| 486 |
fn parse_core_module( |
| 487 |
&mut self, |
| 488 |
module: &mut crate::Module, |
| 489 |
parser: wasmparser::Parser, |
| 490 |
data: &[u8], |
| 491 |
) -> Result<(), Error<Self::Error>> { |
| 492 |
utils::parse_core_module(self, module, parser, data) |
| 493 |
} |
| 494 |
|
| 495 |
fn custom_name_section( |
| 496 |
&mut self, |
| 497 |
section: wasmparser::NameSectionReader<'_>, |
| 498 |
) -> Result<crate::NameSection, Error<Self::Error>> { |
| 499 |
utils::custom_name_section(self, section) |
| 500 |
} |
| 501 |
|
| 502 |
fn parse_custom_name_subsection( |
| 503 |
&mut self, |
| 504 |
names: &mut crate::NameSection, |
| 505 |
section: wasmparser::Name<'_>, |
| 506 |
) -> Result<(), Error<Self::Error>> { |
| 507 |
utils::parse_custom_name_subsection(self, names, section) |
| 508 |
} |
| 509 |
|
| 510 |
fn data_count(&mut self, count: u32) -> u32 { |
| 511 |
count |
| 512 |
} |
| 513 |
|
| 514 |
fn start_section(&mut self, start: u32) -> u32 { |
| 515 |
self.function_index(start) |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
/// An error when re-encoding from `wasmparser` to `wasm-encoder`. |
| 520 |
#[derive(Debug)] |
| 521 |
pub enum Error<E = Infallible> { |
| 522 |
/// There was a type reference that was canonicalized and no longer |
| 523 |
/// references an index into a module's types space, so we cannot encode it |
| 524 |
/// into a Wasm binary again. |
| 525 |
CanonicalizedHeapTypeReference, |
| 526 |
/// The const expression is invalid: not actually constant or something like |
| 527 |
/// that. |
| 528 |
InvalidConstExpr, |
| 529 |
/// The code section size listed was not valid for the wasm binary provided. |
| 530 |
InvalidCodeSectionSize, |
| 531 |
/// There was a section that does not belong into a core wasm module. |
| 532 |
UnexpectedNonCoreModuleSection, |
| 533 |
/// There was a section that does not belong into a compoennt module. |
| 534 |
UnexpectedNonComponentSection, |
| 535 |
/// A core type definition was found in a component that's not supported. |
| 536 |
UnsupportedCoreTypeInComponent, |
| 537 |
/// There was an error when parsing. |
| 538 |
ParseError(wasmparser::BinaryReaderError), |
| 539 |
/// There was a user-defined error when re-encoding. |
| 540 |
UserError(E), |
| 541 |
} |
| 542 |
|
| 543 |
impl<E> From<wasmparser::BinaryReaderError> for Error<E> { |
| 544 |
fn from(err: wasmparser::BinaryReaderError) -> Self { |
| 545 |
Self::ParseError(err) |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
impl<E: std::fmt::Display> std::fmt::Display for Error<E> { |
| 550 |
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 551 |
match self { |
| 552 |
Self::ParseError(_e) => { |
| 553 |
write!(fmt, "There was an error when parsing") |
| 554 |
} |
| 555 |
Self::UserError(e) => write!(fmt, "{e}"), |
| 556 |
Self::InvalidConstExpr => write!(fmt, "The const expression was invalid"), |
| 557 |
Self::UnexpectedNonCoreModuleSection => write!( |
| 558 |
fmt, |
| 559 |
"There was a section that does not belong into a core wasm module" |
| 560 |
), |
| 561 |
Self::UnexpectedNonComponentSection => write!( |
| 562 |
fmt, |
| 563 |
"There was a section that does not belong into a component" |
| 564 |
), |
| 565 |
Self::CanonicalizedHeapTypeReference => write!( |
| 566 |
fmt, |
| 567 |
"There was a canonicalized heap type reference without type index information" |
| 568 |
), |
| 569 |
Self::UnsupportedCoreTypeInComponent => { |
| 570 |
fmt.write_str("unsupported core type in a component") |
| 571 |
} |
| 572 |
Self::InvalidCodeSectionSize => fmt.write_str("invalid code section size"), |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
impl<E: 'static + std::error::Error> std::error::Error for Error<E> { |
| 578 |
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 579 |
match self { |
| 580 |
Self::ParseError(e) => Some(e), |
| 581 |
Self::UserError(e) => Some(e), |
| 582 |
Self::InvalidConstExpr |
| 583 |
| Self::CanonicalizedHeapTypeReference |
| 584 |
| Self::UnexpectedNonCoreModuleSection |
| 585 |
| Self::UnexpectedNonComponentSection |
| 586 |
| Self::UnsupportedCoreTypeInComponent |
| 587 |
| Self::InvalidCodeSectionSize => None, |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/// Reencodes `wasmparser` into `wasm-encoder` so that the encoded wasm is |
| 593 |
/// identical to the input and can be parsed and encoded again. |
| 594 |
#[derive(Debug)] |
| 595 |
pub struct RoundtripReencoder; |
| 596 |
|
| 597 |
impl Reencode for RoundtripReencoder { |
| 598 |
type Error = Infallible; |
| 599 |
} |
| 600 |
|
| 601 |
#[allow(missing_docs)] // FIXME |
| 602 |
pub mod utils { |
| 603 |
use super::{Error, Reencode}; |
| 604 |
use crate::{CoreTypeEncoder, Encode}; |
| 605 |
use std::ops::Range; |
| 606 |
|
| 607 |
pub fn parse_core_module<T: ?Sized + Reencode>( |
| 608 |
reencoder: &mut T, |
| 609 |
module: &mut crate::Module, |
| 610 |
parser: wasmparser::Parser, |
| 611 |
data: &[u8], |
| 612 |
) -> Result<(), Error<T::Error>> { |
| 613 |
fn handle_intersperse_section_hook<T: ?Sized + Reencode>( |
| 614 |
reencoder: &mut T, |
| 615 |
module: &mut crate::Module, |
| 616 |
last_section: &mut Option<crate::SectionId>, |
| 617 |
next_section: Option<crate::SectionId>, |
| 618 |
) -> Result<(), Error<T::Error>> { |
| 619 |
let after = std::mem::replace(last_section, next_section); |
| 620 |
let before = next_section; |
| 621 |
reencoder.intersperse_section_hook(module, after, before) |
| 622 |
} |
| 623 |
|
| 624 |
// Convert from `range` to a byte range within `data` while |
| 625 |
// accounting for various offsets. Then create a |
| 626 |
// `CodeSectionReader` (which notably the payload does not |
| 627 |
// give us here) and recurse with that. This means that |
| 628 |
// users overridding `parse_code_section` always get that |
| 629 |
// function called. |
| 630 |
let orig_offset = parser.offset() as usize; |
| 631 |
let get_original_section = |range: Range<usize>| { |
| 632 |
data.get(range.start - orig_offset..range.end - orig_offset) |
| 633 |
.ok_or(Error::InvalidCodeSectionSize) |
| 634 |
}; |
| 635 |
let mut last_section = None; |
| 636 |
|
| 637 |
for section in parser.parse_all(data) { |
| 638 |
match section? { |
| 639 |
wasmparser::Payload::Version { |
| 640 |
encoding: wasmparser::Encoding::Module, |
| 641 |
.. |
| 642 |
} => (), |
| 643 |
wasmparser::Payload::Version { .. } => { |
| 644 |
return Err(Error::UnexpectedNonCoreModuleSection) |
| 645 |
} |
| 646 |
wasmparser::Payload::TypeSection(section) => { |
| 647 |
handle_intersperse_section_hook( |
| 648 |
reencoder, |
| 649 |
module, |
| 650 |
&mut last_section, |
| 651 |
Some(crate::SectionId::Type), |
| 652 |
)?; |
| 653 |
let mut types = crate::TypeSection::new(); |
| 654 |
reencoder.parse_type_section(&mut types, section)?; |
| 655 |
module.section(&types); |
| 656 |
} |
| 657 |
wasmparser::Payload::ImportSection(section) => { |
| 658 |
handle_intersperse_section_hook( |
| 659 |
reencoder, |
| 660 |
module, |
| 661 |
&mut last_section, |
| 662 |
Some(crate::SectionId::Import), |
| 663 |
)?; |
| 664 |
let mut imports = crate::ImportSection::new(); |
| 665 |
reencoder.parse_import_section(&mut imports, section)?; |
| 666 |
module.section(&imports); |
| 667 |
} |
| 668 |
wasmparser::Payload::FunctionSection(section) => { |
| 669 |
handle_intersperse_section_hook( |
| 670 |
reencoder, |
| 671 |
module, |
| 672 |
&mut last_section, |
| 673 |
Some(crate::SectionId::Function), |
| 674 |
)?; |
| 675 |
let mut functions = crate::FunctionSection::new(); |
| 676 |
reencoder.parse_function_section(&mut functions, section)?; |
| 677 |
module.section(&functions); |
| 678 |
} |
| 679 |
wasmparser::Payload::TableSection(section) => { |
| 680 |
handle_intersperse_section_hook( |
| 681 |
reencoder, |
| 682 |
module, |
| 683 |
&mut last_section, |
| 684 |
Some(crate::SectionId::Table), |
| 685 |
)?; |
| 686 |
let mut tables = crate::TableSection::new(); |
| 687 |
reencoder.parse_table_section(&mut tables, section)?; |
| 688 |
module.section(&tables); |
| 689 |
} |
| 690 |
wasmparser::Payload::MemorySection(section) => { |
| 691 |
handle_intersperse_section_hook( |
| 692 |
reencoder, |
| 693 |
module, |
| 694 |
&mut last_section, |
| 695 |
Some(crate::SectionId::Memory), |
| 696 |
)?; |
| 697 |
let mut memories = crate::MemorySection::new(); |
| 698 |
reencoder.parse_memory_section(&mut memories, section)?; |
| 699 |
module.section(&memories); |
| 700 |
} |
| 701 |
wasmparser::Payload::TagSection(section) => { |
| 702 |
handle_intersperse_section_hook( |
| 703 |
reencoder, |
| 704 |
module, |
| 705 |
&mut last_section, |
| 706 |
Some(crate::SectionId::Tag), |
| 707 |
)?; |
| 708 |
let mut tags = crate::TagSection::new(); |
| 709 |
reencoder.parse_tag_section(&mut tags, section)?; |
| 710 |
module.section(&tags); |
| 711 |
} |
| 712 |
wasmparser::Payload::GlobalSection(section) => { |
| 713 |
handle_intersperse_section_hook( |
| 714 |
reencoder, |
| 715 |
module, |
| 716 |
&mut last_section, |
| 717 |
Some(crate::SectionId::Global), |
| 718 |
)?; |
| 719 |
let mut globals = crate::GlobalSection::new(); |
| 720 |
reencoder.parse_global_section(&mut globals, section)?; |
| 721 |
module.section(&globals); |
| 722 |
} |
| 723 |
wasmparser::Payload::ExportSection(section) => { |
| 724 |
handle_intersperse_section_hook( |
| 725 |
reencoder, |
| 726 |
module, |
| 727 |
&mut last_section, |
| 728 |
Some(crate::SectionId::Export), |
| 729 |
)?; |
| 730 |
let mut exports = crate::ExportSection::new(); |
| 731 |
reencoder.parse_export_section(&mut exports, section)?; |
| 732 |
module.section(&exports); |
| 733 |
} |
| 734 |
wasmparser::Payload::StartSection { func, .. } => { |
| 735 |
handle_intersperse_section_hook( |
| 736 |
reencoder, |
| 737 |
module, |
| 738 |
&mut last_section, |
| 739 |
Some(crate::SectionId::Start), |
| 740 |
)?; |
| 741 |
module.section(&crate::StartSection { |
| 742 |
function_index: reencoder.start_section(func), |
| 743 |
}); |
| 744 |
} |
| 745 |
wasmparser::Payload::ElementSection(section) => { |
| 746 |
handle_intersperse_section_hook( |
| 747 |
reencoder, |
| 748 |
module, |
| 749 |
&mut last_section, |
| 750 |
Some(crate::SectionId::Element), |
| 751 |
)?; |
| 752 |
let mut elements = crate::ElementSection::new(); |
| 753 |
reencoder.parse_element_section(&mut elements, section)?; |
| 754 |
module.section(&elements); |
| 755 |
} |
| 756 |
wasmparser::Payload::DataCountSection { count, .. } => { |
| 757 |
handle_intersperse_section_hook( |
| 758 |
reencoder, |
| 759 |
module, |
| 760 |
&mut last_section, |
| 761 |
Some(crate::SectionId::DataCount), |
| 762 |
)?; |
| 763 |
let count = reencoder.data_count(count); |
| 764 |
module.section(&crate::DataCountSection { count }); |
| 765 |
} |
| 766 |
wasmparser::Payload::DataSection(section) => { |
| 767 |
handle_intersperse_section_hook( |
| 768 |
reencoder, |
| 769 |
module, |
| 770 |
&mut last_section, |
| 771 |
Some(crate::SectionId::Data), |
| 772 |
)?; |
| 773 |
let mut data = crate::DataSection::new(); |
| 774 |
reencoder.parse_data_section(&mut data, section)?; |
| 775 |
module.section(&data); |
| 776 |
} |
| 777 |
wasmparser::Payload::CodeSectionStart { range, .. } => { |
| 778 |
handle_intersperse_section_hook( |
| 779 |
reencoder, |
| 780 |
module, |
| 781 |
&mut last_section, |
| 782 |
Some(crate::SectionId::Code), |
| 783 |
)?; |
| 784 |
let mut codes = crate::CodeSection::new(); |
| 785 |
|
| 786 |
// Convert from `range` to a byte range within `data` while |
| 787 |
// accounting for various offsets. Then create a |
| 788 |
// `CodeSectionReader` (which notably the payload does not |
| 789 |
// give us here) and recurse with that. This means that |
| 790 |
// users overridding `parse_code_section` always get that |
| 791 |
// function called. |
| 792 |
let section = get_original_section(range.clone())?; |
| 793 |
let reader = wasmparser::BinaryReader::new(section, range.start); |
| 794 |
let section = wasmparser::CodeSectionReader::new(reader)?; |
| 795 |
reencoder.parse_code_section(&mut codes, section)?; |
| 796 |
module.section(&codes); |
| 797 |
} |
| 798 |
|
| 799 |
// Parsing of code section entries (function bodies) happens |
| 800 |
// above during the handling of the code section. That means |
| 801 |
// that we just skip all these payloads. |
| 802 |
wasmparser::Payload::CodeSectionEntry(_) => {} |
| 803 |
|
| 804 |
#[cfg(feature = "component-model")] |
| 805 |
wasmparser::Payload::ModuleSection { .. } |
| 806 |
| wasmparser::Payload::InstanceSection(_) |
| 807 |
| wasmparser::Payload::CoreTypeSection(_) |
| 808 |
| wasmparser::Payload::ComponentSection { .. } |
| 809 |
| wasmparser::Payload::ComponentInstanceSection(_) |
| 810 |
| wasmparser::Payload::ComponentAliasSection(_) |
| 811 |
| wasmparser::Payload::ComponentTypeSection(_) |
| 812 |
| wasmparser::Payload::ComponentCanonicalSection(_) |
| 813 |
| wasmparser::Payload::ComponentStartSection { .. } |
| 814 |
| wasmparser::Payload::ComponentImportSection(_) |
| 815 |
| wasmparser::Payload::ComponentExportSection(_) => { |
| 816 |
return Err(Error::UnexpectedNonCoreModuleSection) |
| 817 |
} |
| 818 |
wasmparser::Payload::CustomSection(section) => { |
| 819 |
reencoder.parse_custom_section(module, section)?; |
| 820 |
} |
| 821 |
wasmparser::Payload::End(_) => { |
| 822 |
handle_intersperse_section_hook(reencoder, module, &mut last_section, None)?; |
| 823 |
} |
| 824 |
|
| 825 |
other => match other.as_section() { |
| 826 |
Some((id, range)) => { |
| 827 |
let section = get_original_section(range)?; |
| 828 |
reencoder.parse_unknown_section(module, id, section)?; |
| 829 |
} |
| 830 |
None => unreachable!(), |
| 831 |
}, |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
Ok(()) |
| 836 |
} |
| 837 |
|
| 838 |
/// A hook method that is called inside [`Reencode::parse_core_module`] |
| 839 |
/// before and after every non-custom core wasm section. |
| 840 |
/// |
| 841 |
/// This method can be used to insert new custom sections in between those |
| 842 |
/// sections, or to detect when a non-custom section is missing and insert |
| 843 |
/// it in the [proper order]. |
| 844 |
/// |
| 845 |
/// The `after` parameter is `None` iff the hook is called before the first |
| 846 |
/// non-custom section, and `Some(s)` afterwards, where `s` is the |
| 847 |
/// [`SectionId`] of the previous non-custom section. |
| 848 |
/// |
| 849 |
/// The `before` parameter is `None` iff the hook is called after the last |
| 850 |
/// non-custom section, and `Some(s)` beforehand, where `s` is the |
| 851 |
/// [`SectionId`] of the following non-custom section. |
| 852 |
/// |
| 853 |
/// [proper order]: https://webassembly.github.io/spec/core/binary/modules.html#binary-module |
| 854 |
/// [`SectionId`]: crate::SectionId |
| 855 |
pub fn intersperse_section_hook<T: ?Sized + Reencode>( |
| 856 |
_reencoder: &mut T, |
| 857 |
_module: &mut crate::Module, |
| 858 |
_after: Option<crate::SectionId>, |
| 859 |
_before: Option<crate::SectionId>, |
| 860 |
) -> Result<(), Error<T::Error>> { |
| 861 |
Ok(()) |
| 862 |
} |
| 863 |
|
| 864 |
pub fn memory_index<T: ?Sized + Reencode>(_reencoder: &mut T, memory: u32) -> u32 { |
| 865 |
memory |
| 866 |
} |
| 867 |
|
| 868 |
pub fn mem_arg<T: ?Sized + Reencode>( |
| 869 |
reencoder: &mut T, |
| 870 |
arg: wasmparser::MemArg, |
| 871 |
) -> crate::MemArg { |
| 872 |
crate::MemArg { |
| 873 |
offset: arg.offset, |
| 874 |
align: arg.align.into(), |
| 875 |
memory_index: reencoder.memory_index(arg.memory), |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
pub fn ordering<T: ?Sized + Reencode>( |
| 880 |
_reencoder: &mut T, |
| 881 |
arg: wasmparser::Ordering, |
| 882 |
) -> crate::Ordering { |
| 883 |
match arg { |
| 884 |
wasmparser::Ordering::SeqCst => crate::Ordering::SeqCst, |
| 885 |
wasmparser::Ordering::AcqRel => crate::Ordering::AcqRel, |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
pub fn function_index<T: ?Sized + Reencode>(_reencoder: &mut T, func: u32) -> u32 { |
| 890 |
func |
| 891 |
} |
| 892 |
|
| 893 |
pub fn tag_index<T: ?Sized + Reencode>(_reencoder: &mut T, tag: u32) -> u32 { |
| 894 |
tag |
| 895 |
} |
| 896 |
|
| 897 |
pub fn catch<T: ?Sized + Reencode>(reencoder: &mut T, arg: wasmparser::Catch) -> crate::Catch { |
| 898 |
match arg { |
| 899 |
wasmparser::Catch::One { tag, label } => crate::Catch::One { |
| 900 |
tag: reencoder.tag_index(tag), |
| 901 |
label, |
| 902 |
}, |
| 903 |
wasmparser::Catch::OneRef { tag, label } => crate::Catch::OneRef { |
| 904 |
tag: reencoder.tag_index(tag), |
| 905 |
label, |
| 906 |
}, |
| 907 |
wasmparser::Catch::All { label } => crate::Catch::All { label }, |
| 908 |
wasmparser::Catch::AllRef { label } => crate::Catch::AllRef { label }, |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
pub fn handle<T: ?Sized + Reencode>( |
| 913 |
reencoder: &mut T, |
| 914 |
arg: wasmparser::Handle, |
| 915 |
) -> crate::Handle { |
| 916 |
match arg { |
| 917 |
wasmparser::Handle::OnLabel { tag, label } => crate::Handle::OnLabel { |
| 918 |
tag: reencoder.tag_index(tag), |
| 919 |
label, |
| 920 |
}, |
| 921 |
wasmparser::Handle::OnSwitch { tag } => crate::Handle::OnSwitch { |
| 922 |
tag: reencoder.tag_index(tag), |
| 923 |
}, |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
/// Parses the input `section` given from the `wasmparser` crate and |
| 928 |
/// adds the custom section to the `module`. |
| 929 |
pub fn parse_custom_section<T: ?Sized + Reencode>( |
| 930 |
reencoder: &mut T, |
| 931 |
module: &mut crate::Module, |
| 932 |
section: wasmparser::CustomSectionReader<'_>, |
| 933 |
) -> Result<(), Error<T::Error>> { |
| 934 |
match section.as_known() { |
| 935 |
wasmparser::KnownCustom::Name(name) => { |
| 936 |
module.section(&reencoder.custom_name_section(name)?); |
| 937 |
} |
| 938 |
_ => { |
| 939 |
module.section(&reencoder.custom_section(section)); |
| 940 |
} |
| 941 |
} |
| 942 |
Ok(()) |
| 943 |
} |
| 944 |
|
| 945 |
/// Converts the input `section` given from the `wasmparser` crate into an |
| 946 |
/// encoded custom section. |
| 947 |
pub fn custom_section<'a, T: ?Sized + Reencode>( |
| 948 |
_reencoder: &mut T, |
| 949 |
section: wasmparser::CustomSectionReader<'a>, |
| 950 |
) -> crate::CustomSection<'a> { |
| 951 |
crate::CustomSection { |
| 952 |
data: section.data().into(), |
| 953 |
name: section.name().into(), |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
pub fn export_kind<T: ?Sized + Reencode>( |
| 958 |
_reencoder: &mut T, |
| 959 |
external_kind: wasmparser::ExternalKind, |
| 960 |
) -> crate::ExportKind { |
| 961 |
match external_kind { |
| 962 |
wasmparser::ExternalKind::Func => crate::ExportKind::Func, |
| 963 |
wasmparser::ExternalKind::Table => crate::ExportKind::Table, |
| 964 |
wasmparser::ExternalKind::Memory => crate::ExportKind::Memory, |
| 965 |
wasmparser::ExternalKind::Global => crate::ExportKind::Global, |
| 966 |
wasmparser::ExternalKind::Tag => crate::ExportKind::Tag, |
| 967 |
} |
| 968 |
} |
| 969 |
|
| 970 |
pub fn memory_type<T: ?Sized + Reencode>( |
| 971 |
_reencoder: &mut T, |
| 972 |
memory_ty: wasmparser::MemoryType, |
| 973 |
) -> crate::MemoryType { |
| 974 |
crate::MemoryType { |
| 975 |
minimum: memory_ty.initial, |
| 976 |
maximum: memory_ty.maximum, |
| 977 |
memory64: memory_ty.memory64, |
| 978 |
shared: memory_ty.shared, |
| 979 |
page_size_log2: memory_ty.page_size_log2, |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
pub fn tag_kind<T: ?Sized + Reencode>( |
| 984 |
_reencoder: &mut T, |
| 985 |
kind: wasmparser::TagKind, |
| 986 |
) -> crate::TagKind { |
| 987 |
match kind { |
| 988 |
wasmparser::TagKind::Exception => crate::TagKind::Exception, |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
pub fn type_index<T: ?Sized + Reencode>(_reencoder: &mut T, ty: u32) -> u32 { |
| 993 |
ty |
| 994 |
} |
| 995 |
|
| 996 |
pub fn type_index_unpacked<T: ?Sized + Reencode>( |
| 997 |
reencoder: &mut T, |
| 998 |
ty: wasmparser::UnpackedIndex, |
| 999 |
) -> Result<u32, Error<T::Error>> { |
| 1000 |
ty.as_module_index() |
| 1001 |
.map(|ty| reencoder.type_index(ty)) |
| 1002 |
.ok_or(Error::CanonicalizedHeapTypeReference) |
| 1003 |
} |
| 1004 |
|
| 1005 |
pub fn tag_type<T: ?Sized + Reencode>( |
| 1006 |
reencoder: &mut T, |
| 1007 |
tag_ty: wasmparser::TagType, |
| 1008 |
) -> crate::TagType { |
| 1009 |
crate::TagType { |
| 1010 |
kind: reencoder.tag_kind(tag_ty.kind), |
| 1011 |
func_type_idx: reencoder.type_index(tag_ty.func_type_idx), |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
pub fn abstract_heap_type<T: ?Sized + Reencode>( |
| 1016 |
_reencoder: &mut T, |
| 1017 |
value: wasmparser::AbstractHeapType, |
| 1018 |
) -> crate::AbstractHeapType { |
| 1019 |
use wasmparser::AbstractHeapType::*; |
| 1020 |
match value { |
| 1021 |
Func => crate::AbstractHeapType::Func, |
| 1022 |
Extern => crate::AbstractHeapType::Extern, |
| 1023 |
Any => crate::AbstractHeapType::Any, |
| 1024 |
None => crate::AbstractHeapType::None, |
| 1025 |
NoExtern => crate::AbstractHeapType::NoExtern, |
| 1026 |
NoFunc => crate::AbstractHeapType::NoFunc, |
| 1027 |
Eq => crate::AbstractHeapType::Eq, |
| 1028 |
Struct => crate::AbstractHeapType::Struct, |
| 1029 |
Array => crate::AbstractHeapType::Array, |
| 1030 |
I31 => crate::AbstractHeapType::I31, |
| 1031 |
Exn => crate::AbstractHeapType::Exn, |
| 1032 |
NoExn => crate::AbstractHeapType::NoExn, |
| 1033 |
Cont => crate::AbstractHeapType::Cont, |
| 1034 |
NoCont => crate::AbstractHeapType::NoCont, |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1039 |
/// all the types to the `types` section. |
| 1040 |
pub fn parse_type_section<T: ?Sized + Reencode>( |
| 1041 |
reencoder: &mut T, |
| 1042 |
types: &mut crate::TypeSection, |
| 1043 |
section: wasmparser::TypeSectionReader<'_>, |
| 1044 |
) -> Result<(), Error<T::Error>> { |
| 1045 |
for rec_group in section { |
| 1046 |
reencoder.parse_recursive_type_group(types.ty(), rec_group?)?; |
| 1047 |
} |
| 1048 |
Ok(()) |
| 1049 |
} |
| 1050 |
|
| 1051 |
/// Parses a single [`wasmparser::RecGroup`] and adds it to the `types` section. |
| 1052 |
pub fn parse_recursive_type_group<T: ?Sized + Reencode>( |
| 1053 |
reencoder: &mut T, |
| 1054 |
encoder: CoreTypeEncoder, |
| 1055 |
rec_group: wasmparser::RecGroup, |
| 1056 |
) -> Result<(), Error<T::Error>> { |
| 1057 |
if rec_group.is_explicit_rec_group() { |
| 1058 |
let subtypes = rec_group |
| 1059 |
.into_types() |
| 1060 |
.map(|t| reencoder.sub_type(t)) |
| 1061 |
.collect::<Result<Vec<_>, _>>()?; |
| 1062 |
encoder.rec(subtypes); |
| 1063 |
} else { |
| 1064 |
let ty = rec_group.into_types().next().unwrap(); |
| 1065 |
encoder.subtype(&reencoder.sub_type(ty)?); |
| 1066 |
} |
| 1067 |
Ok(()) |
| 1068 |
} |
| 1069 |
|
| 1070 |
pub fn sub_type<T: ?Sized + Reencode>( |
| 1071 |
reencoder: &mut T, |
| 1072 |
sub_ty: wasmparser::SubType, |
| 1073 |
) -> Result<crate::SubType, Error<T::Error>> { |
| 1074 |
Ok(crate::SubType { |
| 1075 |
is_final: sub_ty.is_final, |
| 1076 |
supertype_idx: sub_ty |
| 1077 |
.supertype_idx |
| 1078 |
.map(|i| reencoder.type_index_unpacked(i.unpack())) |
| 1079 |
.transpose()?, |
| 1080 |
composite_type: reencoder.composite_type(sub_ty.composite_type)?, |
| 1081 |
}) |
| 1082 |
} |
| 1083 |
|
| 1084 |
pub fn composite_type<T: ?Sized + Reencode>( |
| 1085 |
reencoder: &mut T, |
| 1086 |
composite_ty: wasmparser::CompositeType, |
| 1087 |
) -> Result<crate::CompositeType, Error<T::Error>> { |
| 1088 |
let inner = match composite_ty.inner { |
| 1089 |
wasmparser::CompositeInnerType::Func(f) => { |
| 1090 |
crate::CompositeInnerType::Func(reencoder.func_type(f)?) |
| 1091 |
} |
| 1092 |
wasmparser::CompositeInnerType::Array(a) => { |
| 1093 |
crate::CompositeInnerType::Array(reencoder.array_type(a)?) |
| 1094 |
} |
| 1095 |
wasmparser::CompositeInnerType::Struct(s) => { |
| 1096 |
crate::CompositeInnerType::Struct(reencoder.struct_type(s)?) |
| 1097 |
} |
| 1098 |
wasmparser::CompositeInnerType::Cont(c) => { |
| 1099 |
crate::CompositeInnerType::Cont(reencoder.cont_type(c)?) |
| 1100 |
} |
| 1101 |
}; |
| 1102 |
Ok(crate::CompositeType { |
| 1103 |
inner, |
| 1104 |
shared: composite_ty.shared, |
| 1105 |
}) |
| 1106 |
} |
| 1107 |
|
| 1108 |
pub fn func_type<T: ?Sized + Reencode>( |
| 1109 |
reencoder: &mut T, |
| 1110 |
func_ty: wasmparser::FuncType, |
| 1111 |
) -> Result<crate::FuncType, Error<T::Error>> { |
| 1112 |
let mut buf = Vec::with_capacity(func_ty.params().len() + func_ty.results().len()); |
| 1113 |
for ty in func_ty.params().iter().chain(func_ty.results()).copied() { |
| 1114 |
buf.push(reencoder.val_type(ty)?); |
| 1115 |
} |
| 1116 |
Ok(crate::FuncType::from_parts( |
| 1117 |
buf.into(), |
| 1118 |
func_ty.params().len(), |
| 1119 |
)) |
| 1120 |
} |
| 1121 |
|
| 1122 |
pub fn array_type<T: ?Sized + Reencode>( |
| 1123 |
reencoder: &mut T, |
| 1124 |
array_ty: wasmparser::ArrayType, |
| 1125 |
) -> Result<crate::ArrayType, Error<T::Error>> { |
| 1126 |
Ok(crate::ArrayType(reencoder.field_type(array_ty.0)?)) |
| 1127 |
} |
| 1128 |
|
| 1129 |
pub fn struct_type<T: ?Sized + Reencode>( |
| 1130 |
reencoder: &mut T, |
| 1131 |
struct_ty: wasmparser::StructType, |
| 1132 |
) -> Result<crate::StructType, Error<T::Error>> { |
| 1133 |
Ok(crate::StructType { |
| 1134 |
fields: struct_ty |
| 1135 |
.fields |
| 1136 |
.iter() |
| 1137 |
.map(|field_ty| reencoder.field_type(*field_ty)) |
| 1138 |
.collect::<Result<_, _>>()?, |
| 1139 |
}) |
| 1140 |
} |
| 1141 |
|
| 1142 |
pub fn field_type<T: ?Sized + Reencode>( |
| 1143 |
reencoder: &mut T, |
| 1144 |
field_ty: wasmparser::FieldType, |
| 1145 |
) -> Result<crate::FieldType, Error<T::Error>> { |
| 1146 |
Ok(crate::FieldType { |
| 1147 |
element_type: reencoder.storage_type(field_ty.element_type)?, |
| 1148 |
mutable: field_ty.mutable, |
| 1149 |
}) |
| 1150 |
} |
| 1151 |
|
| 1152 |
pub fn storage_type<T: ?Sized + Reencode>( |
| 1153 |
reencoder: &mut T, |
| 1154 |
storage_ty: wasmparser::StorageType, |
| 1155 |
) -> Result<crate::StorageType, Error<T::Error>> { |
| 1156 |
Ok(match storage_ty { |
| 1157 |
wasmparser::StorageType::I8 => crate::StorageType::I8, |
| 1158 |
wasmparser::StorageType::I16 => crate::StorageType::I16, |
| 1159 |
wasmparser::StorageType::Val(v) => crate::StorageType::Val(reencoder.val_type(v)?), |
| 1160 |
}) |
| 1161 |
} |
| 1162 |
|
| 1163 |
pub fn cont_type<T: ?Sized + Reencode>( |
| 1164 |
reencoder: &mut T, |
| 1165 |
cont_ty: wasmparser::ContType, |
| 1166 |
) -> Result<crate::ContType, Error<T::Error>> { |
| 1167 |
Ok(crate::ContType( |
| 1168 |
reencoder.type_index_unpacked(cont_ty.0.unpack())?, |
| 1169 |
)) |
| 1170 |
} |
| 1171 |
|
| 1172 |
pub fn val_type<T: ?Sized + Reencode>( |
| 1173 |
reencoder: &mut T, |
| 1174 |
val_ty: wasmparser::ValType, |
| 1175 |
) -> Result<crate::ValType, Error<T::Error>> { |
| 1176 |
Ok(match val_ty { |
| 1177 |
wasmparser::ValType::I32 => crate::ValType::I32, |
| 1178 |
wasmparser::ValType::I64 => crate::ValType::I64, |
| 1179 |
wasmparser::ValType::F32 => crate::ValType::F32, |
| 1180 |
wasmparser::ValType::F64 => crate::ValType::F64, |
| 1181 |
wasmparser::ValType::V128 => crate::ValType::V128, |
| 1182 |
wasmparser::ValType::Ref(r) => crate::ValType::Ref(reencoder.ref_type(r)?), |
| 1183 |
}) |
| 1184 |
} |
| 1185 |
|
| 1186 |
pub fn ref_type<T: ?Sized + Reencode>( |
| 1187 |
reencoder: &mut T, |
| 1188 |
ref_type: wasmparser::RefType, |
| 1189 |
) -> Result<crate::RefType, Error<T::Error>> { |
| 1190 |
Ok(crate::RefType { |
| 1191 |
nullable: ref_type.is_nullable(), |
| 1192 |
heap_type: reencoder.heap_type(ref_type.heap_type())?, |
| 1193 |
}) |
| 1194 |
} |
| 1195 |
|
| 1196 |
pub fn heap_type<T: ?Sized + Reencode>( |
| 1197 |
reencoder: &mut T, |
| 1198 |
heap_type: wasmparser::HeapType, |
| 1199 |
) -> Result<crate::HeapType, Error<T::Error>> { |
| 1200 |
Ok(match heap_type { |
| 1201 |
wasmparser::HeapType::Concrete(i) => { |
| 1202 |
crate::HeapType::Concrete(reencoder.type_index_unpacked(i)?) |
| 1203 |
} |
| 1204 |
wasmparser::HeapType::Abstract { shared, ty } => crate::HeapType::Abstract { |
| 1205 |
shared, |
| 1206 |
ty: reencoder.abstract_heap_type(ty), |
| 1207 |
}, |
| 1208 |
}) |
| 1209 |
} |
| 1210 |
|
| 1211 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1212 |
/// all the tables to the `tables` section. |
| 1213 |
pub fn parse_table_section<T: ?Sized + Reencode>( |
| 1214 |
reencoder: &mut T, |
| 1215 |
tables: &mut crate::TableSection, |
| 1216 |
section: wasmparser::TableSectionReader<'_>, |
| 1217 |
) -> Result<(), Error<T::Error>> { |
| 1218 |
for table in section { |
| 1219 |
reencoder.parse_table(tables, table?)?; |
| 1220 |
} |
| 1221 |
Ok(()) |
| 1222 |
} |
| 1223 |
|
| 1224 |
/// Parses a single [`wasmparser::Table`] and adds it to the `tables` section. |
| 1225 |
pub fn parse_table<T: ?Sized + Reencode>( |
| 1226 |
reencoder: &mut T, |
| 1227 |
tables: &mut crate::TableSection, |
| 1228 |
table: wasmparser::Table<'_>, |
| 1229 |
) -> Result<(), Error<T::Error>> { |
| 1230 |
let ty = reencoder.table_type(table.ty)?; |
| 1231 |
match table.init { |
| 1232 |
wasmparser::TableInit::RefNull => { |
| 1233 |
tables.table(ty); |
| 1234 |
} |
| 1235 |
wasmparser::TableInit::Expr(e) => { |
| 1236 |
tables.table_with_init(ty, &reencoder.const_expr(e)?); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
Ok(()) |
| 1240 |
} |
| 1241 |
|
| 1242 |
pub fn table_type<T: ?Sized + Reencode>( |
| 1243 |
reencoder: &mut T, |
| 1244 |
table_ty: wasmparser::TableType, |
| 1245 |
) -> Result<crate::TableType, Error<T::Error>> { |
| 1246 |
Ok(crate::TableType { |
| 1247 |
element_type: reencoder.ref_type(table_ty.element_type)?, |
| 1248 |
minimum: table_ty.initial, |
| 1249 |
maximum: table_ty.maximum, |
| 1250 |
table64: table_ty.table64, |
| 1251 |
shared: table_ty.shared, |
| 1252 |
}) |
| 1253 |
} |
| 1254 |
|
| 1255 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1256 |
/// all the tags to the `tags` section. |
| 1257 |
pub fn parse_tag_section<T: ?Sized + Reencode>( |
| 1258 |
reencoder: &mut T, |
| 1259 |
tags: &mut crate::TagSection, |
| 1260 |
section: wasmparser::TagSectionReader<'_>, |
| 1261 |
) -> Result<(), Error<T::Error>> { |
| 1262 |
for tag in section { |
| 1263 |
let tag = tag?; |
| 1264 |
tags.tag(reencoder.tag_type(tag)); |
| 1265 |
} |
| 1266 |
Ok(()) |
| 1267 |
} |
| 1268 |
|
| 1269 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1270 |
/// all the exports to the `exports` section. |
| 1271 |
pub fn parse_export_section<T: ?Sized + Reencode>( |
| 1272 |
reencoder: &mut T, |
| 1273 |
exports: &mut crate::ExportSection, |
| 1274 |
section: wasmparser::ExportSectionReader<'_>, |
| 1275 |
) -> Result<(), Error<T::Error>> { |
| 1276 |
for export in section { |
| 1277 |
reencoder.parse_export(exports, export?); |
| 1278 |
} |
| 1279 |
Ok(()) |
| 1280 |
} |
| 1281 |
|
| 1282 |
/// Parses the single [`wasmparser::Export`] provided and adds it to the |
| 1283 |
/// `exports` section. |
| 1284 |
pub fn parse_export<T: ?Sized + Reencode>( |
| 1285 |
reencoder: &mut T, |
| 1286 |
exports: &mut crate::ExportSection, |
| 1287 |
export: wasmparser::Export<'_>, |
| 1288 |
) { |
| 1289 |
exports.export( |
| 1290 |
export.name, |
| 1291 |
reencoder.export_kind(export.kind), |
| 1292 |
reencoder.external_index(export.kind, export.index), |
| 1293 |
); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1297 |
/// all the globals to the `globals` section. |
| 1298 |
pub fn parse_global_section<T: ?Sized + Reencode>( |
| 1299 |
reencoder: &mut T, |
| 1300 |
globals: &mut crate::GlobalSection, |
| 1301 |
section: wasmparser::GlobalSectionReader<'_>, |
| 1302 |
) -> Result<(), Error<T::Error>> { |
| 1303 |
for global in section { |
| 1304 |
reencoder.parse_global(globals, global?)?; |
| 1305 |
} |
| 1306 |
Ok(()) |
| 1307 |
} |
| 1308 |
|
| 1309 |
/// Parses the single [`wasmparser::Global`] provided and adds it to the |
| 1310 |
/// `globals` section. |
| 1311 |
pub fn parse_global<T: ?Sized + Reencode>( |
| 1312 |
reencoder: &mut T, |
| 1313 |
globals: &mut crate::GlobalSection, |
| 1314 |
global: wasmparser::Global<'_>, |
| 1315 |
) -> Result<(), Error<T::Error>> { |
| 1316 |
globals.global( |
| 1317 |
reencoder.global_type(global.ty)?, |
| 1318 |
&reencoder.const_expr(global.init_expr)?, |
| 1319 |
); |
| 1320 |
Ok(()) |
| 1321 |
} |
| 1322 |
|
| 1323 |
pub fn global_type<T: ?Sized + Reencode>( |
| 1324 |
reencoder: &mut T, |
| 1325 |
global_ty: wasmparser::GlobalType, |
| 1326 |
) -> Result<crate::GlobalType, Error<T::Error>> { |
| 1327 |
Ok(crate::GlobalType { |
| 1328 |
val_type: reencoder.val_type(global_ty.content_type)?, |
| 1329 |
mutable: global_ty.mutable, |
| 1330 |
shared: global_ty.shared, |
| 1331 |
}) |
| 1332 |
} |
| 1333 |
|
| 1334 |
pub fn entity_type<T: ?Sized + Reencode>( |
| 1335 |
reencoder: &mut T, |
| 1336 |
type_ref: wasmparser::TypeRef, |
| 1337 |
) -> Result<crate::EntityType, Error<T::Error>> { |
| 1338 |
Ok(match type_ref { |
| 1339 |
wasmparser::TypeRef::Func(i) => crate::EntityType::Function(reencoder.type_index(i)), |
| 1340 |
wasmparser::TypeRef::Table(t) => crate::EntityType::Table(reencoder.table_type(t)?), |
| 1341 |
wasmparser::TypeRef::Memory(m) => crate::EntityType::Memory(reencoder.memory_type(m)), |
| 1342 |
wasmparser::TypeRef::Global(g) => crate::EntityType::Global(reencoder.global_type(g)?), |
| 1343 |
wasmparser::TypeRef::Tag(t) => crate::EntityType::Tag(reencoder.tag_type(t)), |
| 1344 |
}) |
| 1345 |
} |
| 1346 |
|
| 1347 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1348 |
/// all the imports to the `import` section. |
| 1349 |
pub fn parse_import_section<T: ?Sized + Reencode>( |
| 1350 |
reencoder: &mut T, |
| 1351 |
imports: &mut crate::ImportSection, |
| 1352 |
section: wasmparser::ImportSectionReader<'_>, |
| 1353 |
) -> Result<(), Error<T::Error>> { |
| 1354 |
for import in section { |
| 1355 |
reencoder.parse_import(imports, import?)?; |
| 1356 |
} |
| 1357 |
Ok(()) |
| 1358 |
} |
| 1359 |
|
| 1360 |
/// Parses the single [`wasmparser::Import`] provided and adds it to the |
| 1361 |
/// `import` section. |
| 1362 |
pub fn parse_import<T: ?Sized + Reencode>( |
| 1363 |
reencoder: &mut T, |
| 1364 |
imports: &mut crate::ImportSection, |
| 1365 |
import: wasmparser::Import<'_>, |
| 1366 |
) -> Result<(), Error<T::Error>> { |
| 1367 |
imports.import( |
| 1368 |
import.module, |
| 1369 |
import.name, |
| 1370 |
reencoder.entity_type(import.ty)?, |
| 1371 |
); |
| 1372 |
Ok(()) |
| 1373 |
} |
| 1374 |
|
| 1375 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1376 |
/// all the memories to the `memories` section. |
| 1377 |
pub fn parse_memory_section<T: ?Sized + Reencode>( |
| 1378 |
reencoder: &mut T, |
| 1379 |
memories: &mut crate::MemorySection, |
| 1380 |
section: wasmparser::MemorySectionReader<'_>, |
| 1381 |
) -> Result<(), Error<T::Error>> { |
| 1382 |
for memory in section { |
| 1383 |
let memory = memory?; |
| 1384 |
memories.memory(reencoder.memory_type(memory)); |
| 1385 |
} |
| 1386 |
Ok(()) |
| 1387 |
} |
| 1388 |
|
| 1389 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1390 |
/// all the functions to the `functions` section. |
| 1391 |
pub fn parse_function_section<T: ?Sized + Reencode>( |
| 1392 |
reencoder: &mut T, |
| 1393 |
functions: &mut crate::FunctionSection, |
| 1394 |
section: wasmparser::FunctionSectionReader<'_>, |
| 1395 |
) -> Result<(), Error<T::Error>> { |
| 1396 |
for func in section { |
| 1397 |
functions.function(reencoder.type_index(func?)); |
| 1398 |
} |
| 1399 |
Ok(()) |
| 1400 |
} |
| 1401 |
|
| 1402 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1403 |
/// all the data to the `data` section. |
| 1404 |
pub fn parse_data_section<T: ?Sized + Reencode>( |
| 1405 |
reencoder: &mut T, |
| 1406 |
data: &mut crate::DataSection, |
| 1407 |
section: wasmparser::DataSectionReader<'_>, |
| 1408 |
) -> Result<(), Error<T::Error>> { |
| 1409 |
for datum in section { |
| 1410 |
reencoder.parse_data(data, datum?)?; |
| 1411 |
} |
| 1412 |
Ok(()) |
| 1413 |
} |
| 1414 |
|
| 1415 |
/// Parses a single [`wasmparser::Data`] and adds it to the `data` section. |
| 1416 |
pub fn parse_data<T: ?Sized + Reencode>( |
| 1417 |
reencoder: &mut T, |
| 1418 |
data: &mut crate::DataSection, |
| 1419 |
datum: wasmparser::Data<'_>, |
| 1420 |
) -> Result<(), Error<T::Error>> { |
| 1421 |
match datum.kind { |
| 1422 |
wasmparser::DataKind::Active { |
| 1423 |
memory_index, |
| 1424 |
offset_expr, |
| 1425 |
} => data.active( |
| 1426 |
reencoder.memory_index(memory_index), |
| 1427 |
&reencoder.const_expr(offset_expr)?, |
| 1428 |
datum.data.iter().copied(), |
| 1429 |
), |
| 1430 |
wasmparser::DataKind::Passive => data.passive(datum.data.iter().copied()), |
| 1431 |
}; |
| 1432 |
Ok(()) |
| 1433 |
} |
| 1434 |
|
| 1435 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1436 |
/// all the elements to the `element` section. |
| 1437 |
pub fn parse_element_section<T: ?Sized + Reencode>( |
| 1438 |
reencoder: &mut T, |
| 1439 |
elements: &mut crate::ElementSection, |
| 1440 |
section: wasmparser::ElementSectionReader<'_>, |
| 1441 |
) -> Result<(), Error<T::Error>> { |
| 1442 |
for element in section { |
| 1443 |
reencoder.parse_element(elements, element?)?; |
| 1444 |
} |
| 1445 |
Ok(()) |
| 1446 |
} |
| 1447 |
|
| 1448 |
/// Parses the single [`wasmparser::Element`] provided and adds it to the |
| 1449 |
/// `element` section. |
| 1450 |
pub fn parse_element<T: ?Sized + Reencode>( |
| 1451 |
reencoder: &mut T, |
| 1452 |
elements: &mut crate::ElementSection, |
| 1453 |
element: wasmparser::Element<'_>, |
| 1454 |
) -> Result<(), Error<T::Error>> { |
| 1455 |
let elems = reencoder.element_items(element.items)?; |
| 1456 |
match element.kind { |
| 1457 |
wasmparser::ElementKind::Active { |
| 1458 |
table_index, |
| 1459 |
offset_expr, |
| 1460 |
} => elements.active( |
| 1461 |
// Inform the reencoder that a table index is being used even if |
| 1462 |
// it's not actually present here. That helps wasm-mutate for |
| 1463 |
// example which wants to track uses to know when it's ok to |
| 1464 |
// remove a table. |
| 1465 |
// |
| 1466 |
// If the table index started at `None` and is still zero then |
| 1467 |
// preserve this encoding and keep it at `None`. Otherwise if |
| 1468 |
// the result is nonzero or it was previously nonzero then keep |
| 1469 |
// that encoding too. |
| 1470 |
match (table_index, reencoder.table_index(table_index.unwrap_or(0))) { |
| 1471 |
(None, 0) => None, |
| 1472 |
(_, n) => Some(n), |
| 1473 |
}, |
| 1474 |
&reencoder.const_expr(offset_expr)?, |
| 1475 |
elems, |
| 1476 |
), |
| 1477 |
wasmparser::ElementKind::Passive => elements.passive(elems), |
| 1478 |
wasmparser::ElementKind::Declared => elements.declared(elems), |
| 1479 |
}; |
| 1480 |
Ok(()) |
| 1481 |
} |
| 1482 |
|
| 1483 |
pub fn element_items<'a, T: ?Sized + Reencode>( |
| 1484 |
reencoder: &mut T, |
| 1485 |
items: wasmparser::ElementItems<'a>, |
| 1486 |
) -> Result<crate::Elements<'a>, Error<T::Error>> { |
| 1487 |
Ok(match items { |
| 1488 |
wasmparser::ElementItems::Functions(f) => { |
| 1489 |
let mut funcs = Vec::new(); |
| 1490 |
for func in f { |
| 1491 |
funcs.push(reencoder.function_index(func?)); |
| 1492 |
} |
| 1493 |
crate::Elements::Functions(funcs.into()) |
| 1494 |
} |
| 1495 |
wasmparser::ElementItems::Expressions(ty, e) => { |
| 1496 |
let mut exprs = Vec::new(); |
| 1497 |
for expr in e { |
| 1498 |
exprs.push(reencoder.const_expr(expr?)?); |
| 1499 |
} |
| 1500 |
crate::Elements::Expressions(reencoder.ref_type(ty)?, exprs.into()) |
| 1501 |
} |
| 1502 |
}) |
| 1503 |
} |
| 1504 |
|
| 1505 |
pub fn table_index<T: ?Sized + Reencode>(_reencoder: &mut T, table: u32) -> u32 { |
| 1506 |
table |
| 1507 |
} |
| 1508 |
|
| 1509 |
pub fn global_index<T: ?Sized + Reencode>(_reencoder: &mut T, global: u32) -> u32 { |
| 1510 |
global |
| 1511 |
} |
| 1512 |
|
| 1513 |
pub fn data_index<T: ?Sized + Reencode>(_reencoder: &mut T, data: u32) -> u32 { |
| 1514 |
data |
| 1515 |
} |
| 1516 |
|
| 1517 |
pub fn element_index<T: ?Sized + Reencode>(_reencoder: &mut T, element: u32) -> u32 { |
| 1518 |
element |
| 1519 |
} |
| 1520 |
|
| 1521 |
pub fn const_expr<T: ?Sized + Reencode>( |
| 1522 |
reencoder: &mut T, |
| 1523 |
const_expr: wasmparser::ConstExpr, |
| 1524 |
) -> Result<crate::ConstExpr, Error<T::Error>> { |
| 1525 |
let mut ops = const_expr.get_operators_reader(); |
| 1526 |
let mut bytes = Vec::new(); |
| 1527 |
|
| 1528 |
while !ops.is_end_then_eof() { |
| 1529 |
let insn = reencoder.parse_instruction(&mut ops)?; |
| 1530 |
insn.encode(&mut bytes); |
| 1531 |
} |
| 1532 |
|
| 1533 |
Ok(crate::ConstExpr::raw(bytes)) |
| 1534 |
} |
| 1535 |
|
| 1536 |
pub fn block_type<T: ?Sized + Reencode>( |
| 1537 |
reencoder: &mut T, |
| 1538 |
arg: wasmparser::BlockType, |
| 1539 |
) -> Result<crate::BlockType, Error<T::Error>> { |
| 1540 |
match arg { |
| 1541 |
wasmparser::BlockType::Empty => Ok(crate::BlockType::Empty), |
| 1542 |
wasmparser::BlockType::FuncType(n) => { |
| 1543 |
Ok(crate::BlockType::FunctionType(reencoder.type_index(n))) |
| 1544 |
} |
| 1545 |
wasmparser::BlockType::Type(t) => Ok(crate::BlockType::Result(reencoder.val_type(t)?)), |
| 1546 |
} |
| 1547 |
} |
| 1548 |
|
| 1549 |
pub fn instruction<'a, T: ?Sized + Reencode>( |
| 1550 |
reencoder: &mut T, |
| 1551 |
arg: wasmparser::Operator<'a>, |
| 1552 |
) -> Result<crate::Instruction<'a>, Error<T::Error>> { |
| 1553 |
use crate::Instruction; |
| 1554 |
|
| 1555 |
macro_rules! translate { |
| 1556 |
($( @$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => { |
| 1557 |
Ok(match arg { |
| 1558 |
$( |
| 1559 |
wasmparser::Operator::$op $({ $($arg),* })? => { |
| 1560 |
$( |
| 1561 |
$(let $arg = translate!(map $arg $arg);)* |
| 1562 |
)? |
| 1563 |
translate!(build $op $($($arg)*)?) |
| 1564 |
} |
| 1565 |
)* |
| 1566 |
}) |
| 1567 |
}; |
| 1568 |
|
| 1569 |
// This case is used to map, based on the name of the field, from the |
| 1570 |
// wasmparser payload type to the wasm-encoder payload type through |
| 1571 |
// `Translator` as applicable. |
| 1572 |
(map $arg:ident tag_index) => (reencoder.tag_index($arg)); |
| 1573 |
(map $arg:ident function_index) => (reencoder.function_index($arg)); |
| 1574 |
(map $arg:ident table) => (reencoder.table_index($arg)); |
| 1575 |
(map $arg:ident table_index) => (reencoder.table_index($arg)); |
| 1576 |
(map $arg:ident dst_table) => (reencoder.table_index($arg)); |
| 1577 |
(map $arg:ident src_table) => (reencoder.table_index($arg)); |
| 1578 |
(map $arg:ident type_index) => (reencoder.type_index($arg)); |
| 1579 |
(map $arg:ident array_type_index) => (reencoder.type_index($arg)); |
| 1580 |
(map $arg:ident array_type_index_dst) => (reencoder.type_index($arg)); |
| 1581 |
(map $arg:ident array_type_index_src) => (reencoder.type_index($arg)); |
| 1582 |
(map $arg:ident struct_type_index) => (reencoder.type_index($arg)); |
| 1583 |
(map $arg:ident global_index) => (reencoder.global_index($arg)); |
| 1584 |
(map $arg:ident mem) => (reencoder.memory_index($arg)); |
| 1585 |
(map $arg:ident src_mem) => (reencoder.memory_index($arg)); |
| 1586 |
(map $arg:ident dst_mem) => (reencoder.memory_index($arg)); |
| 1587 |
(map $arg:ident data_index) => (reencoder.data_index($arg)); |
| 1588 |
(map $arg:ident elem_index) => (reencoder.element_index($arg)); |
| 1589 |
(map $arg:ident array_data_index) => (reencoder.data_index($arg)); |
| 1590 |
(map $arg:ident array_elem_index) => (reencoder.element_index($arg)); |
| 1591 |
(map $arg:ident blockty) => (reencoder.block_type($arg)?); |
| 1592 |
(map $arg:ident relative_depth) => ($arg); |
| 1593 |
(map $arg:ident targets) => (( |
| 1594 |
$arg |
| 1595 |
.targets() |
| 1596 |
.collect::<Result<Vec<_>, wasmparser::BinaryReaderError>>()? |
| 1597 |
.into(), |
| 1598 |
$arg.default(), |
| 1599 |
)); |
| 1600 |
(map $arg:ident ty) => (reencoder.val_type($arg)?); |
| 1601 |
(map $arg:ident hty) => (reencoder.heap_type($arg)?); |
| 1602 |
(map $arg:ident from_ref_type) => (reencoder.ref_type($arg)?); |
| 1603 |
(map $arg:ident to_ref_type) => (reencoder.ref_type($arg)?); |
| 1604 |
(map $arg:ident memarg) => (reencoder.mem_arg($arg)); |
| 1605 |
(map $arg:ident ordering) => (reencoder.ordering($arg)); |
| 1606 |
(map $arg:ident local_index) => ($arg); |
| 1607 |
(map $arg:ident value) => ($arg); |
| 1608 |
(map $arg:ident lane) => ($arg); |
| 1609 |
(map $arg:ident lanes) => ($arg); |
| 1610 |
(map $arg:ident array_size) => ($arg); |
| 1611 |
(map $arg:ident field_index) => ($arg); |
| 1612 |
(map $arg:ident try_table) => ($arg); |
| 1613 |
(map $arg:ident argument_index) => (reencoder.type_index($arg)); |
| 1614 |
(map $arg:ident result_index) => (reencoder.type_index($arg)); |
| 1615 |
(map $arg:ident cont_type_index) => (reencoder.type_index($arg)); |
| 1616 |
(map $arg:ident resume_table) => (( |
| 1617 |
$arg.handlers.into_iter().map(|h| reencoder.handle(h)).collect::<Vec<_>>().into() |
| 1618 |
)); |
| 1619 |
|
| 1620 |
// This case takes the arguments of a wasmparser instruction and creates |
| 1621 |
// a wasm-encoder instruction. There are a few special cases for where |
| 1622 |
// the structure of a wasmparser instruction differs from that of |
| 1623 |
// wasm-encoder. |
| 1624 |
(build $op:ident) => (Instruction::$op); |
| 1625 |
(build BrTable $arg:ident) => (Instruction::BrTable($arg.0, $arg.1)); |
| 1626 |
(build I32Const $arg:ident) => (Instruction::I32Const($arg)); |
| 1627 |
(build I64Const $arg:ident) => (Instruction::I64Const($arg)); |
| 1628 |
(build F32Const $arg:ident) => (Instruction::F32Const(f32::from_bits($arg.bits()))); |
| 1629 |
(build F64Const $arg:ident) => (Instruction::F64Const(f64::from_bits($arg.bits()))); |
| 1630 |
(build V128Const $arg:ident) => (Instruction::V128Const($arg.i128())); |
| 1631 |
(build TryTable $table:ident) => (Instruction::TryTable(reencoder.block_type($table.ty)?, { |
| 1632 |
$table.catches.into_iter().map(|c| reencoder.catch(c)).collect::<Vec<_>>().into() |
| 1633 |
})); |
| 1634 |
(build $op:ident $arg:ident) => (Instruction::$op($arg)); |
| 1635 |
(build $op:ident $($arg:ident)*) => (Instruction::$op { $($arg),* }); |
| 1636 |
} |
| 1637 |
|
| 1638 |
wasmparser::for_each_operator!(translate) |
| 1639 |
} |
| 1640 |
|
| 1641 |
/// Parses the input `section` given from the `wasmparser` crate and adds |
| 1642 |
/// all the code to the `code` section. |
| 1643 |
pub fn parse_code_section<T: ?Sized + Reencode>( |
| 1644 |
reencoder: &mut T, |
| 1645 |
code: &mut crate::CodeSection, |
| 1646 |
section: wasmparser::CodeSectionReader<'_>, |
| 1647 |
) -> Result<(), Error<T::Error>> { |
| 1648 |
for func in section { |
| 1649 |
reencoder.parse_function_body(code, func?)?; |
| 1650 |
} |
| 1651 |
Ok(()) |
| 1652 |
} |
| 1653 |
|
| 1654 |
/// Parses a single [`wasmparser::FunctionBody`] and adds it to the `code` section. |
| 1655 |
pub fn parse_function_body<T: ?Sized + Reencode>( |
| 1656 |
reencoder: &mut T, |
| 1657 |
code: &mut crate::CodeSection, |
| 1658 |
func: wasmparser::FunctionBody<'_>, |
| 1659 |
) -> Result<(), Error<T::Error>> { |
| 1660 |
let mut f = reencoder.new_function_with_parsed_locals(&func)?; |
| 1661 |
let mut reader = func.get_operators_reader()?; |
| 1662 |
while !reader.eof() { |
| 1663 |
f.instruction(&reencoder.parse_instruction(&mut reader)?); |
| 1664 |
} |
| 1665 |
code.function(&f); |
| 1666 |
Ok(()) |
| 1667 |
} |
| 1668 |
|
| 1669 |
/// Create a new [`crate::Function`] by parsing the locals declarations from the |
| 1670 |
/// provided [`wasmparser::FunctionBody`]. |
| 1671 |
pub fn new_function_with_parsed_locals<T: ?Sized + Reencode>( |
| 1672 |
reencoder: &mut T, |
| 1673 |
func: &wasmparser::FunctionBody<'_>, |
| 1674 |
) -> Result<crate::Function, Error<T::Error>> { |
| 1675 |
let mut locals = Vec::new(); |
| 1676 |
for pair in func.get_locals_reader()? { |
| 1677 |
let (cnt, ty) = pair?; |
| 1678 |
locals.push((cnt, reencoder.val_type(ty)?)); |
| 1679 |
} |
| 1680 |
Ok(crate::Function::new(locals)) |
| 1681 |
} |
| 1682 |
|
| 1683 |
/// Parses a single instruction from `reader` and adds it to `function`. |
| 1684 |
pub fn parse_instruction<'a, T: ?Sized + Reencode>( |
| 1685 |
reencoder: &mut T, |
| 1686 |
reader: &mut wasmparser::OperatorsReader<'a>, |
| 1687 |
) -> Result<crate::Instruction<'a>, Error<T::Error>> { |
| 1688 |
let instruction = reencoder.instruction(reader.read()?)?; |
| 1689 |
Ok(instruction) |
| 1690 |
} |
| 1691 |
|
| 1692 |
pub fn parse_unknown_section<T: ?Sized + Reencode>( |
| 1693 |
_reencoder: &mut T, |
| 1694 |
module: &mut crate::Module, |
| 1695 |
id: u8, |
| 1696 |
contents: &[u8], |
| 1697 |
) -> Result<(), Error<T::Error>> { |
| 1698 |
module.section(&crate::RawSection { id, data: contents }); |
| 1699 |
Ok(()) |
| 1700 |
} |
| 1701 |
|
| 1702 |
pub fn custom_name_section<T: ?Sized + Reencode>( |
| 1703 |
reencoder: &mut T, |
| 1704 |
section: wasmparser::NameSectionReader<'_>, |
| 1705 |
) -> Result<crate::NameSection, Error<T::Error>> { |
| 1706 |
let mut ret = crate::NameSection::new(); |
| 1707 |
for subsection in section { |
| 1708 |
reencoder.parse_custom_name_subsection(&mut ret, subsection?)?; |
| 1709 |
} |
| 1710 |
Ok(ret) |
| 1711 |
} |
| 1712 |
|
| 1713 |
pub fn parse_custom_name_subsection<T: ?Sized + Reencode>( |
| 1714 |
reencoder: &mut T, |
| 1715 |
names: &mut crate::NameSection, |
| 1716 |
section: wasmparser::Name<'_>, |
| 1717 |
) -> Result<(), Error<T::Error>> { |
| 1718 |
match section { |
| 1719 |
wasmparser::Name::Module { name, .. } => { |
| 1720 |
names.module(name); |
| 1721 |
} |
| 1722 |
wasmparser::Name::Function(map) => { |
| 1723 |
names.functions(&name_map(map, |i| reencoder.function_index(i))?); |
| 1724 |
} |
| 1725 |
wasmparser::Name::Type(map) => { |
| 1726 |
names.types(&name_map(map, |i| reencoder.type_index(i))?); |
| 1727 |
} |
| 1728 |
wasmparser::Name::Local(map) => { |
| 1729 |
names.locals(&indirect_name_map(map, |i| reencoder.function_index(i))?); |
| 1730 |
} |
| 1731 |
wasmparser::Name::Label(map) => { |
| 1732 |
names.labels(&indirect_name_map(map, |i| reencoder.function_index(i))?); |
| 1733 |
} |
| 1734 |
wasmparser::Name::Table(map) => { |
| 1735 |
names.tables(&name_map(map, |i| reencoder.table_index(i))?); |
| 1736 |
} |
| 1737 |
wasmparser::Name::Memory(map) => { |
| 1738 |
names.memories(&name_map(map, |i| reencoder.memory_index(i))?); |
| 1739 |
} |
| 1740 |
wasmparser::Name::Global(map) => { |
| 1741 |
names.globals(&name_map(map, |i| reencoder.global_index(i))?); |
| 1742 |
} |
| 1743 |
wasmparser::Name::Element(map) => { |
| 1744 |
names.elements(&name_map(map, |i| reencoder.element_index(i))?); |
| 1745 |
} |
| 1746 |
wasmparser::Name::Data(map) => { |
| 1747 |
names.data(&name_map(map, |i| reencoder.data_index(i))?); |
| 1748 |
} |
| 1749 |
wasmparser::Name::Tag(map) => { |
| 1750 |
names.tags(&name_map(map, |i| reencoder.tag_index(i))?); |
| 1751 |
} |
| 1752 |
wasmparser::Name::Field(map) => { |
| 1753 |
names.fields(&indirect_name_map(map, |i| reencoder.type_index(i))?); |
| 1754 |
} |
| 1755 |
wasmparser::Name::Unknown { ty, data, .. } => { |
| 1756 |
names.raw(ty, data); |
| 1757 |
} |
| 1758 |
} |
| 1759 |
Ok(()) |
| 1760 |
} |
| 1761 |
|
| 1762 |
pub fn name_map( |
| 1763 |
map: wasmparser::NameMap<'_>, |
| 1764 |
mut map_index: impl FnMut(u32) -> u32, |
| 1765 |
) -> wasmparser::Result<crate::NameMap> { |
| 1766 |
let mut ret = crate::NameMap::new(); |
| 1767 |
for naming in map { |
| 1768 |
let naming = naming?; |
| 1769 |
ret.append(map_index(naming.index), naming.name); |
| 1770 |
} |
| 1771 |
Ok(ret) |
| 1772 |
} |
| 1773 |
|
| 1774 |
pub fn indirect_name_map( |
| 1775 |
map: wasmparser::IndirectNameMap<'_>, |
| 1776 |
mut map_index: impl FnMut(u32) -> u32, |
| 1777 |
) -> wasmparser::Result<crate::IndirectNameMap> { |
| 1778 |
let mut ret = crate::IndirectNameMap::new(); |
| 1779 |
for naming in map { |
| 1780 |
let naming = naming?; |
| 1781 |
ret.append(map_index(naming.index), &name_map(naming.names, |i| i)?); |
| 1782 |
} |
| 1783 |
Ok(ret) |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
impl From<wasmparser::MemArg> for crate::MemArg { |
| 1788 |
fn from(arg: wasmparser::MemArg) -> Self { |
| 1789 |
RoundtripReencoder.mem_arg(arg) |
| 1790 |
} |
| 1791 |
} |
| 1792 |
|
| 1793 |
impl From<wasmparser::Ordering> for crate::Ordering { |
| 1794 |
fn from(arg: wasmparser::Ordering) -> Self { |
| 1795 |
RoundtripReencoder.ordering(arg) |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|
| 1799 |
impl TryFrom<wasmparser::BlockType> for crate::BlockType { |
| 1800 |
type Error = Error; |
| 1801 |
|
| 1802 |
fn try_from(arg: wasmparser::BlockType) -> Result<Self, Self::Error> { |
| 1803 |
RoundtripReencoder.block_type(arg) |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
impl<'a> TryFrom<wasmparser::Operator<'a>> for crate::Instruction<'a> { |
| 1808 |
type Error = Error; |
| 1809 |
|
| 1810 |
fn try_from(arg: wasmparser::Operator<'a>) -> Result<Self, Self::Error> { |
| 1811 |
RoundtripReencoder.instruction(arg) |
| 1812 |
} |
| 1813 |
} |
| 1814 |
|
| 1815 |
impl From<wasmparser::Catch> for crate::Catch { |
| 1816 |
fn from(arg: wasmparser::Catch) -> Self { |
| 1817 |
RoundtripReencoder.catch(arg) |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
impl<'a> TryFrom<wasmparser::ConstExpr<'a>> for crate::ConstExpr { |
| 1822 |
type Error = Error; |
| 1823 |
|
| 1824 |
fn try_from(const_expr: wasmparser::ConstExpr) -> Result<Self, Self::Error> { |
| 1825 |
RoundtripReencoder.const_expr(const_expr) |
| 1826 |
} |
| 1827 |
} |
| 1828 |
|
| 1829 |
impl<'a> From<wasmparser::CustomSectionReader<'a>> for crate::CustomSection<'a> { |
| 1830 |
fn from(section: wasmparser::CustomSectionReader<'a>) -> Self { |
| 1831 |
RoundtripReencoder.custom_section(section) |
| 1832 |
} |
| 1833 |
} |
| 1834 |
|
| 1835 |
impl From<wasmparser::ExternalKind> for crate::ExportKind { |
| 1836 |
fn from(external_kind: wasmparser::ExternalKind) -> Self { |
| 1837 |
RoundtripReencoder.export_kind(external_kind) |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
impl TryFrom<wasmparser::GlobalType> for crate::GlobalType { |
| 1842 |
type Error = Error; |
| 1843 |
|
| 1844 |
fn try_from(global_ty: wasmparser::GlobalType) -> Result<Self, Self::Error> { |
| 1845 |
RoundtripReencoder.global_type(global_ty) |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
impl From<wasmparser::Handle> for crate::Handle { |
| 1850 |
fn from(arg: wasmparser::Handle) -> Self { |
| 1851 |
RoundtripReencoder.handle(arg) |
| 1852 |
} |
| 1853 |
} |
| 1854 |
|
| 1855 |
impl TryFrom<wasmparser::TypeRef> for crate::EntityType { |
| 1856 |
type Error = Error; |
| 1857 |
|
| 1858 |
fn try_from(type_ref: wasmparser::TypeRef) -> Result<Self, Self::Error> { |
| 1859 |
RoundtripReencoder.entity_type(type_ref) |
| 1860 |
} |
| 1861 |
} |
| 1862 |
|
| 1863 |
impl From<wasmparser::MemoryType> for crate::MemoryType { |
| 1864 |
fn from(memory_ty: wasmparser::MemoryType) -> Self { |
| 1865 |
RoundtripReencoder.memory_type(memory_ty) |
| 1866 |
} |
| 1867 |
} |
| 1868 |
|
| 1869 |
impl TryFrom<wasmparser::TableType> for crate::TableType { |
| 1870 |
type Error = Error; |
| 1871 |
|
| 1872 |
fn try_from(table_ty: wasmparser::TableType) -> Result<Self, Self::Error> { |
| 1873 |
RoundtripReencoder.table_type(table_ty) |
| 1874 |
} |
| 1875 |
} |
| 1876 |
|
| 1877 |
impl From<wasmparser::TagKind> for crate::TagKind { |
| 1878 |
fn from(kind: wasmparser::TagKind) -> Self { |
| 1879 |
RoundtripReencoder.tag_kind(kind) |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
impl From<wasmparser::TagType> for crate::TagType { |
| 1884 |
fn from(tag_ty: wasmparser::TagType) -> Self { |
| 1885 |
RoundtripReencoder.tag_type(tag_ty) |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
impl TryFrom<wasmparser::SubType> for crate::SubType { |
| 1890 |
type Error = Error; |
| 1891 |
|
| 1892 |
fn try_from(sub_ty: wasmparser::SubType) -> Result<Self, Self::Error> { |
| 1893 |
RoundtripReencoder.sub_type(sub_ty) |
| 1894 |
} |
| 1895 |
} |
| 1896 |
|
| 1897 |
impl TryFrom<wasmparser::CompositeType> for crate::CompositeType { |
| 1898 |
type Error = Error; |
| 1899 |
|
| 1900 |
fn try_from(composite_ty: wasmparser::CompositeType) -> Result<Self, Self::Error> { |
| 1901 |
RoundtripReencoder.composite_type(composite_ty) |
| 1902 |
} |
| 1903 |
} |
| 1904 |
|
| 1905 |
impl TryFrom<wasmparser::FuncType> for crate::FuncType { |
| 1906 |
type Error = Error; |
| 1907 |
|
| 1908 |
fn try_from(func_ty: wasmparser::FuncType) -> Result<Self, Self::Error> { |
| 1909 |
RoundtripReencoder.func_type(func_ty) |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
impl TryFrom<wasmparser::ArrayType> for crate::ArrayType { |
| 1914 |
type Error = Error; |
| 1915 |
|
| 1916 |
fn try_from(array_ty: wasmparser::ArrayType) -> Result<Self, Self::Error> { |
| 1917 |
RoundtripReencoder.array_type(array_ty) |
| 1918 |
} |
| 1919 |
} |
| 1920 |
|
| 1921 |
impl TryFrom<wasmparser::StructType> for crate::StructType { |
| 1922 |
type Error = Error; |
| 1923 |
|
| 1924 |
fn try_from(struct_ty: wasmparser::StructType) -> Result<Self, Self::Error> { |
| 1925 |
RoundtripReencoder.struct_type(struct_ty) |
| 1926 |
} |
| 1927 |
} |
| 1928 |
|
| 1929 |
impl TryFrom<wasmparser::FieldType> for crate::FieldType { |
| 1930 |
type Error = Error; |
| 1931 |
|
| 1932 |
fn try_from(field_ty: wasmparser::FieldType) -> Result<Self, Self::Error> { |
| 1933 |
RoundtripReencoder.field_type(field_ty) |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
impl TryFrom<wasmparser::StorageType> for crate::StorageType { |
| 1938 |
type Error = Error; |
| 1939 |
|
| 1940 |
fn try_from(storage_ty: wasmparser::StorageType) -> Result<Self, Self::Error> { |
| 1941 |
RoundtripReencoder.storage_type(storage_ty) |
| 1942 |
} |
| 1943 |
} |
| 1944 |
|
| 1945 |
impl TryFrom<wasmparser::ValType> for crate::ValType { |
| 1946 |
type Error = Error; |
| 1947 |
|
| 1948 |
fn try_from(val_ty: wasmparser::ValType) -> Result<Self, Self::Error> { |
| 1949 |
RoundtripReencoder.val_type(val_ty) |
| 1950 |
} |
| 1951 |
} |
| 1952 |
|
| 1953 |
impl TryFrom<wasmparser::RefType> for crate::RefType { |
| 1954 |
type Error = Error; |
| 1955 |
|
| 1956 |
fn try_from(ref_type: wasmparser::RefType) -> Result<Self, Self::Error> { |
| 1957 |
RoundtripReencoder.ref_type(ref_type) |
| 1958 |
} |
| 1959 |
} |
| 1960 |
|
| 1961 |
impl TryFrom<wasmparser::HeapType> for crate::HeapType { |
| 1962 |
type Error = Error; |
| 1963 |
|
| 1964 |
fn try_from(heap_type: wasmparser::HeapType) -> Result<Self, Self::Error> { |
| 1965 |
crate::reencode::utils::heap_type(&mut crate::reencode::RoundtripReencoder, heap_type) |
| 1966 |
} |
| 1967 |
} |
| 1968 |
|
| 1969 |
impl From<wasmparser::AbstractHeapType> for crate::AbstractHeapType { |
| 1970 |
fn from(value: wasmparser::AbstractHeapType) -> Self { |
| 1971 |
RoundtripReencoder.abstract_heap_type(value) |
| 1972 |
} |
| 1973 |
} |