Skip to main content

Module helpers

Module helpers 

Source
Expand description

§Auxiliary Tool Module

This module provides various auxiliary functions and utility tools used by the WASI assembler, including:

  • Utility Functions: Commonly used utility functions.
  • Type Conversion: Conversion functions between different types.
  • Validation Functions: Input validation and checking functions.
  • Constant Definitions: Commonly used constants and enumeration values.

§Main Features

§Utility Functions

Provides various practical utility functions:

  • String processing and validation.
  • Numeric conversion and encoding.
  • Error handling and formatting.
  • Debugging and logging functionality.

§Type Conversion

Supports conversion between various types:

  • Conversion between WebAssembly types and Rust types.
  • Conversion between text format and binary format.
  • Conversion between high-level structures and low-level representations.

§Validation Functions

Provides input validation and checking functions:

  • Syntax validation.
  • Semantic checking.
  • Type validation.
  • Boundary checking.

§Usage Example

use wasi_assembler::helpers::*;

// Validate function name
if is_valid_function_name("my_function") {
    println!("Function name is valid");
}

// Convert WebAssembly type
let wasm_type = rust_type_to_wasm_type::<i32>();
println!("WASM type corresponding to Rust i32: {:?}", wasm_type);

// Encode LEB128 value
let value = 12345u64;
let encoded = encode_leb128(value);
println!("LEB128 encoding result: {:?}", encoded);

// Validate module structure
let module = create_sample_module();
if validate_module_structure(&module) {
    println!("Module structure is valid");
}

§Extensibility

This module is designed to be extensible, allowing for the easy addition of new auxiliary functions:

use wasi_assembler::helpers::*;

// Add custom validation function
pub fn validate_custom_section(name: &str, data: &[u8]) -> bool {
    // Custom validation logic
    !name.is_empty() && !data.is_empty() && data.len() <= 65535
}

// Use existing utility functions
let name = "my_custom_section";
let data = vec![1, 2, 3, 4, 5];
if validate_custom_section(name, &data) {
    println!("Custom section is valid");
}

Functions§

create_sample_module
创建示例模块
encode_leb128
对整数进行 LEB128 编码
is_valid_function_name
检查函数名称是否有效
rust_type_to_wasm_type
将 Rust 类型转换为 WASM 类型
validate_module_structure
验证模块结构