LuaX is a transpiler that converts Lua 5.4 source code into C++20, allowing you to compile Lua scripts into standalone native executables. It bridges the gap between Lua's dynamic flexibility and C++'s performance and portability.
- It is also an experiment in vibe-coding. The vast majority of the code is written solely by AI.
- Lua 5.4 Support: Supports a wide range of Lua 5.4 syntax and semantics.
- Standard Library: Includes implementations for most Lua standard libraries:
math: Full support (trigonometry, random, etc.).string: Pattern matching, formatting, and manipulation.table: Sorting, packing/unpacking, and manipulation.io: File I/O,popen,tmpfile,lines, and more.os: System interaction, date/time, and execution.utf8: UTF-8 string support.coroutine: Thread-based implementation (Parallelism removed for now, but will be readded later once LuaX is more stable)package: Basic module loading support.
- C++ Integration: Generates readable C++ code that uses a custom runtime library (
LuaValue,LuaObject) to emulate Lua's dynamic typing. - Standalone Executables: Compiles your Lua scripts directly into native binaries.
- Lua 5.4: Required to run the transpiler script (
src/luax.lua). - C++ Compiler: A C++20 compliant compiler (e.g.,
clang++,g++). - CMake: For the build process.
To transpile and compile a Lua script (e.g., tests/main.lua):
# Standard compilation
lua5.4 src/luax.lua tests/main.lua -o build/my_program -b build -kThis command will:
- Transpile
tests/main.luaand its dependencies into C++ source files within thebuild/directory. - Compile the generated C++ code alongside the LuaX runtime library.
- Produce an executable named
build/my_program. - Preserve the intermediate C++ files (due to the
-kor--keepflag).
Available Options:
-o, --output: Set the path/name of the resulting executable.-b, --build-dir: Specify where intermediate files are stored (default isbuild).-k, --keep: Preserve the generated C++ code after compilation.-t, --translate-only: Generate C++ source files but skip the compilation/binary step.
Then, simply run the executable:
./build/my_programCheck out the tests/ directory for sample scripts:
tests/main.lua: General feature test.tests/test_io_missing.lua: Tests for IO functions (popen,tmpfile, etc.).tests/test_coroutines.lua: Tests for coroutine functionality.
LuaX works by traversing the Lua AST (Abstract Syntax Tree) and generating equivalent C++ code.
src/luax.lua: The main entry point and build orchestrator.src/translator.lua: Parses Lua code into an AST.src/cpp_translator.lua: Converts the AST into C++ code.lib/: Contains the C++ runtime library (LuaValue,LuaObject,LuaCoroutine, etc.) and standard library implementations (math.cpp,io.cpp, etc.).
debugLibrary: Not implemented.- Dynamic Loading:
load,loadfile, anddofileare not supported because the C++ code is compiled ahead-of-time. Userequirefor static dependencies. - Garbage Collection: The runtime uses C++ smart pointers (
std::shared_ptr) for memory management, which differs from Lua's garbage collector (e.g., reference counting vs. mark-and-sweep). Cycle detection is not currently implemented. - Speed: Partially faster depending on what you are trying to do, though the performance gap is closing in.
LuaX uses two complementary build approaches:
-
For Development (CMake): The
CMakeLists.txtprovides IDE tooling support and buildslibluax_runtime.afor development workflows. -
For User Programs (Dynamic Makefile): The transpiler (
src/luax.lua) dynamically generates a Makefile for each project, making it directory-agnostic and independent of where LuaX is installed.
This separation ensures that:
- Users can transpile Lua programs from anywhere without setup
- Developers get full IDE support when working on the runtime