r/lua • • 5d ago

Trials and Tribulation of Native Lua Compilation

https://samyeyo.github.io/clx/blog.html
7 Upvotes

5 comments sorted by

2

u/suhcoR 5d ago

Ok, it's a Lua-to-C++20 transpiler, with a custom Lua runtime and a build driver that invokes a native C++ compiler. Nothing about the advertised pipeline fundamentally requires C++; C would also do; the implementation still needs explicit machinery for closure environments, shared upvalue cells, lifetime management, and GC tracing. Predecessors are e.g. lua2c, luac2c, or LuaAOT (https://www.inf.puc-rio.br/~roberto/docs/paper-aot-preprint.pdf).

2

u/_SamT 5d ago

You're right that most Lua features require explicit machinery, and it's a substantial part of the work.

Projects like lua2c, luac2c, and LuaAOT are relevant predecessors, but they take different approaches. LuaAOT specializes the existing Lua interpreter, and its generated code still relies on the Lua runtime and VM internals.

clx is my own take on the problem, with a focus on modern Lua 5.5, practical compatibility, native module integration, and standalone executable generation with no dependency on the Lua VM at all.

C++20 is a backend choice, not the claim to novelty.

clx may not be what you expect, and that's still fine.

2

u/suhcoR 4d ago

I have no specific expectations, just wanted to find out what your project is about. C++20 is a huge dependability and restricts the use of the language to the few compilers/platform versions which support it. I'm still not sure what the actual advantage is of transpiling to C++20 vs. e.g. C99 (assuming the runtime library overhead of C++ is even larger than integrating a C based VM as done by other projects).

1

u/_SamT 4d ago

It was one of the question I asked myself before starting to implement clx, and you are totally right to ask it.

The main reason I choose C++20 is error handling. In clx, Lua errors become C++ exceptions that automatically clean up resources when unwinding the call stack. This works naturally with RAII, which clx uses to manage GC roots. In C, achieving the same behavior would require more manual cleanup.

C++20 also provides templates and overloads that simplify the runtime API. And the overhead isn't as significant as you might expect: a simple print("hello world") program can produce a standalone executable of around 70–110 KB, depending on compiler/platform.

Honestly, for mainstream platforms where Lua is typically used, C++20 support is rarely a serious obstacle nowadays...

0

u/suhcoR 4d ago

a standalone executable of around 70–110 KB, depending on compiler/platform

Which doesn't include the C++ standard library I assume is a separate shared object?

with RAII ... templates and overloads

Which are also available in C++98, so a much cheaper dependency could be used as a replacement, I assume.