Skip to main content

mlua_isle/
lib.rs

1//! Thread-isolated Lua VM with cancellation for mlua.
2//!
3//! `mlua-isle` runs a Lua VM on a dedicated thread and communicates via
4//! channels.  This solves two fundamental problems with mlua:
5//!
6//! 1. **`Lua` is `!Send`** — it cannot cross thread boundaries.  By
7//!    confining the VM to one thread and sending requests over a channel,
8//!    callers on any thread (UI, async runtime, etc.) can interact with
9//!    Lua without `Send` issues.
10//!
11//! 2. **Cancellation** — long-running Lua code (including blocking Rust
12//!    callbacks like HTTP calls) can be interrupted via a cancel token
13//!    that triggers both a Lua debug hook and a caller-side signal.
14//!
15//! # Architecture
16//!
17//! ```text
18//! ┌─────────────────┐   mpsc    ┌──────────────────┐
19//! │  caller thread   │─────────►│  Lua thread       │
20//! │  (UI / async)    │          │  (mlua confined)   │
21//! │                  │◄─────────│                    │
22//! │  Isle handle     │  oneshot  │  Lua VM + hook    │
23//! └─────────────────┘           └──────────────────┘
24//! ```
25//!
26//! # Example
27//!
28//! ```rust
29//! use mlua_isle::Isle;
30//!
31//! let isle = Isle::spawn(|lua| {
32//!     lua.globals().set("greeting", "hello")?;
33//!     Ok(())
34//! }).unwrap();
35//!
36//! let result: String = isle.eval("return greeting").unwrap();
37//! assert_eq!(result, "hello");
38//!
39//! isle.shutdown().unwrap();
40//! ```
41
42mod error;
43mod handle;
44mod hook;
45#[cfg(feature = "pool")]
46mod pool;
47mod task;
48mod thread;
49
50#[cfg(feature = "tokio")]
51mod async_isle;
52#[cfg(feature = "tokio")]
53mod async_task;
54
55pub use error::IsleError;
56pub use handle::Isle;
57pub use hook::CancelToken;
58pub use task::Task;
59
60#[cfg(feature = "pool")]
61pub use pool::{IslePool, PoolConfig, PoolStrategy, PooledIsle};
62
63#[cfg(feature = "tokio")]
64pub use async_isle::{AsyncIsle, AsyncIsleBuilder, AsyncIsleDriver};
65#[cfg(feature = "tokio")]
66pub use async_task::AsyncTask;
67
68/// Type alias for exec closures to keep the `Request` enum readable.
69pub(crate) type ExecFn = Box<dyn FnOnce(&mlua::Lua) -> Result<String, IsleError> + Send>;
70
71/// Channel sender for results.
72pub(crate) type ResultTx = std::sync::mpsc::Sender<Result<String, IsleError>>;
73
74/// Request sent from caller to the Lua thread.
75pub(crate) enum Request {
76    /// Evaluate a Lua chunk and return the result as a string.
77    Eval {
78        code: String,
79        cancel: CancelToken,
80        tx: ResultTx,
81    },
82    /// Call a named global function with string arguments.
83    Call {
84        func: String,
85        args: Vec<String>,
86        cancel: CancelToken,
87        tx: ResultTx,
88    },
89    /// Execute an arbitrary closure on the Lua thread.
90    Exec {
91        f: ExecFn,
92        cancel: CancelToken,
93        tx: ResultTx,
94    },
95    /// Graceful shutdown.
96    Shutdown,
97}