mlua/README.md

98 lines
4.6 KiB
Markdown
Raw Normal View History

2017-05-21 20:47:32 -05:00
# rlua -- High level bindings between Rust and Lua
This library is a WIP high level interface between Rust and Lua. Its major goal
is to expose as flexible of an API between Rust and Lua as possible, while also
being completely safe.
There are other high level lua bindings systems for rust, and this crate is an
exploration of a different part of the design space. The main high level
interface to Lua right now is [hlua](https://github.com/tomaka/hlua/) which you
should definitely check out and use if it suits your needs. This crate has the
following differences with hlua:
* Handles to Lua values use the Lua registry, not the stack
* Handles to Lua values are all internally mutable
2017-05-22 10:46:48 -05:00
* Handles to Lua values have non-mutable borrows to the main Lua object, so
there can be multiple handles or long lived handles
* Targets lua 5.3
2017-05-21 20:47:32 -05:00
The key difference here is that rlua handles rust-side references to Lua values
in a fundamentally different way than hlua, more similar to other lua bindings
2017-05-21 20:49:15 -05:00
systems like [Selene](https://github.com/jeremyong/Selene) for C++. Values like
2017-05-21 20:47:32 -05:00
LuaTable and LuaFunction that hold onto Lua values in the Rust stack, instead of
pointing at values in the Lua stack, are placed into the registry with luaL_ref.
In this way, it is possible to have an arbitrary number of handles to internal
Lua values at any time, created and destroyed in arbitrary order. This approach
IS slightly slower than the approach that hlua takes of only manipulating the
lua stack, but this, combined with internal mutability, allows for a much more
flexible API.
Currently exposes a *somewhat* complete Lua API covering values and tables and
functions and userdata, but does not yet cover coroutines. This API is actually
heavily inspired by the lua API that I previously wrote for Starbound, and will
become feature complete with that API over time. Some capabilities that API has
that are on the roadmap:
* Proper coroutine support
* Lua profiling support
* Execution limits like total instruction limits or lua <-> rust recursion
limits
* Security limits on the lua stdlib, and general control over the loaded
lua libraries.
* "Context" or "Sandboxing" support, this was probably a bit too heavyweight
in Starbound's API, but there will be the ability to set the `_ENV` upvalue
of a loaded chunk to a table other than `_G`, so that you can have different
environments for different loaded chunks.
2017-05-21 20:47:32 -05:00
There are also some more general things that need to be done:
* More fleshed out Lua API, things like custom metatables and exposing the
registry.
* MUCH better API documentation, the current API documentation is almost
non-existent.
* Performance testing.
2017-05-21 20:47:32 -05:00
Additionally, there are ways I would like to change this API, once support lands
in rustc. For example:
* Once ATCs land, there should be a way to wrap callbacks based on argument
and return signature, rather than calling lua.pack / lua.unpack inside the
callback. Until then, it is impossible to name the type of the function
that would do the wrapping.
* Once tuple based variadic generics land, the plan is to completely
2017-05-22 15:40:43 -05:00
eliminate the hlist macros in favor of simple tuples.
2017-05-21 20:47:32 -05:00
2017-05-21 20:51:25 -05:00
See [this reddit discussion](http://www.reddit.com/r/rust/comments/5yujt6/) for
2017-05-21 22:34:47 -05:00
details of the current lifetime problem with callback wrapping.
2017-05-21 20:47:32 -05:00
## API Stability or lack thereof
This library is very much Work In Progress, so there may be a lot of API churn.
I will try to follow a pre-1.0 semver (if such a thing exists), but that means
there will just be a large number of API bumps.
2017-05-21 21:04:32 -05:00
## Safety
My *goal* 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". The GOAL is for the crate to handle tricky
situations such as:
* Panic safety, and carrying the panic across the lua api correctly
2017-05-22 15:40:43 -05:00
* Passing rust panics across the lua boundary as lua errors without allowing
lua to catch rust panics as normal errors.
* Lua stack size checking, and correctly handling lua being out of stack
space
* Leaving the correct elements on the lua stack and in the correct order,
and panicking if these invariants are not met (due to internal bugs).
* Correctly guarding the metatables of userdata so that scripts cannot, for
example, swap the `__gc` methods around and cause UB.
2017-05-21 21:04:32 -05:00
2017-05-22 15:40:43 -05:00
The library currently *attempts* to handle each of these situations, but there
2017-05-21 21:04:32 -05:00
are so many ways to cause unsafety with Lua that it just needs more testing.
2017-05-21 20:51:25 -05:00
## Examples
2017-05-21 20:52:14 -05:00
Please look at the examples [here](examples/examples.rs).