Skip to main content

wasi_assembler/program/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use gaia_types::{GaiaError, Result};
4use std::{collections::HashMap, fmt};
5
6/// WASI 程序的高层次表示
7///
8/// 这个结构体可以表示一个完整的 WebAssembly Component 或者一个传统的核心模块
9#[derive(Debug, Clone)]
10pub struct WasiProgram {
11    /// 程序类型:组件或核心模块
12    pub program_type: WasiProgramType,
13    /// 程序名称(可选)
14    pub name: Option<String>,
15    /// 函数类型定义
16    pub function_types: Vec<WasiFunctionType>,
17    /// 函数定义
18    pub functions: Vec<WasiFunction>,
19    /// 导出定义
20    pub exports: Vec<WasiExport>,
21    /// 导入定义
22    pub imports: Vec<WasiImport>,
23    /// 内存定义
24    pub memories: Vec<WasiMemory>,
25    /// 表定义
26    pub tables: Vec<WasiTable>,
27    /// 全局变量定义
28    pub globals: Vec<WasiGlobal>,
29    /// 自定义段
30    pub custom_sections: Vec<WasiCustomSection>,
31    /// 起始函数索引
32    pub start_function: Option<u32>,
33    /// 组件特有的项目(仅当 program_type 为 Component 时使用)
34    pub component_items: Vec<WasiComponentItem>,
35    /// 核心模块列表(用于组件中的嵌套模块)
36    pub core_modules: Vec<WasiCoreModule>,
37    /// 实例列表
38    pub instances: Vec<WasiInstance>,
39    /// 别名定义
40    pub aliases: Vec<WasiAlias>,
41    /// 符号表,用于名称到索引的映射
42    pub symbol_table: HashMap<String, WasiSymbol>,
43}
44
45#[derive(Copy, Clone, Debug, Default)]
46pub struct WasmInfo {
47    pub magic_head: [u8; 4],
48}
49
50/// 程序类型枚举
51#[derive(Debug, Clone, Copy, PartialEq)]
52pub enum WasiProgramType {
53    /// WebAssembly Component Model 组件
54    Component,
55    /// 传统的 WebAssembly 核心模块
56    CoreModule,
57}
58
59/// 组件项目枚举
60#[derive(Debug, Clone)]
61pub enum WasiComponentItem {
62    /// 类型定义
63    Type(WasiTypeDefinition),
64    /// 别名定义
65    Alias(WasiAlias),
66    /// 实例定义
67    Instance(WasiInstance),
68    /// 核心函数定义
69    CoreFunc(WasiCoreFunc),
70    /// 核心实例定义
71    CoreInstance(WasiCoreInstance),
72}
73
74/// 核心模块定义
75#[derive(Debug, Clone)]
76pub struct WasiCoreModule {
77    /// 模块名称(可选)
78    pub name: Option<String>,
79    /// 模块索引
80    pub index: u32,
81    /// 函数类型定义
82    pub function_types: Vec<WasiFunctionType>,
83    /// 函数定义
84    pub functions: Vec<WasiFunction>,
85    /// 导出定义
86    pub exports: Vec<WasiExport>,
87    /// 导入定义
88    pub imports: Vec<WasiImport>,
89    /// 内存定义
90    pub memories: Vec<WasiMemory>,
91    /// 表定义
92    pub tables: Vec<WasiTable>,
93    /// 全局变量定义
94    pub globals: Vec<WasiGlobal>,
95    /// 数据段
96    pub data_segments: Vec<WasiDataSegment>,
97    /// 元素段
98    pub element_segments: Vec<WasiElementSegment>,
99    /// 起始函数索引
100    pub start_function: Option<u32>,
101}
102
103/// 实例定义
104#[derive(Debug, Clone)]
105pub struct WasiInstance {
106    /// 实例名称(可选)
107    pub name: Option<String>,
108    /// 实例索引
109    pub index: u32,
110    /// 实例化的模块或组件名称
111    pub instantiate_target: String,
112    /// 实例化参数
113    pub args: Vec<WasiInstanceArg>,
114    /// 实例类型
115    pub instance_type: WasiInstanceType,
116}
117
118/// 实例类型枚举
119#[derive(Debug, Clone)]
120pub enum WasiInstanceType {
121    /// 核心模块实例
122    CoreModule,
123    /// 组件实例
124    Component,
125}
126
127/// 实例化参数
128#[derive(Debug, Clone)]
129pub struct WasiInstanceArg {
130    /// 参数名称
131    pub name: String,
132    /// 参数值(引用的符号名称)
133    pub value: String,
134}
135
136/// 别名定义
137#[derive(Debug, Clone)]
138pub struct WasiAlias {
139    /// 别名名称(可选)
140    pub name: Option<String>,
141    /// 别名目标
142    pub target: WasiAliasTarget,
143}
144
145/// 别名目标枚举
146#[derive(Debug, Clone)]
147pub enum WasiAliasTarget {
148    /// 外部别名
149    Outer {
150        /// 外部索引
151        outer_index: u32,
152        /// 项目索引
153        item_index: u32,
154    },
155    /// 核心别名
156    Core {
157        /// 核心类型
158        core_type: WasiCoreType,
159        /// 项目索引
160        item_index: u32,
161    },
162    /// 导出别名
163    Export {
164        /// 实例名称
165        instance: String,
166        /// 导出名称
167        name: String,
168    },
169    /// 核心导出别名
170    CoreExport {
171        /// 实例名称
172        instance: String,
173        /// 导出名称
174        name: String,
175    },
176}
177
178/// 核心类型枚举
179#[derive(Debug, Clone, Copy)]
180pub enum WasiCoreType {
181    Module,
182    Func,
183    Table,
184    Memory,
185    Global,
186}
187
188/// 类型定义
189#[derive(Debug, Clone)]
190pub struct WasiTypeDefinition {
191    /// 类型名称(可选)
192    pub name: Option<String>,
193    /// 类型索引
194    pub index: u32,
195    /// 类型内容
196    pub type_content: WasiType,
197}
198
199/// 类型枚举
200#[derive(Debug, Clone)]
201pub enum WasiType {
202    /// 函数类型
203    Func(WasiFunctionType),
204    /// 接口类型
205    Interface(String),
206    /// 实例类型
207    Instance(String),
208    /// 资源类型
209    Resource(WasiResourceType),
210    /// 记录类型
211    Record(Vec<WasiRecordField>),
212    /// 变体类型
213    Variant(Vec<WasiVariantCase>),
214    /// 枚举类型
215    Enum(Vec<String>),
216    /// 联合类型
217    Union(Vec<WasiType>),
218    /// 选项类型
219    Option(Box<WasiType>),
220    /// 列表类型
221    List(Box<WasiType>),
222    /// 元组类型
223    Tuple(Vec<WasiType>),
224    /// 标志类型
225    Flags(Vec<String>),
226    /// Future 类型 (WASIp3)
227    Future(Option<Box<WasiType>>),
228    /// Stream 类型 (WASIp3)
229    Stream(Option<Box<WasiType>>),
230    /// 原始类型
231    Primitive(WasiPrimitiveType),
232}
233
234/// 资源类型
235#[derive(Debug, Clone)]
236pub struct WasiResourceType {
237    /// 资源名称
238    pub name: String,
239    /// 资源方法
240    pub methods: Vec<WasiResourceMethod>,
241}
242
243/// 资源方法
244#[derive(Debug, Clone)]
245pub struct WasiResourceMethod {
246    /// 方法名称
247    pub name: String,
248    /// 方法类型
249    pub method_type: WasiFunctionType,
250}
251
252/// 记录字段
253#[derive(Debug, Clone)]
254pub struct WasiRecordField {
255    /// 字段名称
256    pub name: String,
257    /// 字段类型
258    pub field_type: WasiType,
259}
260
261/// 变体情况
262#[derive(Debug, Clone)]
263pub struct WasiVariantCase {
264    /// 情况名称
265    pub name: String,
266    /// 情况类型(可选)
267    pub case_type: Option<WasiType>,
268}
269
270/// 原始类型枚举
271#[derive(Debug, Clone, Copy)]
272pub enum WasiPrimitiveType {
273    Bool,
274    S8,
275    S16,
276    S32,
277    S64,
278    U8,
279    U16,
280    U32,
281    U64,
282    F32,
283    F64,
284    Char,
285    String,
286}
287
288/// 核心函数定义
289#[derive(Debug, Clone)]
290pub struct WasiCoreFunc {
291    /// 函数名称(可选)
292    pub name: Option<String>,
293    /// 函数索引
294    pub index: u32,
295    /// 函数类型
296    pub func_type: WasiFunctionType,
297    /// 规范化操作(可选)
298    pub canon: Option<WasiCanonicalOperation>,
299}
300
301/// 规范化操作枚举
302#[derive(Debug, Clone)]
303pub enum WasiCanonicalOperation {
304    /// Lower 操作
305    Lower {
306        /// 函数名称
307        func: String,
308        /// 选项
309        options: Vec<WasiCanonOption>,
310    },
311    /// Lift 操作
312    Lift {
313        /// 函数名称
314        func: String,
315        /// 选项
316        options: Vec<WasiCanonOption>,
317    },
318    /// 资源创建
319    ResourceNew(String),
320    /// 资源销毁
321    ResourceDrop(String),
322    /// 资源表示
323    ResourceRep(String),
324}
325
326/// 规范化选项枚举
327#[derive(Debug, Clone)]
328pub enum WasiCanonOption {
329    /// 字符串编码
330    StringEncoding(String),
331    /// 内存
332    Memory(String),
333    /// 重新分配函数
334    Realloc(String),
335    /// 异步模式 (WASIp3)
336    Async,
337    /// 回调函数 (WASIp3)
338    Callback(String),
339}
340
341/// 核心实例定义
342#[derive(Debug, Clone)]
343pub struct WasiCoreInstance {
344    /// 实例名称(可选)
345    pub name: Option<String>,
346    /// 实例索引
347    pub index: u32,
348    /// 实例化的模块名称
349    pub instantiate_target: String,
350    /// 实例化参数
351    pub args: Vec<WasiInstanceArg>,
352}
353
354/// 数据段定义
355#[derive(Debug, Clone)]
356pub struct WasiDataSegment {
357    /// 数据段名称(可选)
358    pub name: Option<String>,
359    /// 内存索引(可选,默认为 0)
360    pub memory_index: Option<u32>,
361    /// 偏移表达式
362    pub offset: Vec<WasiInstruction>,
363    /// 数据内容
364    pub data: Vec<u8>,
365}
366
367/// 元素段定义
368#[derive(Debug, Clone)]
369pub struct WasiElementSegment {
370    /// 元素段名称(可选)
371    pub name: Option<String>,
372    /// 表索引(可选,默认为 0)
373    pub table_index: Option<u32>,
374    /// 偏移表达式
375    pub offset: Vec<WasiInstruction>,
376    /// 元素列表(函数索引)
377    pub elements: Vec<u32>,
378}
379
380/// 符号定义
381#[derive(Debug, Clone)]
382pub struct WasiSymbol {
383    /// 符号名称
384    pub name: String,
385    /// 符号类型
386    pub symbol_type: WasiSymbolType,
387    /// 符号索引
388    pub index: u32,
389}
390
391/// 符号类型枚举
392#[derive(Debug, Clone)]
393pub enum WasiSymbolType {
394    Function,
395    Type,
396    Memory,
397    Table,
398    Global,
399    Instance,
400    Module,
401    Component,
402}
403
404/// WASM 函数类型
405#[derive(Debug, Clone, PartialEq)]
406pub struct WasiFunctionType {
407    /// 参数类型
408    pub params: Vec<WasmValueType>,
409    /// 返回值类型
410    pub results: Vec<WasmValueType>,
411}
412
413/// WASM 值类型
414#[derive(Debug, Clone, Copy, PartialEq)]
415pub enum WasmValueType {
416    I32,
417    I64,
418    F32,
419    F64,
420    V128,
421    Funcref,
422    Externref,
423}
424
425impl TryFrom<u8> for WasmValueType {
426    type Error = GaiaError;
427
428    fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
429        match value {
430            0x7F => Ok(WasmValueType::I32),
431            0x7E => Ok(WasmValueType::I64),
432            0x7D => Ok(WasmValueType::F32),
433            0x7C => Ok(WasmValueType::F64),
434            0x7B => Ok(WasmValueType::V128),
435            0x70 => Ok(WasmValueType::Funcref),
436            0x6F => Ok(WasmValueType::Externref),
437            _ => Err(GaiaError::invalid_data(&format!("Unknown value type: 0x{:02X}", value))),
438        }
439    }
440}
441
442impl fmt::Display for WasmValueType {
443    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444        match self {
445            WasmValueType::I32 => write!(f, "i32"),
446            WasmValueType::I64 => write!(f, "i64"),
447            WasmValueType::F32 => write!(f, "f32"),
448            WasmValueType::F64 => write!(f, "f64"),
449            WasmValueType::V128 => write!(f, "v128"),
450            WasmValueType::Funcref => write!(f, "funcref"),
451            WasmValueType::Externref => write!(f, "externref"),
452        }
453    }
454}
455
456/// WASM 函数定义
457#[derive(Debug, Clone)]
458pub struct WasiFunction {
459    /// 函数类型索引
460    pub type_index: u32,
461    /// 局部变量
462    pub locals: Vec<WasmLocal>,
463    /// 函数体指令
464    pub body: Vec<WasiInstruction>,
465}
466
467/// WASM 局部变量
468#[derive(Copy, Debug, Clone)]
469pub struct WasmLocal {
470    /// 变量数量
471    pub count: u32,
472    /// 变量类型
473    pub value_type: WasmValueType,
474}
475
476/// WASM 指令
477#[derive(Copy, Debug, Clone)]
478pub enum WasiInstruction {
479    /// 无操作
480    Nop,
481    /// 不可达
482    Unreachable,
483    /// 块开始
484    Block {
485        block_type: Option<WasmValueType>,
486    },
487    /// 循环开始
488    Loop {
489        block_type: Option<WasmValueType>,
490    },
491    /// 条件分支
492    If {
493        block_type: Option<WasmValueType>,
494    },
495    /// Else 分支
496    Else,
497    /// 块结束
498    End,
499    /// 分支
500    Br {
501        label_index: u32,
502    },
503    /// 条件分支
504    BrIf {
505        label_index: u32,
506    },
507    /// 返回
508    Return,
509    /// 函数调用
510    Call {
511        function_index: u32,
512    },
513    /// 丢弃栈顶值
514    Drop,
515    /// 选择
516    Select,
517    /// 加载局部变量
518    LocalGet {
519        local_index: u32,
520    },
521    /// 设置局部变量
522    LocalSet {
523        local_index: u32,
524    },
525    /// 加载内存
526    I32Load {
527        offset: u32,
528        align: u32,
529    },
530    I64Load {
531        offset: u32,
532        align: u32,
533    },
534    F32Load {
535        offset: u32,
536        align: u32,
537    },
538    F64Load {
539        offset: u32,
540        align: u32,
541    },
542    /// 存储内存
543    I32Store {
544        offset: u32,
545        align: u32,
546    },
547    I64Store {
548        offset: u32,
549        align: u32,
550    },
551    F32Store {
552        offset: u32,
553        align: u32,
554    },
555    F64Store {
556        offset: u32,
557        align: u32,
558    },
559    /// 结构体操作 (GC Proposal)
560    StructNew {
561        type_index: u32,
562    },
563    StructGet {
564        type_index: u32,
565        field_index: u32,
566    },
567    StructSet {
568        type_index: u32,
569        field_index: u32,
570    },
571    /// 加载常量
572    I32Const {
573        value: i32,
574    },
575    I64Const {
576        value: i64,
577    },
578    F32Const {
579        value: f32,
580    },
581    F64Const {
582        value: f64,
583    },
584    /// 算术运算
585    I32Add,
586    I32Sub,
587    I32Mul,
588    I32DivS,
589    I32DivU,
590    I32RemS,
591    I32RemU,
592    I32And,
593    I32Or,
594    I32Xor,
595    I32Shl,
596    I32ShrS,
597    I32ShrU,
598    I32Rotl,
599    I32Rotr,
600    /// 比较运算
601    I32Eqz,
602    I32Eq,
603    I32Ne,
604    I32LtS,
605    I32LtU,
606    I32GtS,
607    I32GtU,
608    I32LeS,
609    I32LeU,
610    I32GeS,
611    I32GeU,
612    /// 异步任务指令 (WASIp3)
613    TaskBackpressure,
614    TaskReturn,
615    TaskWait,
616    TaskPoll,
617    TaskYield,
618    /// 错误上下文指令 (WASIp3)
619    ErrorContextNew,
620    ErrorContextDebugMessage,
621}
622
623/// WASM 导出
624#[derive(Debug, Clone)]
625pub struct WasiExport {
626    /// 导出名称
627    pub name: String,
628    /// 导出类型
629    pub export_type: WasmExportType,
630}
631
632/// WASM 导出类型
633#[derive(Copy, Debug, Clone)]
634pub enum WasmExportType {
635    Function { function_index: u32 },
636    Table { table_index: u32 },
637    Memory { memory_index: u32 },
638    Global { global_index: u32 },
639}
640
641impl WasmExportType {
642    pub fn function_index(&self) -> Option<u32> {
643        match self {
644            WasmExportType::Function { function_index } => Some(*function_index),
645            _ => None,
646        }
647    }
648}
649
650/// WASM 导入
651#[derive(Debug, Clone)]
652pub struct WasiImport {
653    /// 模块名
654    pub module: String,
655    /// 字段名
656    pub field: String,
657    /// 导入类型
658    pub import_type: WasmImportType,
659}
660
661/// WASM 导入类型
662#[derive(Copy, Debug, Clone)]
663pub enum WasmImportType {
664    Function { type_index: u32 },
665    Table { table_type: WasmTableType },
666    Memory { memory_type: WasmMemoryType },
667    Global { global_type: WasmGlobalType },
668}
669
670/// WASM 内存定义
671#[derive(Copy, Debug, Clone)]
672pub struct WasiMemory {
673    pub memory_type: WasmMemoryType,
674}
675
676/// WASM 内存类型
677#[derive(Copy, Debug, Clone)]
678pub struct WasmMemoryType {
679    /// 最小页数
680    pub min: u32,
681    /// 最大页数(可选)
682    pub max: Option<u32>,
683}
684
685/// WASM 表定义
686#[derive(Copy, Debug, Clone)]
687pub struct WasiTable {
688    pub table_type: WasmTableType,
689}
690
691/// WASM 表类型
692#[derive(Copy, Debug, Clone)]
693pub struct WasmTableType {
694    /// 元素类型
695    pub element_type: WasmReferenceType,
696    /// 最小大小
697    pub min: u32,
698    /// 最大大小(可选)
699    pub max: Option<u32>,
700}
701
702/// WASM 引用类型
703#[derive(Copy, Debug, Clone)]
704pub enum WasmReferenceType {
705    FuncRef,
706    ExternRef,
707}
708
709/// WASM 全局变量
710#[derive(Debug, Clone)]
711pub struct WasiGlobal {
712    pub global_type: WasmGlobalType,
713    pub init_expr: Vec<WasiInstruction>,
714}
715
716/// WASM 全局变量类型
717#[derive(Copy, Debug, Clone)]
718pub struct WasmGlobalType {
719    pub value_type: WasmValueType,
720    pub mutable: bool,
721}
722
723/// WASM 自定义段
724#[derive(Debug, Clone)]
725pub struct WasiCustomSection {
726    /// 段名称
727    pub name: String,
728    /// 段数据
729    pub data: Vec<u8>,
730}
731
732impl WasiProgram {
733    /// 创建新的 WASI 程序
734    pub fn new(program_type: WasiProgramType) -> Self {
735        Self {
736            program_type,
737            name: None,
738            function_types: Vec::new(),
739            functions: Vec::new(),
740            exports: Vec::new(),
741            imports: Vec::new(),
742            memories: Vec::new(),
743            tables: Vec::new(),
744            globals: Vec::new(),
745            custom_sections: Vec::new(),
746            start_function: None,
747            component_items: Vec::new(),
748            core_modules: Vec::new(),
749            instances: Vec::new(),
750            aliases: Vec::new(),
751            symbol_table: HashMap::new(),
752        }
753    }
754
755    /// 创建新的组件程序
756    pub fn new_component() -> Self {
757        Self::new(WasiProgramType::Component)
758    }
759
760    /// 创建新的核心模块程序
761    pub fn new_core_module() -> Self {
762        Self::new(WasiProgramType::CoreModule)
763    }
764
765    /// 添加函数类型定义,返回类型索引
766    pub fn add_function_type(&mut self, func_type: WasiFunctionType) -> u32 {
767        // 检查是否已存在相同的函数类型
768        for (index, existing_type) in self.function_types.iter().enumerate() {
769            if existing_type == &func_type {
770                return index as u32;
771            }
772        }
773
774        let index = self.function_types.len() as u32;
775        self.function_types.push(func_type);
776        index
777    }
778
779    /// 添加函数定义,返回函数索引
780    pub fn add_function(&mut self, function: WasiFunction) -> u32 {
781        let index = self.functions.len() as u32;
782        self.functions.push(function);
783        index
784    }
785
786    /// 添加导出定义
787    pub fn add_export(&mut self, export: WasiExport) {
788        self.exports.push(export);
789    }
790
791    /// 添加导入定义
792    pub fn add_import(&mut self, import: WasiImport) {
793        self.imports.push(import);
794    }
795
796    /// 添加内存定义
797    pub fn add_memory(&mut self, memory: WasiMemory) -> u32 {
798        let index = self.memories.len() as u32;
799        self.memories.push(memory);
800        index
801    }
802
803    /// 添加表定义
804    pub fn add_table(&mut self, table: WasiTable) -> u32 {
805        let index = self.tables.len() as u32;
806        self.tables.push(table);
807        index
808    }
809
810    /// 添加全局变量定义
811    pub fn add_global(&mut self, global: WasiGlobal) -> u32 {
812        let index = self.globals.len() as u32;
813        self.globals.push(global);
814        index
815    }
816
817    /// 添加核心模块
818    pub fn add_core_module(&mut self, core_module: WasiCoreModule) -> u32 {
819        let index = self.core_modules.len() as u32;
820        self.core_modules.push(core_module);
821        index
822    }
823
824    /// 添加实例
825    pub fn add_instance(&mut self, instance: WasiInstance) -> u32 {
826        let index = self.instances.len() as u32;
827        self.instances.push(instance);
828        index
829    }
830
831    /// 添加别名
832    pub fn add_alias(&mut self, alias: WasiAlias) {
833        self.aliases.push(alias);
834    }
835
836    /// 添加符号到符号表
837    pub fn add_symbol(&mut self, name: String, symbol_type: WasiSymbolType, index: u32) {
838        let symbol = WasiSymbol { name: name.clone(), symbol_type, index };
839        self.symbol_table.insert(name, symbol);
840    }
841
842    /// 根据名称查找符号
843    pub fn find_symbol(&self, name: &str) -> Option<&WasiSymbol> {
844        self.symbol_table.get(name)
845    }
846
847    /// 验证程序的完整性
848    pub fn validate(&self) -> Result<()> {
849        // 验证函数类型索引
850        for function in &self.functions {
851            if function.type_index as usize >= self.function_types.len() {
852                return Err(GaiaError::custom_error(format!(
853                    "函数类型索引 {} 超出范围,最大索引为 {}",
854                    function.type_index,
855                    self.function_types.len().saturating_sub(1)
856                )));
857            }
858        }
859
860        // 验证导出索引
861        for export in &self.exports {
862            match &export.export_type {
863                WasmExportType::Function { function_index } => {
864                    let total_functions =
865                        self.imports.iter().filter(|imp| matches!(imp.import_type, WasmImportType::Function { .. })).count()
866                            + self.functions.len();
867
868                    if *function_index as usize >= total_functions {
869                        return Err(GaiaError::custom_error(format!(
870                            "导出函数索引 {} 超出范围,最大索引为 {}",
871                            function_index,
872                            total_functions.saturating_sub(1)
873                        )));
874                    }
875                }
876                _ => {} // 其他类型的验证可以后续添加
877            }
878        }
879
880        Ok(())
881    }
882
883    /// 将程序转换为 WAT AST
884    #[cfg(feature = "wat")]
885    pub fn to_wat(&self) -> gaia_types::Result<oak_wat::ast::WatRoot> {
886        use oak_wat::ast::{
887            WatExport, WatFunc, WatImport, WatInstruction, WatItem, WatLocal, WatModule, WatModuleField, WatParam, WatResult,
888            WatRoot, WatTypeKind,
889        };
890
891        let mut module_items = Vec::new();
892
893        // 1. 写入导入
894        for (i, import) in self.imports.iter().enumerate() {
895            let kind = match import.import_type {
896                WasmImportType::Function { .. } => "func",
897                WasmImportType::Table { .. } => "table",
898                WasmImportType::Memory { .. } => "memory",
899                WasmImportType::Global { .. } => "global",
900            };
901            let index = match import.import_type {
902                WasmImportType::Function { type_index } => type_index,
903                _ => 0, // TODO: handle other types
904            };
905
906            module_items.push(WatModuleField::Import(WatImport {
907                module: import.module.clone(),
908                name: import.field.clone(),
909                kind: kind.to_string(),
910            }));
911        }
912
913        // 2. 写入函数
914        for (i, func) in self.functions.iter().enumerate() {
915            let func_type = &self.function_types[func.type_index as usize];
916
917            let mut params = Vec::new();
918            for (j, param) in func_type.params.iter().enumerate() {
919                params.push(WatParam {
920                    name: Some(format!("$p{}", j)),
921                    ty: match param {
922                        WasmValueType::I32 => WatTypeKind::I32,
923                        WasmValueType::I64 => WatTypeKind::I64,
924                        WasmValueType::F32 => WatTypeKind::F32,
925                        WasmValueType::F64 => WatTypeKind::F64,
926                        _ => WatTypeKind::I32, // Fallback
927                    },
928                });
929            }
930
931            let mut results = Vec::new();
932            for result in &func_type.results {
933                results.push(WatResult {
934                    ty: match result {
935                        WasmValueType::I32 => WatTypeKind::I32,
936                        WasmValueType::I64 => WatTypeKind::I64,
937                        WasmValueType::F32 => WatTypeKind::F32,
938                        WasmValueType::F64 => WatTypeKind::F64,
939                        _ => WatTypeKind::I32, // Fallback
940                    },
941                });
942            }
943
944            let mut locals = Vec::new();
945            let mut local_idx = 0;
946            for local_group in &func.locals {
947                for _ in 0..local_group.count {
948                    locals.push(WatLocal {
949                        name: Some(format!("$l{}", local_idx)),
950                        ty: match local_group.value_type {
951                            WasmValueType::I32 => WatTypeKind::I32,
952                            WasmValueType::I64 => WatTypeKind::I64,
953                            WasmValueType::F32 => WatTypeKind::F32,
954                            WasmValueType::F64 => WatTypeKind::F64,
955                            _ => WatTypeKind::I32, // Fallback
956                        },
957                    });
958                    local_idx += 1;
959                }
960            }
961
962            let mut body = Vec::new();
963            for instr in &func.body {
964                let instr_str = match instr {
965                    WasiInstruction::Nop => "nop".to_string(),
966                    WasiInstruction::Unreachable => "unreachable".to_string(),
967                    WasiInstruction::Return => "return".to_string(),
968                    WasiInstruction::Drop => "drop".to_string(),
969                    WasiInstruction::Select => "select".to_string(),
970                    WasiInstruction::I32Add => "i32.add".to_string(),
971                    WasiInstruction::I32Sub => "i32.sub".to_string(),
972                    WasiInstruction::I32Mul => "i32.mul".to_string(),
973                    WasiInstruction::I32DivS => "i32.div_s".to_string(),
974                    WasiInstruction::I32DivU => "i32.div_u".to_string(),
975                    WasiInstruction::I32RemS => "i32.rem_s".to_string(),
976                    WasiInstruction::I32RemU => "i32.rem_u".to_string(),
977                    WasiInstruction::I32And => "i32.and".to_string(),
978                    WasiInstruction::I32Or => "i32.or".to_string(),
979                    WasiInstruction::I32Xor => "i32.xor".to_string(),
980                    WasiInstruction::I32Shl => "i32.shl".to_string(),
981                    WasiInstruction::I32ShrS => "i32.shr_s".to_string(),
982                    WasiInstruction::I32ShrU => "i32.shr_u".to_string(),
983                    WasiInstruction::I32Rotl => "i32.rotl".to_string(),
984                    WasiInstruction::I32Rotr => "i32.rotr".to_string(),
985                    WasiInstruction::I32Eqz => "i32.eqz".to_string(),
986                    WasiInstruction::I32Eq => "i32.eq".to_string(),
987                    WasiInstruction::I32Ne => "i32.ne".to_string(),
988                    WasiInstruction::I32LtS => "i32.lt_s".to_string(),
989                    WasiInstruction::I32LtU => "i32.lt_u".to_string(),
990                    WasiInstruction::I32GtS => "i32.gt_s".to_string(),
991                    WasiInstruction::I32GtU => "i32.gt_u".to_string(),
992                    WasiInstruction::I32LeS => "i32.le_s".to_string(),
993                    WasiInstruction::I32LeU => "i32.le_u".to_string(),
994                    WasiInstruction::I32GeS => "i32.ge_s".to_string(),
995                    WasiInstruction::I32GeU => "i32.ge_u".to_string(),
996                    WasiInstruction::I32Load { offset, align } => format!("i32.load offset={} align={}", offset, align),
997                    WasiInstruction::I64Load { offset, align } => format!("i64.load offset={} align={}", offset, align),
998                    WasiInstruction::F32Load { offset, align } => format!("f32.load offset={} align={}", offset, align),
999                    WasiInstruction::F64Load { offset, align } => format!("f64.load offset={} align={}", offset, align),
1000                    WasiInstruction::I32Store { offset, align } => format!("i32.store offset={} align={}", offset, align),
1001                    WasiInstruction::I64Store { offset, align } => format!("i64.store offset={} align={}", offset, align),
1002                    WasiInstruction::F32Store { offset, align } => format!("f32.store offset={} align={}", offset, align),
1003                    WasiInstruction::F64Store { offset, align } => format!("f64.store offset={} align={}", offset, align),
1004                    WasiInstruction::StructNew { type_index } => format!("struct.new {}", type_index),
1005                    WasiInstruction::StructGet { type_index, field_index } => {
1006                        format!("struct.get {} {}", type_index, field_index)
1007                    }
1008                    WasiInstruction::StructSet { type_index, field_index } => {
1009                        format!("struct.set {} {}", type_index, field_index)
1010                    }
1011                    WasiInstruction::TaskBackpressure => "task.backpressure".to_string(),
1012                    WasiInstruction::TaskReturn => "task.return".to_string(),
1013                    WasiInstruction::TaskWait => "task.wait".to_string(),
1014                    WasiInstruction::TaskPoll => "task.poll".to_string(),
1015                    WasiInstruction::TaskYield => "task.yield".to_string(),
1016                    WasiInstruction::ErrorContextNew => "error_context.new".to_string(),
1017                    WasiInstruction::ErrorContextDebugMessage => "error_context.debug_message".to_string(),
1018                    WasiInstruction::LocalGet { local_index } => format!("local.get {}", local_index),
1019                    WasiInstruction::LocalSet { local_index } => format!("local.set {}", local_index),
1020                    WasiInstruction::I32Const { value } => format!("i32.const {}", value),
1021                    WasiInstruction::I64Const { value } => format!("i64.const {}", value),
1022                    WasiInstruction::F32Const { value } => format!("f32.const {}", value),
1023                    WasiInstruction::F64Const { value } => format!("f64.const {}", value),
1024                    WasiInstruction::Call { function_index } => format!("call {}", function_index),
1025                    WasiInstruction::Block { .. } => "block".to_string(),
1026                    WasiInstruction::Loop { .. } => "loop".to_string(),
1027                    WasiInstruction::If { .. } => "if".to_string(),
1028                    WasiInstruction::Else => "else".to_string(),
1029                    WasiInstruction::End => "end".to_string(),
1030                    WasiInstruction::Br { label_index } => format!("br {}", label_index),
1031                    WasiInstruction::BrIf { label_index } => format!("br_if {}", label_index),
1032                    _ => format!(";; unknown instruction {:?}", instr),
1033                };
1034                body.push(WatInstruction::Other(instr_str, vec![]));
1035            }
1036
1037            module_items.push(WatModuleField::Func(WatFunc { name: Some(format!("$f{}", i)), params, results, locals, body }));
1038        }
1039
1040        // 3. 写入导出
1041        for export in &self.exports {
1042            let (kind, index) = match export.export_type {
1043                WasmExportType::Function { function_index } => ("func", function_index),
1044                WasmExportType::Table { table_index } => ("table", table_index),
1045                WasmExportType::Memory { memory_index } => ("memory", memory_index),
1046                WasmExportType::Global { global_index } => ("global", global_index),
1047            };
1048            module_items.push(WatModuleField::Export(WatExport {
1049                name: export.name.clone(),
1050                kind: kind.to_string(),
1051                id: index.to_string(),
1052            }));
1053        }
1054
1055        let mut items = Vec::new();
1056        if self.program_type == WasiProgramType::CoreModule {
1057            let module = WatModule { name: self.name.clone(), items: module_items };
1058            items.push(WatItem::Module(module));
1059        }
1060
1061        Ok(WatRoot { items })
1062    }
1063
1064    /// 将程序转换为 WASM 字节码
1065    pub fn to_wasm(&self) -> Result<Vec<u8>> {
1066        let mut writer = crate::formats::wasm::writer::WasmWriter::new(Vec::new());
1067        writer.write(self.clone()).result
1068    }
1069}
1070
1071impl Default for WasiProgram {
1072    fn default() -> Self {
1073        Self::new_core_module()
1074    }
1075}
1076
1077/// WasiProgram 的构建器
1078pub struct WasiProgramBuilder {
1079    program: WasiProgram,
1080}
1081
1082impl WasiProgramBuilder {
1083    /// 创建一个新的 WasiProgramBuilder
1084    pub fn new(program_type: WasiProgramType) -> Self {
1085        Self { program: WasiProgram::new(program_type) }
1086    }
1087
1088    /// 设置程序名称
1089    pub fn with_name(mut self, name: impl Into<String>) -> Self {
1090        self.program.name = Some(name.into());
1091        self
1092    }
1093
1094    /// 添加函数类型定义
1095    pub fn with_function_type(mut self, func_type: WasiFunctionType) -> Self {
1096        self.program.add_function_type(func_type);
1097        self
1098    }
1099
1100    /// 添加函数定义
1101    pub fn with_function(mut self, function: WasiFunction) -> Self {
1102        self.program.add_function(function);
1103        self
1104    }
1105
1106    /// 添加导出定义
1107    pub fn with_export(mut self, export: WasiExport) -> Self {
1108        self.program.add_export(export);
1109        self
1110    }
1111
1112    /// 添加导入定义
1113    pub fn with_import(mut self, import: WasiImport) -> Self {
1114        self.program.add_import(import);
1115        self
1116    }
1117
1118    /// 添加内存定义
1119    pub fn with_memory(mut self, memory: WasiMemory) -> Self {
1120        self.program.add_memory(memory);
1121        self
1122    }
1123
1124    /// 添加表定义
1125    pub fn with_table(mut self, table: WasiTable) -> Self {
1126        self.program.add_table(table);
1127        self
1128    }
1129
1130    /// 添加全局变量定义
1131    pub fn with_global(mut self, global: WasiGlobal) -> Self {
1132        self.program.add_global(global);
1133        self
1134    }
1135
1136    /// 添加自定义段
1137    pub fn with_custom_section(mut self, custom_section: WasiCustomSection) -> Self {
1138        self.program.custom_sections.push(custom_section);
1139        self
1140    }
1141
1142    /// 设置起始函数索引
1143    pub fn with_start_function(mut self, start_function_index: u32) -> Self {
1144        self.program.start_function = Some(start_function_index);
1145        self
1146    }
1147
1148    /// 添加组件项目
1149    pub fn with_component_item(mut self, item: WasiComponentItem) -> Self {
1150        self.program.component_items.push(item);
1151        self
1152    }
1153
1154    /// 添加核心模块
1155    pub fn with_core_module(mut self, core_module: WasiCoreModule) -> Self {
1156        self.program.add_core_module(core_module);
1157        self
1158    }
1159
1160    /// 添加实例
1161    pub fn with_instance(mut self, instance: WasiInstance) -> Self {
1162        self.program.add_instance(instance);
1163        self
1164    }
1165
1166    /// 添加别名
1167    pub fn with_alias(mut self, alias: WasiAlias) -> Self {
1168        self.program.add_alias(alias);
1169        self
1170    }
1171
1172    /// 添加符号到符号表
1173    pub fn with_symbol(mut self, name: impl Into<String>, symbol_type: WasiSymbolType, index: u32) -> Self {
1174        self.program.add_symbol(name.into(), symbol_type, index);
1175        self
1176    }
1177
1178    /// 构建 WasiProgram 实例
1179    pub fn build(self) -> Result<WasiProgram> {
1180        self.program.validate().map(|_| self.program)
1181    }
1182}
1183
1184impl WasiProgram {
1185    /// 创建 WasiProgram 的构建器
1186    pub fn builder(program_type: WasiProgramType) -> WasiProgramBuilder {
1187        WasiProgramBuilder::new(program_type)
1188    }
1189}