Skip to content

Commit 8d2eded

Browse files
committed
implement Alex' suggetions, part one
1 parent a8cdee7 commit 8d2eded

File tree

19 files changed

+38
-38
lines changed

19 files changed

+38
-38
lines changed

crates/wasm-compose/src/encoding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ impl<'a> TypeEncoder<'a> {
731731
&self,
732732
state: &mut TypeState<'a>,
733733
ty: ct::ComponentValType,
734-
elements: usize,
734+
elements: u32,
735735
) -> u32 {
736736
let ty = self.component_val_type(state, ty);
737737
let index = state.cur.encodable.type_count();

crates/wasm-encoder/src/component/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ impl ComponentDefinedTypeEncoder<'_> {
606606
}
607607

608608
/// Define a fixed size list type.
609-
pub fn fixed_size_list(self, ty: impl Into<ComponentValType>, elements: usize) {
609+
pub fn fixed_size_list(self, ty: impl Into<ComponentValType>, elements: u32) {
610610
self.0.push(0x67);
611611
ty.into().encode(self.0);
612-
(elements as u32).encode(self.0);
612+
elements.encode(self.0);
613613
}
614614

615615
/// Define a tuple type.

crates/wasm-wave/src/value/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Type(pub(super) TypeEnum);
1010
pub(super) enum TypeEnum {
1111
Simple(SimpleType),
1212
List(Arc<ListType>),
13-
FixedSizeList(Arc<ListType>, usize),
13+
FixedSizeList(Arc<ListType>, u32),
1414
Record(Arc<RecordType>),
1515
Tuple(Arc<TupleType>),
1616
Variant(Arc<VariantType>),
@@ -57,7 +57,7 @@ impl Type {
5757
}
5858

5959
/// Returns a list type with the given element type.
60-
pub fn fixed_size_list(element_type: impl Into<Self>, elements: usize) -> Self {
60+
pub fn fixed_size_list(element_type: impl Into<Self>, elements: u32) -> Self {
6161
let element = element_type.into();
6262
Self(TypeEnum::FixedSizeList(
6363
Arc::new(ListType { element }),

crates/wasm-wave/src/value/wit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'a> TypeResolver<'a> {
149149
Ok(value::Type::list(element_type))
150150
}
151151

152-
fn resolve_fixed_size_list(&self, element_type: &Type, elements: usize) -> ValueResult {
152+
fn resolve_fixed_size_list(&self, element_type: &Type, elements: u32) -> ValueResult {
153153
let element_type = self.resolve_type(*element_type)?;
154154
Ok(value::Type::fixed_size_list(element_type, elements))
155155
}

crates/wasmparser/src/features.rs

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ define_wasm_features! {
231231
pub wide_arithmetic: WIDE_ARITHMETIC(1 << 28) = false;
232232
/// Support for component model async lift/lower ABI, as well as streams, futures, and errors.
233233
pub component_model_async: COMPONENT_MODEL_ASYNC(1 << 29) = false;
234+
/// Support for fixed size lists
235+
pub component_model_fixed_size_list: COMPONENT_MODEL_FIXED_SIZE_LIST(1 << 30) = false;
234236
}
235237
}
236238

crates/wasmparser/src/readers/component/types.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ pub enum ComponentDefinedType<'a> {
487487
/// The type is a list of the given value type.
488488
List(ComponentValType),
489489
/// The type is a fixed size list of the given value type.
490-
FixedSizeList(ComponentValType, usize),
490+
FixedSizeList(ComponentValType, u32),
491491
/// The type is a tuple of the given value types.
492492
Tuple(Box<[ComponentValType]>),
493493
/// The type is flags with the given names.
@@ -529,9 +529,6 @@ impl<'a> ComponentDefinedType<'a> {
529529
.collect::<Result<_>>()?,
530530
),
531531
0x70 => ComponentDefinedType::List(reader.read()?),
532-
0x67 => {
533-
ComponentDefinedType::FixedSizeList(reader.read()?, reader.read_var_u32()? as usize)
534-
}
535532
0x6f => ComponentDefinedType::Tuple(
536533
reader
537534
.read_iter(MAX_WASM_TUPLE_TYPES, "tuple types")?
@@ -555,8 +552,9 @@ impl<'a> ComponentDefinedType<'a> {
555552
},
556553
0x69 => ComponentDefinedType::Own(reader.read()?),
557554
0x68 => ComponentDefinedType::Borrow(reader.read()?),
558-
0x65 => ComponentDefinedType::Future(reader.read()?),
555+
0x67 => ComponentDefinedType::FixedSizeList(reader.read()?, reader.read_var_u32()?),
559556
0x66 => ComponentDefinedType::Stream(reader.read()?),
557+
0x65 => ComponentDefinedType::Future(reader.read()?),
560558
0x64 => ComponentDefinedType::ErrorContext,
561559
x => return reader.invalid_leading_byte(x, "component defined type"),
562560
})

crates/wasmparser/src/validator/component.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,12 @@ impl ComponentState {
32783278
self.create_component_val_type(ty, offset)?,
32793279
)),
32803280
crate::ComponentDefinedType::FixedSizeList(ty, elements) => {
3281+
if !features.component_model_fixed_size_list() {
3282+
bail!(
3283+
offset,
3284+
"Fixed size lists require the component model fixed size list feature"
3285+
)
3286+
}
32813287
Ok(ComponentDefinedType::FixedSizeList(
32823288
self.create_component_val_type(ty, offset)?,
32833289
elements,

crates/wasmparser/src/validator/component_types.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ pub enum ComponentDefinedType {
10411041
/// The type is a list.
10421042
List(ComponentValType),
10431043
/// The type is a fixed size list.
1044-
FixedSizeList(ComponentValType, usize),
1044+
FixedSizeList(ComponentValType, u32),
10451045
/// The type is a tuple.
10461046
Tuple(TupleType),
10471047
/// The type is a set of flags.
@@ -1215,7 +1215,8 @@ impl ComponentDefinedType {
12151215
ComponentDefinedType::Enum(_) => "enum",
12161216
ComponentDefinedType::Flags(_) => "flags",
12171217
ComponentDefinedType::Option(_) => "option",
1218-
ComponentDefinedType::List(_) | ComponentDefinedType::FixedSizeList(_, _) => "list",
1218+
ComponentDefinedType::List(_) => "list",
1219+
ComponentDefinedType::FixedSizeList(_, _) => "fixed size list",
12191220
ComponentDefinedType::Result { .. } => "result",
12201221
ComponentDefinedType::Own(_) => "own",
12211222
ComponentDefinedType::Borrow(_) => "borrow",

crates/wasmprinter/src/component.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Printer<'_, '_> {
171171
&mut self,
172172
state: &State,
173173
element_ty: &ComponentValType,
174-
elements: usize,
174+
elements: u32,
175175
) -> Result<()> {
176176
self.start_group("list ")?;
177177
self.print_component_val_type(state, element_ty)?;

crates/wast/src/component/types.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -580,18 +580,14 @@ pub struct List<'a> {
580580
/// The element type of the array.
581581
pub element: Box<ComponentValType<'a>>,
582582
/// Optional fixed size
583-
pub elements: Option<usize>,
583+
pub elements: Option<u32>,
584584
}
585585

586586
impl<'a> Parse<'a> for List<'a> {
587587
fn parse(parser: Parser<'a>) -> Result<Self> {
588588
parser.parse::<kw::list>()?;
589589
let tp = parser.parse()?;
590-
let elements = if !parser.is_empty() {
591-
Some(parser.parse::<u32>()? as usize)
592-
} else {
593-
None
594-
};
590+
let elements = parser.parse()?;
595591
Ok(Self {
596592
element: Box::new(tp),
597593
elements,

crates/wit-component/src/encoding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl TypeContents {
320320
}
321321
TypeDefKind::Enum(_) => Self::empty(),
322322
TypeDefKind::List(t) => Self::for_type(resolve, t) | Self::LIST,
323-
TypeDefKind::FixedSizeList(t, _elements) => Self::for_type(resolve, t) | Self::LIST,
323+
TypeDefKind::FixedSizeList(t, _elements) => Self::for_type(resolve, t),
324324
TypeDefKind::Type(t) => Self::for_type(resolve, t),
325325
TypeDefKind::Future(_) => Self::empty(),
326326
TypeDefKind::Stream(_) => Self::empty(),

crates/wit-component/src/printing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ impl<O: Output> WitPrinter<O> {
976976
resolve: &Resolve,
977977
name: Option<&str>,
978978
ty: &Type,
979-
elements: usize,
979+
elements: u32,
980980
) -> Result<()> {
981981
if let Some(name) = name {
982982
self.output.keyword("type");

crates/wit-encoder/src/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum Type {
2525
Borrow(Ident),
2626
Option(Box<Type>),
2727
Result(Box<Result_>),
28-
List(Box<Type>, Option<usize>),
28+
List(Box<Type>, Option<u32>),
2929
Tuple(Tuple),
3030
Future(Option<Box<Type>>),
3131
Stream(Option<Box<Type>>),
@@ -55,7 +55,7 @@ impl Type {
5555
pub fn result_empty() -> Self {
5656
Type::Result(Box::new(Result_::empty()))
5757
}
58-
pub fn list(type_: Type, size: Option<usize>) -> Self {
58+
pub fn list(type_: Type, size: Option<u32>) -> Self {
5959
Type::List(Box::new(type_), size)
6060
}
6161
pub fn tuple(types: impl IntoIterator<Item = Type>) -> Self {

crates/wit-parser/src/ast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ struct Option_<'a> {
902902
struct List<'a> {
903903
span: Span,
904904
ty: Box<Type<'a>>,
905-
fixed_size: Option<usize>,
905+
fixed_size: Option<u32>,
906906
}
907907

908908
struct Future<'a> {
@@ -1379,7 +1379,7 @@ impl<'a> Type<'a> {
13791379
let size = if tokens.eat(Token::Comma)? {
13801380
let number = tokens.next()?;
13811381
if let Some((span, Token::Integer)) = number {
1382-
let size: usize = tokens.get_span(span).parse()?;
1382+
let size: u32 = tokens.get_span(span).parse()?;
13831383
Some(size)
13841384
} else {
13851385
return Err(err_expected(tokens, "fixed size", number).into());

crates/wit-parser/src/ast/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ enum Key {
9191
Flags(Vec<String>),
9292
Tuple(Vec<Type>),
9393
Enum(Vec<String>),
94-
List(Type, Option<usize>),
94+
List(Type, Option<u32>),
9595
Option(Type),
9696
Result(Option<Type>, Option<Type>),
9797
Future(Option<Type>),

crates/wit-parser/src/decoding.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,6 @@ impl Registrar<'_> {
16021602
ComponentDefinedType::FixedSizeList(t, elements) => {
16031603
let ty = match &self.resolve.types[id].kind {
16041604
TypeDefKind::FixedSizeList(r, elements2) if elements2 == elements => r,
1605-
// Note that all cases below have this match and the general
1606-
// idea is that once a type is named or otherwise identified
1607-
// here there's no need to recurse. The purpose of this
1608-
// registrar is to build connections for anonymous types
1609-
// that don't otherwise have a name to ensure that they're
1610-
// decoded to reuse the same constructs consistently. For
1611-
// that reason once something is named we can bail out.
16121605
TypeDefKind::Type(Type::Id(_)) => return Ok(()),
16131606
_ => bail!("expected a fixed size {elements} list"),
16141607
};

crates/wit-parser/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ pub enum TypeDefKind {
569569
Option(Type),
570570
Result(Result_),
571571
List(Type),
572-
FixedSizeList(Type, usize),
572+
FixedSizeList(Type, u32),
573573
Future(Option<Type>),
574574
Stream(Option<Type>),
575575
ErrorContext,
@@ -598,7 +598,8 @@ impl TypeDefKind {
598598
TypeDefKind::Enum(_) => "enum",
599599
TypeDefKind::Option(_) => "option",
600600
TypeDefKind::Result(_) => "result",
601-
TypeDefKind::List(_) | TypeDefKind::FixedSizeList(..) => "list",
601+
TypeDefKind::List(_) => "list",
602+
TypeDefKind::FixedSizeList(..) => "fixed size list",
602603
TypeDefKind::Future(_) => "future",
603604
TypeDefKind::Stream(_) => "stream",
604605
TypeDefKind::ErrorContext => "error-context",

crates/wit-parser/src/sizealign.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ impl SizeAlign {
246246
let field_align = self.align(t);
247247
let field_size = self.size(t);
248248
ElementInfo::new(
249-
ArchitectureSize::new(field_size.bytes * size, field_size.pointers * size),
249+
ArchitectureSize::new(
250+
field_size.bytes.checked_mul(*size as usize).unwrap(),
251+
field_size.pointers.checked_mul(*size as usize).unwrap(),
252+
),
250253
field_align,
251254
)
252255
}

crates/wit-smith/src/generate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ impl<'a> InterfaceGenerator<'a> {
803803
Some(fuel) => fuel,
804804
None => continue,
805805
};
806-
let elements = u.int_in_range(1..=self.config.max_type_parts)?;
806+
let elements = u.int_in_range(1..=self.config.max_type_parts as u32)?;
807807
dst.push_str("list<");
808808
self.gen_type(u, fuel, dst)?;
809809
dst.push_str(&format!(", {elements}>"));

0 commit comments

Comments
 (0)