Lua-简洁、轻量、可扩展的脚本语言

转自:http://rmingwang.com/The-Programming-Language-Lua.html

1. 一、Lua安装及常用库的安装

2. 1.1 Ubuntu

  1. $ sudo apt-get install lua5.2

3. 1.2 OS X

  1. $ brew install lua luarocks # luarocks是lua的模块管理工具
  2. $ sudo ln -s /usr/bin/make /usr/bin/gmake # 解决 sh: gmake: command not found

4. 1.3 luasocket库的安装

  1. $ luarocks install luasocket
  2. $ lua
  3. > socket = require("socket")
  4. > print(socket._VERSION)
  5. LuaSocket 2.0.2

5. 1.4 lua-cjson库的安装

  1. $ luarocks install lua-cjson
  2. $ lua
  3. > cjson = require("cjson")
  4. > print(cjson.encode({ name = "linguofeng" }))
  5. {"name":"linguofeng"}

6. 二、HelloWorld

  1. $ lua
  2. > print("Hello World!!")
  3. Hello World!!

7. 三、数据类型 有8种基本数据类型

  1. 类型 说明
  2. nil 全局变量默认值,如果要删除一个全局变量可以赋值为nil
  3. boolean 布尔值
  4. number 数字型
  5. string 字符串型
  6. userdata 用户自定义类型,一般是C/C++中的类型
  7. function 函数
  8. thread 线程
  9. table
  10. print(type(nil)) -- 输出 nil
  11. print(type(99.7+12*9)) -- 输出 number
  12. print(type(true)) -- 输出 boolean
  13. print(type("Hello Wikipedia")) -- 输出 string
  14. print(type(print)) -- 输出 function
  15. print(type{1, 2, test = "test"}) -- 输出 table

8. 四、函数 第一类值

第一类值指:在Lua中函数和其他值(数值、字符串)一样,函数可以被存放在变量中,也可以存放在表中,可以作为函数的参数,还可以作为函数的返回值。

  1. function add(x, y) -- 定义一个函数add,并接收两个参数
  2. local a = x + y -- 定义一个局部变量a,接收x+y的和,局部变量仅在函数add中有效
  3. return a -- 返回
  4. end -- 结束add函数
  5. print("15 + 64 = " .. add(15, 64)); -- 打印add(15, 64)的结果
  6. local x = 1 -- local 关键字表示该变量为局部变量,作用域为当前上下文
  7. -- 无该关键字修饰时为全局变量,作用于整个Lua状态机
  8. local add = function(x, y) -- 局部函数,作用于当前脚本(chumk
  9. Lib = {}
  10. Lib.add = function(x, y) -- 表函数,作用于Lib

9. 函数闭包

闭包是一个内部函数以及它的upvalues,内部函数使用了外部(父函数)局部变量。

  1. function newCounter()
  2. local i = 0 -- i为匿名函数的外部局部变量(upvalue
  3. return function() -- 匿名内部函数
  4. i = i + 1 -- 使用了i,所以该匿名函数是一个闭包
  5. return i
  6. end
  7. end
  8. c1 = newCounter() -- 得到一个匿名函数的变量(闭包)
  9. print(c1()) -- 调用匿名函数,打印出1
  10. print(c1()) -- 调用匿名函数,打印出2
  11. c2 = newCounter()
  12. print(c2()) --> 1
  13. print(c1()) --> 3
  14. print(c2()) --> 2

10. 五、控制语句

  1. for int i = 0, 10, 2 do -- for循环,2表示步长,省略时为1
  2. print("i = " .. i) -- .. 表示字符串连接符
  3. end -- 结束for
  4. if a > b then -- if条件判断语句
  5. print("a > b")
  6. else
  7. print("b > a")
  8. end
  9. while a > b do -- while循环
  10. print("")
  11. end
  12. repeat -- repeat-until循环
  13. print("")
  14. until a > b

11. 六、逻辑运算符 and、or、not

逻辑运算符认为false和nil是假(false),其他为真,0也是true.

  1. a and b -- 如果afalse,则返回a,否则返回b
  2. a or b -- 如果atrue,则返回a,否则返回b
  3. x = x or v -- 如果xfalse或者nil时则给x赋初始值v
  4. -- 等价于
  5. if not x then
  6. x = v
  7. end
  8. -- 三元运算符
  9. a ? b : c => a and b or c -- and 的优先级别比 or
  10. not -- not 的结果只返回falsetrue,作用类似于"非" "!"取反的意思
  11. print(not nil) -- true
  12. print(not false) -- true
  13. print(not 0) -- false

12. 七、协同程序 coroutine

7.1 创建协同

  1. co = coroutine.create(function () -- 创建一个协同函数,接收一个匿名函数,返回thread类型
  2. print("hi")
  3. end)
  4. print(co) -- thread: 0x7fe1834127d0

7.2 协同的三个状态:挂起态(suspended)、运行态(running)、停止态(dead)。

  1. print(coroutine.status(co)) -- 查看协同的状态,默认状态是挂起态 suspended
  2. coroutine.resume(co) -- 改变协同的状态为运行太 hi
  3. print(coroutine.status(co)) -- 协同运行完以后将变量停止态 dead

7.3 如此挂起正在运行的协同

  1. co = coroutine.create(function ()
  2. print("hi")
  3. coroutine.yield() -- 协同运行到此状态将变成挂起
  4. print("你好")
  5. end)
  6. coroutine.resume(co) -- hi
  7. coroutine.resume(co) -- 你好
  8. coroutine.resume(co) -- false,协同结束后将不能再使用

7.4 协同数据交换

  1. co = coroutine.create(function (x, y) -- 接收两个参数
  2. print("hi", coroutine.yield(x + y)) -- 返回一个值,同时参数也传递给了coroutine.yield
  3. return 100 -- 第三种返回值的方式
  4. end)
  5. print(coroutine.resume(co, 12, 87)) -- 传递两个参数并接收返回值(true, 99)
  6. -- 执行coroutine.yield(x + y)之前协同被挂起,但值被返回,因此print函数未被执行,下面执行
  7. print(coroutine.resume(co, 12, 87)) -- 传递两个参数并接收返回值(true, 100)

13. 八、数据结构 table

8.1 表的创建

  1. arrays = {} -- 创建一个空表
  2. arrays[1] = "abc" -- 第一个索引值为1
  3. arrays[2] = 123
  4. arrays["key"] = "value" -- map
  5. for key, value in pairs(arrays) do -- 迭代table
  6. print(key .. " = " .. value)
  7. end

8.2 表的增删改查

  1. list = {123} -- 初始化表
  2. list[2] = "abc" --
  3. list.x = 123
  4. list.y = 987
  5. list[1] = nil --
  6. list.y = nil
  7. list[2] = 456 --
  8. list.x = 987
  9. print(list[2]) --
  10. print(list.x)
  11. print(list['x'])

8.3 数组

  1. list = {} -- 初始空化数组,数组的下标是整数,遵循Lua的标准,下标从1开始
  2. list[1] = "abc"
  3. list[2] = "edg"
  4. list[3] = "hij"

8.4 矩阵(二维数组)

  1. mt = {} -- 创建矩阵matrix
  2. for i = 1, N do -- 创建N
  3. mt[i] = {} -- 每行都是一个数组(table元素)
  4. for j = 1, M do -- 创建M
  5. mt[i][j] = "a" -- N行第M行的值
  6. end
  7. end

8.5 链表

  1. Singly-linked-list.svg
  2. list = nil
  3. list = {next = list, value = "hello3"}
  4. list = {next = list, value = "hello2"}
  5. list = {next = list, value = "hello1"}
  6. -- 遍历
  7. local l = list
  8. while l do
  9. print(l.value)
  10. l = l.next
  11. end

14. 九、metatable 元表

9.1 元表与元方法

元表也是普通表

  1. t = {}
  2. print(getmetatable(t)) -- 获取表的metatable nil,默认不带
  3. mt = {}
  4. setmetatable(t, mt) -- 设置一个元素
  5. -- metamethod 元表的方法(元方法)
  6. mt.__add -- +
  7. mt.__sub -- -
  8. mt.__mul -- *
  9. mt.__div -- /
  10. mt.__unm -- -
  11. mt.__pow -- ^
  12. mt.__concat -- 连接
  13. mt.__eq -- 等于 =
  14. mt.__lt -- 小于 <
  15. mt.__le -- 大于 >
  16. mt.__tostring -- print调用
  17. mt.__metatable -- 设置该元表不被修改与访问
  18. mt.__index -- 当访问不存在的元素时会去查询,相当于子类继承父类一样
  19. mt.__newindex -- 更新表,如果增加一个不存在的元素,会去查询,有直接用,否则增加

9.2 表的代理

记录下表的增查记录

  1. local index = {} -- 私有的key,用来记录原始表在代理表中的下标
  2. local mt = { -- 创建元表
  3. __index = function(t, k)
  4. print("访问了" .. tostring(k) .. "元素")
  5. return t[index][k] -- 从代理表中获取原始表中k下标的数据
  6. end,
  7. __newindex = function(t, k, v)
  8. print("更新了 " .. tostring(k) .. " 元素的值为 " .. tostring(v))
  9. t[index][k] = v -- 更新代理表中下标为index的原始表中的元素
  10. end
  11. }
  12. function setProxy(t)
  13. local proxy = {} -- 创建代理表
  14. proxy[index] = t -- 把原始表加到代理表的index下标中
  15. setmetatable(proxy, mt) -- 设置代理表的元表
  16. return proxy -- 返回代理表,即所有操作都是直接操作代理表
  17. end
  18. p = setProxy({})
  19. p[2] = 'abcdefg' -- 更新了 2 元素的值为 abcdefg
  20. print(p[2]) -- 访问了2元素

15. 十、环境

10.1 全局变量 _G

  1. > _G["ewgegw"] = "ddddddgege"
  2. > table.foreach(_G, print)
  3. string table: 0x7ffce3407a60
  4. xpcall function: 0x7ffce3404780
  5. package table: 0x7ffce3405780
  6. tostring function: 0x7ffce3405020
  7. print function: 0x7ffce3405160
  8. os table: 0x7ffce34073e0
  9. unpack function: 0x7ffce34050d0
  10. ewgegw ddddddgege -- 上面添加的全局变量
  11. require function: 0x7ffce3405e70
  12. getfenv function: 0x7ffce3404db0
  13. setmetatable function: 0x7ffce3404f60
  14. next function: 0x7ffce3404d20
  15. assert function: 0x7ffce3404a80
  16. tonumber function: 0x7ffce3404fc0
  17. io table: 0x7ffce3406bd0
  18. rawequal function: 0x7ffce34051b0
  19. collectgarbage function: 0x7ffce3404ad0
  20. getmetatable function: 0x7ffce3404e00
  21. module function: 0x7ffce3405e20
  22. rawset function: 0x7ffce3405260
  23. math table: 0x7ffce3408290
  24. debug table: 0x7ffce3408c50
  25. pcall function: 0x7ffce3404d70
  26. table table: 0x7ffce3405f10
  27. newproxy function: 0x7ffce34052e0
  28. type function: 0x7ffce3405080
  29. coroutine table: 0x7ffce3405380 -- 对应的是协同的表
  30. _G table: 0x7ffce3404110
  31. select function: 0x7ffce3404ec0
  32. gcinfo function: 0x7ffce3404150
  33. pairs function: 0x7ffce34048c0
  34. rawget function: 0x7ffce3405210
  35. loadstring function: 0x7ffce3404cc0
  36. ipairs function: 0x7ffce3404830
  37. _VERSION Lua 5.1
  38. dofile function: 0x7ffce3404bd0
  39. setfenv function: 0x7ffce3404f10
  40. load function: 0x7ffce3404c70
  41. error function: 0x7ffce3404c20
  42. loadfile function: 0x7ffce3404e60
  43. > table.foreach(_G.os, print)

10.2 非全局变量 setfenv

  1. --pack.lua---------------------------------------------------------------------
  2. local P = {}
  3. -- 改变P表的__index,这里的_G代表全局环境
  4. setmetatable(P, {__index = _G})
  5. -- 改变当前的环境为P,setfenv前的所有定义都是在全局环境中进行的,后面的则都是在新环境中进行的,互不影响
  6. setfenv(1, P)
  7. -- 声明的add函数在环境P中,如果要在外部访问必须P.add
  8. function add(x, y)
  9. print(x .. ' + ' .. y .. ' = ' .. (x + y))
  10. -- 由于当前新的环境中没有print函数,但是__index指向的是全局环境,所以print是全局的函数
  11. end
  12. return P
  13. --pack1.lua--------------------------------------------------------------------
  14. local P = {}
  15. -- 如果需要改变环境后使用全局环境的方法需要记住,这种方法比上面的要快
  16. local print = print
  17. -- 改变当前的环境为P
  18. setfenv(1, P)
  19. -- 声明的add函数在环境P中,如果要在外部访问必须P.add
  20. function add(x, y)
  21. print(x .. ' + ' .. y .. ' = ' .. (x + y))
  22. end
  23. -- 私有方法
  24. local function div(x, y)
  25. end
  26. return P
  27. --main.lua---------------------------------------------------------------------
  28. local p = require 'pack'
  29. p.add(12, 34)
  30. local p1 = require 'pack1'
  31. p1.add(43, 19)

16. 十一、包 package

11.1 包的定义

  1. -- student.lua
  2. student = {}
  3. function student.sayHi()
  4. print('Hello')
  5. end
  6. return student
  7. -- 使用
  8. student = require("student")
  9. student.sayHi() -- Hello
  10. 11.2 私有函数
  11. local function _add(x, y) -- 私有局部函数
  12. return x + y
  13. end
  14. utils = { -- utils
  15. add = _add -- 对外公开的函数
  16. }
  17. return utils

17. 十二、面向对象

12.1 类与继承

  1. --Person.lua
  2. local Person = {name = ''}
  3. function Person:getName()
  4. return self.name
  5. end
  6. function Person:setName(name)
  7. self.name = name
  8. end
  9. function Person:new(object)
  10. object = object or {}
  11. setmetatable(object, {__index = self}) -- 元表 类似继承的意思
  12. return object
  13. end
  14. return Person
  15. --main.lua
  16. --
  17. local Person = require 'Person'
  18. -- 对象
  19. local student = Person:new({age = 23})
  20. student:setName('Tom')
  21. print('name: ' .. student:getName() .. ' age: ' .. student.age)

12.2 多重继承

实际就是记住所有父类,然后访问不存在的元素的时候去查询哪个父类中有,有就执行

  1. local function search (k, plist)
  2. for i=1, table.getn(plist) do
  3. local v = plist[i][k] -- 去所有父类中获取
  4. if v then return v end
  5. end
  6. end
  7. function Person:new(...)
  8. local o = {}
  9. setmetatable(o, {__index = function (t, k)
  10. local v = search(k, arg)
  11. t[k] = v -- save for next access
  12. return v
  13. end})
  14. return o
  15. end

12.3 私有性

  1. local function createAccount(_name) -- 工厂方法
  2. local self = {name = _name}
  3. local function _setName(name)
  4. self.name = name
  5. end
  6. local function _getName()
  7. return self.name
  8. end
  9. -- 公有方法表
  10. local public = {
  11. setName = _setName,
  12. getName = _getName,
  13. --name = self.name -- 不公开私有成员变量
  14. }
  15. return public
  16. end
  17. local account = createAccount('Tom')
  18. print(account.name) -- 无法访问,因为没有公开

18. 十三、weak table

  1. t = {name = 'table'} -- 创建一个普通表
  2. setmatetable(t, {__made = 'k'}) -- k表示t表中的keysweak的,v表示t表中的valuesweak
  3. k = {} -- 创建一个空表,此时{}被k引用,引用值为1
  4. t[k] = 1 -- 把空表作为key,由于t表的keyweak的,所以k值的引用如果为0则会被gc回收,如
  5. k = {} -- k指向另一个新的{},则旧的{}引用值减1变成0了,目前t[k] = 1还有效
  6. t[k] = 2 -- 把新的{}作为key,值是2
  7. collectgarbage() -- 调用gc,清除引用为0的内存空间,此时,第一个{}的引用是0,会被gc掉,所以第一个t[k]以及值会被删除
  8. for k, v in pairs(a) do print(v) end
  9. -- 此时应该只剩下2了,因为第2k = {}时改变了k指向新的{},而旧的{}引用会变成0,被gc
  10. -------------------------------------------------------------------------
  11. a = {};
  12. setmetatable(a, {__mode = 'v'}); -- valuesweak,当值的引用为0时,删除
  13. v1 = {name = 'v1'}
  14. v2 = {name = 'v2'}
  15. a[1] = v1
  16. a[2] = v2
  17. v1 = v2
  18. collectgarbage(); -- 调用GC,清掉weak表中没有引用的内存
  19. for k, v in pairs(a) do print(v.name) end
  20. -- 输出v2,因为v1重新指向{name = 'v2'},则{name = 'v1'}引用减10
  21. -------------------------------------------------------------------------
  22. a = {};
  23. setmetatable(a, {__mode = 'kv'}); -- 同时检查kv,是上面两种情况的组合
  24. v1 = {name = 'v1'}
  25. v2 = {name = 'v2'}
  26. v = {}
  27. k = {}
  28. a[1] = v
  29. v = {} -- value重新引用新值,旧值被gc,如果旧值在table则对应的记录被清空
  30. a[2] = v
  31. a[k] = 1
  32. k = {} -- key重新引用新值,旧值被gc,如果旧值在table则对应的记录被清空
  33. a[k] = 2
  34. collectgarbage(); -- 调用GC,清掉weak表中没有引用的内存
  35. for k, v in pairs(a) do
  36. print(v)
  37. end

19. 十四、标准库

19.1. 14.1 数学库 Mathematical Functions

  1. 函数 说明
  2. math.abs (x) 求绝对值
  3. math.acos (x) 求反余弦
  4. math.asin (x)
  5. math.atan (x)
  6. math.atan2 (y, x)
  7. math.ceil (x)
  8. math.cos (x)
  9. math.cosh (x)
  10. math.deg (x)
  11. math.exp (x)
  12. math.floor (x)

20. 14.2 table库

  1. 函数 说明
  2. table.concat (table [, sep [, i [, j]]]) 拼接成字符串,sep代表连接符,i开始位置,j结束位置
  3. table.insert (table, [pos,] value) 插入一个元素,默认是最后一个,pos指定位置
  4. table.maxn (table) 获取最大长度
  5. table.remove (table [, pos]) 删除一个元素,默认删除最后一个,pos指定位置
  6. table.sort (table [, comp]) 排序
  7. tables = {1, 2, 3, 4, 5, 6, 7}
  8. print(#tables) -- 5.1开始使用#获取长度 -- 7
  9. table.insert(tables, 8)
  10. print(table.concat(tables)) -- 12345678
  11. table.insert(tables, 1, 0)
  12. print(table.concat(tables)) -- 012345678
  13. print(table.maxn(tables)) -- 9
  14. table.remove(tables)
  15. print(table.concat(tables)) -- 01234567
  16. table.remove(tables, 5)
  17. print(table.concat(tables)) -- 0123567
  18. print(table.concat(tables, ',')) -- 0,1,2,3,5,6,7
  19. print(table.concat(tables, '-', 2)) -- 1-2-3-5-6-7
  20. print(table.concat(tables, '=', 1, 4)) -- 0=1=2=3
  21. table.sort(tables)
  22. print(table.concat(tables)) -- 0123567
  23. table.sort(tables, function(t1, t2)
  24. if t1 > t2 then
  25. return true
  26. else
  27. return false
  28. end
  29. end)
  30. print(table.concat(tables)) -- 7653210

21. 14.3 string库

  1. 函数 说明
  2. string.byte (s [, i [, j]]) 把字符转换成ASCII
  3. string.char (…) ASCII码转换成字符
  4. string.dump (function)
  5. string.find (s, pattern [, init [, plain]]) 查找,pattern查找的字符串,init从那里开始默认为1plain
  6. string.format (formatstring, …) 格式化字符串

22. 14.4 io库

  1. 函数 说明
  2. io.close ([file]) 等效file:close(),如果没有file则关闭默认输出
  3. io.flush () 等效file:flush()
  4. io.input ([file])
  5. io.lines ([filename]) 等效io.input():lines()
  6. io.open (filename [, mode]) 打开一个文件,模式:r,w,a,r+,w+,a+
  7. io.output ([file])
  8. io.popen (prog [, mode]) 依赖系统的,不是所有平台都能用
  9. io.read (…) 等效io.input():read
  10. io.tmpfile () 创建一个临时文件,当程序退出时自动删除
  11. io.type (obj) 判断obj的类型,如果返回file是一个打开的文件句柄,返回close file是一个
  12. 关闭的文件句柄,nil不是文件
  13. io.write (…) 等效io.output():write
  14. file:close () 关闭文件,会自动gc掉,但时间不确定
  15. file:flush () 保存任何数据到文件中
  16. file:lines () 迭代文件的每一行
  17. file:read (…) 读取文件,*n,*a,*l,number
  18. file:seek ([whence] [, offset]) 指定位置,默认是cur,1set,end
  19. file:setvbuf (mode [, size]) 设置buff缓存,no,full,line
  20. file:write (…) 写文件,参数必须是string或者number
  21. local file = io.open('tabletest.lua', 'r')
  22. print(io.type(file))
  23. for line in file:lines() do
  24. --print(line)
  25. end
  26. --file:close()
  27. io.close(file)
  28. print(io.type(file))
  29. ------------------------------------------------------
  30. for line in io.input('tabletest.lua'):lines() do
  31. print(line)
  32. end
  33. for line in io.lines('tabletest.lua') do
  34. --print(line)
  35. end

23. 14.5 os库

  1. 函数 说明
  2. os.clock () 返回程序所使用的cpu时间
  3. os.date ([format [, time]]) 当前系统日期,或格式化某个日期
  4. os.difftime (t2, t1) 时间差
  5. os.execute ([command]) 执行shell命令
  6. os.exit ([code]) 调用Cexit函数
  7. os.getenv (varname) 获取系统环境变量,变量名,不包含$
  8. os.remove (filename) 删除文件,文件名
  9. os.rename (oldname, newname) 修改文件名
  10. os.setlocale (locale [, category]) 设置地区,"all", collate”, ctype”, monetary”, numeric”, or time
  11. os.time ([table]) 返回当前时间或把时间保存在table中,
  12. os.tmpname () 临时文件名
  13. table.foreach(os, print)
  14. print(os.clock())
  15. print(os.date())
  16. print(os.date('%Y-%m-%d %H:%M'))
  17. print(os.time())
  18. print(os.difftime(1364957757, os.time()))
  19. print(os.getenv ('PATH'))
  20. print(os.tmpname ())

14.6 debug库

函数 说明

24. 十五、标准库

15.1 堆栈,后进先出原则

25. 压入

  1. 函数 说明(栈底最后一个元素的索引是1,栈顶第一个元素是-1)
  2. lua_pushnil(lua_State*) 压入一个空值
  3. lua_pushboolean(lua_State*, int) 压入一个布尔值
  4. lua_pushcclosure(lua_State*, lua_CFunction, int) 压入一个C闭包?
  5. lua_pushcfunction(lua_State*, lua_CFunction) 压入一个C函数,由lua_pushcclosure(L, f, 0)宏定义出来
  6. lua_pushlightuserdata(lua_State*, void*) 压入一个指针,不被gc管理
  7. lua_pushinteger(lua_State*, lua_Integer) 压入一个数字
  8. lua_pushnumber(lua_State*, lua_Number) 压入数字
  9. lua_pushstring(lua_State*, const char*) 压入字符串
  10. lua_pushfstring(lua_State*, const char*, …) 压入一个格式化的string
  11. lua_pushvfstring(lua_State*, const char*, va_list) 同上,只是接收一个va_list
  12. lua_pushlstring(lua_State*, const char*, size_t); 压入长字符串
  13. lua_pushliteral(lua_State*, const char*) 压入文字
  14. lua_pushthread(lua_State*) 压入一个线程?

26. 判断类型

  1. 函数 说明
  2. lua_isboolean(lua_State*, int) 是否是布尔类型
  3. lua_iscfunction(lua_State*, int) 是否是C函数类型
  4. lua_isfunction(lua_State*, int) 是否是C函数或者Lua函数
  5. lua_islightuserdata(lua_State*, int) 是否是用户自定义类型指针
  6. lua_isnil(lua_State*, int) 是否是空
  7. lua_isnone(lua_State*, int) 是否是有效的
  8. lua_isnoneornil(lua_State*, int) 是否是上面两者
  9. lua_isnumber(lua_State*, int) 是否是数字
  10. lua_isstring(lua_State*, int) 是否是字符串
  11. lua_istable(lua_State*, int) 是否是table
  12. lua_isthread(lua_State*, int) 是否是线程
  13. lua_isuserdata(lua_State*, int) 是否是用户类型,包括fulllight
  14. lua_type(lua_State*, int) 返回元素的类型,对应LUA_TNIL等枚举
  15. lua_typename(lua_State*, int) 返回元素的类型名称

27. 获取栈中的元素并转换成C类型

  1. 函数 说明 返回值类型
  2. lua_toboolean(lua_State*, int) 把元素转换成C的布尔类型的值 int
  3. lua_tocfunction(lua_State*, int) 把元素转换成C的函数 lua_CFunction
  4. lua_tointeger (lua_State*, int) lua_Integer
  5. lua_tolstring (lua_State*, int, size_t *len) const char*
  6. lua_tonumber (lua_State*, int) lua_Number
  7. lua_topointer (lua_State*, int) const void*
  8. lua_tostring (lua_State*, int) const char*
  9. lua_tothread (lua_State*, int) lua_State
  10. lua_touserdata (lua_State*, int) void*
  11. 栈内元素的操作
  12. 函数 说明
  13. lua_gettop(lua_State*) 返回栈的元素个数,同时也是栈顶元素的索引
  14. lua_settop(lua_State*, int) 设置某个元素为栈顶元素,该元素之上的元素会被清除
  15. lua_pushvalue(lua_State*, int) 压入(拷贝)一个已经存在栈的元素至栈顶
  16. lua_insert(lua_State*, int) 移动栈顶元素至某个位置
  17. lua_remove(lua_State*, int) 删除栈中某个元素
  18. lua_replace(lua_State*, int) 替换栈顶元素至某个位置,相应那个位置的元素至栈顶

参考:http://book.luaer.cn

参考:http://www.lua.org/manual/5.1/manual.html

原文地址:http://blog.linguofeng.com/pages/language/lua.html

原文地址:https://www.cnblogs.com/x_wukong/p/4480227.html