amberjs 1.16.1

JavaScript/TypeScript runtime built with Rust and V8
Documentation
// Multi-language Support Module
// Provides seamless integration between Amber and multiple programming languages
pub mod go_runtime;
pub mod python_runtime;
pub mod rust_native;

use anyhow::Result;
use go_runtime::GoRuntime;
use python_runtime::PythonRuntime;
use rust_native::RustOptimizer;
use std::sync::Arc;

/// Multi-language runtime manager
#[derive(Debug)]
pub struct MultiLanguageRuntime {
    python: Option<PythonRuntime>,
    go: Option<GoRuntime>,
    rust: Arc<RustOptimizer>,
}
impl MultiLanguageRuntime {
    /// Create a new multi-language runtime
    pub fn new() -> Self {
        MultiLanguageRuntime {
            python: None,
            go: None,
            rust: Arc::new(RustOptimizer::new()),
        }
    }
    /// Initialize Python runtime
    pub fn init_python(&mut self, amber_api: Arc<go_runtime::AmberAPI>) -> Result<()> {
        self.python = Some(PythonRuntime::new(amber_api)?);
        Ok(())
    }
    /// Initialize Go runtime
    pub fn init_go(&mut self, amber_api: Arc<go_runtime::AmberAPI>) -> Result<()> {
        self.go = Some(GoRuntime::new(amber_api)?);
        Ok(())
    }
    /// Execute code in specified language
    pub async fn execute(&self, language: &str, code: &str) -> Result<String> {
        match language.to_lowercase().as_str() {
            "python" | "py" => {
                if let Some(python) = &self.python {
                    python.execute_python(code).await
                } else {
                    Err(anyhow::anyhow!("Python runtime not initialized"))
                }
            }
            "go" => {
                if let Some(go) = &self.go {
                    go.execute_go(code).await
                } else {
                    Err(anyhow::anyhow!("Go runtime not initialized"))
                }
            }
            "rust" | "rs" => {
                // Rust is native, just optimize and execute
                let optimized: _ = self.rust.optimize_hot_path(code).await?;
                Ok(optimized.optimized)
            }
            _ => Err(anyhow::anyhow!("Unsupported language: {}", language)),
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::multilang::go_runtime::AmberRuntimeInterface;

    #[tokio::test]
    async fn test_multilang_runtime() {
        let mut runtime = MultiLanguageRuntime::new();
        // Test Python execution
        let python_api: _ = Arc::new(go_runtime::AmberAPI {
            runtime: Arc::new(MockAmberRuntime),
        });
        runtime.init_python(python_api).unwrap();
        let result: _ = runtime.execute("python", "print('Hello Python')").await;
        assert!(result.is_ok());
        // Test Go execution
        let go_api: _ = Arc::new(go_runtime::AmberAPI {
            runtime: Arc::new(MockAmberRuntime),
        });
        runtime.init_go(go_api).unwrap();
        let result: _ = runtime.execute("go", "fmt.Println('Hello Go')").await;
        assert!(result.is_ok());
        // Test Rust execution
        let result: _ = runtime
            .execute("rust", "fn main() { println!('Hello'); }")
            .await;
        assert!(result.is_ok());
    }
    struct MockAmberRuntime;
    impl AmberRuntimeInterface for MockAmberRuntime {
        fn execute_script(&self, script: &str) -> Result<String> {
            Ok(format!("Executed: {}", script))
        }
        fn get_variable(&self, _name: &str) -> Result<String> {
            Ok("mock_value".to_string())
        }
        fn set_variable(&self, _name: &str, _value: &str) -> Result<()> {
            Ok(())
        }
    }
}