外观
Lua 5.4 标准库
约 579 字大约 2 分钟
2026-07-15
下表列出 Lua 表盘宿主提供的脚本级接口。参数和返回语义遵循 Lua 5.4.0;这里给出完整成员索引,详细语义直接以官方手册为准。
基础库
| 函数 | 典型签名 |
|---|---|
assert | assert(value [, message]) -> value, ... |
collectgarbage | collectgarbage([opt [, arg]]) -> result |
dofile | dofile([filename]) -> ... |
error | error(message [, level]) |
getmetatable | getmetatable(object) -> table/nil |
ipairs | ipairs(t) -> iterator, t, 0 |
load | load(chunk [, chunkname [, mode [, env]]]) -> function/error |
loadfile | loadfile([filename [, mode [, env]]]) -> function/error |
next | next(table [, index]) -> key, value |
pairs | pairs(t) -> iterator, t, nil |
pcall | pcall(f [, args...]) -> ok, ... |
print | print(...) |
rawequal | rawequal(v1, v2) -> boolean |
rawget | rawget(table, index) -> value |
rawlen | rawlen(v) -> integer |
rawset | rawset(table, index, value) -> table |
select | select(index, ...) -> ... |
setmetatable | setmetatable(table, metatable) -> table |
tonumber | tonumber(e [, base]) -> number/nil |
tostring | tostring(v) -> string |
type | type(v) -> string |
warn | warn(msg1, ...) |
xpcall | xpcall(f, msgh [, args...]) -> ok, ... |
全局值还包括 _G 和 _VERSION。
Coroutine
coroutine.close、create、isyieldable、resume、running、status、wrap、yield。
Package
全局 require(name);package.config、cpath、loaded、loadlib、path、preload、searchers、searchpath。
注意
package.loadlib 虽然被注册,但固件关闭动态库加载,会返回“dynamic libraries not enabled”。厂商模块由宿主静态放入 package.preload。
String
string.byte、char、dump、find、format、gmatch、gsub、len、lower、match、pack、packsize、rep、reverse、sub、unpack、upper。
UTF-8
utf8.char、charpattern、codes、codepoint、len、offset。
Table
table.concat、insert、move、pack、remove、sort、unpack。
Math
math.abs、acos、asin、atan、ceil、cos、deg、exp、floor、fmod、huge、log、max、maxinteger、min、mininteger、modf、pi、rad、random、randomseed、sin、sqrt、tan、tointeger、type、ult。
嵌入式产品可能以 32 位整数/浮点配置编译。脚本启动时用 math.maxinteger、math.type(1) 和 string.packsize('j') 读取实际 ABI,不要假定桌面 Lua 的 64 位配置。
IO 与文件对象
模块函数:io.close、flush、input、lines、open、output、popen、read、tmpfile、type、write。
文件对象方法:close、flush、lines、read、seek、setvbuf、write,以及 __gc、__close、__tostring。
io.popen() 不受支持。文件路径仍受当前进程权限和系统分区只读属性限制。
OS
os.clock、date、difftime、execute、exit、getenv、remove、rename、setlocale、time、tmpname。
os.execute 是否允许启动任意命令由 Lua 宿主和系统 shell 配置决定;不要据函数名推断第三方脚本一定有该权限。
Debug
debug.debug、gethook、getinfo、getlocal、getmetatable、getregistry、getupvalue、getuservalue、setcstacklimit、sethook、setlocal、setmetatable、setupvalue、setuservalue、traceback、upvalueid、upvaluejoin。
Debug 库可越过普通封装边界,系统应用不应把不可信脚本放进共享 Lua state。
机型可用性
| Xiaomi Smart Band 10 Pro | Xiaomi Watch S4 41mm |
|---|---|
| ✅ | ✅ |