Skip to content

Commit a7e274f

Browse files
feat(jco): add future lower tests, refactor existing
1 parent 952f6ba commit a7e274f

6 files changed

Lines changed: 870 additions & 200 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
mod bindings {
2+
use super::Component;
3+
wit_bindgen::generate!({
4+
world: "future-lower",
5+
});
6+
export!(Component);
7+
}
8+
9+
use wit_bindgen::{FutureReader, StreamReader};
10+
11+
use bindings::exports::jco::test_components::future_lower_async;
12+
use bindings::exports::jco::test_components::future_lower_sync;
13+
use bindings::jco::test_components::resources;
14+
15+
use bindings::exports::jco::test_components::future_lower_async::{
16+
ExampleEnum, ExampleFlags, ExampleRecord, ExampleVariant,
17+
};
18+
19+
struct Component;
20+
21+
impl future_lower_sync::Guest for Component {
22+
fn future_passthrough(rx: FutureReader<u32>) -> FutureReader<u32> {
23+
rx
24+
}
25+
}
26+
27+
impl future_lower_async::Guest for Component {
28+
async fn future_passthrough(rx: FutureReader<u32>) -> FutureReader<u32> {
29+
rx
30+
}
31+
32+
async fn read_future_value_bool(rx: FutureReader<bool>) -> bool {
33+
rx.await
34+
}
35+
36+
async fn read_future_value_u8(rx: FutureReader<u8>) -> u8 {
37+
rx.await
38+
}
39+
40+
async fn read_future_value_s8(rx: FutureReader<i8>) -> i8 {
41+
rx.await
42+
}
43+
44+
async fn read_future_value_u16(rx: FutureReader<u16>) -> u16 {
45+
rx.await
46+
}
47+
48+
async fn read_future_value_s16(rx: FutureReader<i16>) -> i16 {
49+
rx.await
50+
}
51+
52+
async fn read_future_value_u32(rx: FutureReader<u32>) -> u32 {
53+
rx.await
54+
}
55+
56+
async fn read_future_value_s32(rx: FutureReader<i32>) -> i32 {
57+
rx.await
58+
}
59+
60+
async fn read_future_value_u64(rx: FutureReader<u64>) -> u64 {
61+
rx.await
62+
}
63+
64+
async fn read_future_value_s64(rx: FutureReader<i64>) -> i64 {
65+
rx.await
66+
}
67+
68+
async fn read_future_value_f32(rx: FutureReader<f32>) -> f32 {
69+
rx.await
70+
}
71+
72+
async fn read_future_value_f64(rx: FutureReader<f64>) -> f64 {
73+
rx.await
74+
}
75+
76+
async fn read_future_value_string(rx: FutureReader<String>) -> String {
77+
rx.await
78+
}
79+
80+
async fn read_future_value_record(rx: FutureReader<ExampleRecord>) -> ExampleRecord {
81+
rx.await
82+
}
83+
84+
async fn read_future_value_variant(rx: FutureReader<ExampleVariant>) -> ExampleVariant {
85+
rx.await
86+
}
87+
88+
async fn read_future_value_option_string(rx: FutureReader<Option<String>>) -> Option<String> {
89+
rx.await
90+
}
91+
92+
async fn read_future_value_result_string(
93+
rx: FutureReader<Result<String, String>>,
94+
) -> Result<String, String> {
95+
rx.await
96+
}
97+
98+
async fn read_future_value_tuple(rx: FutureReader<(u32, i32, String)>) -> (u32, i32, String) {
99+
rx.await
100+
}
101+
102+
async fn read_future_value_flags(rx: FutureReader<ExampleFlags>) -> ExampleFlags {
103+
rx.await
104+
}
105+
106+
async fn read_future_value_enum(rx: FutureReader<ExampleEnum>) -> ExampleEnum {
107+
rx.await
108+
}
109+
110+
async fn read_future_value_list_u8(rx: FutureReader<Vec<u8>>) -> Vec<u8> {
111+
rx.await
112+
}
113+
114+
async fn read_future_value_list_string(rx: FutureReader<Vec<String>>) -> Vec<String> {
115+
rx.await
116+
}
117+
118+
async fn read_future_value_fixed_list_u32(rx: FutureReader<[u32; 5]>) -> [u32; 5] {
119+
rx.await
120+
}
121+
122+
async fn read_future_value_list_record(
123+
rx: FutureReader<Vec<ExampleRecord>>,
124+
) -> Vec<ExampleRecord> {
125+
rx.await
126+
}
127+
128+
async fn read_future_value_example_resource_own(rx: FutureReader<resources::ExampleResource>) {
129+
let _ = rx.await;
130+
// All vals dropped at the end of this function
131+
}
132+
133+
async fn read_future_value_example_resource_own_attr(
134+
rx: FutureReader<resources::ExampleResource>,
135+
) -> u32 {
136+
rx.await.get_id()
137+
}
138+
139+
async fn read_future_value_future_string(rx: FutureReader<FutureReader<String>>) -> String {
140+
rx.await.await
141+
}
142+
143+
async fn read_future_value_stream_string(
144+
rx: FutureReader<StreamReader<String>>,
145+
) -> Vec<String> {
146+
let s = rx.await;
147+
s.collect().await
148+
}
149+
}
150+
151+
// Stub only to ensure this works as a binary
152+
fn main() {}

crates/test-components/src/bin/stream_rx.rs renamed to crates/test-components/src/bin/stream_lower.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
mod bindings {
22
use super::Component;
33
wit_bindgen::generate!({
4-
world: "stream-rx",
4+
world: "stream-lower",
55
});
66
export!(Component);
77
}
88

9-
use wit_bindgen::StreamReader;
9+
use wit_bindgen::{FutureReader, StreamReader};
1010

11-
use bindings::exports::jco::test_components::use_stream_async;
12-
use bindings::exports::jco::test_components::use_stream_sync;
11+
use bindings::exports::jco::test_components::stream_lower_async;
12+
use bindings::exports::jco::test_components::stream_lower_sync;
1313
use bindings::jco::test_components::resources;
1414

15-
use bindings::exports::jco::test_components::use_stream_async::{
15+
use bindings::exports::jco::test_components::stream_lower_async::{
1616
ExampleEnum, ExampleFlags, ExampleRecord, ExampleVariant,
1717
};
1818

1919
struct Component;
2020

21-
impl use_stream_sync::Guest for Component {
21+
impl stream_lower_sync::Guest for Component {
2222
fn stream_passthrough(rx: StreamReader<u32>) -> StreamReader<u32> {
2323
rx
2424
}
2525
}
2626

27-
impl use_stream_async::Guest for Component {
27+
impl stream_lower_async::Guest for Component {
2828
async fn stream_passthrough(rx: StreamReader<u32>) -> StreamReader<u32> {
2929
rx
3030
}
@@ -153,6 +153,16 @@ impl use_stream_async::Guest for Component {
153153
}
154154
vals
155155
}
156+
157+
async fn read_stream_values_future_string(
158+
mut stream_rx: StreamReader<FutureReader<String>>,
159+
) -> Vec<String> {
160+
let mut vals = Vec::new();
161+
while let Some(fut_rx) = stream_rx.next().await {
162+
vals.push(fut_rx.await);
163+
}
164+
vals
165+
}
156166
}
157167

158168
async fn read_async_values<T>(mut rx: StreamReader<T>) -> Vec<T> {

crates/test-components/wit/all.wit

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ interface get-stream-async {
119119
get-stream-future-string: async func(vals: list<future<string>>) -> stream<future<string>>;
120120
}
121121

122-
123122
interface get-future-async {
124123
use resources.{example-resource};
125124
use example-types.{example-variant, example-enum, example-record, example-flags};
@@ -189,7 +188,7 @@ interface get-future-async {
189188
// The unspooled version should take advantage of host->host optimizations
190189
get-future-stream-string: async func(vals: stream<string>) -> future<stream<string>>;
191190

192-
// The spooled version returns a *new* stream that forces transit through the component
191+
// The spooled version returns a *new* stream that forces transit through the component
193192
get-future-stream-string-spool: async func(vals: list<string>) -> future<stream<string>>;
194193
}
195194

@@ -257,16 +256,16 @@ world basic-run-string {
257256
export local-run-string;
258257
}
259258

260-
//////////////////
261-
// Stream Usage //
262-
//////////////////
259+
//////////////////////
260+
// Stream lowerings //
261+
//////////////////////
263262

264-
interface use-stream-sync {
265-
// TODO(fix): optimize host-only stream usage -- detect when the read & write are held by the host
263+
interface stream-lower-sync {
264+
// TODO(fix): optimize host-only stream usage -- detect when the read & write are held by the host
266265
stream-passthrough: func(s: stream<u32>) -> stream<u32>;
267266
}
268267

269-
interface use-stream-async {
268+
interface stream-lower-async {
270269
use resources.{example-resource};
271270
use example-types.{example-variant, example-enum, example-record, example-flags};
272271

@@ -317,12 +316,91 @@ interface use-stream-async {
317316

318317
read-stream-values-stream-string: async func(s: stream<stream<string>>) -> list<list<string>>;
319318

320-
//read-stream-values-future-string: async func(vals: list<future<string>>) -> result<stream<future<string>>, string>;
319+
read-stream-values-future-string: async func(vals: stream<future<string>>) -> list<string>;
321320
}
322321

323-
world stream-rx {
322+
world stream-lower {
324323
import resources;
325324

326-
export use-stream-sync;
327-
export use-stream-async;
328-
}
325+
export stream-lower-sync;
326+
export stream-lower-async;
327+
}
328+
329+
//////////////////////
330+
// Future lowerings //
331+
//////////////////////
332+
333+
interface future-lower-sync {
334+
// TODO(fix): optimize host-only stream usage -- detect when the read & write are held by the host
335+
future-passthrough: func(s: future<u32>) -> future<u32>;
336+
}
337+
338+
interface future-lower-async {
339+
use resources.{example-resource};
340+
use example-types.{example-variant, example-enum, example-record, example-flags};
341+
342+
future-passthrough: async func(s: future<u32>) -> future<u32>;
343+
344+
read-future-value-bool: async func(f: future<bool>) -> bool;
345+
346+
read-future-value-u32: async func(f: future<u32>) -> u32;
347+
read-future-value-s32: async func(f: future<s32>) -> s32;
348+
349+
read-future-value-u8: async func(f: future<u8>) -> u8;
350+
read-future-value-s8: async func(f: future<s8>) -> s8;
351+
352+
read-future-value-u16: async func(f: future<u16>) -> u16;
353+
read-future-value-s16: async func(f: future<s16>) -> s16;
354+
355+
read-future-value-u64: async func(f: future<u64>) -> u64;
356+
read-future-value-s64: async func(f: future<s64>) -> s64;
357+
358+
read-future-value-f32: async func(f: future<f32>) -> f32;
359+
360+
read-future-value-f64: async func(f: future<f64>) -> f64;
361+
362+
// NOTE: future<char> is not yet supported upfuture (future<u8> can be used instead)
363+
//
364+
// get-future-char: async func(vals: list<char>) -> result<future<char>, string>;
365+
366+
read-future-value-string: async func(f: future<string>) -> string;
367+
368+
read-future-value-record: async func(f: future<example-record>) -> example-record;
369+
370+
read-future-value-variant: async func(f: future<example-variant>) -> example-variant;
371+
372+
read-future-value-list-u8: async func(f: future<list<u8>>) -> list<u8>;
373+
read-future-value-list-string: async func(f: future<list<string>>) -> list<string>;
374+
read-future-value-list-record: async func(f: future<list<example-record>>) -> list<example-record>;
375+
read-future-value-fixed-list-u32: async func(f: future<list<u32, 5>>) -> list<u32, 5>;
376+
377+
read-future-value-tuple: async func(f: future<tuple<u32, s32, string>>) -> tuple<u32, s32, string>;
378+
379+
read-future-value-flags: async func(f: future<example-flags>) -> example-flags;
380+
381+
read-future-value-enum: async func(f: future<example-enum>) -> example-enum;
382+
383+
read-future-value-option-string: async func(f: future<option<string>>) -> option<string>;
384+
385+
read-future-value-result-string: async func(f: future<result<string, string>>) -> result<string, string>;
386+
387+
// NOTE: future<borrow<t>> is not supoprted
388+
//
389+
// get-future-example-resource-borrow: async func(
390+
// vals: list<borrow<example-resource>>
391+
// ) -> result<future<borrow<example-resource>>, string>;
392+
393+
read-future-value-example-resource-own: async func(s: future<example-resource>);
394+
395+
read-future-value-example-resource-own-attr: async func(s: future<example-resource>) -> u32;
396+
397+
read-future-value-future-string: async func(f: future<future<string>>) -> string;
398+
399+
read-future-value-stream-string: async func(vals: future<stream<string>>) -> list<string>;
400+
}
401+
402+
world future-lower {
403+
import resources;
404+
export future-lower-sync;
405+
export future-lower-async;
406+
}

0 commit comments

Comments
 (0)