不管是C++中还是在C#中,在都绕不开一个问题:类对象怎么在Lua中使用的问题,还好Lua提供了Userdata以及ligh Userdata结构类型,通过扩展可以处理这方面的问题。现在的很多框架也大致类似的方式进行处理。

在前面的一些笔记<Lua 与 C 交互之UserData(4)>中提到过几个使用宿主类对象中的问题:

  1. 数据内存
  2. 生命周期
  3. 数据操作

那篇文章中给出的案例是《Lua程序设计》中给出的教程,数据放在Lua中,由Lua来管理对象的内存数据,这种方式未必不好,需要优化的是可以在userdata的metatable中增加__gc元方法,去通知宿主语言释放信息,避免宿主语言中对空指针的使用。

Ulua对象管理方式:Ulua采用对象数据在C#层,userdata值记录对象在c#数组中index,并在metatable提供查找对象、访问对象方法的能力以及释放对象时清除c#数据的能力。当然不同的类型Ulua提供的metatable也不尽相同,暂且已class_object来了解这个过程。

支持对象方法访问

在1.03版本以后,ULua中增加了很多其他类型的支持,比如enum、array等,对于每种类型,都会提供对应的metatable提供访问类元素的能力,比如Object_class。当然metatable也可以用作类型的判断。

 private void createIndexingMetaFunction(IntPtr luaState)
{
LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction");
LuaDLL.luaL_dostring(luaState, MetaFunctions.luaIndexFunction);
//LuaDLL.lua_pushstdcallcfunction(luaState,indexFunction);
LuaDLL.lua_rawset(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
} //创建物体时
private void pushNewObject(IntPtr luaState, object o, int index, string metatable)
{
...
if (metatable == "luaNet_metatable")
{
// Gets or creates the metatable for the object's type
//string meta = t.AssemblyQualifiedName
//LuaDLL.luaL_getmetatable(luaState, meta);
Type t = o.GetType();
PushMetaTable(luaState, o.GetType()); if (LuaDLL.lua_isnil(luaState, -1))
{
string meta = t.AssemblyQualifiedName;
Debugger.LogWarning("Create not wrap ulua type:" + meta);
LuaDLL.lua_settop(luaState, -2);
LuaDLL.luaL_newmetatable(luaState, meta);
LuaDLL.lua_pushstring(luaState, "cache");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_rawset(luaState, -3);
LuaDLL.lua_pushlightuserdata(luaState, LuaDLL.luanet_gettag());
LuaDLL.lua_pushnumber(luaState, 1);
LuaDLL.lua_rawset(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__index");
LuaDLL.lua_pushstring(luaState, "luaNet_indexfunction");
LuaDLL.lua_rawget(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
LuaDLL.lua_rawset(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__gc");
LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaDLL.lua_rawset(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__tostring");
LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaDLL.lua_rawset(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__newindex");
LuaDLL.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
LuaDLL.lua_rawset(luaState, -3);
}
}
else
{
LuaDLL.luaL_getmetatable(luaState, metatable);
}
...
}

在添加新对象的时候,会创建的Metatable,并设置元方法。通过 _ _ index实现对类对象的访问,当然这里是通过反射机制来实现的。

通过Wraper全局注册的方式并不需要这里的_ _index的访问了。Ulua经过几个版本的更新,代码相当的混乱了。到 toLuaC#中其实__Index已经删除。

对象管理

对象列表

C#维护一个weak table,以数组index为key记录对象userdata数据。

 private void createLuaObjectList(IntPtr luaState)
{
LuaDLL.lua_pushstring(luaState, "luaNet_objects");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_pushvalue(luaState, -1);
weakTableRef = LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX);
LuaDLL.lua_pushvalue(luaState, -1);
LuaDLL.lua_setmetatable(luaState, -2);
LuaDLL.lua_pushstring(luaState, "__mode");
LuaDLL.lua_pushstring(luaState, "v");
LuaDLL.lua_settable(luaState, -3);
//LuaDLL.lua_setmetatable(luaState,-2);
LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX);
}

增加对象

在添加物体时 会产生一个index作为该object唯一的参数保存在一张C#列表中,并将这个Indx和userdata数据放在一个weaktable(weakTableRef为LuaIndexes.LUA_REGISTRYINDEX表中索引)。

private void pushNewObject(IntPtr luaState, object o, int index, string metatable)
{
LuaDLL.lua_getref(luaState, weakTableRef);
LuaDLL.luanet_newudata(luaState, index);
...
//创建metatable,并放在入栈
...
LuaDLL.lua_setmetatable(luaState, -2);
LuaDLL.lua_pushvalue(luaState, -1);
LuaDLL.lua_rawseti(luaState, -3, index);
LuaDLL.lua_remove(luaState, -2);
...
}
//返回新的Index
int addObject(object obj)
{
// New object: inserts it in the list
int index = nextObj++;
objects[index] = obj;
objectsBackMap[obj] = index; return index;
}

获取对象

顺便提一下UserData中存储的是index值,了解下luanet_newudata接口的实现就可以明白。

LUALIB_API void luanet_newudata(lua_State *L,int val)
{
int* pointer=(int*)lua_newuserdata(L,sizeof(int));
*pointer=val;
}

那怎么获取对象呢?既然知道userdata中存储的是index的数据,那么就可以在获取userdata之后,直接在C#中查表获得对象。

LUALIB_API int luanet_rawnetobj(lua_State *L,int index)
{
int *udata = lua_touserdata(L,index);
if(udata!=NULL) return *udata;
return -1;
} //C#中接口
internal object getRawNetObject(IntPtr luaState, int index)
{
int udata = LuaDLL.luanet_rawnetobj(luaState, index);
object obj = null;
objects.TryGetValue(udata, out obj);
return obj;
}

删除对象

既然对象保存在C#层,那Lua中如果释放了该对象,C#层如何释放呢?还是通过metatable中元方法,[Lua5.1中userdata支持_ GC方法][3],对象被Lua回收期释放的时候调用这个方法。C#层中将对象移除。

internal void collectObject(int udata)
{
object o;
bool found = objects.TryGetValue(udata, out o);
// The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry
if (found)
{
objects.Remove(udata);
if (o != null && !o.GetType().IsValueType)
{
objectsBackMap.Remove(o);
}
}
}

对于增加新物体时,如果发现这个对象在C#缓冲列表中存在,但是在Lua中不存在,那就可以删除了。这里有使用到weak table,也就是如果userdata不在被引用,就会被Lua垃圾收集器回收。

 public void pushObject(IntPtr luaState, object o, string metatable)
{
if (o == null)
{
LuaDLL.lua_pushnil(luaState);
return;
} int index = -1;
// Object already in the list of Lua objects? Push the stored reference.
bool beValueType = o.GetType().IsValueType;
if (!beValueType && objectsBackMap.TryGetValue(o, out index))
{
if (LuaDLL.tolua_pushudata(luaState, weakTableRef, index))
{
return;
} // Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweepoccurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect object here
// did we find a non nil object in our table? if not, we need to call collect object Remove from both our tables and fall out to get a new ID
collectObject(o, index);
}
index = addObject(o, beValueType);
pushNewObject(luaState, o, index, metatable);
}

存在的问题

  1. 如果Lua中一直持有对象引用,就不会释放这个对象,造成无法回收
  2. 物体不使用的时候直接Destroy掉,会增加GC消耗,增加pool的会更合理一点

    在后续版本中对这些情况也做了对应的优化。

[2]:http://www.cnblogs.com/zsb517/p/6423342.html “Lua 基础之Weak Table(5)”

[3]:http://www.cnblogs.com/zsb517/p/6423472.html "Lua基础之MetaTable(6)"

Ulua对象管理方式的更多相关文章

  1. Kubernetes 对象管理的三种方式

    Kubernetes 中文文档 1. Kubernetes 对象管理的三种方式对比 Kubernetes 中的对象管理方式,根据对象配置信息的位置不同可以分为两大类: 命令式:对象的参数通过命令指定 ...

  2. 24小时学通Linux内核之内存管理方式

    昨天分析的进程的代码让自己还在头昏目眩,脑子中这几天都是关于Linux内核的,对于自己出现的一些问题我会继续改正,希望和大家好好分享,共同进步.今天将会讲诉Linux如何追踪和管理用户空间进程的可用内 ...

  3. [Effective C++ --013]以对象管理资源

    这一节基本讲述的是将资源放进管理对象,防止忘记释放资源. 1.一般New和Delete使用场景 void fun() { SimpleClass* pSimpleClass1 = new Simple ...

  4. ORACLE表空间管理方式segment和extent

    A permanent tablespace contains persistent schema objects. Objects in permanent tablespaces are stor ...

  5. 十天学Linux内核之第三天---内存管理方式

    原文:十天学Linux内核之第三天---内存管理方式 昨天分析的进程的代码让自己还在头昏目眩,脑子中这几天都是关于Linux内核的,对于自己出现的一些问题我会继续改正,希望和大家好好分享,共同进步.今 ...

  6. ObjC如何通过runtime修改Ivar的内存管理方式

    ObjC如何通过runtime修改Ivar的内存管理方式 为什么要这么做? 在iOS 9之前,UITableView(或者更确切的说是 UIScrollView)有一个众所周知的问题: propert ...

  7. Java内存管理及对Java对象管理

    Java内存管理及对Java对象管理 1Java内存管理 1.1Java中的堆和栈 通常来说,人们会将Java内存氛围栈内存(Stack)和堆内存(Heap). 栈内存用来保存基本类型的变量和对象的引 ...

  8. Oracle表空间的管理方式

    解释说明:表空间是一个逻辑概念:=> oracle 逻辑概念段区块管理方式: number one => tablespace number two=> segments Oracl ...

  9. Mybatis事务(一)事务管理方式

    Mybatis管理事务是分为两种方式: (1)使用JDBC的事务管理机制,就是利用java.sql.Connection对象完成对事务的提交 (2)使用MANAGED的事务管理机制,这种机制mybat ...

随机推荐

  1. CSS hack兼容表

    IE6 IE7 IE8 Firefox Opera Safari !important Y Y Y Y Y _ Y * Y Y *+ Y \9 Y Y Y \0 Y nth-of-type(1) Y ...

  2. 配置IIS的负载均衡

    在大型Web应用系统中,由于请求的数据量过大以及并发的因素,导致Web系统会出现宕机的现象,解决这一类问题的方法我个人觉得主要在以下几个方面: 1.IIS 负载均衡. 2.数据库 负载均衡. 3.系统 ...

  3. SpringBoot之使用Scheduled做定时任务

    定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便. 一.@Scheduled注解介绍 这里先贴上@Schedu ...

  4. SQL SERVER 一个SQL语句的执行顺序

    一个SQL 语句的执行顺序 1.From (告诉程序 来自哪张表  如果是表表达式 依旧是如此顺序) 2.Where(条件筛选  谓词筛选 ) 3.Group by(分组) 4.Having(分组   ...

  5. 在Web.config或App.config中的添加自定义配置 <转>

        .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持.最近看到一些项目中还在自定义xml文件做程序的配置 ...

  6. Web前端基础——jQuery(二)

    一.jQuery 中的常用函数 1) $.map(Array,fn); 对数组中的每个元素,都用fn进行处理,fn将处理后的结果返回,最后得到一个数组 //因为这些操作,没有与dom元素相关的,所以可 ...

  7. HDU6201

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  8. blfs(systemv版本)学习笔记-总页

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs(systemv版本)学习笔记:https://www.cnblogs.com/renren-study-notes/p/ ...

  9. BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)

    题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理 ...

  10. java程序的加载与执行

    1.编写java源文件   .java 2.生成字节码文件   类名.class 3.JRE,java运行环境,包括JVM(java虚拟机).java基本类库 4.