mlua/src/lib.rs

227 lines
7.6 KiB
Rust
Raw Normal View History

2017-09-14 15:59:59 -05:00
//! # High-level bindings to Lua
//!
2019-10-01 10:11:12 -05:00
//! The `mlua` crate provides safe high-level bindings to the [Lua programming language].
2017-09-14 15:59:59 -05:00
//!
//! # The `Lua` object
//!
//! The main type exported by this library is the [`Lua`] struct. In addition to methods for
//! [executing] Lua chunks or [evaluating] Lua expressions, it provides methods for creating Lua
//! values and accessing the table of [globals].
//!
//! # Converting data
//!
//! The [`ToLua`] and [`FromLua`] traits allow conversion from Rust types to Lua values and vice
//! versa. They are implemented for many data structures found in Rust's standard library.
//!
//! For more general conversions, the [`ToLuaMulti`] and [`FromLuaMulti`] traits allow converting
//! between Rust types and *any number* of Lua values.
//!
2019-10-01 10:11:12 -05:00
//! Most code in `mlua` is generic over implementors of those traits, so in most places the normal
2017-09-14 15:59:59 -05:00
//! Rust data structures are accepted without having to write any boilerplate.
//!
//! # Custom Userdata
//!
//! The [`UserData`] trait can be implemented by user-defined types to make them available to Lua.
//! Methods and operators to be used from Lua can be added using the [`UserDataMethods`] API.
//! Fields are supported using the [`UserDataFields`] API.
2017-09-14 15:59:59 -05:00
//!
2020-12-12 14:37:17 -06:00
//! # Serde support
//!
2020-12-29 19:43:00 -06:00
//! The [`LuaSerdeExt`] trait implemented for [`Lua`] allows conversion from Rust types to Lua values
//! and vice versa using serde. Any user defined data type that implements [`serde::Serialize`] or
//! [`serde::Deserialize`] can be converted.
//! For convenience, additional functionality to handle `NULL` values and arrays is provided.
2020-12-12 14:37:17 -06:00
//!
2020-12-29 19:43:00 -06:00
//! The [`Value`] enum implements [`serde::Serialize`] trait to support serializing Lua values
2020-12-12 14:37:17 -06:00
//! (including [`UserData`]) into Rust values.
//!
//! Requires `feature = "serialize"`.
//!
2020-04-19 19:52:01 -05:00
//! # Async/await support
//!
//! The [`create_async_function`] allows creating non-blocking functions that returns [`Future`].
//! Lua code with async capabilities can be executed by [`call_async`] family of functions or polling
//! [`AsyncThread`] using any runtime (eg. Tokio).
//!
2020-05-13 20:12:22 -05:00
//! Requires `feature = "async"`.
//!
//! # `Send` requirement
//! By default `mlua` is `!Send`. This can be changed by enabling `feature = "send"` that adds `Send` requirement
2020-12-29 19:43:00 -06:00
//! to [`Function`]s and [`UserData`].
2020-05-13 20:12:22 -05:00
//!
2017-09-14 15:59:59 -05:00
//! [Lua programming language]: https://www.lua.org/
//! [`Lua`]: crate::Lua
//! [executing]: crate::Chunk::exec
//! [evaluating]: crate::Chunk::eval
//! [globals]: crate::Lua::globals
//! [`ToLua`]: crate::ToLua
//! [`FromLua`]: crate::FromLua
//! [`ToLuaMulti`]: crate::ToLuaMulti
//! [`FromLuaMulti`]: crate::FromLuaMulti
//! [`Function`]: crate::Function
//! [`UserData`]: crate::UserData
//! [`UserDataFields`]: crate::UserDataFields
//! [`UserDataMethods`]: crate::UserDataMethods
//! [`LuaSerdeExt`]: crate::LuaSerdeExt
//! [`Value`]: crate::Value
//! [`create_async_function`]: crate::Lua::create_async_function
//! [`call_async`]: crate::Function::call_async
//! [`AsyncThread`]: crate::AsyncThread
//! [`Future`]: std::future::Future
2020-12-29 19:43:00 -06:00
//! [`serde::Serialize`]: https://docs.serde.rs/serde/ser/trait.Serialize.html
//! [`serde::Deserialize`]: https://docs.serde.rs/serde/de/trait.Deserialize.html
2020-12-29 16:53:20 -06:00
// mlua types in rustdoc of other crates get linked to here.
2022-06-02 16:44:07 -05:00
#![doc(html_root_url = "https://docs.rs/mlua/0.8.0")]
2017-09-14 15:59:59 -05:00
// Deny warnings inside doc tests / examples. When this isn't present, rustdoc doesn't show *any*
// warnings at all.
#![doc(test(attr(deny(warnings))))]
2020-12-29 16:53:20 -06:00
#![cfg_attr(docsrs, feature(doc_cfg))]
Another major API change, out of stack space is not an Err It, ahem "should not" be possible to exhaust lua stack space in normal usage, and causing stack errors to be Err is slightly obnoxious. I have been wanting to make this change for a while, and removing the callback API from tables makes this sensible *I think*. I can think of a couple of ways that this is not technically true, but I think that they are acceptable, or should be handled differently. One, you can make arbitrarily sized LuaVariadic values. I think this is maybe a bug already, because there is an argument limit in Lua which is lower than the stack limit. I'm not sure what happens there, but if it is a stack based panic, (or any panic?) it is a bug. Two, I believe that if you recurse over and over between lua -> rust -> lua -> rust etc, and call rlua API functions, you might get a stack panic. I think for trusted lua code, this is morally equivalent to a regular stack overflow in plain rust, which is already.. well it's not a panic but it's some kind of safe crash I'm not sure, so I think this is acceptable. For *untrusted* lua code, this could theoretically be a problem if the API provided a callback that would call back into lua, then some lua script could force a stack based panic. There are so many concerns with untrusted lua code, and this library is NOT safe enough yet for untrusted code (it doesn't even provide an option to limit lua to the safe API subset yet!), so this is not currently an issue. When the library provides support for "safe lua", it should come with big warnings anyway, and being able to force a stack panic is pretty minor in comparison. I think if there are other ways to cause unbounded stack usage, that it is a bug, or there can be an error just for that situation, like argument count limits. This commit also fixes several stupid bugs with tests, stack checking, and panics.
2017-06-25 15:52:32 -05:00
#[macro_use]
2017-12-02 16:04:33 -06:00
mod macros;
mod chunk;
2017-05-21 18:50:59 -05:00
mod conversion;
mod error;
mod ffi;
2018-08-05 08:51:39 -05:00
mod function;
mod hook;
2018-08-05 08:51:39 -05:00
mod lua;
#[cfg(feature = "luau")]
mod luau;
2017-05-21 18:50:59 -05:00
mod multi;
2020-05-06 19:18:56 -05:00
mod scope;
mod stdlib;
mod string;
mod table;
mod thread;
2018-08-05 08:51:39 -05:00
mod types;
mod userdata;
mod userdata_impl;
2018-08-05 08:51:39 -05:00
mod util;
mod value;
2017-05-21 18:50:59 -05:00
pub mod prelude;
pub use crate::{ffi::lua_CFunction, ffi::lua_State};
pub use crate::chunk::{AsChunk, Chunk, ChunkMode};
pub use crate::error::{Error, ExternalError, ExternalResult, Result};
pub use crate::function::{Function, FunctionInfo};
2022-02-25 17:51:51 -06:00
pub use crate::hook::{Debug, DebugEvent, DebugNames, DebugSource, DebugStack};
pub use crate::lua::{GCMode, Lua, LuaOptions};
pub use crate::multi::Variadic;
2020-05-06 19:18:56 -05:00
pub use crate::scope::Scope;
pub use crate::stdlib::StdLib;
pub use crate::string::String;
pub use crate::table::{Table, TableExt, TablePairs, TableSequence};
pub use crate::thread::{Thread, ThreadStatus};
pub use crate::types::{Integer, LightUserData, Number, RegistryKey};
pub use crate::userdata::{
AnyUserData, MetaMethod, UserData, UserDataFields, UserDataMetatable, UserDataMethods,
};
pub use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Value};
2022-02-25 17:51:51 -06:00
#[cfg(not(feature = "luau"))]
pub use crate::hook::HookTriggers;
2022-03-24 19:23:35 -05:00
#[cfg(any(feature = "luau", doc))]
#[cfg_attr(docsrs, doc(cfg(feature = "luau")))]
pub use crate::{chunk::Compiler, function::CoverageInfo, types::VmState};
#[cfg(feature = "async")]
pub use crate::thread::AsyncThread;
2020-12-12 14:37:17 -06:00
#[cfg(feature = "serialize")]
#[doc(inline)]
pub use crate::serde::{
de::Options as DeserializeOptions, ser::Options as SerializeOptions, LuaSerdeExt,
};
2020-12-12 14:37:17 -06:00
#[cfg(feature = "serialize")]
2020-12-29 19:43:00 -06:00
#[cfg_attr(docsrs, doc(cfg(feature = "serialize")))]
2020-12-12 14:37:17 -06:00
pub mod serde;
2020-12-29 16:08:53 -06:00
2021-05-05 15:55:49 -05:00
#[cfg(any(feature = "mlua_derive"))]
2020-12-29 16:08:53 -06:00
#[allow(unused_imports)]
#[macro_use]
extern crate mlua_derive;
2021-05-05 15:55:49 -05:00
/// Create a type that implements [`AsChunk`] and can capture Rust variables.
///
/// This macro allows to write Lua code directly in Rust code.
///
/// Rust variables can be referenced from Lua using `$` prefix, as shown in the example below.
2021-06-26 17:41:21 -05:00
/// User's Rust types needs to implement [`UserData`] or [`ToLua`] traits.
2021-05-05 15:55:49 -05:00
///
2021-06-26 17:41:21 -05:00
/// Captured variables are **moved** into the chunk.
2021-05-05 15:55:49 -05:00
///
/// ```
/// use mlua::{Lua, Result, chunk};
///
/// fn main() -> Result<()> {
/// let lua = Lua::new();
/// let name = "Rustacean";
/// lua.load(chunk! {
/// print("hello, " .. $name)
/// }).exec()
/// }
/// ```
///
/// ## Syntax issues
///
/// Since the Rust tokenizer will tokenize Lua code, this imposes some restrictions.
/// The main thing to remember is:
///
/// - Use double quoted strings (`""`) instead of single quoted strings (`''`).
///
/// (Single quoted strings only work if they contain a single character, since in Rust,
/// `'a'` is a character literal).
///
2021-06-26 17:41:21 -05:00
/// - Using Lua comments `--` is not desirable in **stable** Rust and can have bad side effects.
///
/// This is because procedural macros have Line/Column information available only in
/// **nightly** Rust. Instead, Lua chunks represented as a big single line of code in stable Rust.
///
/// As workaround, Rust comments `//` can be used.
///
2021-05-05 15:55:49 -05:00
/// Other minor limitations:
///
2021-06-26 17:41:21 -05:00
/// - Certain escape codes in string literals don't work.
2021-05-05 15:55:49 -05:00
/// (Specifically: `\a`, `\b`, `\f`, `\v`, `\123` (octal escape codes), `\u`, and `\U`).
///
2021-06-26 17:41:21 -05:00
/// These are accepted: : `\\`, `\n`, `\t`, `\r`, `\xAB` (hex escape codes), and `\0`.
2021-05-05 15:55:49 -05:00
///
/// - The `//` (floor division) operator is unusable, as its start a comment.
///
/// Everything else should work.
///
/// [`AsChunk`]: crate::AsChunk
/// [`UserData`]: crate::UserData
/// [`ToLua`]: crate::ToLua
2021-05-05 15:55:49 -05:00
#[cfg(any(feature = "macros"))]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub use mlua_derive::chunk;
2021-11-25 09:43:28 -06:00
/// Registers Lua module entrypoint.
///
/// You can register multiple entrypoints as required.
///
/// ```
/// use mlua::{Lua, Result, Table};
///
/// #[mlua::lua_module]
/// fn my_module(lua: &Lua) -> Result<Table> {
/// let exports = lua.create_table()?;
/// exports.set("hello", "world")?;
/// Ok(exports)
/// }
/// ```
///
/// Internally in the code above the compiler defines C function `luaopen_my_module`.
///
#[cfg(any(feature = "module", docsrs))]
2021-05-05 15:55:49 -05:00
#[cfg_attr(docsrs, doc(cfg(feature = "module")))]
pub use mlua_derive::lua_module;