Skip to content

Commit

Permalink
wasmparser: Define core Wasm types in a macro
Browse files Browse the repository at this point in the history
This sets the stage for a second set of these definitions that don't use `u32`
indices, but instead use something similar to `CoreTypeId`. This new set of
types will be used in validation and will support canonicalization.

At the same time, I've taken the liberty to rename `StructuralType` to
`CompositeType` to match the latest spec and to change `HeapType::Indexed` to
`HeapType::Concrete` for the same reason.
  • Loading branch information
fitzgen committed Oct 25, 2023
1 parent e18b17f commit 58d9f40
Show file tree
Hide file tree
Showing 34 changed files with 955 additions and 861 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ wasmtime = { version = "12.0.0", default-features = false, features = ['cranelif
url = "2.0.0"
pretty_assertions = "1.3.0"
semver = "1.0.0"
smallvec = "1.11.1"

wasm-compose = { version = "0.4.10", path = "crates/wasm-compose" }
wasm-encoder = { version = "0.35.0", path = "crates/wasm-encoder" }
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl<'a> TypeEncoder<'a> {
wasmparser::HeapType::Struct => HeapType::Struct,
wasmparser::HeapType::Array => HeapType::Array,
wasmparser::HeapType::I31 => HeapType::I31,
wasmparser::HeapType::Indexed(i) => HeapType::Indexed(i),
wasmparser::HeapType::Concrete(i) => HeapType::Concrete(i),
},
}
}
Expand Down
50 changes: 37 additions & 13 deletions crates/wasm-encoder/src/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,51 @@ impl From<RefType> for ValType {
pub enum HeapType {
/// Untyped (any) function.
Func,
/// External heap type.

/// The abstract external heap type.
Extern,
/// The `any` heap type. The common supertype (a.k.a. top) of all internal types.

/// The abstract `any` heap type.
///
/// The common supertype (a.k.a. top) of all internal types.
Any,
/// The `none` heap type. The common subtype (a.k.a. bottom) of all internal types.

/// The abstract `none` heap type.
///
/// The common subtype (a.k.a. bottom) of all internal types.
None,
/// The `noextern` heap type. The common subtype (a.k.a. bottom) of all external types.

/// The abstract `noextern` heap type.
///
/// The common subtype (a.k.a. bottom) of all external types.
NoExtern,
/// The `nofunc` heap type. The common subtype (a.k.a. bottom) of all function types.

/// The abstract `nofunc` heap type.
///
/// The common subtype (a.k.a. bottom) of all function types.
NoFunc,
/// The `eq` heap type. The common supertype of all referenceable types on which comparison

/// The abstract `eq` heap type.
///
/// The common supertype of all referenceable types on which comparison
/// (ref.eq) is allowed.
Eq,
/// The `struct` heap type. The common supertype of all struct types.

/// The abstract `struct` heap type.
///
/// The common supertype of all struct types.
Struct,
/// The `array` heap type. The common supertype of all array types.

/// The abstract `array` heap type.
///
/// The common supertype of all array types.
Array,
/// The i31 heap type.

/// The unboxed `i31` heap type.
I31,
/// User defined type at the given index.
Indexed(u32),

/// A concrete Wasm-defined type at the given index.
Concrete(u32),
}

impl Encode for HeapType {
Expand All @@ -351,7 +375,7 @@ impl Encode for HeapType {
HeapType::I31 => sink.push(0x6C),
// Note that this is encoded as a signed type rather than unsigned
// as it's decoded as an s33
HeapType::Indexed(i) => i64::from(*i).encode(sink),
HeapType::Concrete(i) => i64::from(*i).encode(sink),
}
}
}
Expand All @@ -360,7 +384,7 @@ impl Encode for HeapType {
impl From<wasmparser::HeapType> for HeapType {
fn from(heap_type: wasmparser::HeapType) -> Self {
match heap_type {
wasmparser::HeapType::Indexed(i) => HeapType::Indexed(i),
wasmparser::HeapType::Indexed(i) => HeapType::Concrete(i),
wasmparser::HeapType::Func => HeapType::Func,
wasmparser::HeapType::Extern => HeapType::Extern,
wasmparser::HeapType::Any => HeapType::Any,
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-mutate/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn map_ref_type(ref_ty: wasmparser::RefType) -> Result<RefType> {
wasmparser::HeapType::Struct => HeapType::Struct,
wasmparser::HeapType::Array => HeapType::Array,
wasmparser::HeapType::I31 => HeapType::I31,
wasmparser::HeapType::Indexed(i) => HeapType::Indexed(i.into()),
wasmparser::HeapType::Concrete(i) => HeapType::Concrete(i.into()),
},
})
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-mutate/src/mutators/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ pub fn heapty(t: &mut dyn Translator, ty: &wasmparser::HeapType) -> Result<HeapT
wasmparser::HeapType::Struct => Ok(HeapType::Struct),
wasmparser::HeapType::Array => Ok(HeapType::Array),
wasmparser::HeapType::I31 => Ok(HeapType::I31),
wasmparser::HeapType::Indexed(i) => {
Ok(HeapType::Indexed(t.remap(Item::Type, (*i).into())?))
wasmparser::HeapType::Concrete(i) => {
Ok(HeapType::Concrete(t.remap(Item::Type, (*i).into())?))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasm-smith/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ fn convert_reftype(ty: wasmparser::RefType) -> RefType {
wasmparser::HeapType::Struct => HeapType::Struct,
wasmparser::HeapType::Array => HeapType::Array,
wasmparser::HeapType::I31 => HeapType::I31,
wasmparser::HeapType::Indexed(i) => HeapType::Indexed(i.into()),
wasmparser::HeapType::Concrete(i) => HeapType::Concrete(i.into()),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/wasmparser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude = ["benches/*.wasm"]
[dependencies]
indexmap = { workspace = true }
semver = { workspace = true }
smallvec = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
Expand Down
Loading

0 comments on commit 58d9f40

Please sign in to comment.