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§
- Wasi
Alias - 别名定义
- Wasi
Core Func - 核心函数定义
- Wasi
Core Instance - 核心实例定义
- Wasi
Core Module - 核心模块定义
- Wasi
Custom Section - WASM 自定义段
- Wasi
Data Segment - 数据段定义
- Wasi
Element Segment - 元素段定义
- Wasi
Export - WASM 导出
- Wasi
Function - WASM 函数定义
- Wasi
Function Type - WASM 函数类型
- Wasi
Global - WASM 全局变量
- Wasi
Import - WASM 导入
- Wasi
Instance - 实例定义
- Wasi
Instance Arg - 实例化参数
- Wasi
Memory - WASM 内存定义
- Wasi
Program - WASI 程序的高层次表示
- Wasi
Program Builder - WasiProgram 的构建器
- Wasi
Record Field - 记录字段
- Wasi
Resource Method - 资源方法
- Wasi
Resource Type - 资源类型
- Wasi
Symbol - 符号定义
- Wasi
Table - WASM 表定义
- Wasi
Type Definition - 类型定义
- Wasi
Variant Case - 变体情况
- Wasm
Global Type - WASM 全局变量类型
- Wasm
Info - Wasm
Local - WASM 局部变量
- Wasm
Memory Type - WASM 内存类型
- Wasm
Table Type - WASM 表类型
Enums§
- Wasi
Alias Target - 别名目标枚举
- Wasi
Canon Option - 规范化选项枚举
- Wasi
Canonical Operation - 规范化操作枚举
- Wasi
Component Item - 组件项目枚举
- Wasi
Core Type - 核心类型枚举
- Wasi
Instance Type - 实例类型枚举
- Wasi
Instruction - WASM 指令
- Wasi
Primitive Type - 原始类型枚举
- Wasi
Program Type - 程序类型枚举
- Wasi
Symbol Type - 符号类型枚举
- Wasi
Type - 类型枚举
- Wasm
Export Type - WASM 导出类型
- Wasm
Import Type - WASM 导入类型
- Wasm
Reference Type - WASM 引用类型
- Wasm
Value Type - WASM 值类型