lua_深入用法

来源:互联网 发布:淘宝中药模板 编辑:程序博客网 时间:2024/06/06 02:17

lua 深入用法

C API

两种观点(Lua作为扩展性语言和可扩展的语言)对应的C和Lua中间有两种交互方式。第一种,C作为应用程序语言,Lua作为一个库使用;第二种,反过来,Lua作为程序语言,C作为库使用。这两种方式,C语言都使用相同的API与Lua通信,因此C和Lua交互这部分称为C API。

C API是一个C代码与Lua进行交互的函数集。他有以下部分组成:读写Lua全局变量的函数,调用Lua函数的函数,运行Lua代码片断的函数,注册C函数然后可以在Lua中被调用的函数,等等。(本书中,术语函数实际上指函数或者宏,API有些函数为了方便以宏的方式实现)

C API遵循C语言的语法形式,这Lua有所不同。

在C和Lua之间通信关键内容在于一个虚拟的栈.
几乎所有的API调用都是对栈上的值进行操作,所有C与Lua之间的数据交换也都通过这个栈来完成。另外,你也可以使用栈来保存临时变量。栈的使用解决了C和Lua之间两个不协调的问题:第一,Lua会自动进行垃圾收集,而C要求显示的分配存储单元,两者引起的矛盾。第二,Lua中的动态类型和C中的静态类型不一致引起的混乱。

string 相关

string.split = function(s, p)    local rt= {}    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )    return rtend

table 相关

__index

访问table中的元素,不存在的时候,寻找__index

Window = {}Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}Window.mt = {}function Window.new(o)    setmetatable(o ,Window.mt)    return oendWindow.mt.__index = function (t ,key)    -- body    return 1000endw = Window.new{x = 10 ,y = 20}print(w.wangbin)
__newindex

设置 table中的元素,不存在的时候,进行赋值

Window.mt = {}function Window.new(o)    setmetatable(o ,Window.mt)    return oendWindow.mt.__index = function (t ,key)    return 1000endWindow.mt.__newindex = function (table ,key ,value)    if key == "wangbin" then        rawset(table ,"wangbin" ,"yes,i am")    endendw = Window.new{x = 10 ,y = 20}w.wangbin = "55"print(w.wangbin)
rawget

为了绕过__index

Window = {}Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}Window.mt = {}function Window.new(o)    setmetatable(o ,Window.mt)    return oendWindow.mt.__index = function (t ,key)    return 1000endWindow.mt.__newindex = function (table ,key ,value)    if key == "wangbin" then        rawset(table ,"wangbin" ,"yes,i am")    endendw = Window.new{x = 10 ,y = 20}print(rawget(w ,w.wangbin))打印结果是:nil。这里的元表中__index函数就不再起作用了
rawset

为了绕过__newindex

Window = {}Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}Window.mt = {}function Window.new(o)    setmetatable(o ,Window.mt)    return oendWindow.mt.__index = function (t ,key)    return 1000endWindow.mt.__newindex = function (table ,key ,value)    table.key = "yes,i am"endw = Window.new{x = 10 ,y = 20}w.wangbin = "55"

然后我们的程序就stack overflow了。可见,程序陷入了死循环。

迭代器

for后面跟几个数值,则迭代函数就需要返回多少个数值,遇到第一个为nil的,则迭代器终止。

无状态迭代器

local tb = {"aa", "bb", "cc"}local function iterm(tb, idx)        local len = #tb        if idx > len then             return        end        idx = idx + 1        return idx, tb[idx]endfor k, v in iterm,tb,0  do    print(k, v)end

有状态迭代器: 本质上是利用了闭包函数。

local tb = {"aa", "bb", "cc"}local function iterm(tb)    local idx = 0    local len = #tb    return function()        if idx > len then             return        end        idx = idx + 1        return tb[idx]    endendfor k in iterm(tb) do    print(k)end
0 0