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, LimitKind, Result};
cfg_if::cfg_if! { if #[cfg(not(target_arch = "wasm32"))] {
mod native;
use pyo3::prelude::*;
use pyo3::types::{PyCode, PyDict};
use native::create_context;
}}
cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] {
mod wasm;
use std::cell::RefCell;
}}
pub struct PythonEngine {
#[cfg(not(target_arch = "wasm32"))]
pub globals: Py<PyDict>,
#[cfg(target_arch = "wasm32")]
_initialized: bool, #[cfg(target_arch = "wasm32")]
bound_names: RefCell<Vec<String>>,
}
#[derive(Debug)]
pub struct PyCodeHandle {
#[cfg(not(target_arch = "wasm32"))]
pub(crate) code: Py<PyCode>,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) eval_mode: bool,
#[cfg(not(target_arch = "wasm32"))]
pub(crate) has_result: bool,
pub(crate) src: String,
}
impl PythonEngine {
pub fn new() -> Self {
#[cfg(not(target_arch = "wasm32"))]
{
let globals = create_context();
Self { globals }
}
#[cfg(target_arch = "wasm32")]
{
PythonEngine {
_initialized: false,
bound_names: RefCell::new(Vec::new()),
}
}
}
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,
bound_names: RefCell::new(Vec::new()),
};
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(())
}
}
pub fn with_limits() -> Result<Self> {
Err(Error::LimitUnsupported {
backend: super::EngineType::Python,
limit: "resource budgets",
})
}
pub fn set_var(&self, name: &str, value: serde_json::Value) -> Result<()> {
crate::check_var_name(name)?;
#[cfg(not(target_arch = "wasm32"))]
{
native::set_var(self, name, &value)
}
#[cfg(target_arch = "wasm32")]
{
wasm::set_var(self, name, &value)
}
}
pub fn eval_json(&mut self, code: &str) -> Result<serde_json::Value> {
#[cfg(not(target_arch = "wasm32"))]
{
native::eval_json(self, code)
}
#[cfg(target_arch = "wasm32")]
{
wasm::eval_json(self, code)
}
}
pub fn compile_py(&self, src: &str) -> Result<PyCodeHandle> {
if src.len() > crate::DEFAULT_MAX_SOURCE_LEN {
return Err(Error::ResourceExhausted {
backend: super::EngineType::Python,
kind: LimitKind::Source,
});
}
#[cfg(not(target_arch = "wasm32"))]
{
native::compile_py(src)
}
#[cfg(target_arch = "wasm32")]
{
wasm::compile_py(src)
}
}
pub fn eval_compiled(&mut self, handle: &PyCodeHandle) -> Result<serde_json::Value> {
#[cfg(not(target_arch = "wasm32"))]
{
native::eval_compiled(self, handle)
}
#[cfg(target_arch = "wasm32")]
{
wasm::eval_compiled(self, handle)
}
}
pub fn fork(&self) -> Result<Self> {
#[cfg(not(target_arch = "wasm32"))]
{
native::fork(self)
}
#[cfg(target_arch = "wasm32")]
{
wasm::fork(self)
}
}
pub fn clear(&mut self) -> Result<()> {
#[cfg(not(target_arch = "wasm32"))]
{
native::clear(self)
}
#[cfg(target_arch = "wasm32")]
{
wasm::clear(self)
}
}
}
#[cfg(any(test, target_arch = "wasm32"))]
pub(super) fn py_string_literal(json_text: &str) -> String {
let mut out = String::with_capacity(json_text.len() + 2);
out.push('"');
for c in json_text.chars() {
match c {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
_ => out.push(c),
}
}
out.push('"');
out
}
#[cfg(all(test, feature = "python"))]
mod tests {
use super::py_string_literal;
#[test]
fn py_string_literal_escapes_exactly() {
assert_eq!(py_string_literal(""), "\"\"");
assert_eq!(py_string_literal("abc"), "\"abc\"");
assert_eq!(py_string_literal("a\"b"), "\"a\\\"b\"");
assert_eq!(py_string_literal("a\\b"), "\"a\\\\b\"");
assert_eq!(py_string_literal("a\\nb"), "\"a\\\\nb\"");
assert_eq!(py_string_literal("a\\u0001b"), "\"a\\\\u0001b\"");
assert_eq!(py_string_literal("it's"), "\"it's\"");
assert_eq!(py_string_literal("héllo"), "\"héllo\"");
}
}
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
}
}