Skip to main content

Grove/Binary/Build/
RuntimeBuild.rs

1//! Runtime Build Module
2//!
3//! Provides runtime construction for the Grove extension host.
4//! Handles building and initializing the host runtime.
5
6use std::sync::Arc;
7
8use anyhow::{Context, Result};
9use crate::dev_log;
10
11use crate::{
12	Host::{ExtensionHost::ExtensionHostImpl, HostConfig},
13	Transport::Strategy::Transport,
14	WASM::Runtime::{WASMConfig, WASMRuntime},
15};
16
17/// Runtime build utilities
18pub struct RuntimeBuild;
19
20impl RuntimeBuild {
21	/// Build a Groove extension host with the specified configuration
22	pub async fn build_host(
23		transport:Transport,
24		_wasm_runtime:Arc<WASMRuntime>,
25		host_config:HostConfig,
26	) -> Result<ExtensionHostImpl> {
27		dev_log!("grove", "Building Grove extension host");
28
29		// In a real implementation, we would use the provided wasm_runtime
30		// For now, we create the host with default configuration
31
32		let host = ExtensionHostImpl::with_config(transport, host_config.clone())
33			.await
34			.context("Failed to build extension host")?;
35
36		dev_log!("grove", "Extension host built successfully");
37
38		Ok(host)
39	}
40
41	/// Build a Grove extension host with default WASM configuration
42	pub async fn build_host_with_defaults(
43		transport:Transport,
44		wasi:bool,
45		memory_limit_mb:u64,
46		max_execution_time_ms:u64,
47	) -> Result<ExtensionHostImpl> {
48		dev_log!("grove", "Building Grove extension host with defaults");
49
50		let wasm_config = WASMConfig::new(memory_limit_mb, max_execution_time_ms, wasi);
51		let wasm_runtime = Arc::new(WASMRuntime::new(wasm_config).await?);
52
53		let host_config = HostConfig::default().with_activation_timeout(max_execution_time_ms);
54
55		Self::build_host(transport, wasm_runtime, host_config).await
56	}
57
58	/// Build a minimal extension host for testing
59	pub async fn build_minimal_host(transport:Transport) -> Result<ExtensionHostImpl> {
60		dev_log!("grove", "Building minimal extension host");
61
62		let host_config = HostConfig::default().with_max_extensions(10).with_lazy_activation(true);
63
64		let wasm_config = WASMConfig::new(64, 10000, false);
65		let wasm_runtime = Arc::new(WASMRuntime::new(wasm_config).await?);
66
67		Self::build_host(transport, wasm_runtime, host_config).await
68	}
69
70	/// Validate build configuration
71	pub fn validate_config(config:&HostConfig) -> Result<()> {
72		if config.max_extensions == 0 {
73			return Err(anyhow::anyhow!("max_extensions must be at least 1"));
74		}
75
76		if config.activation_timeout_ms == 0 {
77			return Err(anyhow::anyhow!("activation_timeout_ms must be at least 1"));
78		}
79
80		Ok(())
81	}
82}
83
84impl Default for RuntimeBuild {
85	fn default() -> Self { Self }
86}
87
88#[cfg(test)]
89mod tests {
90	use super::*;
91
92	#[test]
93	fn test_runtime_build_default() {
94		let builder = RuntimeBuild::default();
95		// Just test that it can be created
96		let _ = builder;
97	}
98
99	#[test]
100	fn test_validate_config() {
101		let valid_config = HostConfig::default();
102		assert!(RuntimeBuild::validate_config(&valid_config).is_ok());
103
104		let mut invalid_config = HostConfig::default();
105		invalid_config.max_extensions = 0;
106		assert!(RuntimeBuild::validate_config(&invalid_config).is_err());
107	}
108}