Luau Language Documentation

repository·master·Indexed 26 days ago

https://github.com/luau-lang/luau

Luau is a fast, small, safe, and gradually typed embeddable scripting language derived from Lua 5.1, designed for high performance and game development. It features a state-of-the-art type inference system, a Control Flow Graph (CFG) in Static Single Assignment (SSA) form for reliable type stating, and CLI tools including a REPL (`luau`), a type checker and linter (`luau-analyze`), and a bytecode analysis tool.

Tokens
6K
Snippets
17
Records
44
Agent score
83%

What's inside Luau

  1. Understand the Control Flow Graph (CFG) design

    master
    Luau uses a Control Flow Graph (CFG) in Static Single Assignment (SSA) form to facilitate reliable type stating and refinements. This structure allows the analyzer to determine exactly which version of a variable is being referenced at any given point in the code, making it easier to reason about back edges in control flow and enabling advanced analysis like effect tracking.
  2. Use Luau CLI tools

    master

    Luau provides two primary command-line tools:

    1. luau: A command-line REPL and script runner. Note that the REPL runs in a sandboxed environment and cannot access the file system directly, except for require-ing modules.
    2. luau-analyze: A command-line type checker and linter. It uses file configuration via --! comments or .luaurc files to produce errors and warnings.
  3. Handle Isocline asynchronously

    master

    Isocline is not thread-safe. All ic_readline_xxx and ic_print_xxx functions must be called from a single dedicated thread.

    To use Isocline in an asynchronous application, run the Isocline functions in a blocking dedicated thread and communicate results back to your async event loop. To unblock a current ic_readline call from another thread (simulating a ctrl-c which returns NULL), use the thread-safe ic_async_stop() function.

    bool ic_async_stop(void)
  4. Build Luau from source using CMake

    master

    To build Luau binaries from source on any platform, use CMake with the following commands:

    mkdir cmake && cd cmake
    cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
    cmake --build . --target Luau.Repl.CLI --config RelWithDebInfo
    cmake --build . --target Luau.Analyze.CLI --config RelWithDebInfo
    mkdir cmake && cd cmake
    cmake .. -DCMAKE_BUILD_TYPE=RelWithDebInfo
    cmake --build . --target Luau.Repl.CLI --config RelWithDebInfo
    cmake --build . --target Luau.Analyze.CLI --config RelWithDebInfo
  5. Install Luau

    master

    You can install Luau by downloading compiled binaries from the official releases. Ensure the luau and luau-analyze binaries are added to your PATH or copied to a system directory like /usr/local/bin on Linux/macOS.

    Alternatively, use these community-maintained package managers:

    • macOS: brew install luau
    • Arch Linux: pacman -Syu luau
    • Alpine Linux: apk add luau
    • Gentoo Linux: emerge dev-lang/luau (may require --autounmask=y)
    brew install luau
  6. Install Isocline as a single source file

    master

    For a minimal setup without dependencies, you can add Isocline as a git submodule and compile the source directly into your project.

    1. Add the submodule: git submodule add https://github.com/daanx/isocline
    2. Add isocline/src/isocline.c to your build rules.
    $ git submodule add https://github.com/daanx/isocline
  7. Build Isocline using CMake

    master

    To build Isocline as a static library (libisocline.a or isocline.lib), clone the repository and use CMake to generate the build files.

    $ git clone https://github.com/daanx/isocline
    $ cd isocline
    $ mkdir -p build/release
    $ cd build/release
    $ cmake ../..
    $ cmake --build .
  8. Choose a CodeGenContext implementation

    master

    Luau provides two primary implementations of BaseCodeGenContext for managing native code generation state. Choose based on your VM architecture:

    • StandaloneCodeGenContext: A VM-specific context. Use this for the "simple" implementation when native code generation is used with a single Luau VM.
    • SharedCodeGenContext: Supports use from multiple Luau VMs concurrently. It allows for sharing of executable native code and related metadata across VMs.
  9. Configure Isocline color palettes via COLORTERM

    master

    Isocline supports 24-bit colors. If automatic detection fails, you can force a specific color palette by setting the COLORTERM environment variable. Use these values to control color depth:

    • COLORTERM=truecolor: Enables 24-bit colors.
    • COLORTERM=256color: Uses the ANSI 256 color palette.
    • COLORTERM=16color: Uses the regular ANSI 16 color palette (8 normal, 8 bright).
    • COLORTERM=8color: Uses bold for bright colors.
    • COLORTERM=monochrome: Disables all color.
    # Example of testing color support
    gcc -o test_colors -Iinclude test/test_colors.c src/isocline.c
    ./test_colors
    COLORTERM=truecolor ./test_colors
    COLORTERM=16color ./test_colors
  10. Run Luau scripts and files

    master
    You can execute Luau scripts by passing file names as arguments. The executable will attempt to find files with .luau or .lua extensions. If you use the -i or --interactive flag, the REPL will start after the last specified script has finished executing.
  11. Enable Native Code Generation (Codegen)

    master

    Luau supports executing code using native code generation. This can be enabled via several flags:

    • --codegen: Standard native code generation.
    • --codegen-cold: Includes functions that might not be profitable to natively compile.
    • --codegen-perf: (Linux only) Enables native code generation and profiles using perf.