r/lua • u/ianm818 • May 25 '26
lua-rs: Lua 5.4.7 implemented from scratch in Rust - passes upstream Lua C test suite fully
Github: https://github.com/ianm199/lua-rs/tree/main
Highlights:
- Passes full upstream tests
- Performance is near parity with C checkout dashboard here on optimization. Table ops seem to be much faster
- Has GC, VM, and supports all Lua sytnax. Any pure lua script should run
- Limited unsafe calls - mostly just in the GC for now
My motivation here was that in the long run we want the core internet utilities to run on memory safe languages, big ones like redis and nginx expose scripting via Lua so if we really want to replace core infra fully in Rust, you'd need a full Rust Lua that doesn't bundle C. After that you should be able to i.e. build drop in replacements for those without a C ABI (or that's part of the way there).
Long term goals:
- Get to stable, production ready
- Get performance at parity or faster then Lua C
- Replace current unsafe GC with fully safe if possible
- (Maybe) support Lua 5.1, LuaJIT
12
Upvotes
19
u/topchetoeuwastaken May 25 '26 edited May 25 '26
i once tried to make my own lua interpreter in C, and it did in fact work. it had multithreading (i think i even managed to get it running in all cases i was testing for), it interpreted its own compiler, written in lua. however, it now sits abandoned.
why? because it offered virtually no tangible advantage over PUC lua's interpreter. it was slower, but even if i had spent the time optimizing it, it would probably not get much better than luajit's interpreter (which i consider close to the theoretically best-performing interpreter). and even if i did that, that would be for virtually no benefit, as i would essentially be creating a piece of software that offers no added value, but at the cost of forcing adopters to move from PUC lua/luajit to my interpreter.
this is why most rewrites lay abandoned, because they really have no reason to exist. luajit beat the odds, because it has a lot of added value over stock lua - an FFI, performance and great compatibility with stock lua. it is an overall exceptionally well designed piece of software.
even ignoring the whole baloney argument of memory-safety (any sufficiently disciplined C program will be sufficiently memory-safe, while on the other hand, rust isn't even memory-safe, it gives you the allure of memory safety by screaming at you when it thinks you're doing something unsafe), a rewrite almost never has any benefits, other than educational.
PS: also, specify when your rewrite is using AI, as otherwise it comes across as dishonest and makes you look like a script kiddie
4
u/msanatan May 25 '26
Just so I can appreciate the use of Rust, do you have empirical data to back up the use of Rust instead of C for Lua in particular?
4
u/gorv256 May 26 '26
But why!?
If you had made at least some benchmarks to compare it to C It would be understandable. But just for some vague "safety" claims? Have you even read your entire codebase yourself?
PUC-Lua is one of the highest-quality code bases out there. You should burn your tokens on finding bugs instead if you doubt that and want to improve security.
44
u/drcforbin May 25 '26
"Implemented from scratch in Rust" is very misleading. This is a LLM-generated port of the C code to Rust.
1
u/Old_County5271 May 25 '26
Could've done Lua 5.5, why 5.4?
1
u/Lagger625 May 27 '26
where performance
1
u/arkt8 May 27 '26
As a toy or project it is nice.
Rewriting from scratch:
You have an entire code surface prone to attacks in the sense it is not battle tested.. suite tests are good but not as good of decades of bugfixes and many unorthodox ways of using it as standalone or embedded.
Rewrite it from scratch is like to back to the days of the Lua 1.0.
"run in memory safe languages".... C is memory safe if has not unsafe code. Rust maybe safe if it have no unsafe code... that is not the case for many many lowlevel or specialized code.
So, sorry, but your project is filled with passion and bad arguments that for serious and critical usage it is very risky (if not unsafe)
1
u/AvailableArcher6530 May 28 '26
Why not Lua-5.4.8?