Skip to main content

wasi_assembler/program/
types.rs

1use gaia_types::{GaiaError, Result};
2use std::fmt;
3
4/// 程序类型枚举
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub enum WasiProgramType {
7    /// WebAssembly Component Model 组件
8    Component,
9    /// 传统的 WebAssembly 核心模块
10    CoreModule,
11}
12
13/// WASM 值类型
14#[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/// WASM 函数类型
57#[derive(Debug, Clone, PartialEq)]
58pub struct WasiFunctionType {
59    /// 参数类型
60    pub params: Vec<WasmValueType>,
61    /// 返回值类型
62    pub results: Vec<WasmValueType>,
63}
64
65/// 原始类型枚举
66#[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/// 核心类型枚举
84#[derive(Debug, Clone, Copy)]
85pub enum WasiCoreType {
86    Module,
87    Func,
88    Table,
89    Memory,
90    Global,
91}
92
93/// 实例类型枚举
94#[derive(Debug, Clone)]
95pub enum WasiInstanceType {
96    /// 核心模块实例
97    CoreModule,
98    /// 组件实例
99    Component,
100}
101
102/// WASM 引用类型
103#[derive(Copy, Debug, Clone)]
104pub enum WasmReferenceType {
105    FuncRef,
106    ExternRef,
107}
108
109/// WASM 导出类型
110#[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/// WASM 导入类型
128#[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/// WASM 表类型
137#[derive(Copy, Debug, Clone)]
138pub struct WasmTableType {
139    /// 元素类型
140    pub element_type: WasmReferenceType,
141    /// 最小大小
142    pub min: u32,
143    /// 最大大小(可选)
144    pub max: Option<u32>,
145}
146
147/// WASM 内存类型
148#[derive(Copy, Debug, Clone)]
149pub struct WasmMemoryType {
150    /// 最小页数
151    pub min: u32,
152    /// 最大页数(可选)
153    pub max: Option<u32>,
154}
155
156/// WASM 全局变量类型
157#[derive(Copy, Debug, Clone)]
158pub struct WasmGlobalType {
159    pub value_type: WasmValueType,
160    pub mutable: bool,
161}
162
163/// 符号类型枚举
164#[derive(Debug, Clone)]
165pub enum WasiSymbolType {
166    Function,
167    Type,
168    Memory,
169    Table,
170    Global,
171    Instance,
172    Module,
173    Component,
174}