Skip to content

Commit

Permalink
fix: error impl on streams/futures
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Adossi <[email protected]>
  • Loading branch information
vados-cosmonic committed Feb 4, 2025
1 parent b513b1e commit 21d72a1
Show file tree
Hide file tree
Showing 20 changed files with 557 additions and 177 deletions.
15 changes: 5 additions & 10 deletions Cargo.lock

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

9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ system-interface = { version = "0.27.1", features = ["cap_std_impls"] }
io-lifetimes = { version = "2.0.3", default-features = false }
io-extras = "0.18.1"
rustix = "0.38.43"
# wit-bindgen:
wit-bindgen = { version = "0.38.0", default-features = false }
wit-bindgen-rt = { version = "0.38.0", default-features = false }
wit-bindgen-rust-macro = { version = "0.38.0", default-features = false }
# TODO: switch back to released wit-bindgen
wit-bindgen = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "97b7b12fdc36542aee2350f34c178439bd562bbe", default-features = false}
wit-bindgen-rt = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "97b7b12fdc36542aee2350f34c178439bd562bbe", default-features = false}
wit-bindgen-rust-macro = { git = "https://github.com/vados-cosmonic/wit-bindgen", rev = "97b7b12fdc36542aee2350f34c178439bd562bbe", default-features = false}

# wasm-tools family:
wasmparser = { version = "0.224.0", default-features = false, features = ['simd'] }
Expand Down Expand Up @@ -583,4 +583,3 @@ wasm-mutate = { git = "https://github.com/bytecodealliance/wasm-tools" }
wit-parser = { git = "https://github.com/bytecodealliance/wasm-tools" }
wit-component = { git = "https://github.com/bytecodealliance/wasm-tools" }
wasm-wave = { git = "https://github.com/bytecodealliance/wasm-tools" }

79 changes: 48 additions & 31 deletions crates/cranelift/src/compiler/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,35 @@ impl<'a> TrampolineCompiler<'a> {
Trampoline::TaskYield { async_ } => self.translate_task_yield_call(*async_),
Trampoline::SubtaskDrop { instance } => self.translate_subtask_drop_call(*instance),
Trampoline::StreamNew { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
&[ty.as_u32()],
None,
host::stream_new,
ir::types::I64,
),
Trampoline::StreamRead { ty, options } => {
Trampoline::StreamRead {
ty,
err_ctx_ty,
options,
} => {
let tys = &[ty.as_u32(), err_ctx_ty.as_u32()];
if let Some(info) = self.flat_stream_element_info(*ty) {
self.translate_flat_stream_call(*ty, options, host::flat_stream_read, &info)
self.translate_flat_stream_call(tys, options, host::flat_stream_read, &info)
} else {
self.translate_future_or_stream_call(
ty.as_u32(),
tys,
Some(options),
host::stream_read,
ir::types::I64,
)
}
}
Trampoline::StreamWrite { ty, options } => {
let tys = &[ty.as_u32()];
if let Some(info) = self.flat_stream_element_info(*ty) {
self.translate_flat_stream_call(*ty, options, host::flat_stream_write, &info)
self.translate_flat_stream_call(tys, options, host::flat_stream_write, &info)
} else {
self.translate_future_or_stream_call(
ty.as_u32(),
tys,
Some(options),
host::stream_write,
ir::types::I64,
Expand All @@ -156,31 +162,36 @@ impl<'a> TrampolineCompiler<'a> {
self.translate_cancel_call(ty.as_u32(), *async_, host::stream_cancel_write)
}
Trampoline::StreamCloseReadable { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
&[ty.as_u32()],
None,
host::stream_close_readable,
ir::types::I8,
),
Trampoline::StreamCloseWritable { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
None,
host::stream_close_writable,
ir::types::I8,
),
Trampoline::StreamCloseWritable { ty, err_ctx_ty } => self
.translate_future_or_stream_call(
&[ty.as_u32(), err_ctx_ty.as_u32()],
None,
host::stream_close_writable,
ir::types::I8,
),
Trampoline::FutureNew { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
&[ty.as_u32()],
None,
host::future_new,
ir::types::I64,
),
Trampoline::FutureRead { ty, options } => self.translate_future_or_stream_call(
ty.as_u32(),
Trampoline::FutureRead {
ty,
err_ctx_ty,
options,
} => self.translate_future_or_stream_call(
&[ty.as_u32(), err_ctx_ty.as_u32()],
Some(&options),
host::future_read,
ir::types::I64,
),
Trampoline::FutureWrite { ty, options } => self.translate_future_or_stream_call(
ty.as_u32(),
&[ty.as_u32()],
Some(options),
host::future_write,
ir::types::I64,
Expand All @@ -192,17 +203,18 @@ impl<'a> TrampolineCompiler<'a> {
self.translate_cancel_call(ty.as_u32(), *async_, host::future_cancel_write)
}
Trampoline::FutureCloseReadable { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
&[ty.as_u32()],
None,
host::future_close_readable,
ir::types::I8,
),
Trampoline::FutureCloseWritable { ty } => self.translate_future_or_stream_call(
ty.as_u32(),
None,
host::future_close_writable,
ir::types::I8,
),
Trampoline::FutureCloseWritable { ty, err_ctx_ty } => self
.translate_future_or_stream_call(
&[ty.as_u32(), err_ctx_ty.as_u32()],
None,
host::future_close_writable,
ir::types::I8,
),
Trampoline::ErrorContextNew { ty, options } => self.translate_error_context_call(
*ty,
options,
Expand Down Expand Up @@ -1127,7 +1139,7 @@ impl<'a> TrampolineCompiler<'a> {

fn translate_future_or_stream_call(
&mut self,
ty: u32,
tys: &[u32],
options: Option<&CanonicalOptions>,
get_libcall: fn(
&dyn TargetIsa,
Expand Down Expand Up @@ -1168,7 +1180,9 @@ impl<'a> TrampolineCompiler<'a> {
);
}

callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(ty)));
for ty in tys {
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(*ty)));
}

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

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

fn translate_flat_stream_call(
&mut self,
ty: TypeStreamTableIndex,
tys: &[u32],
options: &CanonicalOptions,
get_libcall: fn(
&dyn TargetIsa,
Expand Down Expand Up @@ -1205,16 +1219,19 @@ impl<'a> TrampolineCompiler<'a> {
),
None => self.builder.ins().iconst(pointer_type, 0),
},
self.builder
.ins()
.iconst(ir::types::I32, i64::from(ty.as_u32())),
];
for ty in tys {
callee_args.push(self.builder.ins().iconst(ir::types::I32, i64::from(*ty)));
}

callee_args.extend([
self.builder
.ins()
.iconst(ir::types::I32, i64::from(info.size32)),
self.builder
.ins()
.iconst(ir::types::I32, i64::from(info.align32)),
];
]);

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

Expand Down
10 changes: 5 additions & 5 deletions crates/environ/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,33 @@ macro_rules! foreach_builtin_component_function {
#[cfg(feature = "component-model-async")]
future_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, future: u32, address: u32) -> u64;
#[cfg(feature = "component-model-async")]
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, future: u32, address: u32) -> u64;
future_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, err_ctx_ty: u32, future: u32, address: u32) -> u64;
#[cfg(feature = "component-model-async")]
future_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
#[cfg(feature = "component-model-async")]
future_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
#[cfg(feature = "component-model-async")]
future_close_writable(vmctx: vmctx, ty: u32, writer: u32, error: u32) -> bool;
future_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
#[cfg(feature = "component-model-async")]
future_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
#[cfg(feature = "component-model-async")]
stream_new(vmctx: vmctx, ty: u32) -> u64;
#[cfg(feature = "component-model-async")]
stream_write(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
#[cfg(feature = "component-model-async")]
stream_read(vmctx: vmctx, memory: ptr_u8, realloc: ptr_u8, string_encoding: u8, ty: u32, stream: u32, address: u32, count: u32) -> u64;
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;
#[cfg(feature = "component-model-async")]
stream_cancel_write(vmctx: vmctx, ty: u32, async_: u8, writer: u32) -> u64;
#[cfg(feature = "component-model-async")]
stream_cancel_read(vmctx: vmctx, ty: u32, async_: u8, reader: u32) -> u64;
#[cfg(feature = "component-model-async")]
stream_close_writable(vmctx: vmctx, ty: u32, writer: u32, error: u32) -> bool;
stream_close_writable(vmctx: vmctx, ty: u32, err_ctx_ty: u32, writer: u32, error: u32) -> bool;
#[cfg(feature = "component-model-async")]
stream_close_readable(vmctx: vmctx, ty: u32, reader: u32) -> bool;
#[cfg(feature = "component-model-async")]
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;
#[cfg(feature = "component-model-async")]
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;
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;
#[cfg(feature = "component-model-async")]
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;
#[cfg(feature = "component-model-async")]
Expand Down
32 changes: 26 additions & 6 deletions crates/environ/src/component/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ pub enum Trampoline {
},
StreamRead {
ty: TypeStreamTableIndex,
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
options: CanonicalOptions,
},
StreamWrite {
Expand All @@ -329,12 +330,14 @@ pub enum Trampoline {
},
StreamCloseWritable {
ty: TypeStreamTableIndex,
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
},
FutureNew {
ty: TypeFutureTableIndex,
},
FutureRead {
ty: TypeFutureTableIndex,
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
options: CanonicalOptions,
},
FutureWrite {
Expand All @@ -354,6 +357,7 @@ pub enum Trampoline {
},
FutureCloseWritable {
ty: TypeFutureTableIndex,
err_ctx_ty: TypeComponentLocalErrorContextTableIndex,
},
ErrorContextNew {
ty: TypeComponentLocalErrorContextTableIndex,
Expand Down Expand Up @@ -797,8 +801,13 @@ impl LinearizeDfg<'_> {
instance: *instance,
},
Trampoline::StreamNew { ty } => info::Trampoline::StreamNew { ty: *ty },
Trampoline::StreamRead { ty, options } => info::Trampoline::StreamRead {
Trampoline::StreamRead {
ty,
err_ctx_ty,
options,
} => info::Trampoline::StreamRead {
ty: *ty,
err_ctx_ty: *err_ctx_ty,
options: self.options(options),
},
Trampoline::StreamWrite { ty, options } => info::Trampoline::StreamWrite {
Expand All @@ -816,12 +825,20 @@ impl LinearizeDfg<'_> {
Trampoline::StreamCloseReadable { ty } => {
info::Trampoline::StreamCloseReadable { ty: *ty }
}
Trampoline::StreamCloseWritable { ty } => {
info::Trampoline::StreamCloseWritable { ty: *ty }
Trampoline::StreamCloseWritable { ty, err_ctx_ty } => {
info::Trampoline::StreamCloseWritable {
ty: *ty,
err_ctx_ty: *err_ctx_ty,
}
}
Trampoline::FutureNew { ty } => info::Trampoline::FutureNew { ty: *ty },
Trampoline::FutureRead { ty, options } => info::Trampoline::FutureRead {
Trampoline::FutureRead {
ty,
err_ctx_ty,
options,
} => info::Trampoline::FutureRead {
ty: *ty,
err_ctx_ty: *err_ctx_ty,
options: self.options(options),
},
Trampoline::FutureWrite { ty, options } => info::Trampoline::FutureWrite {
Expand All @@ -839,8 +856,11 @@ impl LinearizeDfg<'_> {
Trampoline::FutureCloseReadable { ty } => {
info::Trampoline::FutureCloseReadable { ty: *ty }
}
Trampoline::FutureCloseWritable { ty } => {
info::Trampoline::FutureCloseWritable { ty: *ty }
Trampoline::FutureCloseWritable { ty, err_ctx_ty } => {
info::Trampoline::FutureCloseWritable {
ty: *ty,
err_ctx_ty: *err_ctx_ty,
}
}
Trampoline::ErrorContextNew { ty, options } => info::Trampoline::ErrorContextNew {
ty: *ty,
Expand Down
Loading

0 comments on commit 21d72a1

Please sign in to comment.