Show HN: Clx – 通过 C++20 将 Lua 编译为原生可执行文件
Show HN: Clx – Compile Lua to Native Executables Through C++20

原始链接: https://github.com/samyeyo/clx

**clx** 是一款预先(AOT)Lua 编译器及运行时,旨在生成独立的本地可执行文件。通过利用现代 C++ 工具链(Clang、GCC、MSVC),clx 消除了字节码解释器的开销,从而实现快速启动、可预测的运行时性能以及紧凑的二进制文件体积。 **主要特性包括:** * **AOT 编译:** 直接将兼容 Lua 5.5 的代码编译为本地机器码。 * **性能:** 通过激进的编译器优化和轻量级、面向值的运行时,提供极具竞争力的运行速度。 * **可移植性:** 支持创建独立的本地应用程序,并提供用于构建可移植二进制模块的 C++ API。 * **效率:** 采用 16 字节标记值(tagged values)、内联字符串优化以及快速路径表访问,以最大限度地减少内存开销。 clx 目前处于测试阶段,支持核心语言特性、表、元表和协程,且正在持续改进兼容性。虽然它不支持动态代码加载(如 `load()`)或传统的 Lua C API,但为希望部署高性能、自包含 Lua 应用程序的开发者提供了一个强大的替代方案。 项目文件、基准测试及完整文档可在 [GitHub](https://github.com/samyeyo/clx) 获取。clx 在 MIT 许可证下发布。

**Clx** 是一个全新的标准 Lua 预编译(AOT)编译器,可将代码转换为 C++20。它利用 GCC、Clang 或 MSVC 等现有工具链生成独立的本地可执行文件,作为基于 LLVM 或直接生成机器码方案的一种便携式替代方案。 作为现代 C++ 后端实用性的一次实验,Clx 相比标准 Lua 解释器带来了显著的性能提升,并在特定的计算密集型任务中能够超越 LuaJIT。最新版本引入了用于替代 NaN 标记的“影子类型”(shadow-types)、完整的 int64 支持,以及对 ARM64 macOS 协程的兼容性。该项目仓库中包含基于自定义 Sokol API 构建的图形示例,如 Pong 游戏和曼德博集合浏览器。
相关文章

原文

clx is a cross-platform ahead-of-time Lua compiler and runtime that generates standalone native executables through modern C++ toolchains. clx is not trying to be the fastest Lua implementation in every workload.

Its goal is to provide:

  • ahead-of-time native compilation,
  • deployable standalone executables,
  • predictable runtime performance,
  • fast startup times,
  • integration with existing C++ toolchains,
  • and strong optimization opportunities through modern native compilers.
git clone https://github.com/samyeyo/clx.git
cd clx
./build.sh install       # or build.bat install on Windows
clx examples/hello/hello.lua
./hello

Hello clx !
  • Competitive performance with strong results on many AOT-friendly workloads
  • No bytecode interpreter overhead — compiles to standalone native executables
  • Aggressive optimizations — leverages modern optimizations via Clang/GCC/MSVC
  • Small binaries — size-oriented builds can produce very compact executables (Lua programs can be under 100 KB with --minimal)
  • Targets Lua 5.5 compatibility — coroutines, metamethods, tables, and more
  • 16-byte tagged values — 8-byte payload + separate type tag
  • Inline string optimization (strings ≤ 6 bytes stored in value, no allocation)
  • Fast-path table access caches
  • Lightweight AOT-oriented runtime
  • clx C++ API: develop portable native modules using a value-oriented API

clx comes with examples, that uses a Sokol clx binary module for graphics, and demonstrate native desktop application development with standard Lua code.

Pong

pong : a complete game written in Lua and compiled into a standalone native executable.

Mandelbrot

Mandelbrot : a Mandelbrot viewer written in Lua and compiled into a standalone native executable.

clx is currently in beta. The compiler is already capable of compiling non-trivial Lua applications, but compatibility work and optimization improvements are ongoing.

  • Linux: g++ (recommended for TCO) or clang++
  • macOS: clang++ (Xcode) or g++ via Homebrew (for TCO)
  • Windows: g++ (LLVM) or MSVC
  • CMake 3.15+ for building

Note: The compiler used to build clx is fixed at build time via CMake and used for all Lua script compilation. This ensures ABI compatibility between the runtime libraries and generated code. Rebuild clx with a different compiler if you need a different backend.

./build.sh              # Release (default)
./build.sh debug        # Debug
./build.sh clean        # Removes build/ + /usr/local install
./build.sh install      # Release + install to /usr/local
./build.sh uninstall    # Removes installed files in /usr/local
./build.bat              # Release (default)
./build.bat debug        # Debug
./build.bat clean        # Removes build/ + ./bin and ./lib
./build.bat install      # Release + install to /bin and ./lib
./build.bat uninstall    # Removes previously installed clx

Also works directly with CMake:

mkdir -p build && cmake -S . -B build && cmake --build build

Once compiled, you will find :

  • build/clx — The compiler executable
  • build/libclx.a — Static runtime library
  • build/libclx_size.a — Static runtime library optimized for size
./build/clx file.lua                         # Compile to executable (default flags)
./build/clx --object file.lua                # Object file (.o/.obj)
./build/clx --static file.lua                # Static clx module (.a/.lib)
./build/clx --cpp file.lua                   # Generate C++ source, don't compile
./build/clx file.lua -O2                     # Forward unknown clx flags to the backend compiler
./build/clx file.lua --output f.exe          # Custom output name
./build/clx file.lua --debug                 # No optimizations, debug symbols
./build/clx file.lua --minimal               # base + package modules only
./build/clx file.lua --fast                  # Optimize for speed
./build/clx file.lua --size                  # Optimize for size (default)
./build/clx --version                        # Print version
./build/clx --help                           # Display help

clx targets Lua 5.5 compatibility.

Current status:

  • Core language: largely implemented
  • Tables and metatables: implemented
  • Coroutines: implemented
  • Modules: implemented
  • Most standard libraries: implemented

See compatibility.md for detailed status.

  • load() / dofile() / loadfile() / string.dump() — dynamic code loading requires a runtime interpreter
  • debug module — very complex in a pure AOT model
  • The traditional Lua C API is not supported.
  • Binary modules should be written using the clx C++ API.
./tests/run.sh              # POSIX
./tests/run.bat             # Windows

Each .lua in tests/ is compiled to a binary and executed. Tests print [OK]/[FAIL] per assertion.

Results are expressed in speedup factor against standard Lua 5.5 interpreter :

Script lua 5.5 LuaJIT clx --fast
fib.lua 0.311s (1.00x) 0.045s (6.91x) 0.005s (62.20x)
arraysum.lua 0.128s (1.00x) 0.052s (2.46x) 0.031s (4.13x)
spectralnorm.lua 0.310s (1.00x) 0.018s (17.22x) 0.029s (10.69x)
canada.lua 0.372s (1.00x) 0.142s (2.62x) 0.286s (1.30x)
warmup.lua 0.006s (1.00x) 0.005s (1.20x) 0.005s (1.20x)

Measured on Intel® Core™ i5 Ultra 125U CPU @ 4.30GHz · Linux · GCC 13.3.0 · Avg of 10 runs

Full benchmarks are available in clx benchmarks

Documentation is available in the doc/ directory, including :

  • Getting Started
  • CLI Reference
  • Compatibility Status
  • Modules and Migration Guide
  • C++ API Reference
  • Runtime Internals
  • Architecture Overview
  • Optimizations
  • Benchmarks

See Documentation Index

clx is MIT Licensed — Copyright (c) 2026 Tine Samir

联系我们 contact @ memedata.com