Skip to content

Commit d510879

Browse files
fix: error impl on streams/futures (#3)
Signed-off-by: Victor Adossi <[email protected]> Co-authored-by: Joel Dice <[email protected]>
1 parent e4ffbf1 commit d510879

25 files changed

+832
-243
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ system-interface = { version = "0.27.1", features = ["cap_std_impls"] }
299299
io-lifetimes = { version = "2.0.3", default-features = false }
300300
io-extras = "0.18.1"
301301
rustix = "0.38.43"
302-
# wit-bindgen:
303-
wit-bindgen = { version = "0.39.0", default-features = false }
304-
wit-bindgen-rt = { version = "0.39.0", default-features = false }
305-
wit-bindgen-rust-macro = { version = "0.39.0", default-features = false }
302+
# TODO: switch back to released wit-bindgen
303+
wit-bindgen = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "2eacbfc036beeb151d43acc6d1b241172fa80576", default-features = false}
304+
wit-bindgen-rt = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "2eacbfc036beeb151d43acc6d1b241172fa80576", default-features = false}
305+
wit-bindgen-rust-macro = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "2eacbfc036beeb151d43acc6d1b241172fa80576", default-features = false}
306306

307307
# wasm-tools family:
308308
wasmparser = { version = "0.225.0", default-features = false, features = ['simd'] }

crates/cranelift/src/compiler/component.rs

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,35 @@ impl<'a> TrampolineCompiler<'a> {
120120
Trampoline::TaskYield { async_ } => self.translate_task_yield_call(*async_),
121121
Trampoline::SubtaskDrop { instance } => self.translate_subtask_drop_call(*instance),
122122
Trampoline::StreamNew { ty } => self.translate_future_or_stream_call(
123-
ty.as_u32(),
123+
&[ty.as_u32()],
124124
None,
125125
host::stream_new,
126126
ir::types::I64,
127127
),
128-
Trampoline::StreamRead { ty, options } => {
128+
Trampoline::StreamRead {
129+
ty,
130+
err_ctx_ty,
131+
options,
132+
} => {
133+
let tys = &[ty.as_u32(), err_ctx_ty.as_u32()];
129134
if let Some(info) = self.flat_stream_element_info(*ty) {
130-
self.translate_flat_stream_call(*ty, options, host::flat_stream_read, &info)
135+
self.translate_flat_stream_call(tys, options, host::flat_stream_read, &info)
131136
} else {
132137
self.translate_future_or_stream_call(
133-
ty.as_u32(),
138+
tys,
134139
Some(options),
135140
host::stream_read,
136141
ir::types::I64,
137142
)
138143
}
139144
}
140145
Trampoline::StreamWrite { ty, options } => {
146+
let tys = &[ty.as_u32()];
141147
if let Some(info) = self.flat_stream_element_info(*ty) {
142-
self.translate_flat_stream_call(*ty, options, host::flat_stream_write, &info)
148+
self.translate_flat_stream_call(tys, options, host::flat_stream_write, &info)
143149
} else {
144150
self.translate_future_or_stream_call(
145-
ty.as_u32(),
151+
tys,
146152
Some(options),
147153
host::stream_write,
148154
ir::types::I64,
@@ -156,31 +162,36 @@ impl<'a> TrampolineCompiler<'a> {
156162
self.translate_cancel_call(ty.as_u32(), *async_, host::stream_cancel_write)
157163
}
158164
Trampoline::StreamCloseReadable { ty } => self.translate_future_or_stream_call(
159-
ty.as_u32(),
165+
&[ty.as_u32()],
160166
None,
161167
host::stream_close_readable,
162168
ir::types::I8,
163169
),
164-
Trampoline::StreamCloseWritable { ty } => self.translate_future_or_stream_call(
165-
ty.as_u32(),
166-
None,
167-
host::stream_close_writable,
168-
ir::types::I8,
169-
),
170+
Trampoline::StreamCloseWritable { ty, err_ctx_ty } => self
171+
.translate_future_or_stream_call(
172+
&[ty.as_u32(), err_ctx_ty.as_u32()],
173+
None,
174+
host::stream_close_writable,
175+
ir::types::I8,
176+
),
170177
Trampoline::FutureNew { ty } => self.translate_future_or_stream_call(
171-
ty.as_u32(),
178+
&[ty.as_u32()],
172179
None,
173180
host::future_new,
174181
ir::types::I64,
175182
),
176-
Trampoline::FutureRead { ty, options } => self.translate_future_or_stream_call(
177-
ty.as_u32(),
183+
Trampoline::FutureRead {
184+
ty,
185+
err_ctx_ty,
186+
options,
187+
} => self.translate_future_or_stream_call(
188+
&[ty.as_u32(), err_ctx_ty.as_u32()],
178189
Some(&options),
179190
host::future_read,
180191
ir::types::I64,
181192
),
182193
Trampoline::FutureWrite { ty, options } => self.translate_future_or_stream_call(
183-
ty.as_u32(),
194+
&[ty.as_u32()],
184195
Some(options),
185196
host::future_write,
186197
ir::types::I64,
@@ -192,17 +203,18 @@ impl<'a> TrampolineCompiler<'a> {
192203
self.translate_cancel_call(ty.as_u32(), *async_, host::future_cancel_write)
193204
}
194205
Trampoline::FutureCloseReadable { ty } => self.translate_future_or_stream_call(
195-
ty.as_u32(),
206+
&[ty.as_u32()],
196207
None,
197208
host::future_close_readable,
198209
ir::types::I8,
199210
),
200-
Trampoline::FutureCloseWritable { ty } => self.translate_future_or_stream_call(
201-
ty.as_u32(),
202-
None,
203-
host::future_close_writable,
204-
ir::types::I8,
205-
),
211+
Trampoline::FutureCloseWritable { ty, err_ctx_ty } => self
212+
.translate_future_or_stream_call(
213+
&[ty.as_u32(), err_ctx_ty.as_u32()],
214+
None,
215+
host::future_close_writable,
216+
ir::types::I8,
217+
),
206218
Trampoline::ErrorContextNew { ty, options } => self.translate_error_context_call(
207219
*ty,
208220
options,
@@ -1127,7 +1139,7 @@ impl<'a> TrampolineCompiler<'a> {
11271139

11281140
fn translate_future_or_stream_call(
11291141
&mut self,
1130-
ty: u32,
1142+
tys: &[u32],
11311143
options: Option<&CanonicalOptions>,
11321144
get_libcall: fn(
11331145
&dyn TargetIsa,
@@ -1168,7 +1180,9 @@ impl<'a> TrampolineCompiler<'a> {
11681180
);
11691181
}
11701182

1171-
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(ty)));
1183+
for ty in tys {
1184+
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(*ty)));
1185+
}
11721186

11731187
callee_args.extend(args[2..].iter().copied());
11741188

@@ -1177,7 +1191,7 @@ impl<'a> TrampolineCompiler<'a> {
11771191

11781192
fn translate_flat_stream_call(
11791193
&mut self,
1180-
ty: TypeStreamTableIndex,
1194+
tys: &[u32],
11811195
options: &CanonicalOptions,
11821196
get_libcall: fn(
11831197
&dyn TargetIsa,
@@ -1205,16 +1219,19 @@ impl<'a> TrampolineCompiler<'a> {
12051219
),
12061220
None => self.builder.ins().iconst(pointer_type, 0),
12071221
},
1208-
self.builder
1209-
.ins()
1210-
.iconst(ir::types::I32, i64::from(ty.as_u32())),
1222+
];
1223+
for ty in tys {
1224+
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(*ty)));
1225+
}
1226+
1227+
callee_args.extend([
12111228
self.builder
12121229
.ins()
12131230
.iconst(ir::types::I32, i64::from(info.size32)),
12141231
self.builder
12151232
.ins()
12161233
.iconst(ir::types::I32, i64::from(info.align32)),
1217-
];
1234+
]);
12181235

12191236
callee_args.extend(args[2..].iter().copied());
12201237

crates/environ/src/component.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,33 @@ macro_rules! foreach_builtin_component_function {
108108
#[cfg(feature = "component-model-async")]
109109
future_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, future: u32, address: u32) -> u64;
110110
#[cfg(feature = "component-model-async")]
111-
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, future: u32, address: u32) -> u64;
111+
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
112112
#[cfg(feature = "component-model-async")]
113113
future_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
114114
#[cfg(feature = "component-model-async")]
115115
future_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
116116
#[cfg(feature = "component-model-async")]
117-
future_close_writable(vmctx: vmctx, ty: u32, writer: u32, error: u32) -> bool;
117+
future_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
118118
#[cfg(feature = "component-model-async")]
119119
future_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
120120
#[cfg(feature = "component-model-async")]
121121
stream_new(vmctx: vmctx, ty: u32) -> u64;
122122
#[cfg(feature = "component-model-async")]
123123
stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
124124
#[cfg(feature = "component-model-async")]
125-
stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
125+
stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, stream: u32, address: u32, count: u32) -> u64;
126126
#[cfg(feature = "component-model-async")]
127127
stream_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
128128
#[cfg(feature = "component-model-async")]
129129
stream_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
130130
#[cfg(feature = "component-model-async")]
131-
stream_close_writable(vmctx: vmctx, ty: u32, writer: u32, error: u32) -> bool;
131+
stream_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
132132
#[cfg(feature = "component-model-async")]
133133
stream_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
134134
#[cfg(feature = "component-model-async")]
135135
flat_stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
136136
#[cfg(feature = "component-model-async")]
137-
flat_stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
137+
flat_stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, ty: u32, err_ctx_ty: u32, payload_size: u32, payload_align: u32, stream: u32, address: u32, count: u32) -> u64;
138138
#[cfg(feature = "component-model-async")]
139139
error_context_new(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, debug_msg_address: u32, debug_msg_len: u32) -> u64;
140140
#[cfg(feature = "component-model-async")]

crates/environ/src/component/dfg.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ pub enum Trampoline {
310310
},
311311
StreamRead {
312312
ty: TypeStreamTableIndex,
313+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
313314
options: CanonicalOptions,
314315
},
315316
StreamWrite {
@@ -329,12 +330,14 @@ pub enum Trampoline {
329330
},
330331
StreamCloseWritable {
331332
ty: TypeStreamTableIndex,
333+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
332334
},
333335
FutureNew {
334336
ty: TypeFutureTableIndex,
335337
},
336338
FutureRead {
337339
ty: TypeFutureTableIndex,
340+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
338341
options: CanonicalOptions,
339342
},
340343
FutureWrite {
@@ -354,6 +357,7 @@ pub enum Trampoline {
354357
},
355358
FutureCloseWritable {
356359
ty: TypeFutureTableIndex,
360+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
357361
},
358362
ErrorContextNew {
359363
ty: TypeComponentLocalErrorContextTableIndex,
@@ -797,8 +801,13 @@ impl LinearizeDfg<'_> {
797801
instance: *instance,
798802
},
799803
Trampoline::StreamNew { ty } => info::Trampoline::StreamNew { ty: *ty },
800-
Trampoline::StreamRead { ty, options } => info::Trampoline::StreamRead {
804+
Trampoline::StreamRead {
805+
ty,
806+
err_ctx_ty,
807+
options,
808+
} => info::Trampoline::StreamRead {
801809
ty: *ty,
810+
err_ctx_ty: *err_ctx_ty,
802811
options: self.options(options),
803812
},
804813
Trampoline::StreamWrite { ty, options } => info::Trampoline::StreamWrite {
@@ -816,12 +825,20 @@ impl LinearizeDfg<'_> {
816825
Trampoline::StreamCloseReadable { ty } => {
817826
info::Trampoline::StreamCloseReadable { ty: *ty }
818827
}
819-
Trampoline::StreamCloseWritable { ty } => {
820-
info::Trampoline::StreamCloseWritable { ty: *ty }
828+
Trampoline::StreamCloseWritable { ty, err_ctx_ty } => {
829+
info::Trampoline::StreamCloseWritable {
830+
ty: *ty,
831+
err_ctx_ty: *err_ctx_ty,
832+
}
821833
}
822834
Trampoline::FutureNew { ty } => info::Trampoline::FutureNew { ty: *ty },
823-
Trampoline::FutureRead { ty, options } => info::Trampoline::FutureRead {
835+
Trampoline::FutureRead {
836+
ty,
837+
err_ctx_ty,
838+
options,
839+
} => info::Trampoline::FutureRead {
824840
ty: *ty,
841+
err_ctx_ty: *err_ctx_ty,
825842
options: self.options(options),
826843
},
827844
Trampoline::FutureWrite { ty, options } => info::Trampoline::FutureWrite {
@@ -839,8 +856,11 @@ impl LinearizeDfg<'_> {
839856
Trampoline::FutureCloseReadable { ty } => {
840857
info::Trampoline::FutureCloseReadable { ty: *ty }
841858
}
842-
Trampoline::FutureCloseWritable { ty } => {
843-
info::Trampoline::FutureCloseWritable { ty: *ty }
859+
Trampoline::FutureCloseWritable { ty, err_ctx_ty } => {
860+
info::Trampoline::FutureCloseWritable {
861+
ty: *ty,
862+
err_ctx_ty: *err_ctx_ty,
863+
}
844864
}
845865
Trampoline::ErrorContextNew { ty, options } => info::Trampoline::ErrorContextNew {
846866
ty: *ty,

crates/environ/src/component/info.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,10 @@ pub enum Trampoline {
741741
StreamRead {
742742
/// The table index for the specific `stream` type and caller instance.
743743
ty: TypeStreamTableIndex,
744+
745+
/// The table index for the `error-context` type in the caller instance.
746+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
747+
744748
/// Any options (e.g. string encoding) to use when storing values to
745749
/// memory.
746750
options: CanonicalOptions,
@@ -787,6 +791,9 @@ pub enum Trampoline {
787791
StreamCloseWritable {
788792
/// The table index for the specific `stream` type and caller instance.
789793
ty: TypeStreamTableIndex,
794+
795+
/// The table index for the `error-context` type in the caller instance.
796+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
790797
},
791798

792799
/// A `future.new` intrinsic to create a new `future` handle of the
@@ -800,6 +807,10 @@ pub enum Trampoline {
800807
FutureRead {
801808
/// The table index for the specific `future` type and caller instance.
802809
ty: TypeFutureTableIndex,
810+
811+
/// The table index for the `error-context` type in the caller instance.
812+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
813+
803814
/// Any options (e.g. string encoding) to use when storing values to
804815
/// memory.
805816
options: CanonicalOptions,
@@ -846,6 +857,9 @@ pub enum Trampoline {
846857
FutureCloseWritable {
847858
/// The table index for the specific `future` type and caller instance.
848859
ty: TypeFutureTableIndex,
860+
861+
/// The table index for the `error-context` type in the caller instance.
862+
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
849863
},
850864

851865
/// A `error-context.new` intrinsic to create a new `error-context` with a

0 commit comments

Comments
 (0)