Skip to content

Commit c87e8c5

Browse files
committed
necessary parts to make the wit-bindgen test pass
1 parent cff811b commit c87e8c5

14 files changed

Lines changed: 132 additions & 37 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,16 @@ wit-bindgen = { version = "0.41.0", default-features = false }
307307
wit-bindgen-rust-macro = { version = "0.41.0", default-features = false }
308308

309309
# wasm-tools family:
310-
wasmparser = { version = "0.229.0", default-features = false, features = ['simd'] }
311-
wat = "1.229.0"
312-
wast = "229.0.0"
313-
wasmprinter = "0.229.0"
314-
wasm-encoder = "0.229.0"
315-
wasm-smith = "0.229.0"
316-
wasm-mutate = "0.229.0"
317-
wit-parser = "0.229.0"
318-
wit-component = "0.229.0"
319-
wasm-wave = "0.229.0"
310+
wasmparser = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
311+
wat = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
312+
wast = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
313+
wasmprinter = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
314+
wasm-encoder = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
315+
wasm-smith = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
316+
wasm-mutate = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
317+
wit-parser = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
318+
wit-component = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
319+
wasm-wave = { git = "https://github.com/cpetig/wasm-tools", branch = "fixed-length-list" }
320320

321321
# Non-Bytecode Alliance maintained dependencies:
322322
# --------------------------

crates/cli-flags/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ wasmtime_option_group! {
386386
pub exceptions: Option<bool>,
387387
/// DEPRECATED: Configure support for the legacy exceptions proposal.
388388
pub legacy_exceptions: Option<bool>,
389+
/// Component model support for fixed size lists: this corresponds
390+
/// to the 🔧 emoji in the component model specification
391+
pub component_model_fixed_size_list: Option<bool>,
389392
}
390393

391394
enum Wasm {
@@ -1015,6 +1018,7 @@ impl CommonOptions {
10151018
("component-model-async", component_model_async, wasm_component_model_async)
10161019
("component-model-async", component_model_async_builtins, wasm_component_model_async_builtins)
10171020
("component-model-async", component_model_async_stackful, wasm_component_model_async_stackful)
1021+
("component-model", component_model_fixed_size_list, wasm_component_model_fixed_size_lists)
10181022
("threads", threads, wasm_threads)
10191023
("gc", gc, wasm_gc)
10201024
("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

@@ -285,6 +287,7 @@ pub struct ComponentTypes {
285287
pub(super) stream_tables: PrimaryMap<TypeStreamTableIndex, TypeStreamTable>,
286288
pub(super) error_context_tables:
287289
PrimaryMap<TypeComponentLocalErrorContextTableIndex, TypeErrorContextTable>,
290+
pub(super) fixed_size_lists: PrimaryMap<TypeFixedSizeListIndex, TypeFixedSizeList>,
288291
}
289292

290293
impl TypeTrace for ComponentTypes {
@@ -358,6 +361,7 @@ impl ComponentTypes {
358361
InterfaceType::Enum(i) => &self[*i].abi,
359362
InterfaceType::Option(i) => &self[*i].abi,
360363
InterfaceType::Result(i) => &self[*i].abi,
364+
InterfaceType::FixedSizeList(i) => &self[*i].abi,
361365
}
362366
}
363367

@@ -407,6 +411,7 @@ impl_index! {
407411
impl Index<TypeFutureTableIndex> for ComponentTypes { TypeFutureTable => future_tables }
408412
impl Index<TypeStreamTableIndex> for ComponentTypes { TypeStreamTable => stream_tables }
409413
impl Index<TypeComponentLocalErrorContextTableIndex> for ComponentTypes { TypeErrorContextTable => error_context_tables }
414+
impl Index<TypeFixedSizeListIndex> for ComponentTypes { TypeFixedSizeList => fixed_size_lists }
410415
}
411416

412417
// Additionally forward anything that can index `ModuleTypes` to `ModuleTypes`
@@ -582,6 +587,7 @@ pub enum InterfaceType {
582587
Future(TypeFutureTableIndex),
583588
Stream(TypeStreamTableIndex),
584589
ErrorContext(TypeComponentLocalErrorContextTableIndex),
590+
FixedSizeList(TypeFixedSizeListIndex),
585591
}
586592

587593
/// Bye information about a type in the canonical ABI, with metadata for both
@@ -1129,6 +1135,17 @@ pub struct TypeList {
11291135
pub element: InterfaceType,
11301136
}
11311137

1138+
/// Shape of a "fixed size list" interface type.
1139+
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
1140+
pub struct TypeFixedSizeList {
1141+
/// The element type of the list.
1142+
pub element: InterfaceType,
1143+
/// The fixed length of the list.
1144+
pub size: u32,
1145+
/// Byte information about this type in the canonical ABI.
1146+
pub abi: CanonicalAbiInfo,
1147+
}
1148+
11321149
/// Maximum number of flat types, for either params or results.
11331150
pub const MAX_FLAT_TYPES: usize = if MAX_FLAT_PARAMS > MAX_FLAT_RESULTS {
11341151
MAX_FLAT_PARAMS

crates/environ/src/component/types_builder.rs

Lines changed: 29 additions & 0 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,
@@ -111,6 +112,7 @@ impl ComponentTypesBuilder {
111112
component_types: ComponentTypes::default(),
112113
type_info: TypeInformationCache::default(),
113114
resources: ResourcesBuilder::default(),
115+
fixed_size_lists: HashMap::default(),
114116
}
115117
}
116118

@@ -418,6 +420,9 @@ impl ComponentTypesBuilder {
418420
ComponentDefinedType::Stream(ty) => {
419421
InterfaceType::Stream(self.stream_table_type(types, ty)?)
420422
}
423+
ComponentDefinedType::FixedSizeList(ty, size) => {
424+
InterfaceType::FixedSizeList(self.fixed_size_list_type(types, ty, *size)?)
425+
}
421426
};
422427
let info = self.type_information(&ret);
423428
if info.depth > MAX_TYPE_DEPTH {
@@ -531,6 +536,19 @@ impl ComponentTypesBuilder {
531536
self.add_tuple_type(TypeTuple { types, abi })
532537
}
533538

539+
fn fixed_size_list_type(&mut self, types: TypesRef<'_>, ty: &ComponentValType, size: u32) -> Result<TypeFixedSizeListIndex> {
540+
assert_eq!(types.id(), self.module_types.validator_id());
541+
let element = self.valtype(types, ty)?;
542+
Ok(self.new_fixed_size_list_type(element, size))
543+
}
544+
545+
pub(crate) fn new_fixed_size_list_type(&mut self, element: InterfaceType, size: u32) -> TypeFixedSizeListIndex {
546+
let element_abi = self.component_types.canonical_abi(&element);
547+
let abi = CanonicalAbiInfo::record(
548+
(0..size).into_iter().map(|_| element_abi));
549+
self.add_fixed_size_list_type(TypeFixedSizeList { element, size, abi })
550+
}
551+
534552
fn flags_type(&mut self, flags: &IndexSet<KebabString>) -> TypeFlagsIndex {
535553
let flags = TypeFlags {
536554
names: flags.iter().map(|s| s.to_string()).collect(),
@@ -647,6 +665,11 @@ impl ComponentTypesBuilder {
647665
intern_and_fill_flat_types!(self, tuples, ty)
648666
}
649667

668+
/// Interns a new tuple type within this type information.
669+
pub fn add_fixed_size_list_type(&mut self, ty: TypeFixedSizeList) -> TypeFixedSizeListIndex {
670+
intern_and_fill_flat_types!(self, fixed_size_lists, ty)
671+
}
672+
650673
/// Interns a new variant type within this type information.
651674
pub fn add_variant_type(&mut self, ty: TypeVariant) -> TypeVariantIndex {
652675
intern_and_fill_flat_types!(self, variants, ty)
@@ -782,6 +805,7 @@ impl ComponentTypesBuilder {
782805
InterfaceType::Enum(i) => &self.type_info.enums[*i],
783806
InterfaceType::Option(i) => &self.type_info.options[*i],
784807
InterfaceType::Result(i) => &self.type_info.results[*i],
808+
InterfaceType::FixedSizeList(i) => &self.type_info.fixed_size_lists[*i],
785809
}
786810
}
787811
}
@@ -891,6 +915,7 @@ struct TypeInformationCache {
891915
options: PrimaryMap<TypeOptionIndex, TypeInformation>,
892916
results: PrimaryMap<TypeResultIndex, TypeInformation>,
893917
lists: PrimaryMap<TypeListIndex, TypeInformation>,
918+
fixed_size_lists: PrimaryMap<TypeFixedSizeListIndex, TypeInformation>,
894919
}
895920

896921
struct TypeInformation {
@@ -1040,6 +1065,10 @@ impl TypeInformation {
10401065
self.build_record(ty.types.iter().map(|t| types.type_information(t)));
10411066
}
10421067

1068+
fn fixed_size_lists(&mut self, types: &ComponentTypesBuilder, ty: &TypeFixedSizeList) {
1069+
self.build_record((0..ty.size).into_iter().map(|_| types.type_information(&ty.element)));
1070+
}
1071+
10431072
fn enums(&mut self, _types: &ComponentTypesBuilder, _ty: &TypeEnum) {
10441073
self.depth = 1;
10451074
self.flat.push(FlatType::I32, FlatType::I32);

crates/environ/src/fact/trampoline.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
//! can be somewhat arbitrary, an intentional decision.
1717
1818
use crate::component::{
19-
CanonicalAbiInfo, ComponentTypesBuilder, FixedEncoding as FE, FlatType, InterfaceType,
20-
StringEncoding, Transcode, TypeComponentLocalErrorContextTableIndex, TypeEnumIndex,
21-
TypeFlagsIndex, TypeFutureTableIndex, TypeListIndex, TypeOptionIndex, TypeRecordIndex,
22-
TypeResourceTableIndex, TypeResultIndex, TypeStreamTableIndex, TypeTupleIndex,
23-
TypeVariantIndex, VariantInfo, FLAG_MAY_ENTER, FLAG_MAY_LEAVE, MAX_FLAT_PARAMS,
19+
CanonicalAbiInfo, ComponentTypesBuilder, FixedEncoding as FE, FlatType, InterfaceType, StringEncoding, Transcode, TypeComponentLocalErrorContextTableIndex, TypeEnumIndex, TypeFixedSizeListIndex, TypeFlagsIndex, TypeFutureTableIndex, TypeListIndex, TypeOptionIndex, TypeRecordIndex, TypeResourceTableIndex, TypeResultIndex, TypeStreamTableIndex, TypeTupleIndex, TypeVariantIndex, VariantInfo, FLAG_MAY_ENTER, FLAG_MAY_LEAVE, MAX_FLAT_PARAMS
2420
};
2521
use crate::fact::signature::Signature;
2622
use crate::fact::transcode::Transcoder;
@@ -1044,6 +1040,7 @@ impl<'a, 'b> Compiler<'a, 'b> {
10441040
| InterfaceType::Future(_)
10451041
| InterfaceType::Stream(_)
10461042
| InterfaceType::ErrorContext(_) => 1,
1043+
InterfaceType::FixedSizeList(i) => self.types[*i].size as usize,
10471044
};
10481045

10491046
match self.fuel.checked_sub(cost) {
@@ -1083,6 +1080,9 @@ impl<'a, 'b> Compiler<'a, 'b> {
10831080
InterfaceType::ErrorContext(t) => {
10841081
self.translate_error_context(*t, src, dst_ty, dst)
10851082
}
1083+
InterfaceType::FixedSizeList(t) => {
1084+
self.translate_fixed_size_list(*t, src, dst_ty, dst);
1085+
}
10861086
}
10871087
}
10881088

@@ -2628,6 +2628,31 @@ impl<'a, 'b> Compiler<'a, 'b> {
26282628
}
26292629
}
26302630

2631+
fn translate_fixed_size_list(
2632+
&mut self,
2633+
src_ty: TypeFixedSizeListIndex,
2634+
src: &Source<'_>,
2635+
dst_ty: &InterfaceType,
2636+
dst: &Destination,
2637+
) {
2638+
let src_ty = &self.types[src_ty];
2639+
let dst_ty = match dst_ty {
2640+
InterfaceType::FixedSizeList(t) => &self.types[*t],
2641+
_ => panic!("expected a fixed size list"),
2642+
};
2643+
2644+
// TODO: subtyping
2645+
assert_eq!(src_ty.size, dst_ty.size);
2646+
2647+
let srcs = src
2648+
.record_field_srcs(self.types, (0..src_ty.size).into_iter().map(|_| src_ty.element));
2649+
let dsts = dst
2650+
.record_field_dsts(self.types, (0..dst_ty.size).into_iter().map(|_| dst_ty.element));
2651+
for (src, dst) in srcs.zip(dsts) {
2652+
self.translate(&src_ty.element, &src, &dst_ty.element, &dst);
2653+
}
2654+
}
2655+
26312656
fn translate_variant(
26322657
&mut self,
26332658
src_ty: TypeVariantIndex,

crates/wasmtime/src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,16 @@ impl Config {
11591159
self
11601160
}
11611161

1162+
/// This corresponds to the 🔧 emoji in the component model specification.
1163+
///
1164+
/// Please note that Wasmtime's support for this feature is _very_
1165+
/// incomplete.
1166+
#[cfg(feature = "component-model")]
1167+
pub fn wasm_component_model_fixed_size_lists(&mut self, enable: bool) -> &mut Self {
1168+
self.wasm_feature(WasmFeatures::CM_FIXED_SIZE_LIST, enable);
1169+
self
1170+
}
1171+
11621172
#[doc(hidden)] // FIXME(#3427) - if/when implemented then un-hide this
11631173
pub fn wasm_exceptions(&mut self, enable: bool) -> &mut Self {
11641174
self.wasm_feature(WasmFeatures::EXCEPTIONS, enable);

0 commit comments

Comments
 (0)