This library is a high level interface between Rust and Lua. Its major goal is to expose as easy to use, practical, and flexible of an API between Rust and Lua as possible, while also being completely safe.
rlua
is designed around "registry handles" to values inside the Lua state.
This means that when you get a type like rlua::Table
or rlua::Function
in
Rust, what you actually hold is an integer key into the Lua registry. This is
different from the bare Lua C API, where you create tables / functions on the
Lua stack and must be aware of their stack location. This is also similar to
how other Lua bindings systems like
Selene for C++ work, but it means that
using rlua
may be slightly slower than what you could conceivably write using
the C API. The reasons for this design are safety and flexibility, and to
prevent the user of rlua
from having to be aware of the Lua stack at all.
There are currently a few missing pieces of this API:
_ENV
upvalue of a loaded chunk to a table other than _G
, so that you can
have different environments for different loaded chunks.Additionally, there are ways I would like to change this API, once support lands in rustc. For example:
It is also worth it to list some non-goals for the project:
This library is very much Work In Progress, so there is a some API churn. Currently, it follows a pre-1.0 semver, so all API changes should be accompanied by 0.x version bumps.
The goal of this library is complete safety, it should not be possible to cause undefined behavior whatsoever with the API, even in edge cases. There is, however, QUITE a lot of unsafe code in this crate, and I would call the current safety level of the crate "Work In Progress". Still, I am not currently aware of any way to cause UB, and UB is considered the most serious kind of bug, so if you find the ability to cause UB with this API at all, please file a bug report.
Another goal of this library is complete protection from panics and aborts.
Currently, it should not be possible for a script to trigger a panic or abort
(with some important caveats described below). Similarly to the safety goal,
there ARE several internal panics and even aborts in rlua
source, but they
should not be possible to trigger, and if you trigger them this should be
considered a bug.
There are some caveats to the panic / abort guarantee, however:
rlua
reserves the right to panic on API usage errors. Currently, the only
time this will happen is when passed a registry handle type from a different
main Lua state.realloc
from libc
, but it is
wrapped in such a way that OOM errors are guaranteed to abort. This is
not currently such a huge deal outside of untrusted scripts, as this matches
the behavior of Rust itself. Doing this allows the internals of rlua
to,
in certain cases, call 'm' Lua C API functions with the garbage collector
disabled and know that these cannot error. Eventually, rlua
will support
memory limits on scripts, and those memory limits will cause regular memory
errors rather than OOM aborts.Yet another goal of the library is to, in all cases, safely handle panics generated by Rust callbacks. Panic unwinds in Rust callbacks should currently be handled correctly -- the unwind is caught and carried across the Lua API boundary as a regular Lua error in a way that prevents Lua from catching it. This is done by overriding the normal Lua 'pcall' and 'xpcall' with custom versions that cannot catch errors that are actually from Rust panics, and by handling panic errors on the receiving Rust side by resuming the panic.
In summary, here is a list of rlua
behaviors that should be considered a bug.
If you encounter them, a bug report would be very welcome:
rlua
without typing the word "unsafe",
this is absolutely 100% a bug.gcc
crate, and
cfg!(debug_assertions)
is true, Lua is built with the LUA_USE_APICHECK
define set. Any abort caused by this internal Lua API checking is
absolutely a bug, particularly because without LUA_USE_APICHECK
it would
generally be unsafe.rlua
is triggering a
longjmp over your Rust stack frames, this is a bug!