- Lua和C通过栈进行数据和功能的共享
- lua对于C来说,一切皆数据,数据类型不同,在堆栈上的操作不同
- lua编译错误:去掉sdl检测,cpp改为c进行测试
- luaState中的top永远比当前大一位
- Lua 提供了元表(Metatable),允许我们改变table的行为__index,从而实现了数据和方法的分离
a.metatable的使用
luaObject* wrapper = (luaObject*) lua_newuserdata( L, sizeof(luaObject) );
wrapper->data = new CObject;
luaL_getmetatable( L, ”ObjectMetatable“);
lua_setmetatable( L, -2 );
int lua_setmetatable (lua_State *L, int index);
Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index.
为Index为-2的设置metatable,其中把栈顶的设置给他后把栈顶的给去掉
b. metatable的创建
lua_setfield(L, -2, “__index”);
为Index为-2的设置__index = 栈顶值;并把栈顶的给去掉
luaL_newmetatable(L, pMetatableName);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
=》等价于
local ss = setmatatable(ss,{__index=ss})
void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l);
Opens a library.
When called with libname equal to NULL, it simply registers all functions in the list l (see luaL_Reg) into the table on the top of the stack.
When called with a non-null libname, luaL_register creates a new table t, sets it as the value of the global variable libname, sets it as the value of package.loaded[libname], and registers on it all functions in the list l. If there is a table in package.loaded[libname] or in variable libname, reuses this table instead of creating a new one.
In any case the function leaves the table on the top of the stack.
libname为空,则是为栈顶的table注册方法,如果不为空则创建一个名为libname的table并且把所有方法注册给他
luaL_newmetatable(L, pMetatableName);
luaL_register(L, NULL, arrayFunc_meta);
luaL_register(L, “Student”, arrayFunc);