1use gaia_types::{GaiaError, Result};
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq)]
6pub enum WasiProgramType {
7 Component,
9 CoreModule,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq)]
15pub enum WasmValueType {
16 I32,
17 I64,
18 F32,
19 F64,
20 V128,
21 Funcref,
22 Externref,
23}
24
25impl TryFrom<u8> for WasmValueType {
26 type Error = GaiaError;
27
28 fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
29 match value {
30 0x7F => Ok(WasmValueType::I32),
31 0x7E => Ok(WasmValueType::I64),
32 0x7D => Ok(WasmValueType::F32),
33 0x7C => Ok(WasmValueType::F64),
34 0x7B => Ok(WasmValueType::V128),
35 0x70 => Ok(WasmValueType::Funcref),
36 0x6F => Ok(WasmValueType::Externref),
37 _ => Err(GaiaError::invalid_data(&format!("Unknown value type: 0x{:02X}", value))),
38 }
39 }
40}
41
42impl fmt::Display for WasmValueType {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 match self {
45 WasmValueType::I32 => write!(f, "i32"),
46 WasmValueType::I64 => write!(f, "i64"),
47 WasmValueType::F32 => write!(f, "f32"),
48 WasmValueType::F64 => write!(f, "f64"),
49 WasmValueType::V128 => write!(f, "v128"),
50 WasmValueType::Funcref => write!(f, "funcref"),
51 WasmValueType::Externref => write!(f, "externref"),
52 }
53 }
54}
55
56#[derive(Debug, Clone, PartialEq)]
58pub struct WasiFunctionType {
59 pub params: Vec<WasmValueType>,
61 pub results: Vec<WasmValueType>,
63}
64
65#[derive(Debug, Clone, Copy)]
67pub enum WasiPrimitiveType {
68 Bool,
69 S8,
70 S16,
71 S32,
72 S64,
73 U8,
74 U16,
75 U32,
76 U64,
77 F32,
78 F64,
79 Char,
80 String,
81}
82
83#[derive(Debug, Clone, Copy)]
85pub enum WasiCoreType {
86 Module,
87 Func,
88 Table,
89 Memory,
90 Global,
91}
92
93#[derive(Debug, Clone)]
95pub enum WasiInstanceType {
96 CoreModule,
98 Component,
100}
101
102#[derive(Copy, Debug, Clone)]
104pub enum WasmReferenceType {
105 FuncRef,
106 ExternRef,
107}
108
109#[derive(Copy, Debug, Clone)]
111pub enum WasmExportType {
112 Function { function_index: u32 },
113 Table { table_index: u32 },
114 Memory { memory_index: u32 },
115 Global { global_index: u32 },
116}
117
118impl WasmExportType {
119 pub fn function_index(&self) -> Option<u32> {
120 match self {
121 WasmExportType::Function { function_index } => Some(*function_index),
122 _ => None,
123 }
124 }
125}
126
127#[derive(Copy, Debug, Clone)]
129pub enum WasmImportType {
130 Function { type_index: u32 },
131 Table { table_type: WasmTableType },
132 Memory { memory_type: WasmMemoryType },
133 Global { global_type: WasmGlobalType },
134}
135
136#[derive(Copy, Debug, Clone)]
138pub struct WasmTableType {
139 pub element_type: WasmReferenceType,
141 pub min: u32,
143 pub max: Option<u32>,
145}
146
147#[derive(Copy, Debug, Clone)]
149pub struct WasmMemoryType {
150 pub min: u32,
152 pub max: Option<u32>,
154}
155
156#[derive(Copy, Debug, Clone)]
158pub struct WasmGlobalType {
159 pub value_type: WasmValueType,
160 pub mutable: bool,
161}
162
163#[derive(Debug, Clone)]
165pub enum WasiSymbolType {
166 Function,
167 Type,
168 Memory,
169 Table,
170 Global,
171 Instance,
172 Module,
173 Component,
174}