Skip to main content

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// Note: ModuleLoader, MemoryManager, HostBridge, FunctionExport must be
46// accessed via module prefix
47use anyhow::Result;
48
49/// Default configuration for WASM runtime
50/// Default memory limit in megabytes
51pub const DEFAULT_MEMORY_LIMIT_MB:u64 = 512;
52/// Default maximum execution time in milliseconds
53pub const DEFAULT_MAX_EXECUTION_TIME_MS:u64 = 30000;
54/// Default table size
55pub const DEFAULT_TABLE_SIZE:u32 = 1024;
56
57/// WASM runtime statistics
58#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
59pub struct WASMStats {
60	/// Number of loaded modules
61	pub modules_loaded:usize,
62	/// Number of active instances
63	pub active_instances:usize,
64	/// Total memory used ( MB)
65	pub total_memory_mb:u64,
66	/// Total execution time (ms)
67	pub total_execution_time_ms:u64,
68	/// Number of function calls
69	pub function_calls:u64,
70}
71
72impl Default for WASMStats {
73	fn default() -> Self {
74		Self {
75			modules_loaded:0,
76			active_instances:0,
77			total_memory_mb:0,
78			total_execution_time_ms:0,
79			function_calls:0,
80		}
81	}
82}
83
84/// Initialize WASM runtime with default configuration
85///
86/// # Example
87///
88/// ```rust,no_run
89/// use grove::WASM;
90///
91/// # async fn example() -> anyhow::Result<()> {
92/// let runtime = WASM::init_wasm_runtime().await?;
93/// # Ok(())
94/// # }
95/// ```
96pub async fn init_wasm_runtime() -> Result<Runtime::WASMRuntime> {
97	Runtime::WASMRuntime::new(Runtime::WASMConfig::default()).await
98}
99
100#[cfg(test)]
101mod tests {
102	use super::*;
103
104	#[test]
105	fn test_default_config() {
106		let config = Runtime::WASMConfig::default();
107		assert_eq!(config.memory_limit_mb, DEFAULT_MEMORY_LIMIT_MB);
108	}
109
110	#[test]
111	fn test_stats_default() {
112		let stats = WASMStats::default();
113		assert_eq!(stats.modules_loaded, 0);
114		assert_eq!(stats.active_instances, 0);
115	}
116}