LinuxCommandLibrary

luajit

Execute Lua programs with high performance

TLDR

Start an interactive Lua shell

$ luajit
copy

Execute a Lua script
$ luajit [path/to/script.lua] [--optional-argument]
copy

Execute a Lua expression
$ luajit -e '[print("Hello World")]'
copy

SYNOPSIS

luajit [options] [script [args]]
luajit -e "chunk" [args]
luajit -l file [args]
luajit -b [options] infile [outfile]

PARAMETERS

-e chunk
    Executes the given Lua chunk string.

-l file
    Loads and executes the Lua file.

-b
    Enables bytecode dumping or loading mode. Used with other options to specify format (e.g., -t for text, -c for C source). If no output file is specified, it writes to standard output.

-b -t
    Dumps bytecode in a human-readable text format.

-b -c
    Dumps bytecode as C source code, which can be compiled directly into a C program.

-b -a
    Appends the output to the specified output file instead of overwriting it, when used with -b.

-b -n name
    Sets the C array name for bytecode dumps in C source format (-b -c).

-b -g
    Includes debug information (e.g., line numbers, local variable names) in bytecode dumps.

-j opt=val
    Sets JIT compiler options. Common options include: on/off (enable/disable JIT), hotloop=N (minimum iterations for a loop to be considered hot), opt=level (optimization level 0-9), dump (dump generated machine code), trace (dump trace assembly), mcode=N (max machine code size in KB).

-O level
    Sets the JIT optimization level (0-9). This is a shorthand for -j opt=level.

-v
    Shows version information for LuaJIT, including the Lua version it supports and the JIT version.

-h
    Displays a brief help message with common command-line options and their usage.

--
    Signals the end of command-line options. All subsequent arguments are treated as script arguments, even if they start with a hyphen.

script
    The path to the Lua script file to be executed by LuaJIT.

args
    Arguments passed to the Lua script. These arguments are typically accessible within the script via the global `arg` table.

DESCRIPTION

luajit is a Just-In-Time (JIT) compiler and extended interpreter for the Lua programming language. It is designed for maximum performance while maintaining compatibility with the Lua 5.1 language specification and C API.

Unlike standard Lua interpreters, LuaJIT dynamically compiles frequently executed code sections (hot loops) into machine code at runtime, significantly boosting execution speed. It also includes powerful non-standard extensions like the Foreign Function Interface (FFI) for direct interaction with C libraries and data structures, and the bitop library for bitwise operations.

LuaJIT is widely used in performance-critical applications, including game development, web services (e.g., OpenResty), scientific computing, and embedded systems, where its speed and low memory footprint are highly valued.

CAVEATS

LuaJIT is primarily compatible with Lua 5.1 and does not fully support all features introduced in later Lua versions (5.2, 5.3, 5.4), such as `goto` statements, bitwise operators (unless using the bundled `bitop` library explicitly), or newer additions to the standard library. Its JIT tracing benefits are most pronounced for long-running processes with hot loops and consistent data types, and less so for very short-lived scripts or code with highly polymorphic types.

FOREIGN FUNCTION INTERFACE (FFI)

A significant non-standard extension in LuaJIT, FFI allows Lua programs to directly call external C functions and access C data structures without writing C binding code. This dramatically simplifies integration with C libraries and enables high-performance interactions, blurring the lines between Lua and C and avoiding marshalling overhead.

BYTECODE COMPILATION AND EMBEDDING

LuaJIT provides robust tools for compiling Lua source code into its internal bytecode format (-b option). This bytecode can then be executed directly, or even dumped into a text or C source file. This feature is valuable for distributing applications, obfuscating code, or embedding Lua scripts directly into C/C++ executables without needing the original source files present at runtime.

HISTORY

LuaJIT was originally created by Mike Pall, with initial development starting in the late 2000s and the first public release around 2009. Its primary goal was to provide Lua with performance comparable to C, leveraging advanced JIT compilation techniques, particularly trace-based JIT. It quickly gained popularity for its speed and efficient resource usage, becoming a cornerstone in various high-performance systems and games (e.g., OpenResty, Torch). While core development has slowed in recent years, it remains a highly influential and widely deployed runtime.

SEE ALSO

lua(1), luac(1)

Copied to clipboard