pub use wasmparser;
use cranelift_entity::entity_impl;
use serde_derive::{Deserialize, Serialize};
use std::fmt;
mod error;
pub use error::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WasmValType {
I32,
I64,
F32,
F64,
V128,
Ref(WasmRefType),
}
impl fmt::Display for WasmValType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WasmValType::I32 => write!(f, "i32"),
WasmValType::I64 => write!(f, "i64"),
WasmValType::F32 => write!(f, "f32"),
WasmValType::F64 => write!(f, "f64"),
WasmValType::V128 => write!(f, "v128"),
WasmValType::Ref(rt) => write!(f, "{rt}"),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WasmRefType {
pub nullable: bool,
pub heap_type: WasmHeapType,
}
impl WasmRefType {
pub const EXTERNREF: WasmRefType = WasmRefType {
nullable: true,
heap_type: WasmHeapType::Extern,
};
pub const FUNCREF: WasmRefType = WasmRefType {
nullable: true,
heap_type: WasmHeapType::Func,
};
}
impl fmt::Display for WasmRefType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::FUNCREF => write!(f, "funcref"),
Self::EXTERNREF => write!(f, "externref"),
_ => {
if self.nullable {
write!(f, "(ref null {})", self.heap_type)
} else {
write!(f, "(ref {})", self.heap_type)
}
}
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum WasmHeapType {
Func,
Extern,
TypedFunc(ModuleInternedTypeIndex),
}
impl fmt::Display for WasmHeapType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Func => write!(f, "func"),
Self::Extern => write!(f, "extern"),
Self::TypedFunc(i) => write!(f, "func_sig{}", i.as_u32()),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct WasmFuncType {
params: Box<[WasmValType]>,
externref_params_count: usize,
returns: Box<[WasmValType]>,
externref_returns_count: usize,
}
impl WasmFuncType {
#[inline]
pub fn new(params: Box<[WasmValType]>, returns: Box<[WasmValType]>) -> Self {
let externref_params_count = params
.iter()
.filter(|p| match **p {
WasmValType::Ref(rt) => rt.heap_type == WasmHeapType::Extern,
_ => false,
})
.count();
let externref_returns_count = returns
.iter()
.filter(|r| match **r {
WasmValType::Ref(rt) => rt.heap_type == WasmHeapType::Extern,
_ => false,
})
.count();
WasmFuncType {
params,
externref_params_count,
returns,
externref_returns_count,
}
}
#[inline]
pub fn params(&self) -> &[WasmValType] {
&self.params
}
#[inline]
pub fn externref_params_count(&self) -> usize {
self.externref_params_count
}
#[inline]
pub fn returns(&self) -> &[WasmValType] {
&self.returns
}
#[inline]
pub fn externref_returns_count(&self) -> usize {
self.externref_returns_count
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct FuncIndex(u32);
entity_impl!(FuncIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DefinedFuncIndex(u32);
entity_impl!(DefinedFuncIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DefinedTableIndex(u32);
entity_impl!(DefinedTableIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DefinedMemoryIndex(u32);
entity_impl!(DefinedMemoryIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct OwnedMemoryIndex(u32);
entity_impl!(OwnedMemoryIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DefinedGlobalIndex(u32);
entity_impl!(DefinedGlobalIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct TableIndex(u32);
entity_impl!(TableIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct GlobalIndex(u32);
entity_impl!(GlobalIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct MemoryIndex(u32);
entity_impl!(MemoryIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct TypeIndex(u32);
entity_impl!(TypeIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct ModuleInternedTypeIndex(u32);
entity_impl!(ModuleInternedTypeIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct DataIndex(u32);
entity_impl!(DataIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct ElemIndex(u32);
entity_impl!(ElemIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct TagIndex(u32);
entity_impl!(TagIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub struct StaticModuleIndex(u32);
entity_impl!(StaticModuleIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
pub enum EntityIndex {
Function(FuncIndex),
Table(TableIndex),
Memory(MemoryIndex),
Global(GlobalIndex),
}
impl From<FuncIndex> for EntityIndex {
fn from(idx: FuncIndex) -> EntityIndex {
EntityIndex::Function(idx)
}
}
impl From<TableIndex> for EntityIndex {
fn from(idx: TableIndex) -> EntityIndex {
EntityIndex::Table(idx)
}
}
impl From<MemoryIndex> for EntityIndex {
fn from(idx: MemoryIndex) -> EntityIndex {
EntityIndex::Memory(idx)
}
}
impl From<GlobalIndex> for EntityIndex {
fn from(idx: GlobalIndex) -> EntityIndex {
EntityIndex::Global(idx)
}
}
#[allow(missing_docs)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EntityType {
Global(Global),
Memory(Memory),
Tag(Tag),
Table(Table),
Function(ModuleInternedTypeIndex),
}
impl EntityType {
pub fn unwrap_global(&self) -> &Global {
match self {
EntityType::Global(g) => g,
_ => panic!("not a global"),
}
}
pub fn unwrap_memory(&self) -> &Memory {
match self {
EntityType::Memory(g) => g,
_ => panic!("not a memory"),
}
}
pub fn unwrap_tag(&self) -> &Tag {
match self {
EntityType::Tag(g) => g,
_ => panic!("not a tag"),
}
}
pub fn unwrap_table(&self) -> &Table {
match self {
EntityType::Table(g) => g,
_ => panic!("not a table"),
}
}
pub fn unwrap_func(&self) -> ModuleInternedTypeIndex {
match self {
EntityType::Function(g) => *g,
_ => panic!("not a func"),
}
}
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Global {
pub wasm_ty: crate::WasmValType,
pub mutability: bool,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub enum GlobalInit {
I32Const(i32),
I64Const(i64),
F32Const(u32),
F64Const(u64),
V128Const(u128),
GetGlobal(GlobalIndex),
RefNullConst,
RefFunc(FuncIndex),
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Table {
pub wasm_ty: WasmRefType,
pub minimum: u32,
pub maximum: Option<u32>,
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Memory {
pub minimum: u64,
pub maximum: Option<u64>,
pub shared: bool,
pub memory64: bool,
}
impl From<wasmparser::MemoryType> for Memory {
fn from(ty: wasmparser::MemoryType) -> Memory {
Memory {
minimum: ty.initial,
maximum: ty.maximum,
shared: ty.shared,
memory64: ty.memory64,
}
}
}
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Tag {
pub ty: TypeIndex,
}
impl From<wasmparser::TagType> for Tag {
fn from(ty: wasmparser::TagType) -> Tag {
match ty.kind {
wasmparser::TagKind::Exception => Tag {
ty: TypeIndex::from_u32(ty.func_type_idx),
},
}
}
}
pub trait TypeConvert {
fn convert_global_type(&self, ty: &wasmparser::GlobalType) -> Global {
Global {
wasm_ty: self.convert_valtype(ty.content_type),
mutability: ty.mutable,
}
}
fn convert_table_type(&self, ty: &wasmparser::TableType) -> Table {
Table {
wasm_ty: self.convert_ref_type(ty.element_type),
minimum: ty.initial,
maximum: ty.maximum,
}
}
fn convert_func_type(&self, ty: &wasmparser::FuncType) -> WasmFuncType {
let params = ty
.params()
.iter()
.map(|t| self.convert_valtype(*t))
.collect();
let results = ty
.results()
.iter()
.map(|t| self.convert_valtype(*t))
.collect();
WasmFuncType::new(params, results)
}
fn convert_valtype(&self, ty: wasmparser::ValType) -> WasmValType {
match ty {
wasmparser::ValType::I32 => WasmValType::I32,
wasmparser::ValType::I64 => WasmValType::I64,
wasmparser::ValType::F32 => WasmValType::F32,
wasmparser::ValType::F64 => WasmValType::F64,
wasmparser::ValType::V128 => WasmValType::V128,
wasmparser::ValType::Ref(t) => WasmValType::Ref(self.convert_ref_type(t)),
}
}
fn convert_ref_type(&self, ty: wasmparser::RefType) -> WasmRefType {
WasmRefType {
nullable: ty.is_nullable(),
heap_type: self.convert_heap_type(ty.heap_type()),
}
}
fn convert_heap_type(&self, ty: wasmparser::HeapType) -> WasmHeapType {
match ty {
wasmparser::HeapType::Func => WasmHeapType::Func,
wasmparser::HeapType::Extern => WasmHeapType::Extern,
wasmparser::HeapType::Concrete(i) => self.lookup_heap_type(i),
wasmparser::HeapType::Any
| wasmparser::HeapType::Exn
| wasmparser::HeapType::None
| wasmparser::HeapType::NoExtern
| wasmparser::HeapType::NoFunc
| wasmparser::HeapType::Eq
| wasmparser::HeapType::Struct
| wasmparser::HeapType::Array
| wasmparser::HeapType::I31 => {
unimplemented!("unsupported heap type {ty:?}");
}
}
}
fn lookup_heap_type(&self, index: wasmparser::UnpackedIndex) -> WasmHeapType;
}