components: Fix support for 0-sized flags (#4560)

This commit goes through and updates support in the various argument
passing routines to support 0-sized flags. A bit of a degenerate case
but clarified in WebAssembly/component-model#76 as intentional.
This commit is contained in:
Alex Crichton
2022-08-01 11:05:09 -05:00
committed by GitHub
parent 05e6abf2f6
commit 893fadb485
9 changed files with 59 additions and 9 deletions

View File

@@ -606,6 +606,10 @@ impl Type {
}
Type::Flags(handle) => match FlagsSize::from_count(handle.names().len()) {
FlagsSize::Size0 => SizeAndAlignment {
size: 0,
alignment: 1,
},
FlagsSize::Size1 => SizeAndAlignment {
size: 1,
alignment: 1,

View File

@@ -699,6 +699,7 @@ impl Val {
ty: handle.clone(),
count: u32::try_from(handle.names().len())?,
value: match FlagsSize::from_count(handle.names().len()) {
FlagsSize::Size0 => Box::new([]),
FlagsSize::Size1 => iter::once(u8::load(mem, bytes)? as u32).collect(),
FlagsSize::Size2 => iter::once(u16::load(mem, bytes)? as u32).collect(),
FlagsSize::Size4Plus(n) => (0..n)
@@ -850,6 +851,7 @@ impl Val {
Val::Flags(Flags { count, value, .. }) => {
match FlagsSize::from_count(*count as usize) {
FlagsSize::Size0 => {}
FlagsSize::Size1 => u8::try_from(value[0]).unwrap().store(mem, offset)?,
FlagsSize::Size2 => u16::try_from(value[0]).unwrap().store(mem, offset)?,
FlagsSize::Size4Plus(_) => {
@@ -1018,6 +1020,7 @@ fn lower_list<T>(
/// Note that this will always return at least 1, even if the `count` parameter is zero.
pub(crate) fn u32_count_for_flag_count(count: usize) -> usize {
match FlagsSize::from_count(count) {
FlagsSize::Size0 => 0,
FlagsSize::Size1 | FlagsSize::Size2 => 1,
FlagsSize::Size4Plus(n) => n,
}