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

来源:互联网 发布:gta5没网络能玩吗 编辑:程序博客网 时间:2024/05/12 15:15

很早的时候,我纠结过一件事,假如,我在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 0
原创粉丝点击