lua栈的理解

2 篇文章 0 订阅
  1. Lua和C通过栈进行数据和功能的共享
  2. lua对于C来说,一切皆数据,数据类型不同,在堆栈上的操作不同
  3. lua编译错误:去掉sdl检测,cpp改为c进行测试
  4. luaState中的top永远比当前大一位
  5. 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值