Debian Codegraph @

crates/wasm-mutate/src/module.rs

raw
1 use crate::{Error, Result};
2 use wasm_encoder::{BlockType, HeapType, RefType, ValType};
3
4 #[derive(Debug, Copy, Clone, PartialEq)]
5 pub enum PrimitiveTypeInfo {
6 I32,
7 I64,
8 F32,
9 F64,
10 V128,
11 FuncRef,
12 ExternRef,
13 Empty,
14 }
15
16 #[derive(Debug, Clone)]
17 pub struct FuncInfo {
18 pub params: Vec<PrimitiveTypeInfo>,
19 pub returns: Vec<PrimitiveTypeInfo>,
20 }
21
22 #[derive(Debug, Clone)]
23 pub enum TypeInfo {
24 Func(FuncInfo),
25 // TODO: module linking support will require instance and module types.
26 }
27
28 impl TryFrom<wasmparser::ValType> for PrimitiveTypeInfo {
29 type Error = Error;
30
31 fn try_from(value: wasmparser::ValType) -> Result<Self> {
32 Ok(match value {
33 wasmparser::ValType::I32 => PrimitiveTypeInfo::I32,
34 wasmparser::ValType::I64 => PrimitiveTypeInfo::I64,
35 wasmparser::ValType::F32 => PrimitiveTypeInfo::F32,
36 wasmparser::ValType::F64 => PrimitiveTypeInfo::F64,
37 wasmparser::ValType::V128 => PrimitiveTypeInfo::V128,
38 wasmparser::ValType::Ref(t) => t.try_into()?,
39 })
40 }
41 }
42
43 impl TryFrom<wasmparser::RefType> for PrimitiveTypeInfo {
44 type Error = Error;
45
46 fn try_from(value: wasmparser::RefType) -> Result<Self> {
47 Ok(match value {
48 wasmparser::RefType::FUNCREF => PrimitiveTypeInfo::FuncRef,
49 wasmparser::RefType::EXTERNREF => PrimitiveTypeInfo::ExternRef,
50 other => return Err(Error::unsupported(format!("type {other:?}"))),
51 })
52 }
53 }
54
55 impl TryFrom<wasmparser::FuncType> for TypeInfo {
56 type Error = Error;
57
58 fn try_from(ft: wasmparser::FuncType) -> Result<Self> {
59 Ok(TypeInfo::Func(FuncInfo {
60 params: ft
61 .params()
62 .iter()
63 .map(|&t| PrimitiveTypeInfo::try_from(t))
64 .collect::<Result<_>>()?,
65 returns: ft
66 .results()
67 .iter()
68 .map(|&t| PrimitiveTypeInfo::try_from(t))
69 .collect::<Result<_>>()?,
70 }))
71 }
72 }
73
74 pub fn map_type(tpe: wasmparser::ValType) -> Result<ValType> {
75 match tpe {
76 wasmparser::ValType::I32 => Ok(ValType::I32),
77 wasmparser::ValType::I64 => Ok(ValType::I64),
78 wasmparser::ValType::F32 => Ok(ValType::F32),
79 wasmparser::ValType::F64 => Ok(ValType::F64),
80 wasmparser::ValType::V128 => Ok(ValType::V128),
81 wasmparser::ValType::Ref(t) => Ok(ValType::Ref(map_ref_type(t)?)),
82 }
83 }
84
85 pub fn map_ref_type(ref_ty: wasmparser::RefType) -> Result<RefType> {
86 Ok(RefType {
87 nullable: ref_ty.is_nullable(),
88 heap_type: match ref_ty.heap_type() {
89 wasmparser::HeapType::Concrete(i) => HeapType::Concrete(i.as_module_index().unwrap()),
90 wasmparser::HeapType::Abstract { shared, ty } => {
91 let ty = ty.into();
92 HeapType::Abstract { shared, ty }
93 }
94 },
95 })
96 }
97
98 pub fn map_block_type(ty: wasmparser::BlockType) -> Result<BlockType> {
99 match ty {
100 wasmparser::BlockType::Empty => Ok(BlockType::Empty),
101 wasmparser::BlockType::Type(ty) => Ok(BlockType::Result(map_type(ty)?)),
102 wasmparser::BlockType::FuncType(f) => Ok(BlockType::FunctionType(f)),
103 }
104 }
105
106 // The SectionId is stored as a `u8`. This macro will ensure that all of the `SectionId`s are
107 // matched and also takes care of converting the patterns to `u8` for matching.
108 macro_rules! match_section_id {
109 (match $scrutinee:expr;
110 $($pat:ident => $result:expr,)*
111 _ => $otherwise:expr,
112 ) => {'result: loop {
113 #[allow(unreachable_code, non_upper_case_globals)]
114 {
115 $(const $pat: u8 = SectionId::$pat as u8;)*
116 break 'result match $scrutinee {
117 $($pat => $result,)*
118 _ => $otherwise,
119 };
120 // Check exhaustiveness of the SectionId match
121 match SectionId::Type {
122 $(SectionId::$pat => (),)*
123 };
124 }
125 }}
126 }
127 pub(crate) use match_section_id;