Skip to content

Commit

Permalink
feat: add support for stream with no <T> (#1130)
Browse files Browse the repository at this point in the history
* feat: add support for `stream` with no `<T>`

Signed-off-by: Roman Volosatovs <[email protected]>

test: return `future` in future return test WIT

Signed-off-by: Roman Volosatovs <[email protected]>

build: update wasm-tools

Signed-off-by: Roman Volosatovs <[email protected]>

avoid divide-by-zero in `StreamReader::poll_next`

I hit this when testing "unit streams" (i.e. streams with no payload type)
end-to-end.

BTW, I can't wait to get some runtime tests going in this repo so we can catch
this kind of thing in CI.

Signed-off-by: Joel Dice <[email protected]>

* update to latest wasm-tools

Signed-off-by: Joel Dice <[email protected]>

---------

Signed-off-by: Joel Dice <[email protected]>
Co-authored-by: Joel Dice <[email protected]>
  • Loading branch information
rvolosatovs and dicej authored Jan 22, 2025
1 parent 967d075 commit 9808631
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 82 deletions.
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ prettyplease = "0.2.20"
syn = { version = "2.0.89", features = ["printing"] }
futures = "0.3.31"

wasmparser = "0.223.0"
wasm-encoder = "0.223.0"
wasm-metadata = "0.223.0"
wit-parser = "0.223.0"
wit-component = "0.223.0"
wasmparser = "0.224.0"
wasm-encoder = "0.224.0"
wasm-metadata = "0.224.0"
wit-parser = "0.224.0"
wit-component = "0.224.0"

wit-bindgen-core = { path = 'crates/core', version = '0.37.0' }
wit-bindgen-c = { path = 'crates/c', version = '0.37.0' }
Expand Down
4 changes: 2 additions & 2 deletions crates/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ void __wasm_export_{ns}_{snake}_dtor({ns}_{snake}_t* arg) {{
todo!()
}

fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
_ = (id, name, ty, docs);
todo!()
}
Expand Down Expand Up @@ -1450,7 +1450,7 @@ impl<'a> wit_bindgen_core::AnonymousTypeGenerator<'a> for InterfaceGenerator<'a>
todo!("print_anonymous_type for future");
}

fn anonymous_type_stream(&mut self, _id: TypeId, _ty: &Type, _docs: &Docs) {
fn anonymous_type_stream(&mut self, _id: TypeId, _ty: &Option<Type>, _docs: &Docs) {
todo!("print_anonymous_type for stream");
}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,13 @@ def_instruction! {

/// Create an `i32` from a stream.
StreamLower {
payload: &'a Type,
payload: &'a Option<Type>,
ty: TypeId,
} : [1] => [1],

/// Create a stream from an `i32`.
StreamLift {
payload: &'a Type,
payload: &'a Option<Type>,
ty: TypeId,
} : [1] => [1],

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub trait InterfaceGenerator<'a> {
fn type_list(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs);
fn type_builtin(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs);
fn type_future(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs);
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs);
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs);
fn type_error_context(&mut self, id: TypeId, name: &str, docs: &Docs);
fn types(&mut self, iface: InterfaceId) {
let iface = &self.resolve().interfaces[iface];
Expand Down Expand Up @@ -195,7 +195,7 @@ pub trait AnonymousTypeGenerator<'a> {
fn anonymous_type_result(&mut self, id: TypeId, ty: &Result_, docs: &Docs);
fn anonymous_type_list(&mut self, id: TypeId, ty: &Type, docs: &Docs);
fn anonymous_type_future(&mut self, id: TypeId, ty: &Option<Type>, docs: &Docs);
fn anonymous_type_stream(&mut self, id: TypeId, ty: &Type, docs: &Docs);
fn anonymous_type_stream(&mut self, id: TypeId, ty: &Option<Type>, docs: &Docs);
fn anonymous_type_type(&mut self, id: TypeId, ty: &Type, docs: &Docs);
fn anonymous_type_error_context(&mut self);

Expand Down
2 changes: 1 addition & 1 deletion crates/csharp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> {
todo!()
}

fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
_ = (id, name, ty, docs);
todo!()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/go/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl InterfaceGenerator<'_> {
TypeDefKind::Stream(t) => {
let mut src = String::new();
src.push_str("Stream");
src.push_str(&self.ty_name(t));
src.push_str(&self.optional_ty_name(t.as_ref()));
src.push('T');
src
}
Expand Down Expand Up @@ -1271,7 +1271,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
todo!()
}

fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
_ = (id, name, ty, docs);
todo!()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/guest-rust/rt/src/async_support/stream_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl<T> Stream for StreamReader<T> {
};
Box::pin(async move {
let mut buffer = iter::repeat_with(MaybeUninit::uninit)
.take(ceiling(64 * 1024, mem::size_of::<T>()))
.take(ceiling(64 * 1024, mem::size_of::<T>().max(1)))
.collect::<Vec<_>>();

let result =
Expand Down
17 changes: 11 additions & 6 deletions crates/markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,16 @@ impl InterfaceGenerator<'_> {
self.push_str("future");
}
},
TypeDefKind::Stream(t) => {
self.push_str("stream<");
self.print_ty(t);
self.push_str(">");
}
TypeDefKind::Stream(t) => match t {
Some(t) => {
self.push_str("stream<");
self.print_ty(t);
self.push_str(">");
}
None => {
self.push_str("stream");
}
},
TypeDefKind::ErrorContext => {
self.push_str("error-context");
}
Expand Down Expand Up @@ -661,7 +666,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
todo!()
}

fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
_ = (id, name, ty, docs);
todo!()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/moonbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for InterfaceGenerator<'a> {
todo!()
}

fn type_stream(&mut self, id: TypeId, name: &str, ty: &Type, docs: &Docs) {
fn type_stream(&mut self, id: TypeId, name: &str, ty: &Option<Type>, docs: &Docs) {
_ = (id, name, ty, docs);
todo!()
}
Expand Down
10 changes: 7 additions & 3 deletions crates/rust/src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,13 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::StreamLift { payload, .. } => {
let async_support = self.gen.gen.async_support_path();
let op = &operands[0];
let name = self
.gen
.type_name_owned_with_id(payload, Identifier::StreamOrFuturePayload);
let name = payload
.as_ref()
.map(|ty| {
self.gen
.type_name_owned_with_id(ty, Identifier::StreamOrFuturePayload)
})
.unwrap_or_else(|| "()".into());
let ordinal = self.gen.gen.stream_payloads.get_index_of(&name).unwrap();
let path = self.gen.path_to_root();
results.push(format!(
Expand Down
Loading

0 comments on commit 9808631

Please sign in to comment.