• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

luars232库中用到的一些C API for lua

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

代码就不贴了,这里只是梳理一下前两篇里面忽略的一些东西,作为读代码的记录吧。

1、头文件

#include <lauxlib.h>
#include <lua.h>

All API functions and related types and constants are declared in the header file “lua.h”。

The auxiliary library provides several convenient functions to interface C with Lua. While the basic API provides the primitive functions for all interactions between C and Lua, the auxiliary library provides higher-level functions for some common tasks. All functions from the auxiliary library are defined in header file lauxlib.h and have a prefix luaL_.

 

2、void *lua_newuserdata (lua_State *L, size_t size)。

首先我们需要知道一下什么叫userdata?额,我的理解,C里面的所有数据类型,在lua中只有一个名字userdata。事实上,这里只是一块内存而已。如果还记得lightuserdata的话,也应该记得,这个是一个指针。

lua_newuserdata这个调用是申请了一块大小为size的内存,然后返回指向这块userdata的一个指针(void型),指针指向这块空间首地址;同时往堆上push一个userdata类型element。

lua并不能理解userdata,不能对其内容进行运算等操作。所以,使用userdata,我们还需要添加相关的操作方法,另外,new出来的userdata,还需要给他添加gc元方法供lua收回释放。

至于什么时候userdata会被gc收回。。。唔,我找了半天没明确答案,或是可信答案,也暂时不想翻代码,不过有人说是这样的:gc假设所有的userdata都是需要结束的,然后开始找它指向的根对象是否有效(至于怎么个有效法暂时未知),如果无效则回收userdata的空间。

追了一下这里的调用情形:

1)我们在L中new出来一个size为指针类型的空间,它的值是dll返回来的一个地址,由指针p带回来这个地址;如此,在堆顶有一个userdata类型的element。这里不需要我们再pushinteger到lua。

2)给这个element设置metatable。

3)metatable.__gc()中是通过调用dll里操作来实现的。直接调用dll里面的方法,free(p)。如此就完成了这个gc。

 

3、int luaL_optint (lua_State *L, int narg, int d)

检查narg,如果是一个number,那么返回一个int类型的数;如果是nil,或者是没有,那么返回参数d。没想明白这个是用来干嘛的,待了解。

 

4、没有4了居然,好吧,草草的把这个wrapper看了一遍。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
lua学习笔记之userdata发布时间:2022-07-22
下一篇:
Lua生成Guid(uuid)发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap