强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Lua 从入门到精通 / 01 - 初识 Lua / Introduction to Lua

初识 Lua / Introduction to Lua

Lua 是什么? / What is Lua?

Lua(葡萄牙语意为"月亮")是一门轻量级、可嵌入的脚本语言,由巴西里约热内卢天主教大学的 Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo 于 1993 年开发。

Lua (Portuguese for “moon”) is a lightweight, embeddable scripting language created in 1993 at the Pontifical Catholic University of Rio de Janeiro, Brazil, by Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo.

核心特点 / Core Characteristics:

┌─────────────────────────────────────────────────┐
│                    Lua                           │
│                                                  │
│   ┌──────────┐  ┌──────────┐  ┌──────────────┐  │
│   │  轻量级  │  │ 可嵌入   │  │  高性能      │  │
│   │ Lightweight│  │Embeddable│  │  Fast        │  │
│   └──────────┘  └──────────┘  └──────────────┘  │
│                                                  │
│   ┌──────────┐  ┌──────────┐  ┌──────────────┐  │
│   │  简洁    │  │ 一等函数 │  │  协程支持    │  │
│   │ Simple   │  │ 1st-class│  │  Coroutines  │  │
│   └──────────┘  └──────────┘  └──────────────┘  │
└─────────────────────────────────────────────────┘

Lua 整个语言只有 21 个关键字,参考手册不到 100 页,编译后只有约 200KB。但正是这种极简设计,让它成为世界上嵌入最广泛的脚本语言之一。


🟢 基础 / Basics — 5 分钟上手 Lua

1. 安装 Lua / Installing Lua

macOS:

brew install lua

Ubuntu / Debian:

sudo apt update
sudo apt install lua5.4

从源码编译 / Build from source:

curl -L -o lua-5.4.7.tar.gz "https://www.lua.org/ftp/lua-5.4.7.tar.gz"
tar zxf lua-5.4.7.tar.gz
cd lua-5.4.7
make linux    # 或 make macosx
sudo make install

验证安装 / Verify:

lua -v
# Lua 5.4.7  Copyright (C) 1994-2024 Lua.org, PUC-Rio

2. Hello, World!

交互模式(REPL)/ Interactive mode:

$ lua
Lua 5.4.7  Copyright (C) 1994-2024 Lua.org, PUC-Rio
> print("Hello, World!")
Hello, World!
> 1 + 1
2
> "Lua" .. " is awesome!"
Lua is awesome!

脚本模式 / Script mode:

创建文件 hello.lua:

-- 我的第一个 Lua 程序 / My first Lua program
print("Hello, World!")
print("你好,世界!")

运行:

lua hello.lua
# Hello, World!
# 你好,世界!

3. 21 个关键字 / The 21 Keywords

-- Lua 的所有关键字 / All Lua keywords:
and       break     do        else      elseif
end       false     for       function  goto
if        in        local     nil       not
or        repeat    return    then      true
until     while

就这么多了。没有 classtry/catchswitchimport——这些在其他语言中常见的关键字,Lua 全部用更基础的机制实现。

4. 注释 / Comments

-- 单行注释 / Single-line comment

--[[
   多行注释 / Multi-line comment
   可以跨越多行
]]

-- 多行注释的陷阱(后面会讲到)/ Trap with multi-line comments:
--[[
print("这行被注释了 / This is commented out")
--]]

-- Lua 5.1+ 的安全多行注释写法 / Safe multi-line comment (Lua 5.1+):
--[=[ 
   这种写法可以避免嵌套问题
   This avoids nesting issues
]=]

🟡 进阶 / Intermediate — Lua 的应用场景

1. 游戏开发 / Game Development

Lua 是游戏行业使用最广泛的脚本语言。

┌─────────────────────────────────────────────────────┐
│                  游戏引擎架构                        │
│              Game Engine Architecture                │
│                                                      │
│  ┌─────────────────────────────────────────────┐    │
│  │            Lua 脚本层 / Script Layer          │    │
│  │  游戏逻辑 · AI · UI · 任务系统 · 热更新     │    │
│  └──────────────────┬──────────────────────────┘    │
│                     │ C API 桥接 / Bridge            │
│  ┌──────────────────┴──────────────────────────┐    │
│  │            C/C++ 引擎层 / Engine Layer        │    │
│  │  渲染 · 物理 · 音频 · 网络 · 资源管理       │    │
│  └─────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────┘

用 Lua 的知名游戏 / Notable games using Lua:

游戏 / Game引擎 / EngineLua 用途 / Usage
World of Warcraft自研UI 插件系统
RobloxRoblox Studio游戏逻辑(Luau 方言)
Dota 2Source 2Mod 脚本
Angry Birds自研游戏逻辑
Civilization V自研游戏 AI 和脚本

为什么游戏引擎爱用 Lua? / Why game engines love Lua:

-- 游戏逻辑示例:一个简单的 NPC AI
-- Example: Simple NPC AI

local npc = {
    name = "守卫",
    hp = 100,
    state = "idle",
}

function npc:update(player)
    local distance = self:distanceTo(player)
    
    if distance < 5 then
        self.state = "attack"
        self:attack(player)
    elseif distance < 20 then
        self.state = "chase"
        self:moveToward(player)
    else
        self.state = "idle"
        self:patrol()
    end
end

-- 热更新:修改脚本后无需重新编译引擎!
-- Hot-reload: change script without recompiling the engine!

2. Web 开发:OpenResty / Web Development with OpenResty

OpenResty = Nginx + LuaJIT,是高性能 Web 平台。

-- OpenResty 中的 Lua 请求处理
-- Lua request handling in OpenResty

local ngx = require "ngx"
local cjson = require "cjson.safe"

-- 处理 HTTP 请求 / Handle HTTP request
local args = ngx.req.get_uri_args()
local name = args.name or "World"

ngx.header.content_type = "application/json"
ngx.say(cjson.encode({
    message = "Hello, " .. name .. "!",
    timestamp = ngx.now(),
}))

谁在用 OpenResty? / Who uses OpenResty?

  • Cloudflare(全球 CDN)
  • 阿里巴巴 / Alibaba
  • 网易 / NetEase
  • Kong(API 网关)

3. 嵌入式与配置 / Embedded & Configuration

-- 用 Lua 写配置文件 / Using Lua as a config file
-- config.lua

return {
    server = {
        host = "0.0.0.0",
        port = 8080,
        workers = 4,
    },
    database = {
        driver = "postgres",
        host = "127.0.0.1",
        port = 5432,
        name = "myapp",
    },
    logging = {
        level = "info",
        file = "/var/log/app.log",
    },
}

比 JSON 更强大——Lua 配置文件可以包含逻辑

-- 有逻辑的配置文件 / Config with logic
local env = os.getenv("APP_ENV") or "development"

return {
    debug = (env == "development"),
    database = {
        host = env == "production" 
            and "prod-db.example.com" 
            or  "localhost",
        pool_size = env == "production" and 20 or 5,
    },
}

4. Lua 与其他嵌入式语言对比

特性LuaPython (嵌入)JavaScript (V8)Wasm
二进制大小~200KB~10MB+~10MB+变动
启动速度极快中等
嵌入难度极简复杂复杂中等
学习曲线
性能高 (LuaJIT)极高
GC 暂停
热更新✅ 天然支持困难困难困难

🔴 高级 / Advanced — Lua 内部架构

1. Lua 的实现架构 / Implementation Architecture

┌──────────────────────────────────────────────────────────┐
│                     Lua 源代码                             │
│              local x = 1 + 2                              │
└──────────────────┬───────────────────────────────────────┘
                   │
                   ▼
┌──────────────────────────────────────────────────────────┐
│                   词法分析 / Lexer                         │
│         将源码拆分为 token 流                               │
│   [LOCAL] [NAME:"x"] ['='] [NUMBER:1] [+] [NUMBER:2]     │
└──────────────────┬───────────────────────────────────────┘
                   │
                   ▼
┌──────────────────────────────────────────────────────────┐
│                   语法分析 / Parser                        │
│         构建 AST(抽象语法树)                              │
│                  Assign                                    │
│                 /      \                                   │
│            [x]          Add                                │
│                        /    \                              │
│                      1       2                             │
└──────────────────┬───────────────────────────────────────┘
                   │
                   ▼
┌──────────────────────────────────────────────────────────┐
│                   代码生成 / Code Generator                 │
│         生成字节码(Bytecode)                              │
│                                                          │
│    1  LOADK    0 0    -- R(0) = 1                         │
│    2  LOADK    1 1    -- R(1) = 2                         │
│    3  ADD      0 0 1  -- R(0) = R(0) + R(1)              │
│    4  SETLOCAL 0      -- x = R(0)                         │
│    5  RETURN   0 1                                        │
└──────────────────┬───────────────────────────────────────┘
                   │
                   ▼
┌──────────────────────────────────────────────────────────┐
│                   虚拟机 / Virtual Machine                  │
│         执行字节码(基于寄存器的 VM)                        │
│         Register-based VM                                 │
└──────────────────────────────────────────────────────────┘

关键点 / Key Points:

  • Lua 是 寄存器式虚拟机(Register-based VM),而 Python、Ruby 是栈式虚拟机
  • 寄存器式 VM 的指令更少,因为不需要频繁的 push/pop
  • 字节码不跨版本兼容(Lua 5.3 字节码不能在 5.4 上运行)

2. 查看字节码 / Inspecting Bytecode

# 使用 luac 编译器查看字节码
# Use luac compiler to view bytecode

$ luac -l -p -e 'local x = 1 + 2'

main <(command line):0,0> (4 instructions at 0x...)
0+ params, 2 slots, 0 upvalues, 1 local, 2 constants, 0 functions
    1   [1] LOADK       0 0   ; 1
    2   [1] LOADK       1 1   ; 2
    3   [1] ADD         0 0 1
    4   [1] RETURN      0 1
constants (2) for 0x...:
    1   1
    2   2
locals (1) for 0x...:
    0   x   2   5

3. Lua 虚拟机寄存器模型 / VM Register Model

┌─────────────────────────────────────┐
│          Lua Stack Frame            │
│                                     │
│   ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐  │
│   │ R(0)│ │ R(1)│ │ R(2)│ │ ... │  │  ← 寄存器 / Registers
│   │  1  │ │  2  │ │  3  │ │     │  │
│   └─────┘ └─────┘ └─────┘ └─────┘  │
│                                     │
│   ┌──────────────────────────────┐  │
│   │         常量表 / Constants    │  │
│   │    [1] = 1    [2] = 2        │  │
│   └──────────────────────────────┘  │
│                                     │
│   ┌──────────────────────────────┐  │
│   │         指令 / Instructions   │  │
│   │    ADD R(0) R(0) R(1)        │  │
│   └──────────────────────────────┘  │
└─────────────────────────────────────┘

4. 全局表 _G / The Global Table

-- Lua 中,所有全局变量都存储在一个普通的表中
-- In Lua, all global variables are stored in a regular table

-- 这些写法等价 / These are equivalent:
print("hello")
_G.print("hello")

-- 你可以遍历所有全局变量 / You can iterate all globals
for name, value in pairs(_G) do
    print(name, type(value))
end
-- 输出 / Output:
-- print  function
-- table  table
-- string table
-- ...

这是 Lua 极简设计的精髓:没有"全局变量"这个特殊概念,只是一个普通的表 _G,通过元表机制实现全局变量的读写。


小结 / Summary

层级你需要知道的 / What You Need to Know
🟢 基础Lua 是轻量脚本语言,21 个关键字,print(“Hello”) 就能跑
🟡 进阶游戏脚本(WoW, Roblox)、Web(OpenResty/Kong)、嵌入式配置
🔴 高级寄存器式 VM、字节码编译流程、全局表 _G 的本质

下一章:语法基础 / Syntax Basics