Skip to content

Commit

Permalink
Pick up upstream's into_iter_err_on_gc_types (#39)
Browse files Browse the repository at this point in the history
This patch reintroduces the `into_iter_err_on_gc_types` from upstream,
and renames our custom implementation (as it is still needed in our
fork of wasmtime).
  • Loading branch information
dhil authored Dec 5, 2023
1 parent dba7d18 commit 49005a0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 70 deletions.
5 changes: 1 addition & 4 deletions crates/wasm-compose/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ use anyhow::{anyhow, bail, Result};
use indexmap::{IndexMap, IndexSet};
use petgraph::EdgeDirection;
use smallvec::SmallVec;
use std::collections::{hash_map::Entry, HashMap};
use std::mem;
use std::{
borrow::Cow,
collections::{hash_map::Entry, HashMap},
};
use wasm_encoder::*;
use wasmparser::{
names::KebabString,
Expand Down
36 changes: 15 additions & 21 deletions crates/wasm-mutate/src/mutators/add_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::Mutator;
use crate::module::map_type;
use crate::{Error, Result};
use crate::Result;
use rand::Rng;
use std::iter;

Expand Down Expand Up @@ -56,26 +56,20 @@ impl Mutator for AddTypeMutator {
// Copy the existing types section over into the encoder.
let reader = wasmparser::TypeSectionReader::new(old_types.data, 0)?;
for ty in reader.into_iter_err_on_gc_types() {
match ty? {
wasmparser::FuncOrContType::Func(ty) => {
let params = ty
.params()
.iter()
.copied()
.map(map_type)
.collect::<Result<Vec<_>, _>>()?;
let results = ty
.results()
.iter()
.copied()
.map(map_type)
.collect::<Result<Vec<_>, _>>()?;
types.function(params, results);
}
wasmparser::FuncOrContType::Cont(_) => {
return Err(Error::unsupported("Cont types are not supported yet."));
}
}
let ty = ty?;
let params = ty
.params()
.iter()
.copied()
.map(map_type)
.collect::<Result<Vec<_>, _>>()?;
let results = ty
.results()
.iter()
.copied()
.map(map_type)
.collect::<Result<Vec<_>, _>>()?;
types.function(params, results);
}
// And then add our new type.
types.function(params, results);
Expand Down
9 changes: 2 additions & 7 deletions crates/wasm-mutate/src/mutators/remove_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,8 @@ impl RemoveItem {
TypeSectionReader::new(section.data, 0)?.into_iter_err_on_gc_types(),
Item::Type,
|me, ty, section| {
match ty {
wasmparser::FuncOrContType::Func(ty) => {
me.translate_func_type(ty,section)?;
Ok(())
},
wasmparser::FuncOrContType::Cont(_) => Err(Error::unsupported("Cont types are not supported yet.")),
}
me.translate_func_type(ty,section)?;
Ok(())
},
)?;
},
Expand Down
48 changes: 21 additions & 27 deletions crates/wasm-smith/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,34 +609,28 @@ impl Module {
let serialized_sig_idx = match available_types.get_mut(parsed_sig_idx as usize) {
None => panic!("signature index refers to a type out of bounds"),
Some((_, Some(idx))) => *idx as usize,
Some((func_type, index_store)) => match func_type {
wasmparser::FuncOrContType::Func(func_type) => {
let multi_value_required = func_type.results().len() > 1;
let new_index = first_type_index + new_types.len();
if new_index >= max_types || (multi_value_required && !multi_value_enabled)
{
return None;
}
let func_type = Rc::new(FuncType {
params: func_type
.params()
.iter()
.map(|t| convert_type(*t))
.collect(),
results: func_type
.results()
.iter()
.map(|t| convert_type(*t))
.collect(),
});
index_store.replace(new_index as u32);
new_types.push(Type::Func(Rc::clone(&func_type)));
new_index
}
wasmparser::FuncOrContType::Cont(_) => {
unimplemented!("Continuation types are not supported yet.")
Some((func_type, index_store)) => {
let multi_value_required = func_type.results().len() > 1;
let new_index = first_type_index + new_types.len();
if new_index >= max_types || (multi_value_required && !multi_value_enabled) {
return None;
}
},
let func_type = Rc::new(FuncType {
params: func_type
.params()
.iter()
.map(|t| convert_type(*t))
.collect(),
results: func_type
.results()
.iter()
.map(|t| convert_type(*t))
.collect(),
});
index_store.replace(new_index as u32);
new_types.push(Type::Func(Rc::clone(&func_type)));
new_index
}
};
match &new_types[serialized_sig_idx - first_type_index] {
Type::Func(f) => Some((serialized_sig_idx as u32, Rc::clone(f))),
Expand Down
28 changes: 27 additions & 1 deletion crates/wasmparser/src/readers/core/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,33 @@ impl<'a> TypeSectionReader<'a> {
/// Returns an iterator over this type section which will only yield
/// function types and any usage of GC types from the GC proposal will
/// be translated into an error.
pub fn into_iter_err_on_gc_types(self) -> impl Iterator<Item = Result<FuncOrContType>> + 'a {
pub fn into_iter_err_on_gc_types(self) -> impl Iterator<Item = Result<FuncType>> + 'a {
self.into_iter_with_offsets().map(|item| {
let (offset, group) = item?;
let mut types = group.into_types();
let ty = match (types.next(), types.next()) {
(Some(ty), None) => ty,
_ => bail!(offset, "gc proposal not supported"),
};
if !ty.is_final || ty.supertype_idx.is_some() {
bail!(offset, "gc proposal not supported");
}
match ty.composite_type {
CompositeType::Func(f) => Ok(f),
CompositeType::Array(_) | CompositeType::Struct(_) => {
bail!(offset, "gc proposal not supported");
}
CompositeType::Cont(_) => {
bail!(offset, "typed continuations proposal not supported");
}
}
})
}

/// Returns an iterator over this type section which will only yield
/// function types and any usage of GC types from the GC proposal will
/// be translated into an error.
pub fn into_iter_err_on_gc_types_tc(self) -> impl Iterator<Item = Result<FuncOrContType>> + 'a {
// TODO(dhil): Upstream need only return a FuncType at the
// moment, we need both FuncType and ContType. Thus we
// temporary fix the return to be a sum. Eventually upstream
Expand Down
7 changes: 1 addition & 6 deletions crates/wit-component/src/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,7 @@ impl<'a> Module<'a> {
Payload::End(_) => {}
Payload::TypeSection(s) => {
for ty in s.into_iter_err_on_gc_types() {
match ty? {
wasmparser::FuncOrContType::Func(ty) => self.types.push(ty),
wasmparser::FuncOrContType::Cont(_) => {
unimplemented!("Continuation types are not supported yet.")
}
}
self.types.push(ty?);
}
}
Payload::ImportSection(s) => {
Expand Down
4 changes: 0 additions & 4 deletions crates/wit-component/src/linking/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ impl<'a> Metadata<'a> {
Payload::TypeSection(reader) => {
types = reader
.into_iter_err_on_gc_types()
.map(|ty| {
ty.expect("Continuation types are not supported yet.")
.unwrap_func()
})
.collect::<Result<Vec<_>, _>>()?;
}

Expand Down

0 comments on commit 49005a0

Please sign in to comment.