Skip to content

Commit 6656c9b

Browse files
committed
necessary parts to make the wit-bindgen test pass
1 parent 228f448 commit 6656c9b

8 files changed

Lines changed: 95 additions & 2 deletions

File tree

crates/cli-flags/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ wasmtime_option_group! {
409409
pub exceptions: Option<bool>,
410410
/// Whether or not any GC infrastructure in Wasmtime is enabled or not.
411411
pub gc_support: Option<bool>,
412+
/// Component model support for fixed size lists: this corresponds
413+
/// to the 🔧 emoji in the component model specification
414+
pub component_model_fixed_size_list: Option<bool>,
412415
}
413416

414417
enum Wasm {
@@ -1067,6 +1070,7 @@ impl CommonOptions {
10671070
("component-model-async", component_model_async_stackful, wasm_component_model_async_stackful)
10681071
("component-model-async", component_model_threading, wasm_component_model_threading)
10691072
("component-model", component_model_error_context, wasm_component_model_error_context)
1073+
("component-model", component_model_fixed_size_list, wasm_component_model_fixed_size_lists)
10701074
("threads", threads, wasm_threads)
10711075
("gc", gc, wasm_gc)
10721076
("gc", reference_types, wasm_reference_types)

crates/environ/src/component/types.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ indices! {
8989
pub struct TypeResultIndex(u32);
9090
/// Index pointing to a list type in the component model.
9191
pub struct TypeListIndex(u32);
92+
/// Index pointing to a fixed size list type in the component model.
93+
pub struct TypeFixedSizeListIndex(u32);
9294
/// Index pointing to a future type in the component model.
9395
pub struct TypeFutureIndex(u32);
9496

@@ -296,6 +298,7 @@ pub struct ComponentTypes {
296298
pub(super) stream_tables: PrimaryMap<TypeStreamTableIndex, TypeStreamTable>,
297299
pub(super) error_context_tables:
298300
PrimaryMap<TypeComponentLocalErrorContextTableIndex, TypeErrorContextTable>,
301+
pub(super) fixed_size_lists: PrimaryMap<TypeFixedSizeListIndex, TypeFixedSizeList>,
299302
}
300303

301304
impl TypeTrace for ComponentTypes {
@@ -369,6 +372,7 @@ impl ComponentTypes {
369372
InterfaceType::Enum(i) => &self[*i].abi,
370373
InterfaceType::Option(i) => &self[*i].abi,
371374
InterfaceType::Result(i) => &self[*i].abi,
375+
InterfaceType::FixedSizeList(i) => &self[*i].abi,
372376
}
373377
}
374378

@@ -418,6 +422,7 @@ impl_index! {
418422
impl Index<TypeFutureTableIndex> for ComponentTypes { TypeFutureTable => future_tables }
419423
impl Index<TypeStreamTableIndex> for ComponentTypes { TypeStreamTable => stream_tables }
420424
impl Index<TypeComponentLocalErrorContextTableIndex> for ComponentTypes { TypeErrorContextTable => error_context_tables }
425+
impl Index<TypeFixedSizeListIndex> for ComponentTypes { TypeFixedSizeList => fixed_size_lists }
421426
}
422427

423428
// Additionally forward anything that can index `ModuleTypes` to `ModuleTypes`
@@ -595,6 +600,7 @@ pub enum InterfaceType {
595600
Future(TypeFutureTableIndex),
596601
Stream(TypeStreamTableIndex),
597602
ErrorContext(TypeComponentLocalErrorContextTableIndex),
603+
FixedSizeList(TypeFixedSizeListIndex),
598604
}
599605

600606
/// Bye information about a type in the canonical ABI, with metadata for both
@@ -1175,6 +1181,17 @@ pub struct TypeList {
11751181
pub element: InterfaceType,
11761182
}
11771183

1184+
/// Shape of a "fixed size list" interface type.
1185+
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
1186+
pub struct TypeFixedSizeList {
1187+
/// The element type of the list.
1188+
pub element: InterfaceType,
1189+
/// The fixed length of the list.
1190+
pub size: u32,
1191+
/// Byte information about this type in the canonical ABI.
1192+
pub abi: CanonicalAbiInfo,
1193+
}
1194+
11781195
/// Maximum number of flat types, for either params or results.
11791196
pub const MAX_FLAT_TYPES: usize = if MAX_FLAT_PARAMS > MAX_FLAT_RESULTS {
11801197
MAX_FLAT_PARAMS

crates/environ/src/component/types_builder.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct ComponentTypesBuilder {
5050
future_tables: HashMap<TypeFutureTable, TypeFutureTableIndex>,
5151
stream_tables: HashMap<TypeStreamTable, TypeStreamTableIndex>,
5252
error_context_tables: HashMap<TypeErrorContextTable, TypeComponentLocalErrorContextTableIndex>,
53+
fixed_size_lists: HashMap<TypeFixedSizeList, TypeFixedSizeListIndex>,
5354

5455
component_types: ComponentTypes,
5556
module_types: ModuleTypesBuilder,
@@ -118,6 +119,7 @@ impl ComponentTypesBuilder {
118119
type_info: TypeInformationCache::default(),
119120
resources: ResourcesBuilder::default(),
120121
abstract_resources: 0,
122+
fixed_size_lists: HashMap::default(),
121123
}
122124
}
123125

@@ -456,8 +458,8 @@ impl ComponentTypesBuilder {
456458
ComponentDefinedType::Stream(ty) => {
457459
InterfaceType::Stream(self.stream_table_type(types, ty)?)
458460
}
459-
ComponentDefinedType::FixedSizeList(..) => {
460-
bail!("support not implemented for fixed-size-lists");
461+
ComponentDefinedType::FixedSizeList(ty, size) => {
462+
InterfaceType::FixedSizeList(self.fixed_size_list_type(types, ty, *size)?)
461463
}
462464
ComponentDefinedType::Map(..) => {
463465
bail!("support not implemented for map type");
@@ -575,6 +577,19 @@ impl ComponentTypesBuilder {
575577
self.add_tuple_type(TypeTuple { types, abi })
576578
}
577579

580+
fn fixed_size_list_type(&mut self, types: TypesRef<'_>, ty: &ComponentValType, size: u32) -> Result<TypeFixedSizeListIndex> {
581+
assert_eq!(types.id(), self.module_types.validator_id());
582+
let element = self.valtype(types, ty)?;
583+
Ok(self.new_fixed_size_list_type(element, size))
584+
}
585+
586+
pub(crate) fn new_fixed_size_list_type(&mut self, element: InterfaceType, size: u32) -> TypeFixedSizeListIndex {
587+
let element_abi = self.component_types.canonical_abi(&element);
588+
let abi = CanonicalAbiInfo::record(
589+
(0..size).into_iter().map(|_| element_abi));
590+
self.add_fixed_size_list_type(TypeFixedSizeList { element, size, abi })
591+
}
592+
578593
fn flags_type(&mut self, flags: &IndexSet<KebabString>) -> TypeFlagsIndex {
579594
let flags = TypeFlags {
580595
names: flags.iter().map(|s| s.to_string()).collect(),
@@ -691,6 +706,11 @@ impl ComponentTypesBuilder {
691706
intern_and_fill_flat_types!(self, tuples, ty)
692707
}
693708

709+
/// Interns a new tuple type within this type information.
710+
pub fn add_fixed_size_list_type(&mut self, ty: TypeFixedSizeList) -> TypeFixedSizeListIndex {
711+
intern_and_fill_flat_types!(self, fixed_size_lists, ty)
712+
}
713+
694714
/// Interns a new variant type within this type information.
695715
pub fn add_variant_type(&mut self, ty: TypeVariant) -> TypeVariantIndex {
696716
intern_and_fill_flat_types!(self, variants, ty)
@@ -826,6 +846,7 @@ impl ComponentTypesBuilder {
826846
InterfaceType::Enum(i) => &self.type_info.enums[*i],
827847
InterfaceType::Option(i) => &self.type_info.options[*i],
828848
InterfaceType::Result(i) => &self.type_info.results[*i],
849+
InterfaceType::FixedSizeList(i) => &self.type_info.fixed_size_lists[*i],
829850
}
830851
}
831852
}
@@ -935,6 +956,7 @@ struct TypeInformationCache {
935956
options: PrimaryMap<TypeOptionIndex, TypeInformation>,
936957
results: PrimaryMap<TypeResultIndex, TypeInformation>,
937958
lists: PrimaryMap<TypeListIndex, TypeInformation>,
959+
fixed_size_lists: PrimaryMap<TypeFixedSizeListIndex, TypeInformation>,
938960
}
939961

940962
struct TypeInformation {
@@ -1084,6 +1106,10 @@ impl TypeInformation {
10841106
self.build_record(ty.types.iter().map(|t| types.type_information(t)));
10851107
}
10861108

1109+
fn fixed_size_lists(&mut self, types: &ComponentTypesBuilder, ty: &TypeFixedSizeList) {
1110+
self.build_record((0..ty.size).into_iter().map(|_| types.type_information(&ty.element)));
1111+
}
1112+
10871113
fn enums(&mut self, _types: &ComponentTypesBuilder, _ty: &TypeEnum) {
10881114
self.depth = 1;
10891115
self.flat.push(FlatType::I32, FlatType::I32);

crates/environ/src/fact/trampoline.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
11071107
| InterfaceType::Future(_)
11081108
| InterfaceType::Stream(_)
11091109
| InterfaceType::ErrorContext(_) => 1,
1110+
InterfaceType::FixedSizeList(i) => self.types[*i].size as usize,
11101111
};
11111112

11121113
match self.fuel.checked_sub(cost) {
@@ -1146,6 +1147,9 @@ impl<'a, 'b> Compiler<'a, 'b> {
11461147
InterfaceType::ErrorContext(t) => {
11471148
self.translate_error_context(*t, src, dst_ty, dst)
11481149
}
1150+
InterfaceType::FixedSizeList(t) => {
1151+
self.translate_fixed_size_list(*t, src, dst_ty, dst);
1152+
}
11491153
}
11501154
}
11511155

@@ -2839,6 +2843,31 @@ impl<'a, 'b> Compiler<'a, 'b> {
28392843
}
28402844
}
28412845

2846+
fn translate_fixed_size_list(
2847+
&mut self,
2848+
src_ty: TypeFixedSizeListIndex,
2849+
src: &Source<'_>,
2850+
dst_ty: &InterfaceType,
2851+
dst: &Destination,
2852+
) {
2853+
let src_ty = &self.types[src_ty];
2854+
let dst_ty = match dst_ty {
2855+
InterfaceType::FixedSizeList(t) => &self.types[*t],
2856+
_ => panic!("expected a fixed size list"),
2857+
};
2858+
2859+
// TODO: subtyping
2860+
assert_eq!(src_ty.size, dst_ty.size);
2861+
2862+
let srcs = src
2863+
.record_field_srcs(self.types, (0..src_ty.size).into_iter().map(|_| src_ty.element));
2864+
let dsts = dst
2865+
.record_field_dsts(self.types, (0..dst_ty.size).into_iter().map(|_| dst_ty.element));
2866+
for (src, dst) in srcs.zip(dsts) {
2867+
self.translate(&src_ty.element, &src, &dst_ty.element, &dst);
2868+
}
2869+
}
2870+
28422871
fn translate_variant(
28432872
&mut self,
28442873
src_ty: TypeVariantIndex,

crates/wasmtime/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,16 @@ impl Config {
12961296
self
12971297
}
12981298

1299+
/// This corresponds to the 🔧 emoji in the component model specification.
1300+
///
1301+
/// Please note that Wasmtime's support for this feature is _very_
1302+
/// incomplete.
1303+
#[cfg(feature = "component-model")]
1304+
pub fn wasm_component_model_fixed_size_lists(&mut self, enable: bool) -> &mut Self {
1305+
self.wasm_feature(WasmFeatures::CM_FIXED_SIZE_LIST, enable);
1306+
self
1307+
}
1308+
12991309
/// Configures whether the [Exception-handling proposal][proposal] is enabled or not.
13001310
///
13011311
/// [proposal]: https://github.com/WebAssembly/exception-handling

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,7 @@ pub fn desc(ty: &InterfaceType) -> &'static str {
28952895
InterfaceType::Future(_) => "future",
28962896
InterfaceType::Stream(_) => "stream",
28972897
InterfaceType::ErrorContext(_) => "error-context",
2898+
InterfaceType::FixedSizeList(_) => "list<_, N>",
28982899
}
28992900
}
29002901

crates/wasmtime/src/runtime/component/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ impl TypeChecker<'_> {
158158
(InterfaceType::Stream(_), _) => false,
159159
(InterfaceType::ErrorContext(_), InterfaceType::ErrorContext(_)) => true,
160160
(InterfaceType::ErrorContext(_), _) => false,
161+
(InterfaceType::FixedSizeList(_), _) => todo!(),
161162
}
162163
}
163164

@@ -855,6 +856,7 @@ impl Type {
855856
InterfaceType::Future(index) => Type::Future(instance.future_type(*index)),
856857
InterfaceType::Stream(index) => Type::Stream(instance.stream_type(*index)),
857858
InterfaceType::ErrorContext(_) => Type::ErrorContext,
859+
InterfaceType::FixedSizeList(_) => todo!(),
858860
}
859861
}
860862

crates/wasmtime/src/runtime/component/values.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl Val {
215215
InterfaceType::ErrorContext(_) => {
216216
ErrorContext::linear_lift_from_flat(cx, ty, next(src))?.into_val()
217217
}
218+
InterfaceType::FixedSizeList(_) => todo!(),
218219
})
219220
}
220221

@@ -341,6 +342,7 @@ impl Val {
341342
InterfaceType::ErrorContext(_) => {
342343
ErrorContext::linear_lift_from_memory(cx, ty, bytes)?.into_val()
343344
}
345+
InterfaceType::FixedSizeList(_) => todo!(),
344346
})
345347
}
346348

@@ -491,6 +493,7 @@ impl Val {
491493
)
492494
}
493495
(InterfaceType::ErrorContext(_), _) => unexpected(ty, self),
496+
(InterfaceType::FixedSizeList(_), _) => todo!(),
494497
}
495498
}
496499

@@ -644,6 +647,7 @@ impl Val {
644647
)
645648
}
646649
(InterfaceType::ErrorContext(_), _) => unexpected(ty, self),
650+
(InterfaceType::FixedSizeList(_), _) => todo!(),
647651
}
648652
}
649653

0 commit comments

Comments
 (0)