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;
45mod task;
46mod thread;
47
48#[cfg(feature = "tokio")]
49mod async_isle;
50#[cfg(feature = "tokio")]
51mod async_task;
52
53pub use error::IsleError;
54pub use handle::Isle;
55pub use hook::CancelToken;
56pub use task::Task;
57
58#[cfg(feature = "tokio")]
59pub use async_isle::{AsyncIsle, AsyncIsleBuilder, AsyncIsleDriver};
60#[cfg(feature = "tokio")]
61pub use async_task::AsyncTask;
62
63/// Type alias for exec closures to keep the `Request` enum readable.
64pub(crate) type ExecFn = Box<dyn FnOnce(&mlua::Lua) -> Result<String, IsleError> + Send>;
65
66/// Channel sender for results.
67pub(crate) type ResultTx = std::sync::mpsc::Sender<Result<String, IsleError>>;
68
69/// Request sent from caller to the Lua thread.
70pub(crate) enum Request {
71    /// Evaluate a Lua chunk and return the result as a string.
72    Eval {
73        code: String,
74        cancel: CancelToken,
75        tx: ResultTx,
76    },
77    /// Call a named global function with string arguments.
78    Call {
79        func: String,
80        args: Vec<String>,
81        cancel: CancelToken,
82        tx: ResultTx,
83    },
84    /// Execute an arbitrary closure on the Lua thread.
85    Exec {
86        f: ExecFn,
87        cancel: CancelToken,
88        tx: ResultTx,
89    },
90    /// Graceful shutdown.
91    Shutdown,
92}