use crate::component::{ComponentTypesBuilder, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS};
use crate::fact::{AdapterOptions, Context, Options};
use crate::prelude::*;
use wasm_encoder::ValType;
#[derive(Debug)]
pub struct Signature {
pub params: Vec<ValType>,
pub results: Vec<ValType>,
}
impl ComponentTypesBuilder {
pub(super) fn signature(&self, options: &AdapterOptions, context: Context) -> Signature {
let ty = &self[options.ty];
let ptr_ty = options.options.ptr();
let mut params = match self.flatten_types(
&options.options,
MAX_FLAT_PARAMS,
self[ty.params].types.iter().copied(),
) {
Some(list) => list,
None => {
vec![ptr_ty]
}
};
let results = match self.flatten_types(
&options.options,
MAX_FLAT_RESULTS,
self[ty.results].types.iter().copied(),
) {
Some(list) => list,
None => {
match context {
Context::Lift => vec![ptr_ty],
Context::Lower => {
params.push(ptr_ty);
Vec::new()
}
}
}
};
Signature { params, results }
}
pub(super) fn flatten_types(
&self,
opts: &Options,
max: usize,
tys: impl IntoIterator<Item = InterfaceType>,
) -> Option<Vec<ValType>> {
let mut dst = Vec::new();
for ty in tys {
for ty in opts.flat_types(&ty, self)? {
if dst.len() == max {
return None;
}
dst.push((*ty).into());
}
}
Some(dst)
}
pub(super) fn align(&self, opts: &Options, ty: &InterfaceType) -> u32 {
self.size_align(opts, ty).1
}
pub(super) fn size_align(&self, opts: &Options, ty: &InterfaceType) -> (u32, u32) {
let abi = self.canonical_abi(ty);
if opts.memory64 {
(abi.size64, abi.align64)
} else {
(abi.size32, abi.align32)
}
}
pub(super) fn contains_borrow_resource(&self, options: &AdapterOptions) -> bool {
let ty = &self[options.ty];
debug_assert!(!self[ty.results]
.types
.iter()
.any(|t| self.ty_contains_borrow_resource(t)));
self[ty.params]
.types
.iter()
.any(|t| self.ty_contains_borrow_resource(t))
}
}