Skip to content

Commit ad33634

Browse files
authored
feat: add support for stream with no <T> (#1978)
* feat: add support for `stream` with no `<T>` Signed-off-by: Roman Volosatovs <[email protected]> * test(wit-parser): add a test for `stream` Signed-off-by: Roman Volosatovs <[email protected]> --------- Signed-off-by: Roman Volosatovs <[email protected]>
1 parent a4cd054 commit ad33634

File tree

25 files changed

+184
-91
lines changed

25 files changed

+184
-91
lines changed

crates/wasm-compose/src/encoding.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,8 @@ impl<'a> TypeEncoder<'a> {
813813
index
814814
}
815815

816-
fn stream(&self, state: &mut TypeState<'a>, ty: ct::ComponentValType) -> u32 {
817-
let ty = self.component_val_type(state, ty);
816+
fn stream(&self, state: &mut TypeState<'a>, ty: Option<ct::ComponentValType>) -> u32 {
817+
let ty = ty.map(|ty| self.component_val_type(state, ty));
818818

819819
let index = state.cur.encodable.type_count();
820820
state.cur.encodable.ty().defined_type().stream(ty);
@@ -1255,9 +1255,7 @@ impl DependencyRegistrar<'_, '_> {
12551255
| ComponentDefinedType::Enum(_)
12561256
| ComponentDefinedType::Flags(_)
12571257
| ComponentDefinedType::ErrorContext => {}
1258-
ComponentDefinedType::List(t)
1259-
| ComponentDefinedType::Option(t)
1260-
| ComponentDefinedType::Stream(t) => self.val_type(*t),
1258+
ComponentDefinedType::List(t) | ComponentDefinedType::Option(t) => self.val_type(*t),
12611259
ComponentDefinedType::Own(r) | ComponentDefinedType::Borrow(r) => {
12621260
self.ty(ComponentAnyTypeId::Resource(*r))
12631261
}
@@ -1291,6 +1289,11 @@ impl DependencyRegistrar<'_, '_> {
12911289
self.val_type(*ty);
12921290
}
12931291
}
1292+
ComponentDefinedType::Stream(ty) => {
1293+
if let Some(ty) = ty {
1294+
self.val_type(*ty);
1295+
}
1296+
}
12941297
}
12951298
}
12961299
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl ComponentDefinedTypeEncoder<'_> {
680680
}
681681

682682
/// Define a `stream` type with the specified payload.
683-
pub fn stream(self, payload: ComponentValType) {
683+
pub fn stream(self, payload: Option<ComponentValType>) {
684684
self.0.push(0x66);
685685
payload.encode(self.0);
686686
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ pub mod component_utils {
804804
defined.future(t.map(|t| reencoder.component_val_type(t)));
805805
}
806806
wasmparser::ComponentDefinedType::Stream(t) => {
807-
defined.stream(reencoder.component_val_type(t));
807+
defined.stream(t.map(|t| reencoder.component_val_type(t)));
808808
}
809809
wasmparser::ComponentDefinedType::ErrorContext => defined.error_context(),
810810
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ pub enum ComponentDefinedType<'a> {
508508
/// A future type with the specified payload type.
509509
Future(Option<ComponentValType>),
510510
/// A stream type with the specified payload type.
511-
Stream(ComponentValType),
511+
Stream(Option<ComponentValType>),
512512
/// The error-context type.
513513
ErrorContext,
514514
}

crates/wasmparser/src/validator/component.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,10 @@ impl ComponentState {
759759
.as_ref()
760760
.map(|ty| types.type_named_valtype(ty, set))
761761
.unwrap_or(true),
762-
ComponentDefinedType::Stream(ty) => types.type_named_valtype(ty, set),
762+
ComponentDefinedType::Stream(ty) => ty
763+
.as_ref()
764+
.map(|ty| types.type_named_valtype(ty, set))
765+
.unwrap_or(true),
763766
}
764767
}
765768

@@ -1255,7 +1258,9 @@ impl ComponentState {
12551258

12561259
let mut info = LoweringInfo::default();
12571260
info.requires_memory = true;
1258-
info.requires_realloc = payload_type.contains_ptr(types);
1261+
info.requires_realloc = payload_type
1262+
.map(|ty| ty.contains_ptr(types))
1263+
.unwrap_or_default();
12591264
self.check_options(None, &info, &options, types, offset, features, true)?;
12601265

12611266
self.core_funcs
@@ -1439,7 +1444,7 @@ impl ComponentState {
14391444
info.requires_memory = true;
14401445
info.requires_realloc = payload_type
14411446
.map(|ty| ty.contains_ptr(types))
1442-
.unwrap_or(false);
1447+
.unwrap_or_default();
14431448
self.check_options(None, &info, &options, types, offset, features, true)?;
14441449

14451450
self.core_funcs
@@ -3291,7 +3296,8 @@ impl ComponentState {
32913296
.transpose()?,
32923297
)),
32933298
crate::ComponentDefinedType::Stream(ty) => Ok(ComponentDefinedType::Stream(
3294-
self.create_component_val_type(ty, offset)?,
3299+
ty.map(|ty| self.create_component_val_type(ty, offset))
3300+
.transpose()?,
32953301
)),
32963302
crate::ComponentDefinedType::ErrorContext => Ok(ComponentDefinedType::ErrorContext),
32973303
}

crates/wasmparser/src/validator/component_types.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ pub enum ComponentDefinedType {
10621062
/// A future type with the specified payload type.
10631063
Future(Option<ComponentValType>),
10641064
/// A stream type with the specified payload type.
1065-
Stream(ComponentValType),
1065+
Stream(Option<ComponentValType>),
10661066
/// The error-context type.
10671067
ErrorContext,
10681068
}
@@ -1950,9 +1950,7 @@ impl TypeAlloc {
19501950
}
19511951
}
19521952
}
1953-
ComponentDefinedType::List(ty)
1954-
| ComponentDefinedType::Option(ty)
1955-
| ComponentDefinedType::Stream(ty) => {
1953+
ComponentDefinedType::List(ty) | ComponentDefinedType::Option(ty) => {
19561954
self.free_variables_valtype(ty, set);
19571955
}
19581956
ComponentDefinedType::Result { ok, err } => {
@@ -1971,6 +1969,11 @@ impl TypeAlloc {
19711969
self.free_variables_valtype(ty, set);
19721970
}
19731971
}
1972+
ComponentDefinedType::Stream(ty) => {
1973+
if let Some(ty) = ty {
1974+
self.free_variables_valtype(ty, set);
1975+
}
1976+
}
19741977
}
19751978
}
19761979

@@ -2094,9 +2097,9 @@ impl TypeAlloc {
20942097
.map(|t| self.type_named_valtype(t, set))
20952098
.unwrap_or(true)
20962099
}
2097-
ComponentDefinedType::List(ty)
2098-
| ComponentDefinedType::Option(ty)
2099-
| ComponentDefinedType::Stream(ty) => self.type_named_valtype(ty, set),
2100+
ComponentDefinedType::List(ty) | ComponentDefinedType::Option(ty) => {
2101+
self.type_named_valtype(ty, set)
2102+
}
21002103

21012104
// own/borrow themselves don't have to be named, but the resource
21022105
// they refer to must be named.
@@ -2108,6 +2111,11 @@ impl TypeAlloc {
21082111
.as_ref()
21092112
.map(|ty| self.type_named_valtype(ty, set))
21102113
.unwrap_or(true),
2114+
2115+
ComponentDefinedType::Stream(ty) => ty
2116+
.as_ref()
2117+
.map(|ty| self.type_named_valtype(ty, set))
2118+
.unwrap_or(true),
21112119
}
21122120
}
21132121

@@ -2277,9 +2285,7 @@ where
22772285
}
22782286
}
22792287
}
2280-
ComponentDefinedType::List(ty)
2281-
| ComponentDefinedType::Option(ty)
2282-
| ComponentDefinedType::Stream(ty) => {
2288+
ComponentDefinedType::List(ty) | ComponentDefinedType::Option(ty) => {
22832289
any_changed |= self.remap_valtype(ty, map);
22842290
}
22852291
ComponentDefinedType::Result { ok, err } => {
@@ -2293,7 +2299,7 @@ where
22932299
ComponentDefinedType::Own(id) | ComponentDefinedType::Borrow(id) => {
22942300
any_changed |= self.remap_resource_id(id, map);
22952301
}
2296-
ComponentDefinedType::Future(ty) => {
2302+
ComponentDefinedType::Future(ty) | ComponentDefinedType::Stream(ty) => {
22972303
if let Some(ty) = ty {
22982304
any_changed |= self.remap_valtype(ty, map);
22992305
}
@@ -3234,9 +3240,14 @@ impl<'a> SubtypeCx<'a> {
32343240
(Some(_), None) => bail!(offset, "expected future type to not be present"),
32353241
},
32363242
(Future(_), b) => bail!(offset, "expected {}, found future", b.desc()),
3237-
(Stream(a), Stream(b)) => self
3238-
.component_val_type(a, b, offset)
3239-
.with_context(|| "type mismatch in stream"),
3243+
(Stream(a), Stream(b)) => match (a, b) {
3244+
(None, None) => Ok(()),
3245+
(Some(a), Some(b)) => self
3246+
.component_val_type(a, b, offset)
3247+
.with_context(|| "type mismatch in stream"),
3248+
(None, Some(_)) => bail!(offset, "expected stream type, but found none"),
3249+
(Some(_), None) => bail!(offset, "expected stream type to not be present"),
3250+
},
32403251
(Stream(_), b) => bail!(offset, "expected {}, found stream", b.desc()),
32413252
(ErrorContext, ErrorContext) => Ok(()),
32423253
(ErrorContext, b) => bail!(offset, "expected {}, found error-context", b.desc()),

crates/wasmprinter/src/component.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,14 @@ impl Printer<'_, '_> {
240240
Ok(())
241241
}
242242

243-
fn print_stream_type(&mut self, state: &State, ty: ComponentValType) -> Result<()> {
243+
fn print_stream_type(&mut self, state: &State, ty: Option<ComponentValType>) -> Result<()> {
244244
self.start_group("stream")?;
245-
self.result.write_str(" ")?;
246-
self.print_component_val_type(state, &ty)?;
245+
246+
if let Some(ty) = ty {
247+
self.result.write_str(" ")?;
248+
self.print_component_val_type(state, &ty)?;
249+
}
250+
247251
self.end_group()?;
248252

249253
Ok(())

crates/wast/src/component/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn encode_defined_type(encoder: ComponentDefinedTypeEncoder, ty: &ComponentDefin
133133
}
134134
ComponentDefinedType::Own(i) => encoder.own((*i).into()),
135135
ComponentDefinedType::Borrow(i) => encoder.borrow((*i).into()),
136-
ComponentDefinedType::Stream(s) => encoder.stream(s.element.as_ref().into()),
136+
ComponentDefinedType::Stream(s) => encoder.stream(s.element.as_deref().map(Into::into)),
137137
ComponentDefinedType::Future(f) => encoder.future(f.element.as_deref().map(Into::into)),
138138
}
139139
}

crates/wast/src/component/expand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,9 @@ impl<'a> Expander<'a> {
762762
}
763763
ComponentDefinedType::Own(_) | ComponentDefinedType::Borrow(_) => {}
764764
ComponentDefinedType::Stream(t) => {
765-
self.expand_component_val_ty(&mut t.element);
765+
if let Some(ty) = &mut t.element {
766+
self.expand_component_val_ty(ty);
767+
}
766768
}
767769
ComponentDefinedType::Future(t) => {
768770
if let Some(ty) = &mut t.element {

crates/wast/src/component/resolve.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,9 @@ impl<'a> Resolver<'a> {
553553
self.resolve_ns(t, Ns::Type)?;
554554
}
555555
ComponentDefinedType::Stream(s) => {
556-
self.component_val_type(&mut s.element)?;
556+
if let Some(ty) = &mut s.element {
557+
self.component_val_type(ty)?;
558+
}
557559
}
558560
ComponentDefinedType::Future(f) => {
559561
if let Some(ty) = &mut f.element {

0 commit comments

Comments
 (0)