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>,
#[cfg(target_arch = "wasm32")]
_initialized: bool, }
impl PythonEngine {
pub fn new() -> Self {
#[cfg(not(target_arch = "wasm32"))]
{
let (globals, locals) = create_context();
Self { globals, locals }
}
#[cfg(target_arch = "wasm32")]
{
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")]
{
let mut engine = Self {
_initialized: false,
};
wasm::register_bundle(&mut engine, bundle)?;
Ok(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")]
{
wasm::register_bundle(self, bundle)?;
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
}
}