对lua的简单扩展,使它能快速关联C++的数据。

很早的时候,我纠结过一件事,假如,我在C++公开给脚本的接口中,要使用C++里的某一个对象,并且,我的程序中有很多个不同的lua虚拟机,每一个虚拟机要关联一个C++对象,并且这是多线程的,那么这时候应该如何快速的利用lua_State指针来定位到对象指针呢?


以前我没有能力读懂lua的源码,也可以说不知道关键部分如何操作,我当时的做法,是利用临界区和std::map来解决问题的,很明显这个方式的效率很低很低。


现在有能力读lua源码了,当然有更有效的解决办法了,因为在我们利用lua的过程中,lua_State这个结构指针是要贯穿所有用到lua的地方的,那么我就可以对这个结构进行扩展,让它能够保存我的数据,只需要保存一个指针即可。


lua_State这个结构,定义在 lstate.h中   (lua.h中只是作者为了不让用户能够主动访问结构成员而定义的空结构指针,各种开源脚本引擎都是这样,为了安全性,大家懂的)


以lua5.2.3为例,该结构原始定义如下:


struct lua_State {
  CommonHeader;
  lu_byte status;
  StkId top;  /* first free slot in the stack */
  global_State *l_G;
  CallInfo *ci;  /* call info for current function */
  const Instruction *oldpc;  /* last pc traced */
  StkId stack_last;  /* last free slot in the stack */
  StkId stack;  /* stack base */
  int stacksize;
  unsigned short nny;  /* number of non-yieldable calls in stack */
  unsigned short nCcalls;  /* number of nested C calls */
  lu_byte hookmask;
  lu_byte allowhook;
  int basehookcount;
  int hookcount;
  lua_Hook hook;
  GCObject *openupval;  /* list of open upvalues in this stack */
  GCObject *gclist;
  struct lua_longjmp *errorJmp;  /* current error recover point */
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
};


那么对这个结构扩展之后如下:

struct lua_State {
  CommonHeader;
  lu_byte status;
  StkId top;  /* first free slot in the stack */
  global_State *l_G;
  CallInfo *ci;  /* call info for current function */
  const Instruction *oldpc;  /* last pc traced */
  StkId stack_last;  /* last free slot in the stack */
  StkId stack;  /* stack base */
  int stacksize;
  unsigned short nny;  /* number of non-yieldable calls in stack */
  unsigned short nCcalls;  /* number of nested C calls */
  lu_byte hookmask;
  lu_byte allowhook;
  int basehookcount;
  int hookcount;
  lua_Hook hook;
  GCObject *openupval;  /* list of open upvalues in this stack */
  GCObject *gclist;
  struct lua_longjmp *errorJmp;  /* current error recover point */
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
  int __mydata;//这里
};


//同时增加两个lua接口,可以将函数接口放到lapi.c中,声明放到lua.h中即可,或者你是发烧追求极限效率不在乎更多的扩展和更新的朋友,那么你可以用硬编码定位,__mydata的偏移是0x70。

LUA_API void lua_setmydata(lua_State *L, int data){
 L->__mydata = data;
}

LUA_API int lua_getmydata(lua_State *L){
 return L->__mydata;
}


这样就万事具备了,重新编译lua,试试结果如何:


更抽象一点的做法:






使用硬编码进行定位:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值