xlua源码分析(四) lua访问C#的值类型
上一节我们主要探讨了C#是如何使用interface和delegate访问lua层的table和function的,本节我们跟着Examples 05_NoGc,来看看xlua是如何实现lua层无gc访问C#的值类型的。
首先例子中用到的lua代码如下:
function id(...)return ...
endfunction add(a, b) return a + b endfunction array_exchange(arr)arr[0], arr[1] = arr[1], arr[0]
endlocal v3 = CS.UnityEngine.Vector3(7, 8, 9)
local vt = CS.XLuaTest.MyStruct(5, 6)function lua_access_csharp()monoBehaviour:FloatParamMethod(123) --primitivemonoBehaviour:Vector3ParamMethod(v3) --vector3local rnd = math.random(1, 100)local r = monoBehaviour:Vector3ParamMethod({x = 1, y = 2, z = rnd}) --vector3assert(r.x == 1 and r.y == 2 and r.z == rnd)monoBehaviour:StructParamMethod(vt) --custom structr = monoBehaviour:StructParamMethod({a = 1, b = rnd, e = {c = rnd}})assert(r.b == rnd and r.e.c == rnd)monoBehaviour:EnumParamMethod(CS.XLuaTest.MyEnum.E2) --enummonoBehaviour:DecimalParamMethod(monoBehaviour.a5[0])monoBehaviour.a1[0], monoBehaviour.a1[1] = monoBehaviour.a1[1], monoBehaviour.a1[0] -- field
endexchanger = {exchange = function(self, arr)array_exchange(arr)end
}A = { B = { C = 789}}
GDATA = 1234;
回到C#,例子在Start函数中,push了当前的NoGc这个C#对象到lua层:
luaenv.Global.Set("monoBehaviour", this);
我们之前有提到,xlua对C#类信息的提取是lazy的,只会在需要用到类型信息的时候触发。比如这里,把一个C#对象push到lua层,意味着lua层需要知道对象的类型id,也就是C#层的getTypeId
函数,它的主体代码如下:
internal int getTypeId(RealStatePtr L, Type type, out bool is_first, LOGLEVEL log_level = LOGLEVEL.WARN)
{if (!typeIdMap.TryGetValue(type, out type_id)) // no reference{LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName);if (LuaAPI.lua_isnil(L, -1)) //no meta yet, try to use reflection meta{LuaAPI.lua_pop(L, 1);if (TryDelayWrapLoader(L, alias_type == null ? type : alias_type)){LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName);}else{throw new Exception("Fatal: can not load metatable of type:" + type);}}//循环依赖,自身依赖自己的class,比如有个自身类型的静态readonly对象。if (typeIdMap.TryGetValue(type, out type_id)){LuaAPI.lua_pop(L, 1);}else{type_id = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);LuaAPI.lua_pushnumber(L, type_id);LuaAPI.xlua_rawseti(L, -2, 1);LuaAPI.lua_pop(L, 1);if (type.IsValueType()){typeMap.Add(type_id, type);}typeIdMap.Add(type, type_id);}}return type_id;
}
代码中TryDelayWrapLoader
这个函数负责真正类型的wrap,我们先看一下getTypeId
的主体逻辑。
- 第一步,在
typeIdMap
中查找C#类型对应的type id,如果查找到了说明wrap过了,直接返回即可; - 如果没有查找到,首先获取一下该类型的metatable,如果metatable存在,说明当前调用是发生在递归的过程,也就是正在wrap中,此时不需要走到真正wrap的逻辑中;
- 如果metatable不存在,说明尚未wrap,需要调用
TryDelayWrapLoader
; - 在调用
TryDelayWrapLoader
的过程中,是有可能又调用getTypeId
这个函数的,这里的代码注释也提到了,如果有个自身类型的静态readonly对象也需要wrap,那么在访问这个对象的时候也需要知道其类型的type id; - 如果是这种情况,首先要避免重复wrap,这里就是代码的高明之处了,C#类型的metatable是在wrap的时候生成的,而只有metatable不存在时,才会调用wrap;
- 其次,还要避免重复生成type id,所以代码在新增type id时,要再次判断
typeIdMap
中是否已包含该类型。
我们在之前的文章详细分析了使用反射进行wrap的方式,这里NoGc类采用了事先生成wrap代码的方式对C#类进行注册,注册函数为XLuaTestNoGcWrap.__Register
:
public static void __Register(RealStatePtr L)
{ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);System.Type type = typeof(XLuaTest.NoGc);Utils.BeginObjectRegister(type, L, translator, 0, 5, 5, 5);Utils.RegisterFunc(L, Utils.METHOD_IDX, "FloatParamMethod", _m_FloatParamMethod);Utils.RegisterFunc(L, Utils.METHOD_IDX, "Vector3ParamMethod", _m_Vector3ParamMethod);Utils.RegisterFunc(L, Utils.METHOD_IDX, "StructParamMethod", _m_StructParamMethod);Utils.RegisterFunc(L, Utils.METHOD_IDX, "EnumParamMethod", _m_EnumParamMethod);Utils.RegisterFunc(L, Utils.METHOD_IDX, "DecimalParamMethod", _m_DecimalParamMethod);Utils.RegisterFunc(L, Utils.GETTER_IDX, "a1", _g_get_a1);Utils.RegisterFunc(L, Utils.GETTER_IDX, "a2", _g_get_a2);Utils.RegisterFunc(L, Utils.GETTER_IDX, "a3", _g_get_a3);Utils.RegisterFunc(L, Utils.GETTER_IDX, "a4", _g_get_a4);Utils.RegisterFunc(L, Utils.GETTER_IDX, "a5", _g_get_a5);Utils.RegisterFunc(L, Utils.SETTER_IDX, "a1", _s_set_a1);Utils.RegisterFunc(L, Utils.SETTER_IDX, "a2", _s_set_a2);Utils.RegisterFunc(L, Utils.SETTER_IDX, "a3", _s_set_a3);Utils.RegisterFunc(L, Utils.SETTER_IDX, "a4", _s_set_a4);Utils.RegisterFunc(L, Utils.SETTER_IDX, "a5", _s_set_a5);Utils.EndObjectRegister(type, L, translator, null, null,null, null, null);Utils.BeginClassRegister(type, L, __CreateInstance, 1, 0, 0);Utils.EndClassRegister(type, L, translator);
}
对照着NoGc类,很容易找到wrap所对应的属性和方法:
public class NoGc : MonoBehaviour
{[NonSerialized]public double[] a1 = new double[] { 1, 2 };[NonSerialized]public Vector3[] a2 = new Vector3[] { new Vector3(1, 2, 3), new Vector3(4, 5, 6) };[NonSerialized]public MyStruct[] a3 = new MyStruct[] { new MyStruct(1, 2), new MyStruct(3, 4) };[NonSerialized]public MyEnum[] a4 = new MyEnum[] { MyEnum.E1, MyEnum.E2 };[NonSerialized]public decimal[] a5 = new decimal[] { 1.00001M, 2.00002M };public float FloatParamMethod(float p){return p;}public Vector3 Vector3ParamMethod(Vector3 p){return p;}public MyStruct StructParamMethod(MyStruct p){return p;}public MyEnum EnumParamMethod(MyEnum p){return p;}public decimal DecimalParamMethod(decimal p){return p;}
}
可以看到,NoGc类的wrap分为两大块,一块是针对object的,一块是针对class的,两者的区别在于lua层是访问userdata调用C#方法,还是访问class table调用C#方法。这里BeginObjectRegister
函数最后3个参数为5,5,5,分别表示要注册的方法数量,get属性数量,set属性数量。如果数量不为0,xlua会创建一个table容纳接下来要注册进来的方法,如果为0则会push一个nil值进行占位。
if (method_count == 0)
{LuaAPI.lua_pushnil(L);
}
else
{LuaAPI.lua_createtable(L, 0, method_count);
}if (getter_count == 0)
{LuaAPI.lua_pushnil(L);
}
else
{LuaAPI.lua_createtable(L, 0, getter_count);
}if (setter_count == 0)
{LuaAPI.lua_pushnil(L);
}
else
{LuaAPI.lua_createtable(L, 0, setter_count);
}
此时lua栈如图所示:
RegisterFunc
的第二个参数表示要操作的table所在的index,可以看到xlua的定义与图中所示的完全一致:
public const int OBJ_META_IDX = -4;
public const int METHOD_IDX = -3;
public const int GETTER_IDX = -2;
public const int SETTER_IDX = -1;
RegisterFunc
的实现相当简单,就是往table里塞C#方法/属性的名字和包装函数:
public static void RegisterFunc(RealStatePtr L, int idx, string name, LuaCSFunction func)
{idx = abs_idx(LuaAPI.lua_gettop(L), idx);LuaAPI.xlua_pushasciistring(L, name);LuaAPI.lua_pushstdcallcfunction(L, func);LuaAPI.lua_rawset(L, idx);
}
EndObjectRegister
要稍微复杂一些,它的声明是这样的:
public static void EndObjectRegister(Type type, RealStatePtr L, ObjectTranslator translator, LuaCSFunction csIndexer,LuaCSFunction csNewIndexer, Type base_type, LuaCSFunction arrayIndexer, LuaCSFunction arrayNewIndexer)
{...
}
这个函数的主要作用,就是设置object metatable的__index
和__newindex
元方法,分别绑定gen_obj_indexer
和gen_obj_newindexer
生成的函数。这两个方法之前也分析过了,这里就不展开了。在绑定之前,EndObjectRegister
要准备好调用环境,以gen_obj_indexer
为例:
int top = LuaAPI.lua_gettop(L);
int meta_idx = abs_idx(top, OBJ_META_IDX);
int method_idx = abs_idx(top, METHOD_IDX);
int getter_idx = abs_idx(top, GETTER_IDX);
int setter_idx = abs_idx(top, SETTER_IDX);//begin index gen
LuaAPI.xlua_pushasciistring(L, "__index");
LuaAPI.lua_pushvalue(L, method_idx);
LuaAPI.lua_pushvalue(L, getter_idx);if (csIndexer == null)
{LuaAPI.lua_pushnil(L);
}
else
{LuaAPI.lua_pushstdcallcfunction(L, csIndexer);
}translator.Push(L, type == null ? base_type : type.BaseType());LuaAPI.xlua_pushasciistring(L, LuaIndexsFieldName);
LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);
if (arrayIndexer == null)
{LuaAPI.lua_pushnil(L);
}
else
{LuaAPI.lua_pushstdcallcfunction(L, arrayIndexer);
}LuaAPI.gen_obj_indexer(L);
在调用gen_obj_indexer
前的lua栈如下:
后面的故事我们已经知道了,gen_obj_indexer
会把栈上的这些值,作为upvalue吸收掉,最后只留下生成的函数在栈顶,然后__index显然指向它。其中,csindexer主要表示C#类重载下标操作符的行为,例如Vector3可以直接使用下标访问其xyz分量;而arrayindexer主要表示数组类型,例如Array可以通过下标访问其元素。
BeginClassRegister
的逻辑与object的类似,它需要传入表示class field,static getter,static setter数量的3个参数:
public static void BeginClassRegister(Type type, RealStatePtr L, LuaCSFunction creator, int class_field_count,int static_getter_count, int static_setter_count)
同样只要有数量大于0的,就新建一个table备用,和object稍微不同的是,object metatable最后是设置在userdata上的,class metatable要设置在class table上,因此这里还要创建一个class table。class所包含的静态方法,无需放在metatable上,直接放在class table里即可。此时的lua栈如下:
EndClassRegister
函数的主要目的也是为了设置class metatable。gen_cls_indexer
调用前的lua栈如下:
继续看C#的Start函数:
luaenv.Global.Get("id", out f1);
luaenv.Global.Get("id", out f2);
luaenv.Global.Get("id", out f3);
luaenv.Global.Get("id", out f4);
luaenv.Global.Get("id", out f5);luaenv.Global.Get("array_exchange", out farr);
luaenv.Global.Get("lua_access_csharp", out flua);
luaenv.Global.Get("exchanger", out ie);
luaenv.Global.Get("add", out add);
这里就是获取到lua的function和table,保存到对应的C#的interface和delegate变量上。再看Update函数,首先是把C#的Vector3 push到lua层:
f2(new Vector3(1, 2, 3)); // vector3
内部实现会调用到PushUnityEngineVector3
这个函数上:
public void PushUnityEngineVector3(RealStatePtr L, UnityEngine.Vector3 val)
{if (UnityEngineVector3_TypeID == -1){bool is_first;UnityEngineVector3_TypeID = getTypeId(L, typeof(UnityEngine.Vector3), out is_first);}IntPtr buff = LuaAPI.xlua_pushstruct(L, 12, UnityEngineVector3_TypeID);if (!CopyByValue.Pack(buff, 0, val)){throw new Exception("pack fail fail for UnityEngine.Vector3 ,value="+val);}}
xlua_pushstruct
的实现很简单,就是在C层创建一个指定大小的userdata,设置其metatable为C# struct的metatable:
typedef struct {int fake_id;unsigned int len;char data[1];
} CSharpStruct;LUA_API void *xlua_pushstruct(lua_State *L, unsigned int size, int meta_ref) {CSharpStruct *css = (CSharpStruct *)lua_newuserdata(L, size + sizeof(int) + sizeof(unsigned int));css->fake_id = -1;css->len = size;lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);lua_setmetatable(L, -2);return css;
}
实际分配的userdata大小要比传入的size大一些,这是因为剩余的部分需要用来存放fake_id
和len
这两个成员。这里传入的size大小为12,也就是刚好可以容纳一个Vector3的大小。到这里就可以猜到了,为了避免拆装箱导致的gc,这里肯定是把C#的值拷贝到lua层了。CopyByValue.Pack
就是做这个的:
public static bool Pack(IntPtr buff, int offset, UnityEngine.Vector3 field)
{if(!LuaAPI.xlua_pack_float3(buff, offset, field.x, field.y, field.z)){return false;}return true;
}
xlua_pack_float3
这个函数执行真正的拷贝逻辑:
LUALIB_API int xlua_pack_float3(void *p, int offset, float f1, float f2, float f3) {CSharpStruct *css = (CSharpStruct *)p;if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) {return 0;} else {float *pos = (float *)(&(css->data[0]) + offset);pos[0] = f1;pos[1] = f2;pos[2] = f3;return 1;}
}
f2对应的lua函数是有返回值的,它会把push进来的参数原封不动地返回出去,类似地也有CopyByValue.UnPack
这样一个函数,负责把lua层的数据拷贝到C#层:
public static bool UnPack(IntPtr buff, int offset, out UnityEngine.Vector3 field)
{field = default(UnityEngine.Vector3);float x = default(float);float y = default(float);float z = default(float);if(!LuaAPI.xlua_unpack_float3(buff, offset, out x, out y, out z)){return false;}field.x = x;field.y = y;field.z = z;return true;
}
同样也是xlua_unpack_float3
执行真正的拷贝:
LUALIB_API int xlua_unpack_float3(void *p, int offset, float *f1, float *f2, float *f3) {CSharpStruct *css = (CSharpStruct *)p;if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) {return 0;} else {float *pos = (float *)(&(css->data[0]) + offset);*f1 = pos[0];*f2 = pos[1];*f3 = pos[2];return 1;}
}
接下来看看如何将自定义的struct push到lua层:
f3(mystruct1); // custom complex value type
MyStruct的定义如下:
[GCOptimize]
[LuaCallCSharp]
public struct Pedding
{public byte c;
}[GCOptimize]
[LuaCallCSharp]
public struct MyStruct
{public MyStruct(int p1, int p2){a = p1;b = p2;c = p2;e.c = (byte)p1;}public int a;public int b;public decimal c;public Pedding e;
}
打上GCOptimize标签的struct,会自动生成代码,push struct时会调用到PushXLuaTestMyStruct
这个函数,它和Vector3的版本很像:
public void PushXLuaTestMyStruct(RealStatePtr L, XLuaTest.MyStruct val)
{if (XLuaTestMyStruct_TypeID == -1){bool is_first;XLuaTestMyStruct_TypeID = getTypeId(L, typeof(XLuaTest.MyStruct), out is_first);}IntPtr buff = LuaAPI.xlua_pushstruct(L, 25, XLuaTestMyStruct_TypeID);if (!CopyByValue.Pack(buff, 0, val)){throw new Exception("pack fail fail for XLuaTest.MyStruct ,value="+val);}}
25表示MyStruct所占的字节数。MyStruct由2个int,1个decimal,1个struct所组成,因此Pack分为四个步骤:
public static bool Pack(IntPtr buff, int offset, XLuaTest.MyStruct field)
{if(!Pack(buff, offset, field.a)){return false;}if(!Pack(buff, offset + 4, field.b)){return false;}if(!Pack(buff, offset + 8, field.c)){return false;}if(!Pack(buff, offset + 24, field.e)){return false;}return true;
}
同样地,Unpack也需要分成4个步骤,逐一把数据从lua层拷贝到C#:
public static bool UnPack(IntPtr buff, int offset, out XLuaTest.MyStruct field)
{field = default(XLuaTest.MyStruct);if(!UnPack(buff, offset, out field.a)){return false;}if(!UnPack(buff, offset + 4, out field.b)){return false;}if(!UnPack(buff, offset + 8, out field.c)){return false;}if(!UnPack(buff, offset + 24, out field.e)){return false;}return true;
}
下一行代码展示了如何把enum push到lua层:
f4(MyEnum.E1); //enum
MyEnum的定义如下:
[LuaCallCSharp]
public enum MyEnum
{E1,E2
}
相应的push函数为PushXLuaTestMyEnum
,它和之前push struct稍稍有些不同:
public void PushXLuaTestMyEnum(RealStatePtr L, XLuaTest.MyEnum val)
{if (XLuaTestMyEnum_TypeID == -1){bool is_first;XLuaTestMyEnum_TypeID = getTypeId(L, typeof(XLuaTest.MyEnum), out is_first);if (XLuaTestMyEnum_EnumRef == -1){Utils.LoadCSTable(L, typeof(XLuaTest.MyEnum));XLuaTestMyEnum_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);}}if (LuaAPI.xlua_tryget_cachedud(L, (int)val, XLuaTestMyEnum_EnumRef) == 1){return;}IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, XLuaTestMyEnum_TypeID);if (!CopyByValue.Pack(buff, 0, (int)val)){throw new Exception("pack fail fail for XLuaTest.MyEnum ,value="+val);}LuaAPI.lua_getref(L, XLuaTestMyEnum_EnumRef);LuaAPI.lua_pushvalue(L, -2);LuaAPI.xlua_rawseti(L, -2, (int)val);LuaAPI.lua_pop(L, 1);}
由于enum的值是常量,因此拷贝到lua层的userdata只需要创建一次即可,创建完成之后在lua层进行缓存,下次C#层再push时,先从缓存中查找userdata。lua层拷贝enum到C#层的逻辑和struct类似,这里就不再展开了。
如果在lua层持有了C# struct的array,那么对array中的元素进行get/set也会触发到上述列举的这些方法,相关的封装代码在__tryArrayGet
和__tryArraySet
中:
public partial class StaticLuaCallbacks
{internal static bool __tryArrayGet(Type type, RealStatePtr L, ObjectTranslator translator, object obj, int index){if (type == typeof(UnityEngine.Vector2[])){UnityEngine.Vector2[] array = obj as UnityEngine.Vector2[];translator.PushUnityEngineVector2(L, array[index]);return true;}...}internal static bool __tryArraySet(Type type, RealStatePtr L, ObjectTranslator translator, object obj, int array_idx, int obj_idx){if (type == typeof(UnityEngine.Vector2[])){UnityEngine.Vector2[] array = obj as UnityEngine.Vector2[];translator.Get(L, obj_idx, out array[array_idx]);return true;}...}
}
当然,lua层除了支持从userdata拷贝struct的值到C#之外,还可以直接通过构造一个table,将值拷贝到C# struct的对应字段上,比如Vector3,有这样一个方法:
public static void UnPack(ObjectTranslator translator, RealStatePtr L, int idx, out UnityEngine.Vector3 val)
{val = new UnityEngine.Vector3();int top = LuaAPI.lua_gettop(L);if (Utils.LoadField(L, idx, "x")){translator.Get(L, top + 1, out val.x);}LuaAPI.lua_pop(L, 1);if (Utils.LoadField(L, idx, "y")){translator.Get(L, top + 1, out val.y);}LuaAPI.lua_pop(L, 1);if (Utils.LoadField(L, idx, "z")){translator.Get(L, top + 1, out val.z);}LuaAPI.lua_pop(L, 1);}
只要table中包含x,y,z三个字段,就可以对应地拷贝到Vector3上了。
自此,我们算是比较系统地分析了xlua中值类型的实现。与tolua相比,两者都实现了无gc的值类型传递。不过xlua对值类型中的方法,默认使用wrap的方式,也就是说lua调用C# 值类型的方法,需要把数据拷贝到C#层,再进行函数调用,如果调用频繁的话,其性能开销就不甚理想了。而tolua的值类型,默认会在lua层实现一份类似的代码,lua层在调用时,完全是走的lua层的逻辑,不会涉及拷贝数据到C#层的逻辑,只有作为函数调用参数和返回值时,才涉及到数据的拷贝。这样做的好处,就是避免了在函数调用过程的频繁的数据拷贝开销,不方便的地方就是需要在lua层自己实现一遍C#的值类型,而使用wrap则只需要自动生成代码即可,没有额外的开发负担。