Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More refactoring/reshuffling of component async intrinsics #2064

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 34 additions & 78 deletions crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ impl<'a> EncodingState<'a> {
.unwrap();
let exports = self.info.exports_for(module);
let realloc_index = exports
.export_realloc_for(key, func)
.export_realloc_for(key, &func.name)
.map(|name| self.core_alias_export(instance_index, name, ExportKind::Func));
let mut options = options
.into_iter(encoding, self.memory_index, realloc_index)?
Expand Down Expand Up @@ -1271,7 +1271,6 @@ impl<'a> EncodingState<'a> {
for_module,
info,
kind,
async_: _,
} => {
let metadata = self.info.module_metadata_for(*for_module);
let exports = self.info.exports_for(*for_module);
Expand All @@ -1280,21 +1279,21 @@ impl<'a> EncodingState<'a> {
(
metadata
.import_encodings
.get(resolve, &info.key, &info.function.name),
exports.import_realloc_for(info.interface, &info.function.name),
.get(resolve, &info.key, &info.function),
exports.import_realloc_for(info.interface, &info.function),
)
} else {
(
metadata
.export_encodings
.get(resolve, &info.key, &info.function.name),
.get(resolve, &info.key, &info.function),
exports.export_realloc_for(&info.key, &info.function),
)
};
let encoding = encoding.unwrap_or(StringEncoding::UTF8);
let realloc_index = realloc
.map(|name| self.core_alias_export(instance_index, name, ExportKind::Func));
let type_index = self.payload_type_index(info.ty, info.imported)?;
let type_index = self.payload_type_index(info)?;
let options =
shim.options
.into_iter(encoding, self.memory_index, realloc_index)?;
Expand Down Expand Up @@ -1360,32 +1359,14 @@ impl<'a> EncodingState<'a> {
/// Note that the payload type `T` of `stream<T>` or `future<T>` may be an
/// imported or exported type, and that determines the appropriate type
/// encoder to use.
fn payload_type_index(&mut self, ty: TypeId, imported: bool) -> Result<u32> {
// `stream` and `future` types don't have owners, but their payload
// types (or the payload type of the payload type, etc. in the case of
// nesting) might have an owner, in which case we need to find that in
// order to make the types match up e.g. when we're exporting a resource
// that's used as a payload type.
fn owner(resolve: &Resolve, ty: TypeId) -> Option<InterfaceId> {
let def = &resolve.types[ty];
match &def.kind {
TypeDefKind::Future(Some(Type::Id(ty)))
| TypeDefKind::Stream(Some(Type::Id(ty))) => owner(resolve, *ty),
_ => match &def.owner {
TypeOwner::World(_) | TypeOwner::None => None,
TypeOwner::Interface(id) => Some(*id),
},
}
}

fn payload_type_index(&mut self, info: &PayloadInfo) -> Result<u32> {
let resolve = &self.info.encoder.metadata.resolve;
let ComponentValType::Type(type_index) = if imported {
self.root_import_type_encoder(None)
let ComponentValType::Type(type_index) = if info.imported {
self.root_import_type_encoder(info.interface)
} else {
let owner = owner(resolve, ty);
self.root_export_type_encoder(owner)
self.root_export_type_encoder(info.interface)
}
.encode_valtype(resolve, &Type::Id(ty))?
.encode_valtype(resolve, &Type::Id(info.ty))?
else {
unreachable!()
};
Expand Down Expand Up @@ -1609,10 +1590,10 @@ impl<'a> EncodingState<'a> {
AbiVariant::GuestImport,
)
}
Import::ExportedTaskReturn(interface, function) => {
Import::ExportedTaskReturn(interface, _function, result) => {
let mut encoder = self.root_export_type_encoder(*interface);

let result = match &function.result {
let result = match result {
Some(ty) => Some(encoder.encode_valtype(resolve, ty)?),
None => None,
};
Expand All @@ -1639,96 +1620,76 @@ impl<'a> EncodingState<'a> {
Ok((ExportKind::Func, index))
}
Import::StreamNew(info) => {
let ty = self.payload_type_index(info.ty, info.imported)?;
let ty = self.payload_type_index(info)?;
let index = self.component.stream_new(ty);
Ok((ExportKind::Func, index))
}
Import::StreamRead { async_, info } => Ok(self.materialize_payload_import(
Import::StreamRead { info, .. } => Ok(self.materialize_payload_import(
shims,
for_module,
*async_,
info,
PayloadFuncKind::StreamRead,
)),
Import::StreamWrite { async_, info } => Ok(self.materialize_payload_import(
Import::StreamWrite { info, .. } => Ok(self.materialize_payload_import(
shims,
for_module,
*async_,
info,
PayloadFuncKind::StreamWrite,
)),
Import::StreamCancelRead {
ty,
imported,
async_,
} => {
let ty = self.payload_type_index(*ty, *imported)?;
Import::StreamCancelRead { info, async_ } => {
let ty = self.payload_type_index(info)?;
let index = self.component.stream_cancel_read(ty, *async_);
Ok((ExportKind::Func, index))
}
Import::StreamCancelWrite {
ty,
imported,
async_,
} => {
let ty = self.payload_type_index(*ty, *imported)?;
Import::StreamCancelWrite { info, async_ } => {
let ty = self.payload_type_index(info)?;
let index = self.component.stream_cancel_write(ty, *async_);
Ok((ExportKind::Func, index))
}
Import::StreamCloseReadable { ty, imported } => {
let type_index = self.payload_type_index(*ty, *imported)?;
Import::StreamCloseReadable(info) => {
let type_index = self.payload_type_index(info)?;
let index = self.component.stream_close_readable(type_index);
Ok((ExportKind::Func, index))
}
Import::StreamCloseWritable { ty, imported } => {
let type_index = self.payload_type_index(*ty, *imported)?;
Import::StreamCloseWritable(info) => {
let type_index = self.payload_type_index(info)?;
let index = self.component.stream_close_writable(type_index);
Ok((ExportKind::Func, index))
}
Import::FutureNew(info) => {
let ty = self.payload_type_index(info.ty, info.imported)?;
let ty = self.payload_type_index(info)?;
let index = self.component.future_new(ty);
Ok((ExportKind::Func, index))
}
Import::FutureRead { async_, info } => Ok(self.materialize_payload_import(
Import::FutureRead { info, .. } => Ok(self.materialize_payload_import(
shims,
for_module,
*async_,
info,
PayloadFuncKind::FutureRead,
)),
Import::FutureWrite { async_, info } => Ok(self.materialize_payload_import(
Import::FutureWrite { info, .. } => Ok(self.materialize_payload_import(
shims,
for_module,
*async_,
info,
PayloadFuncKind::FutureWrite,
)),
Import::FutureCancelRead {
ty,
imported,
async_,
} => {
let ty = self.payload_type_index(*ty, *imported)?;
Import::FutureCancelRead { info, async_ } => {
let ty = self.payload_type_index(info)?;
let index = self.component.future_cancel_read(ty, *async_);
Ok((ExportKind::Func, index))
}
Import::FutureCancelWrite {
ty,
imported,
async_,
} => {
let ty = self.payload_type_index(*ty, *imported)?;
Import::FutureCancelWrite { info, async_ } => {
let ty = self.payload_type_index(info)?;
let index = self.component.future_cancel_write(ty, *async_);
Ok((ExportKind::Func, index))
}
Import::FutureCloseReadable { ty, imported } => {
let type_index = self.payload_type_index(*ty, *imported)?;
Import::FutureCloseReadable(info) => {
let type_index = self.payload_type_index(info)?;
let index = self.component.future_close_readable(type_index);
Ok((ExportKind::Func, index))
}
Import::FutureCloseWritable { ty, imported } => {
let type_index = self.payload_type_index(*ty, *imported)?;
Import::FutureCloseWritable(info) => {
let type_index = self.payload_type_index(info)?;
let index = self.component.future_close_writable(type_index);
Ok((ExportKind::Func, index))
}
Expand Down Expand Up @@ -1783,15 +1744,13 @@ impl<'a> EncodingState<'a> {
&mut self,
shims: &Shims<'_>,
for_module: CustomModule<'_>,
async_: bool,
info: &PayloadInfo,
kind: PayloadFuncKind,
) -> (ExportKind, u32) {
self.materialize_shim_import(
shims,
&ShimKind::PayloadFunc {
for_module,
async_,
info,
kind,
},
Expand Down Expand Up @@ -2033,8 +1992,6 @@ enum ShimKind<'a> {
/// Which instance to pull the `realloc` function and string encoding
/// from, if necessary.
for_module: CustomModule<'a>,
/// Whether this read/write call is using the `async` option.
async_: bool,
/// Additional information regarding the function where this `stream` or
/// `future` type appeared, which we use in combination with
/// `for_module` to determine which `realloc` and string encoding to
Expand Down Expand Up @@ -2387,7 +2344,6 @@ impl<'a> Shims<'a> {
),
kind: ShimKind::PayloadFunc {
for_module,
async_,
info,
kind,
},
Expand Down
Loading