lua開發記錄
編譯lua5.3
cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c ren lua.obj lua.o ren luac.obj luac.o link /DLL /IMPLIB:lua5.3.0.lib /OUT:lua5.3.0.dll *.obj link /OUT:lua.exe lua.o lua5.3.0.lib lib /OUT:lua5.3.0-static.lib *.obj link /OUT:luac.exe luac.o lua5.3.0-static.lib
require模塊
http://cloudwu.github.io/lua53doc/manual.html#pdf-require當請求 a.b.c 時, 它將查找 a 這個 C 庫。 如果找得到,它會在里面找子模塊的加載函數。 在我們的例子中,就是找 luaopen_a_b_c。 利用這個機制,可以把若干 C 子模塊打包進單個庫。 每個子模塊都可以有原本的加載函數名。
require不會重復加載
顯示當前加載的所有模塊
for k,v in pairs(package.loaded) do print(k,v) end
Lua Stack
+-----------------------+ | element with index 6 | <-- top ("relative" index -1) +-----------------------+ | element with index 5 | <-- -2 +-----------------------+ | element with index 4 | <-- -3 +-----------------------+ | element with index 3 | <-- -4 +-----------------------+ | element with index 2 | <-- -5 +-----------------------+ | element with index 1 | <-- bottom ("relative" index -6 ) +-----------------------+
PIL
Programming in Lualua_call
a = f("how", t.x, 14)
lua_getglobal(L, "f"); /* function to be called */ lua_pushliteral(L, "how"); /* 1st argument */ lua_getglobal(L, "t"); /* table to be indexed */ lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ lua_remove(L, -2); /* remove 't' from the stack */ lua_pushinteger(L, 14); /* 3rd argument */ lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ lua_setglobal(L, "a"); /* set global 'a' */
lua_setfield
void lua_setfield (lua_State *L, int index, const char *k);
做一個等價于 t[k] = v 的操作, 這里 t 是給出的索引處的值, 而 v 是棧頂的那個值。
dynamic libraries not enabled
Error:
lua 5.3 error: dynamic libraries not enabled; check your Lua installation
Fix:
luaconf.h #define LUA_DL_DLL
8 basic types
nil,boolean,number,string,function,userdata,thread, andtableLight Userdata
ref:https://www.lua.org/pil/28.5.htmlUserdata
The userdata type allows arbitrary C data to be stored in Lua variables.userdata 操作
冒號語法糖
冒號語法可以用來定義方法,就是說,函數可以有一個隱式的形參 self。因此,如下語句function c:f(params) body end
是這樣一種寫法的語法糖
c.f = function(self, params) body end
冒號語法糖方法訪問原則
不按規矩來,則要注意了:
可以這樣理解,當用非冒號調用,則如果定義的函數用了冒號糖,則將糖展開為function foo(self, ...) end。------吃糖要先剝紙就是這個道理