postscriptum 0.1.0

Multi-interpreted-language scripting engine for Rust with Rhai and Python backends
Documentation
pub mod function_bundle;

cfg_if::cfg_if! {
    if #[cfg(target_arch = "wasm32")] {
        pub mod wasm_function_bundle;
    }
}

use super::{FunctionBundle, ScriptEngine};
use crate::{Error, Result};

cfg_if::cfg_if! { if #[cfg(not(target_arch = "wasm32"))] {
    mod native;
    use pyo3::prelude::*;
    use pyo3::types::PyDict;
    use native::create_context;
}}

cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] {
    mod wasm;
}}

pub struct PythonEngine {
    #[cfg(not(target_arch = "wasm32"))]
    pub globals: Py<PyDict>,
    #[cfg(not(target_arch = "wasm32"))]
    pub locals: Py<PyDict>,
    // Empty struct for native, has initialized field for WASM
    #[cfg(target_arch = "wasm32")]
    _initialized: bool, // renamed to avoid unused warning on WASM
}

impl PythonEngine {
    pub fn new() -> Self {
        #[cfg(not(target_arch = "wasm32"))]
        {
            let (globals, locals) = create_context();
            Self { globals, locals }
        }
        #[cfg(target_arch = "wasm32")]
        {
            // We can't guarantee Pyodide is loaded at this time, so we just mark as uninitialized
            PythonEngine {
                _initialized: false,
            }
        }
    }

    pub fn with_bundle(bundle: &FunctionBundle) -> Result<Self> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            native::create_with_bundle(bundle).map_err(|e| Error::Python(e.to_string()))
        }

        #[cfg(target_arch = "wasm32")]
        {
            // For WASM, we create the engine and then register the bundle if it's available
            let mut engine = Self {
                _initialized: false,
            };
            // Try to register the bundle immediately - this will work if Pyodide is already loaded
            wasm::register_bundle(&mut engine, bundle)?;
            Ok(engine)
        }
    }

    // Register a function bundle with this Python engine
    pub fn register_bundle(&mut self, bundle: &FunctionBundle) -> Result<()> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            native::register_bundle(self, bundle).map_err(|e| Error::Python(e.to_string()))
        }

        #[cfg(target_arch = "wasm32")]
        {
            // For WASM, we register with Pyodide when the bundle is available
            wasm::register_bundle(self, bundle)?;
            // Update the initialized status after successful registration
            self._initialized = true;
            Ok(())
        }
    }
}

impl Default for PythonEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl ScriptEngine for PythonEngine {
    fn eval(&mut self, code: &str) -> Result<String> {
        #[cfg(not(target_arch = "wasm32"))]
        {
            native::eval(self, code)
        }

        #[cfg(target_arch = "wasm32")]
        {
            wasm::eval(self, code)
        }
    }

    fn as_any(&mut self) -> &mut dyn std::any::Any {
        self
    }
}