use smallvec::{smallvec, SmallVec};
use wasmparser::BlockType;
use wasmtime_environ::{
FuncIndex, GlobalIndex, ModuleTranslation, PtrSize, TypeConvert, VMOffsets, WasmFuncType,
WasmType,
};
pub struct FuncEnv<'a, P> {
pub vmoffsets: VMOffsets<P>,
pub translation: &'a ModuleTranslation<'a>,
}
impl<'a, P: PtrSize> FuncEnv<'a, P> {
pub fn new(ptr: P, translation: &'a ModuleTranslation) -> Self {
let vmoffsets = VMOffsets::new(ptr, &translation.module);
Self {
vmoffsets,
translation,
}
}
pub fn callee_from_index(&self, idx: FuncIndex) -> Callee {
let types = &self.translation.get_types();
let ty = types[types.function_at(idx.as_u32())].unwrap_func();
let ty = self.translation.module.convert_func_type(ty);
let import = self.translation.module.is_imported_function(idx);
Callee {
ty,
import,
index: idx,
}
}
pub fn resolve_block_type(&self, blockty: BlockType) -> SmallVec<[WasmType; 1]> {
use BlockType::*;
match blockty {
Empty => smallvec![],
Type(ty) => smallvec![self.translation.module.convert_valtype(ty)],
_ => unimplemented!("multi-value"),
}
}
pub fn resolve_global_type_and_offset(&self, index: GlobalIndex) -> (WasmType, u32) {
let ty = self.translation.module.globals[index].wasm_ty;
let offset = match self.translation.module.defined_global_index(index) {
Some(defined_index) => self.vmoffsets.vmctx_vmglobal_definition(defined_index),
None => self.vmoffsets.vmctx_vmglobal_import_from(index),
};
(ty, offset)
}
}
pub struct Callee {
pub ty: WasmFuncType,
pub import: bool,
pub index: FuncIndex,
}