grove/WASM/mod.rs
1//! WebAssembly Runtime Module
2//!
3//! This module provides WebAssembly runtime support using WASMtime,
4//! enabling Grove to execute VS Code extensions compiled to WebAssembly.
5//!
6//! # Architecture
7//!
8//! ```text
9//! +++++++++++++++++++++++++++++++++++++++
10//! + WASM Runtime +
11//! +++++++++++++++++++++++++++++++++++++++
12//! + wasmty::Engine +
13//! + wasmty::Store +
14//! + wasmty::Module +
15//! + wasmty::Instance +
16//! +++++++++++++++++++++++++++++++++++++++
17//! + +
18//! ▼ ▼
19//! ++++++++++++++++++++ ++++++++++++++++++++
20//! + HostBridge + + MemoryManager +
21//! + (Communication) + + (Memory mgmt) +
22//! ++++++++++++++++++++ ++++++++++++++++++++
23//! +
24//! ▼
25//! ++++++++++++++++++++
26//! + FunctionExport +
27//! + (Host exports) +
28//! ++++++++++++++++++++
29//! ```
30//!
31//! # Key Components
32//!
33//! - [`Runtime`] - WASMtime engine and store management
34//! - [`ModuleLoader`] - WASM module compilation and instantiation
35//! - [`MemoryManager`] - WASM memory allocation and management
36//! - [`HostBridge`] - Host-WASM function communication bridge
37//! - [`FunctionExport`] - Export host functions to WASM
38
39pub mod FunctionExport;
40pub mod HostBridge;
41pub mod MemoryManager;
42pub mod ModuleLoader;
43pub mod Runtime;
44
45// Re-exports for convenience - use module prefix to avoid E0255 conflicts
46pub use Runtime::{WASMConfig, WASMRuntime};
47// Note: ModuleLoader, MemoryManager, HostBridge, FunctionExport must be accessed via module prefix
48use anyhow::Result;
49
50/// Default configuration for WASM runtime
51/// Default memory limit in megabytes
52pub const DEFAULT_MEMORY_LIMIT_MB:u64 = 512;
53/// Default maximum execution time in milliseconds
54pub const DEFAULT_MAX_EXECUTION_TIME_MS:u64 = 30000;
55/// Default table size
56pub const DEFAULT_TABLE_SIZE:u32 = 1024;
57
58/// WASM runtime statistics
59#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
60pub struct WASMStats {
61 /// Number of loaded modules
62 pub modules_loaded:usize,
63 /// Number of active instances
64 pub active_instances:usize,
65 /// Total memory used ( MB)
66 pub total_memory_mb:u64,
67 /// Total execution time (ms)
68 pub total_execution_time_ms:u64,
69 /// Number of function calls
70 pub function_calls:u64,
71}
72
73impl Default for WASMStats {
74 fn default() -> Self {
75 Self {
76 modules_loaded:0,
77 active_instances:0,
78 total_memory_mb:0,
79 total_execution_time_ms:0,
80 function_calls:0,
81 }
82 }
83}
84
85/// Initialize WASM runtime with default configuration
86///
87/// # Example
88///
89/// ```rust,no_run
90/// use grove::WASM;
91///
92/// # async fn example() -> anyhow::Result<()> {
93/// let runtime = WASM::init_wasm_runtime().await?;
94/// # Ok(())
95/// # }
96/// ```
97pub async fn init_wasm_runtime() -> Result<WASMRuntime> { WASMRuntime::new(WASMConfig::default()).await }
98
99#[cfg(test)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_default_config() {
105 let config = WASMConfig::default();
106 assert_eq!(config.memory_limit_mb, DEFAULT_MEMORY_LIMIT_MB);
107 }
108
109 #[test]
110 fn test_stats_default() {
111 let stats = WASMStats::default();
112 assert_eq!(stats.modules_loaded, 0);
113 assert_eq!(stats.active_instances, 0);
114 }
115}