Skip to main content

Module program

Module program 

Source
Expand description

§WASM 程序高层次抽象

这个模块定义了 WASM 程序的高层次表示,用于在 WAT AST 和二进制 WASM 之间提供中间抽象层。 支持 WebAssembly Component Model 和传统的核心模块。

§核心概念

§WasiProgram

主要的程序结构,可以表示两种类型的 WebAssembly 程序:

  • 核心模块 (WasiProgramType::CoreModule): 传统的 WebAssembly 模块
  • 组件 (WasiProgramType::Component): WebAssembly Component Model 组件

§程序组件

程序包含以下主要组件:

  • 函数类型: 定义函数的签名(参数和返回值类型)
  • 函数: 实际的函数实现
  • 导入: 从外部模块导入的函数、内存等
  • 导出: 向外部暴露的函数、内存等
  • 内存: 线性内存定义
  • : 函数表定义
  • 全局变量: 全局变量定义
  • 自定义段: 自定义数据段

§组件模型支持

对于组件类型,还支持:

  • 组件项目: 类型定义、别名、实例等
  • 核心模块: 嵌套的核心模块
  • 实例: 组件或模块实例
  • 别名: 名称别名定义

§使用示例

§创建简单的核心模块

use wasi_assembler::program::{
    WasiProgram, WasiProgramType, WasiFunctionType, WasiFunction,
    WasiExport, WasmExportType, WasiInstruction, WasmValueType
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建核心模块程序
    let mut program = WasiProgram::new(WasiProgramType::CoreModule);
    
    // 添加函数类型 (i32, i32) -> i32
    program.function_types.push(WasiFunctionType {
        params: vec![WasmValueType::I32, WasmValueType::I32],
        results: vec![WasmValueType::I32],
    });
    
    // 添加函数实现
    program.functions.push(WasiFunction {
        type_index: 0, // 使用第一个函数类型
        locals: vec![],   // 没有局部变量
        body: vec![
            WasiInstruction::LocalGet { local_index: 0 },  // 获取第一个参数
            WasiInstruction::LocalGet { local_index: 1 },  // 获取第二个参数
            WasiInstruction::I32Add,       // i32 加法
        ],
    });
    
    // 导出函数
    program.exports.push(WasiExport {
        name: "add".to_string(),
        export_type: WasmExportType::Function { function_index: 0 },
    });
    
    // 生成 WASM 字节码
    let wasm_bytes = program.to_wasm()?;
    Ok(())
}

§创建组件

use wasi_assembler::program::{
    WasiProgram, WasiProgramType, WasiComponentItem,
    WasiTypeDefinition, WasiType, WasiInstance, WasiInstanceType
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建组件程序
    let mut program = WasiProgram::new(WasiProgramType::Component);
    
    // 添加接口类型
    program.component_items.push(WasiComponentItem::Type(
        WasiTypeDefinition {
            name: Some("calculator-interface".to_string()),
            index: 0,
            type_content: WasiType::Interface("calculator".to_string()),
        }
    ));
    
    // 添加实例
    program.instances.push(WasiInstance {
        name: Some("calc".to_string()),
        index: 0,
        instantiate_target: "calculator-interface".to_string(),
        args: vec![],
        instance_type: WasiInstanceType::Component,
    });
    
    // 生成组件字节码
    let component_bytes = program.to_wasm()?;
    Ok(())
}

§符号表管理

程序包含一个符号表,用于管理名称到索引的映射:

use wasi_assembler::program::{WasiProgram, WasiProgramType, WasiSymbol, WasiSymbolType};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut program = WasiProgram::new(WasiProgramType::CoreModule);
    
    // 符号表自动维护名称到索引的映射
    program.symbol_table.insert("my_function".to_string(), WasiSymbol {
        name: "my_function".to_string(),
        symbol_type: WasiSymbolType::Function,
        index: 0,
    });
    program.symbol_table.insert("my_memory".to_string(), WasiSymbol {
        name: "my_memory".to_string(),
        symbol_type: WasiSymbolType::Memory,
        index: 0,
    });
    
    // 通过名称查找符号
    if let Some(symbol) = program.symbol_table.get("my_function") {
        println!("函数 my_function 的索引是: {}", symbol.index);
    }
    Ok(())
}

Structs§

WasiAlias
别名定义
WasiCoreFunc
核心函数定义
WasiCoreInstance
核心实例定义
WasiCoreModule
核心模块定义
WasiCustomSection
WASM 自定义段
WasiDataSegment
数据段定义
WasiElementSegment
元素段定义
WasiExport
WASM 导出
WasiFunction
WASM 函数定义
WasiFunctionType
WASM 函数类型
WasiGlobal
WASM 全局变量
WasiImport
WASM 导入
WasiInstance
实例定义
WasiInstanceArg
实例化参数
WasiMemory
WASM 内存定义
WasiProgram
WASI 程序的高层次表示
WasiProgramBuilder
WasiProgram 的构建器
WasiRecordField
记录字段
WasiResourceMethod
资源方法
WasiResourceType
资源类型
WasiSymbol
符号定义
WasiTable
WASM 表定义
WasiTypeDefinition
类型定义
WasiVariantCase
变体情况
WasmGlobalType
WASM 全局变量类型
WasmInfo
WasmLocal
WASM 局部变量
WasmMemoryType
WASM 内存类型
WasmTableType
WASM 表类型

Enums§

WasiAliasTarget
别名目标枚举
WasiCanonOption
规范化选项枚举
WasiCanonicalOperation
规范化操作枚举
WasiComponentItem
组件项目枚举
WasiCoreType
核心类型枚举
WasiInstanceType
实例类型枚举
WasiInstruction
WASM 指令
WasiPrimitiveType
原始类型枚举
WasiProgramType
程序类型枚举
WasiSymbolType
符号类型枚举
WasiType
类型枚举
WasmExportType
WASM 导出类型
WasmImportType
WASM 导入类型
WasmReferenceType
WASM 引用类型
WasmValueType
WASM 值类型