Tsuki
Tsuki is a port of Lua 5.4 to Rust. This is a port, not binding; which mean all code are Rust and can be using without C compiler[^1]. The initial works was done by C2Rust. Note that this port was done without compatibility with the previous version. You can see a list of the differences here.
[!WARNING] Tsuki currently in a pre-1.0 so prepare for a lot of breaking changes!
Status
The VM to run Lua code is fully working almost exactly as vanilla Lua (see some of differences below). Some functions on Lua standard library are still missing.
[!IMPORTANT] All types in Tsuki does not implement
SendandSyncand no plan to support this at the moment.
Safety
All public API of Tsuki should provide 100% safety as long as you don't use unsafe API incorrectly.
Tsuki was not designed to run untrusted Lua script. Although you can limit what Lua script can do by not expose a function to it but there is no way to limit amount of memory or execution time used by Lua script. The meaning of this is Lua script can cause a panic due to out of memory or never return the control back to Rust with infinite loop.
Performance
Interpreter
Tsuki is slower than Lua about 60%. The only possibility for Tsuki to be faster than Lua with computed goto is JIT since computed goto does not available on Rust.
Async
A call to async function without any suspend on Tsuki is faster than mlua about 3.5x. For 1 suspend Tsuki it faster about 3x. For 8 suspend Tsuki is faster about 2x.
Features
- 100% Rust code.
- libc is required at the moment.
- Support both synchronous and asynchronous.
- Safe, ergonomic and low overhead API.
- Strongly typed registry.
- Rust collections to store Lua values (e.g. BTreeMap).
- Any error propagated to the caller via Rust
Resultinstead of a long jump. core::any::Anyas Lua userdata and can be created without the need to define its metatable.- Metatable for a userdata is lookup with
core::any::TypeIdinstead of a string. - Property system on userdata to store per-object values for fast access from Lua.
Differences from Lua
VM and Language
- Binary chunk is not supported.
- Hook functions is not supported.
- Light userdata is not supported.
- Panic when memory allocation is failed without retry (same as Rust).
- GC has only one mode and cannot control from outside.
- Chunk name does not have a prefix (e.g.
@). - Second argument to
__closemetamethod alwaysnil. __gcmetamethod is not supported.__namemetavalue must be UTF-8 string.__tostringmetamethod must return a UTF-8 string.- Float to string conversion does not truncate precision (Lua limit to 14 digits by default).
- Float literal does not accept hexadecimal format.
- U+000B VERTICAL TAB is not considered as a whitespace.
- C locale is ignored (once
libchas been completely removed).
Standard library
- No
_VERSION,collectgarbage,dofile,loadfile,warn,xpcall,string.dumpand debug library. - Second argument of
assertaccept only a UTF-8 string. - Arguments of
error:- First argument accept only a UTF-8 string.
- Second argument is not supported and it is always assume 1.
- Arguments of
load:- First argument accept only a string.
- Second argument accept only a UTF-8 string and will be empty when absent.
- Third argument must be
nilor"t".
string.formatrequires UTF-8 string for both format string and format value.string.findandstring.gsubdoes not support classz.- Native module is not supported.
- Environment variable
LUA_PATHandLUA_PATH_5_4is ignored. LUA_NOENVin registry is ignored.
Non-goals
- Become a superset of Lua (e.g. Luau).
- C API compatibility.
- Stand-alone mode.
- 16-bit systems.
Roadmap
- Complete Lua standard library.
- Remove libc.
Breaking changes in 0.3
TryCallhas been removed andContext::try_forwardwas merged withContext::forward.- Return type of
Context::forwardhas been changed. Arg::get_metatablewas renamed toArg::metatable.Arg::as_strhas parameter to allow converting a number to string.Arg::to_strandArg::to_nilable_strnow convert a number to string in-place.Arg::to_floatandArg::to_nilable_floatnow returnFloatinstead off64.NumberNo longer implementPartialEq.Value::FloatandNumber::Floatvalue is changed fromf64toFloat.Value::Fpvalue is changed fromfntoFp.Value::AsyncFpvalue is changed fromfntoAsyncFp.Value::Boolhas been replaced withValue::FalseandValue::True.Thread::async_callnow accept onlyLuaFn.Module::Instancewas renamed toModule::Inst.StringLibwas renamed toStrLib.Table::contains_str_keynow requiresAsRef<str>on the key. UseTable::contains_bytes_keyif you want old requirements.Table::get_str_keynow requiresAsRef<str>on the key. UseTable::get_bytes_keyif you want old requirements.Opsnow a private type.Contextand its related types now live incontextmodule.- Float to string conversion does not truncate precision (Lua limit this to 14 digits by default).
- Float literal no longer accept hexadecimal format.
- U+000B VERTICAL TAB no longer considered as a whitespace.
Frequently Asked Questions
Can we have zero-based indexing?
This requires too much changes to the language so the answer is no. See #16 for more details.
License
Same as Lua, which is MIT.
[^1]: On Windows, a proxy to sprintf written in C++ is required at the moment. This proxy will be removed when we replace sprintf calls with Rust equivalent.