随笔分类 - Lua
摘要://顶 -1 -2 -3 //顶 3 2 1 #include #include extern "C"{ #include #include #include } #pragma comment(lib,"lua.lib") void stackDump(lua_State* L) { int i; int top = lua_gett...
阅读全文
摘要:#include #include extern "C"{ #include #include #include } #pragma comment(lib,"lua.lib") int main() { char buff[256]; int error; lua_State* L = luaL_newstate(); lua...
阅读全文
摘要:Window = {} Window.prototype = {x = 0, y = 0, width = 70, height = 100} Window.mt = {} function Window.new(o) setmetatable(o, Window.mt) return o end --[[Window.mt.__index = function (table...
阅读全文
摘要:Set = {} Set.mt = {} function Set.new(t) local set = {} setmetatable(set, Set.mt) for _, l in ipairs(t) do set[l] = true end return set end function Set.union(a, b) local res = S...
阅读全文
摘要:function list_iter(t) local i = 0 local n = table.getn(t) return function() i = i + 1 if i <= n then return t[i] end end end t = {10, 20, 30} iter = list_iter(t) whil...
阅读全文
摘要:--匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues function newCounter() local i = 0 return function() i = i + 1 return i end end c1 = newCounter() print(c1()) print(c1()...
阅读全文
摘要:关于项目中lua任务(某些没弄懂,但lua上耗费时间有点长了不看了)这段时间看了lua语法和项目中lua应用1.在lua中注册c库,在lua5.2中好像都是注册c库,相当于在lua中定义一个table,key是函数名字,value是函数指针m_spLuaState->RegLib("game", f...
阅读全文
摘要:--lua仿单继承Account = { balance = 0}--对于成员变量,第一此访问要使用元表中的,在第一次也赋值到自己的域中了--将不涉及到__index了function Account:new(o) o = o or {} --setmetatable看到后面,忘了这个啥意思了...
阅读全文
摘要:--lua仿单继承Account = { balance = 0}function Account:new(o) o = o or {} setmetatable(o, self)--Account表本身作为o的metatable self.__index = self--自己作为自己的原型 ...
阅读全文
摘要:Account = { balance = 0}function Account:deposit(v) self.balance = self.balance + vendfunction Account:new(o) o = o or {} setmetatable(o, self)--A...
阅读全文
摘要:Account ={ balance = 0, withdraw = function(self, v) self.balance = self.balance - v end}--:操作符隐藏了self或者this参数,操作方便function Account:deposit(v) ...
阅读全文
摘要:--sort中的匿名函数中的grades称为外部局部变量,或者upvalue--函数内部定义的函数可以访问函数的变量--这个就是简单的闭包--function sortbygrade (names, grades)-- table.sort(names, function(n1, n2)-- ...
阅读全文
摘要:Set = {}Set.mt = {}--定义普通的表作为元表,为了避免命名污染直接放在Set内部function Set.new(t) local set = {} setmetatable(set, Set.mt)--一组相关的表共享一个metatable(通过这个可以描述他们共同的...
阅读全文
摘要:#include #include #define MAX_COLOR 255 extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/ls...
阅读全文
摘要:#include #define MAX_COLOR 255 extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/lstate.h"}i...
阅读全文
摘要:#include extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/lstate.h"}//lua与c交互栈的索引,假如栈中有5个元素//5 -1...
阅读全文
摘要:#include #include extern "C"{#include "lua-5.2.2/src/lua.h"#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"}//定义库函数static int l_dir...
阅读全文
摘要:#include #include extern "C"{/*头文件lua.h定义了Lua提供的基础函数,包括创建Lua环境、调用Lua函数、读写Lua环境中全局变量,以及注册供Lua调用的新函数等等*/#include "lua-5.2.2/src/lua.h"/*头文件lauxlib.h定义了辅...
阅读全文
摘要:lua 中pairs 和 ipairs区别标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (string.gmatch)等等。LUA手册中对与pairs,ipairs解释如下:ipairs (t)...
阅读全文