|
13 | 13 | * limitations under the License.
|
14 | 14 | */
|
15 | 15 |
|
16 |
| -use std::fmt::{self, Debug, Write}; |
17 |
| - |
18 | 16 | use crate::limits::{
|
19 | 17 | MAX_WASM_FUNCTION_PARAMS, MAX_WASM_FUNCTION_RETURNS, MAX_WASM_STRUCT_FIELDS,
|
20 | 18 | MAX_WASM_SUPERTYPES, MAX_WASM_TYPES,
|
21 | 19 | };
|
22 | 20 | use crate::types::CoreTypeId;
|
23 | 21 | use crate::{BinaryReader, BinaryReaderError, FromReader, Result, SectionLimited};
|
| 22 | +use std::fmt::{self, Debug, Write}; |
| 23 | +use std::hash::{Hash, Hasher}; |
24 | 24 |
|
25 | 25 | mod matches;
|
26 | 26 | pub(crate) use self::matches::{Matches, WithRecGroup};
|
@@ -384,6 +384,20 @@ impl RecGroup {
|
384 | 384 | }
|
385 | 385 | }
|
386 | 386 |
|
| 387 | +impl Hash for RecGroup { |
| 388 | + fn hash<H: Hasher>(&self, hasher: &mut H) { |
| 389 | + self.types().hash(hasher) |
| 390 | + } |
| 391 | +} |
| 392 | + |
| 393 | +impl PartialEq for RecGroup { |
| 394 | + fn eq(&self, other: &RecGroup) -> bool { |
| 395 | + self.types() == other.types() |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | +impl Eq for RecGroup {} |
| 400 | + |
387 | 401 | /// Represents a subtype of possible other types in a WebAssembly module.
|
388 | 402 | #[derive(Debug, Clone, Hash, PartialEq, Eq)]
|
389 | 403 | pub struct SubType {
|
@@ -416,6 +430,35 @@ impl SubType {
|
416 | 430 | pub fn unwrap_struct(&self) -> &StructType {
|
417 | 431 | self.composite_type.unwrap_struct()
|
418 | 432 | }
|
| 433 | + |
| 434 | + /// Maps any `UnpackedIndex` via the specified closure. |
| 435 | + pub(crate) fn remap_indices( |
| 436 | + &mut self, |
| 437 | + f: &mut dyn FnMut(&mut PackedIndex) -> Result<()>, |
| 438 | + ) -> Result<()> { |
| 439 | + if let Some(idx) = &mut self.supertype_idx { |
| 440 | + f(idx)?; |
| 441 | + } |
| 442 | + match &mut self.composite_type { |
| 443 | + CompositeType::Func(ty) => { |
| 444 | + for ty in ty.params_mut() { |
| 445 | + ty.remap_indices(f)?; |
| 446 | + } |
| 447 | + for ty in ty.results_mut() { |
| 448 | + ty.remap_indices(f)?; |
| 449 | + } |
| 450 | + } |
| 451 | + CompositeType::Array(ty) => { |
| 452 | + ty.0.remap_indices(f)?; |
| 453 | + } |
| 454 | + CompositeType::Struct(ty) => { |
| 455 | + for field in ty.fields.iter_mut() { |
| 456 | + field.remap_indices(f)?; |
| 457 | + } |
| 458 | + } |
| 459 | + } |
| 460 | + Ok(()) |
| 461 | + } |
419 | 462 | }
|
420 | 463 |
|
421 | 464 | /// Represents a composite type in a WebAssembly module.
|
@@ -562,6 +605,19 @@ pub struct FieldType {
|
562 | 605 | pub mutable: bool,
|
563 | 606 | }
|
564 | 607 |
|
| 608 | +impl FieldType { |
| 609 | + /// Maps any `UnpackedIndex` via the specified closure. |
| 610 | + pub(crate) fn remap_indices( |
| 611 | + &mut self, |
| 612 | + f: &mut dyn FnMut(&mut PackedIndex) -> Result<()>, |
| 613 | + ) -> Result<()> { |
| 614 | + match &mut self.element_type { |
| 615 | + StorageType::I8 | StorageType::I16 => Ok(()), |
| 616 | + StorageType::Val(ty) => ty.remap_indices(f), |
| 617 | + } |
| 618 | + } |
| 619 | +} |
| 620 | + |
565 | 621 | /// Represents storage types introduced in the GC spec for array and struct fields.
|
566 | 622 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
567 | 623 | pub enum StorageType {
|
@@ -640,6 +696,23 @@ impl ValType {
|
640 | 696 | Self::Ref(rt) => rt.is_nullable(),
|
641 | 697 | }
|
642 | 698 | }
|
| 699 | + |
| 700 | + /// Maps any `UnpackedIndex` via the specified closure. |
| 701 | + pub(crate) fn remap_indices( |
| 702 | + &mut self, |
| 703 | + map: &mut dyn FnMut(&mut PackedIndex) -> Result<()>, |
| 704 | + ) -> Result<()> { |
| 705 | + match self { |
| 706 | + ValType::Ref(r) => { |
| 707 | + if let Some(mut idx) = r.type_index() { |
| 708 | + map(&mut idx)?; |
| 709 | + *r = RefType::concrete(r.is_nullable(), idx); |
| 710 | + } |
| 711 | + } |
| 712 | + ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64 | ValType::V128 => {} |
| 713 | + } |
| 714 | + Ok(()) |
| 715 | + } |
643 | 716 | }
|
644 | 717 |
|
645 | 718 | /// A reference type.
|
|
0 commit comments