MLua Extras
[!NOTE] Feel free to use this crate and start working with ideas and features that could be useful.
Pull requests and contribution are encouraged
If you want to discuss this project, you can do that here
The goal of this project is to add a light convenience layer wrapping the mlua crate. The goal isn't to change the way that mlua is used, but instead to make lua embedded development in Rust more enjoyable.
Similar Projects
Tealr: A project to enhance and extendmluawith a focus in type information and documentation along with a type syntax in the lua code itself with thetealrsyntax.- This crate is a great choice if you need: type syntax, type information, documentation generation
Features
-
Helper Traits
LuaExtras- Manipulate the lua
pathandcpathvariables withappend,prepend, andsetmethods for each variant. It also includes the ability to add multiple paths with each variant. - Set global variables and functions with
set_global("value", "value")andset_global_function("func", |lua, ()| Ok(()))which wold replacelua.globals().set("value", "value)andlua.globals().set("func", lua.create_function(|lua, ()| Ok(()))?)respectively
- Manipulate the lua
-
Typed Lua Traits
Typed- Generate a
TypeandParamfor a rust type so it can be used both as a type and as a parameter for a function
- Generate a
TypedUserData- Typed variant of
mlua::UserDatawith an additionaladd_documentationmethod to add doc comments to theUserDatatype - An extra
documentmethod is added to theTypedDataFieldsandTypedDataMethodsforadd_fieldsandadd_methods. This will queue doc comments to be added to the next field or method that is added. - All types from function parameters and and return types are stored for fields, functions, and methods.
- This trait is mainly used when generating type definitions. If it is called through the
UserDataderive macro it will ignore all types and documentation
- Typed variant of
TypedDataFields: Implemented on a generator forTypedUserData(add_fields)TypedDataMethods: Implemented on a generator forTypedUserData(add_methods)TypedDataDocumentation: Implemented on a generator forTypedUserData(add_documentation)
-
Derive Macros
Typed: Auto implement theTypedtrait to get type information for bothstructandenumUserData: Auto implement themlua::UserDatatrait for rust types that also implementTypedUserData. This will pass through theUserDataadd_methodsandadd_fieldsto theTypedUserData's version. This will ignore all documentation and types.
Ideas and Planned Features
- Fully featured definition file generation
- Fully featured documentation generation
- Fully featured addon generator when creating a lua modules with
mlua'smodulefeature - Better and more informative type errors associated with lua type definitions and output generation
- More expressive way of defining exposed lua api types
- Generic types
- Doc comments for params and return types
References
Example Syntax
Helpers
use ;
use ;
Types
use Deserialize;
use ;
Produces the following definition file
--- init.d.lua
--- @meta
--- @alias System SystemBlack
--- | SystemRed
--- | SystemGreen
--- | SystemYellow
--- | SystemBlue
--- | SystemCyan
--- | SystemMagenta
--- | SystemWhite
--- @class _System
--- @class SystemBlack: _System
--- @class SystemRed: _System
--- @class SystemGreen: _System
--- @class SystemYellow: _System
--- @class SystemBlue: _System
--- @class SystemCyan: _System
--- @class SystemMagenta: _System
--- @class SystemWhite: _System
System,
Xterm,
Rgb,
--- @alias Color ColorSystem | ColorXterm | ColorRgb
--- @class _Color
--- @class ColorSystem: _Color
--- @field [1] SystemColor
--- @class ColorXterm: _Color
--- @field [1] integer
--- @class ColorRgb: _Color
--- @field [1] integer
--- @field [2] integer
--- @field [3] integer
--- This is a doc comment section for the overall type
--- @class Example
--- Example complex type
--- @field color Color
--- Example module
--- @type Example
example = nil
--- Greet the name that was passed in
--- @param name string Name of the person to greet
--- @param param0 Color
Macros
There are helper macros that make writing lua integrations simplier and less manual. There are variants that support recording type information, and variants that just focus on making the creation of custom userdata types simple.
use PathBuf;
use ;
/// Simple Counter
Results in the lua type definition
--- @meta
--- Simple Counter
--- @class Counter
--- Direction of the counter
--- @field direction string
--- @field value integer
local _CLASS_Counter_ =
Testing
To run all the tests in one shot, use cargo test --features luau,vendored,send,async,serialize,derive
Some features of this crate generate luau compatible definition files, or use luau specific features. To add an additional layer of validation to the tests you can install luau-lsp and the tests will run the type checker, and fail if the results are not as expected.
See our luau docs for more information on installing the lsp; there are pre-built binaries available which makes it quick and painless.
The TEST_LUAU environment variable controls luau-lsp validation:
| Value | Behavior |
|---|---|
| (unset) | Auto-detect luau-lsp on PATH. If found, run validation; otherwise skip. |
0 |
Skip validation entirely, even if luau-lsp is on PATH. |
| path | Use the given value as the luau-lsp binary path (e.g. TEST_LUAU=/tmp/luau-lsp). |
Some tests validate the LuaLS-format (.d.lua) definition files generated by
DefinitionFileGenerator. These tests use
lua-language-server, which
ships as a self-contained binary with no runtime dependencies. Pre-built
archives for Linux, macOS, and Windows are available on the
releases page.
To install on Linux x64:
VERSION=
|
The TEST_LUALS environment variable controls validation:
| Value | Behavior |
|---|---|
| (unset) | Auto-detect lua-language-server on PATH. If found, run validation; otherwise skip. |
0 |
Skip validation entirely, even if lua-language-server is on PATH. |
| path | Use the given value as the binary path (e.g. TEST_LUALS=/opt/lua-ls/bin/lua-language-server). |