lua 学习资源
lua-users.org
lua manual
luagit网站
chunkspy
openresty nginx_lua_module
lua 空指针
lua 中值为nil时,相当于C语言的空指针,还是需要小心谨慎。
lua table使用
table使用有几个易错点,请看下图:
local m = {}
m['search'] = 'key'
m['result'] = 'value'
--print (type(m))
print ("the length of table m: " .. table.getn(m))
print (m['search'])
print (m['result'])
--for k,v in ipairs(m) -[[什么都没输出,为什么?因为控制变量初始值是按升序来遍历的,当key为1时,value为nil,此时便停止了遍历, 所有什么结果都没输出]]--
for k,v in pairs(m) --全部输出
do
print(k)
print(v)
end
lua C接口
以下代码保存为 lua_test.c,并使用gcc编译
gcc -fPIC -shared lua_test.c -llua -o mytestlib.so
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
static int l_add(lua_State* L)
{
char *op1 = luaL_checkstring(L,1);
double op2 = luaL_checknumber(L,2);
lua_pushstring(L, "this is a test!\n");
return 1;
}
static luaL_Reg mylibs[] = {
{"add",l_add},
{NULL,NULL}
};
int luaopen_mytestlib(lua_State* L)
{
const char *libName = "mytestlib";
luaL_register(L, libName, mylibs);
return 1;
}
lua 中使用封装的接口,/usr/bin/lua test.lua
mytestlib = require("mytestlib")
print (mytestlib.add("helloworld", 2))
lua 协程
待续