Lua的标准库
在Lua中可以使用require
来导入其他模块,使用其内容。但也有些内容是天然就在Lua中的,不必做什么操作就可以直接调用。如果是在C中内嵌Lua(如之前的博客《尝试将C++中变量与函数提供给Lua调用》),则需要在一开始调用函数:luaL_openlibs
。
这些被官方称为 Standard Libraries,我习惯称其为“内建库”,包括:
1. 基础函数
The basic library provides core functions to Lua
Lua中的基础核心库。
例如:
- dofile
- 等等
2. coroutine(协同操作)
This library comprises the operations to manipulate coroutines, which come inside the table coroutine. See §2.6 for a general description of coroutines.
这个库包含了协同操作的函数。
打印其中包含的函数:
> for k,v in pairs(coroutine) do
>> print(k)
>> end
resume
yield
status
wrap
create
running
3. package(包)
The package library provides basic facilities for loading modules in Lua. It exports one function directly in the global environment:
require
.
这个库提供了Lua读取模块的基础机制。它有一个直接在全局环境中的函数require
。
剩下的在package
中:
> for k,v in pairs(package) do
>> print(k)
>> end
preload
loadlib
loaded
loaders
cpath
config
path
seeall
4. string(字符串)
This library provides generic functions for string manipulation, such as finding and extracting substrings, and pattern matching.
这个库提供了字符串的通用操作,例如查找和提取子字符串,还有正则表达式。
> for k,v in pairs(string) do
>> print(k)
>> end
sub
upper
len
gfind
rep
find
match
char
dump
gmatch
reverse
byte
format
gsub
lower
5. table(表)
This library provides generic functions for table manipulation.
这个库提供了“表”的通用操作。
> for k,v in pairs(table) do
>> print(k)
>> end
setn
insert
getn
foreachi
maxn
foreach
concat
sort
remove
6. math(数学)
This library provides basic mathematical functions.
基础的数学函数。
> for k,v in pairs(math) do
>> print(k)
>> end
log
max
acos
huge
ldexp
pi
cos
tanh
pow
deg
tan
cosh
sinh
random
randomseed
frexp
ceil
floor
rad
abs
sqrt
modf
asin
min
mod
fmod
log10
atan2
exp
sin
atan
7. io(输入输出)
> for k,v in pairs(io) do
>> print(k)
>> end
lines
write
close
flush
open
output
type
read
stderr
stdin
input
stdout
popen
tmpfile
8. os(操作系统)
> for k,v in pairs(os) do
>> print(k)
>> end
exit
setlocale
date
getenv
difftime
remove
time
clock
tmpname
rename
execute
9. debug(调试)
This library provides the functionality of the debug interface to Lua programs.
提供了调试接口的函数
> for k,v in pairs(debug) do
>> print(k)
>> end
getupvalue
debug
sethook
getmetatable
gethook
setmetatable
setlocal
traceback
setfenv
getinfo
setupvalue
getlocal
getregistry
getfenv