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