post AI cleanup & fixes

This commit is contained in:
2025-08-12 22:28:01 +02:00
parent 088b7f48aa
commit 81e0954e3a
5 changed files with 434 additions and 24 deletions

400
Cargo.lock generated Normal file
View File

@@ -0,0 +1,400 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ahash"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
dependencies = [
"cfg-if",
"const-random",
"getrandom 0.3.3",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "const-random"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359"
dependencies = [
"const-random-macro",
]
[[package]]
name = "const-random-macro"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e"
dependencies = [
"getrandom 0.2.16",
"once_cell",
"tiny-keccak",
]
[[package]]
name = "crunchy"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.1+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi 0.14.2+wasi-0.2.4",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indoc"
version = "2.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd"
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
]
[[package]]
name = "libc"
version = "0.2.175"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
[[package]]
name = "memoffset"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
dependencies = [
"autocfg",
]
[[package]]
name = "no-std-compat"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c"
dependencies = [
"spin",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
dependencies = [
"portable-atomic",
]
[[package]]
name = "portable-atomic"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
[[package]]
name = "proc-macro2"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d61789d7719defeb74ea5fe81f2fdfdbd28a803847077cecce2ff14e1472f6f1"
dependencies = [
"unicode-ident",
]
[[package]]
name = "pyo3"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8970a78afe0628a3e3430376fc5fd76b6b45c4d43360ffd6cdd40bdde72b682a"
dependencies = [
"indoc",
"libc",
"memoffset",
"once_cell",
"portable-atomic",
"pyo3-build-config",
"pyo3-ffi",
"pyo3-macros",
"unindent",
]
[[package]]
name = "pyo3-build-config"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458eb0c55e7ece017adeba38f2248ff3ac615e53660d7c71a238d7d2a01c7598"
dependencies = [
"once_cell",
"target-lexicon",
]
[[package]]
name = "pyo3-ffi"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7114fe5457c61b276ab77c5055f206295b812608083644a5c5b2640c3102565c"
dependencies = [
"libc",
"pyo3-build-config",
]
[[package]]
name = "pyo3-macros"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8725c0a622b374d6cb051d11a0983786448f7785336139c3c94f5aa6bef7e50"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
"quote",
"syn",
]
[[package]]
name = "pyo3-macros-backend"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4109984c22491085343c05b0dbc54ddc405c3cf7b4374fc533f5c3313a572ccc"
dependencies = [
"heck",
"proc-macro2",
"pyo3-build-config",
"quote",
"syn",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rhai"
version = "1.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2780e813b755850e50b178931aaf94ed24f6817f46aaaf5d21c13c12d939a249"
dependencies = [
"ahash",
"bitflags",
"instant",
"no-std-compat",
"num-traits",
"once_cell",
"rhai_codegen",
"smallvec",
"smartstring",
"thin-vec",
]
[[package]]
name = "rhai-python"
version = "0.1.0"
dependencies = [
"pyo3",
"pyo3-build-config",
"rhai",
]
[[package]]
name = "rhai_codegen"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5a11a05ee1ce44058fa3d5961d05194fdbe3ad6b40f904af764d81b86450e6b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "smartstring"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb72c633efbaa2dd666986505016c32c3044395ceaf881518399d2f4127ee29"
dependencies = [
"autocfg",
"static_assertions",
"version_check",
]
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "target-lexicon"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e502f78cdbb8ba4718f566c418c52bc729126ffd16baee5baa718cf25dd5a69a"
[[package]]
name = "thin-vec"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "144f754d318415ac792f9d69fc87abbbfc043ce2ef041c60f16ad828f638717d"
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "unindent"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -8,8 +8,8 @@ name = "rhai_python"
crate-type = ["cdylib"]
[dependencies]
rhai = { version = "1.17", features = ["sync"] }
pyo3 = { version = "0.20", features = ["extension-module"] }
rhai = { version = "1.22", features = ["sync"] }
pyo3 = { version = "0.25", features = ["extension-module"] }
[build-dependencies]
pyo3-build-config = "0.20"
pyo3-build-config = "0.25"

View File

@@ -1,5 +1,5 @@
[build-system]
requires = ["maturin>=1.0,<2.0"]
requires = ["maturin>=1.8,<2.0"]
build-backend = "maturin"
[project]
@@ -26,4 +26,10 @@ dynamic = ["version"]
[tool.maturin]
features = ["pyo3/extension-module"]
module-name = "rhai_python"
module-name = "rhai.rhai_python"
python-source = "python"
[tool.uv]
dev-dependencies = [
"maturin>=1.8.0",
]

View File

@@ -4,9 +4,9 @@ Rhai Python Bindings
Python bindings for the Rhai scripting language.
"""
from .rhai_python import RhaiEngine, __version__
from .rhai_python import RhaiEngine
__all__ = ["RhaiEngine", "__version__"]
__all__ = ["RhaiEngine", "eval"]
# Convenience function for quick evaluation

View File

@@ -1,17 +1,22 @@
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use rhai::{Array, Dynamic, Engine, EvalAltResult, Map, Scope};
use std::collections::HashMap;
use pyo3::IntoPyObjectExt;
use rhai::{Array, Dynamic, Engine, Map, Scope};
/// Convert Rhai Dynamic to Python object
fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
match value.type_name() {
"i64" => Ok(value.as_int().unwrap().to_object(py)),
"f64" => Ok(value.as_float().unwrap().to_object(py)),
"bool" => Ok(value.as_bool().unwrap().to_object(py)),
"string" => Ok(value.into_string().unwrap().to_object(py)),
"char" => Ok(value.as_char().unwrap().to_string().to_object(py)),
"i64" => Ok(value.as_int().unwrap().into_py_any(py).unwrap()),
"f64" => Ok(value.as_float().unwrap().into_py_any(py).unwrap()),
"bool" => Ok(value.as_bool().unwrap().into_py_any(py).unwrap()),
"string" => Ok(value.into_string().unwrap().into_py_any(py).unwrap()),
"char" => Ok(value
.as_char()
.unwrap()
.to_string()
.into_py_any(py)
.unwrap()),
"()" => Ok(py.None()),
"array" => {
let array = value.cast::<Array>();
@@ -19,17 +24,17 @@ fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
for item in array {
py_list.append(dynamic_to_python(py, item)?)?;
}
Ok(py_list.to_object(py))
Ok(py_list.into_py_any(py)?.into())
}
"map" => {
let map = value.cast::<Map>();
let py_dict = PyDict::new(py);
for (key, value) in map {
let py_key = key.to_object(py);
let py_key = key.to_string().into_py_any(py).unwrap();
let py_value = dynamic_to_python(py, value)?;
py_dict.set_item(py_key, py_value)?;
}
Ok(py_dict.to_object(py))
Ok(py_dict.into_py_any(py)?.into())
}
_ => Err(PyRuntimeError::new_err(format!(
"Unsupported Rhai type: {}",
@@ -39,7 +44,7 @@ fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
}
/// Convert Python object to Rhai Dynamic
fn python_to_dynamic(py: Python<'_>, obj: &PyAny) -> PyResult<Dynamic> {
fn python_to_dynamic(py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<Dynamic> {
if obj.is_none() {
Ok(Dynamic::UNIT)
} else if let Ok(val) = obj.extract::<bool>() {
@@ -53,14 +58,14 @@ fn python_to_dynamic(py: Python<'_>, obj: &PyAny) -> PyResult<Dynamic> {
} else if let Ok(py_list) = obj.downcast::<PyList>() {
let mut array = Array::new();
for item in py_list {
array.push(python_to_dynamic(py, item)?);
array.push(python_to_dynamic(py, &item)?);
}
Ok(Dynamic::from(array))
} else if let Ok(py_dict) = obj.downcast::<PyDict>() {
let mut map = Map::new();
for (key, value) in py_dict {
let key_str = key.extract::<String>()?;
let rhai_value = python_to_dynamic(py, value)?;
let rhai_value = python_to_dynamic(py, &value)?;
map.insert(key_str.into(), rhai_value);
}
Ok(Dynamic::from(map))
@@ -100,7 +105,7 @@ impl RhaiEngine {
}
/// Set a variable in the engine scope
fn set_var(&mut self, py: Python<'_>, name: &str, value: &PyAny) -> PyResult<()> {
fn set_var(&mut self, py: Python<'_>, name: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
let dynamic_value = python_to_dynamic(py, value)?;
self.scope.set_value(name, dynamic_value);
Ok(())
@@ -142,15 +147,14 @@ impl RhaiEngine {
fn list_vars(&self) -> Vec<String> {
self.scope
.iter()
.map(|(name, _)| name.to_string())
.map(|(name, _, _)| name.to_string())
.collect()
}
}
/// A Python module implemented in Rust using PyO3
#[pymodule]
fn rhai_python(_py: Python, m: &PyModule) -> PyResult<()> {
fn rhai_python(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<RhaiEngine>()?;
m.add("__version__", "0.1.0")?;
Ok(())
}