public static int MetaFuncIndex(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); Type type = translator.FastGetCSObj(L, 2) as Type; if (type == null) { return(LuaAPI.luaL_error(L, "#2 param need a System.Type!")); } //UnityEngine.Debug.Log("============================load type by __index:" + type); //translator.TryDelayWrapLoader(L, type); translator.GetTypeId(L, type); LuaAPI.lua_pushvalue(L, 2); LuaAPI.lua_rawget(L, 1); return(1); } catch (System.Exception e) { return(LuaAPI.luaL_error(L, "c# exception in MetaFuncIndex:" + e)); } }
// no boxing version get public void RawGet <TKey, TValue>(TKey key, out TValue value) { #if THREAD_SAFE || HOTFIX_ENABLE lock (luaEnv.luaEnvLock) { #endif var L = luaEnv.L; var translator = luaEnv.translator; int oldTop = LuaAPI.lua_gettop(L); LuaAPI.lua_getref(L, luaReference); translator.PushByType(L, key); LuaAPI.lua_rawget(L, -2); LuaTypes lua_type = LuaAPI.lua_type(L, -1); Type type_of_value = typeof(TValue); if (lua_type == LuaTypes.LUA_TNIL && type_of_value.IsValueType()) { throw new InvalidCastException("can not assign nil to " + type_of_value.GetFriendlyName()); } try { translator.Get(L, -1, out value); } catch (Exception e) { throw e; } finally { LuaAPI.lua_settop(L, oldTop); } #if THREAD_SAFE || HOTFIX_ENABLE } #endif }
public static void loadUpvalue(RealStatePtr L, Type type, string metafunc, int num) { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); LuaAPI.xlua_pushasciistring(L, metafunc); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_rawget(L, -2); for (int i = 1; i <= num; i++) { LuaAPI.lua_getupvalue(L, -i, i); if (LuaAPI.lua_isnil(L, -1)) { LuaAPI.lua_pop(L, 1); LuaAPI.lua_newtable(L); LuaAPI.lua_pushvalue(L, -1); LuaAPI.lua_setupvalue(L, -i - 2, i); } } for (int i = 0; i < num; i++) { LuaAPI.lua_remove(L, -num - 1); } }
// no boxing version get public bool ContainsRawKey <TKey>(TKey key) { #if THREAD_SAFE || HOTFIX_ENABLE lock (luaEnv.luaEnvLock) { #endif var L = luaEnv.L; var translator = luaEnv.translator; int oldTop = LuaAPI.lua_gettop(L); LuaAPI.lua_getref(L, luaReference); translator.PushByType(L, key); LuaAPI.lua_rawget(L, -2); bool ret = LuaAPI.lua_type(L, -1) != LuaTypes.LUA_TNIL; LuaAPI.lua_settop(L, oldTop); return(ret); #if THREAD_SAFE || HOTFIX_ENABLE } #endif }
internal static int LoadCS(RealStatePtr L) { LuaAPI.xlua_pushasciistring(L, LuaEnv.CSHARP_NAMESPACE); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); return(1); }
public Delegate CreateDelegateBridge(RealStatePtr L, Type delegateType, int idx) { LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); if (!LuaAPI.lua_isnil(L, -1)) { int referenced = LuaAPI.xlua_tointeger(L, -1); LuaAPI.lua_pop(L, 1); if (delegate_bridges[referenced].IsAlive) { DelegateBridgeBase exist_bridge = delegate_bridges[referenced].Target as DelegateBridgeBase; Delegate exist_delegate; if (exist_bridge.TryGetDelegate(delegateType, out exist_delegate)) { return(exist_delegate); } else { exist_delegate = exist_bridge.GetDelegateByType(delegateType); exist_bridge.AddDelegate(delegateType, exist_delegate); return(exist_delegate); } } } else { LuaAPI.lua_pop(L, 1); } LuaAPI.lua_pushvalue(L, idx); int reference = LuaAPI.luaL_ref(L); LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_pushnumber(L, reference); LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX); DelegateBridgeBase bridge; try { #if UNITY_EDITOR if (!DelegateBridge.Gen_Flag) { bridge = Activator.CreateInstance(delegate_birdge_type, new object[] { reference, luaEnv }) as DelegateBridgeBase; } else #endif { bridge = new DelegateBridge(reference, luaEnv); } } catch (Exception e) { LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_pushnil(L); LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.xlua_rawseti(L, LuaIndexes.LUA_REGISTRYINDEX, reference); throw e; } try { var ret = bridge.GetDelegateByType(delegateType); bridge.AddDelegate(delegateType, ret); delegate_bridges[reference] = new WeakReference(bridge); return(ret); } catch (Exception e) { bridge.Dispose(); throw e; } }
public static void EndObjectRegister(Type type, RealStatePtr L, ObjectTranslator translator, LuaCSFunction csIndexer, LuaCSFunction csNewIndexer, Type base_type, LuaCSFunction arrayIndexer, LuaCSFunction arrayNewIndexer) { 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, Utils.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); if (type != null) { LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua indexs function tables translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); } LuaAPI.lua_rawset(L, meta_idx); //end index gen //begin newindex gen LuaAPI.xlua_pushasciistring(L, "__newindex"); LuaAPI.lua_pushvalue(L, setter_idx); if (csNewIndexer == null) { LuaAPI.lua_pushnil(L); } else { LuaAPI.lua_pushstdcallcfunction(L, csNewIndexer); } translator.Push(L, type == null ? base_type : type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); if (arrayNewIndexer == null) { LuaAPI.lua_pushnil(L); } else { LuaAPI.lua_pushstdcallcfunction(L, arrayNewIndexer); } LuaAPI.gen_obj_newindexer(L); if (type != null) { LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX);//store in lua newindexs function tables translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); } LuaAPI.lua_rawset(L, meta_idx); //end new index gen LuaAPI.lua_pop(L, 4); }
public static void ReflectionWrap(RealStatePtr L, Type type) { int top_enter = LuaAPI.lua_gettop(L); ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); //create obj meta table LuaAPI.luaL_getmetatable(L, type.FullName); if (LuaAPI.lua_isnil(L, -1)) { LuaAPI.lua_pop(L, 1); LuaAPI.luaL_newmetatable(L, type.FullName); } LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag()); LuaAPI.lua_pushnumber(L, 1); LuaAPI.lua_rawset(L, -3); int obj_meta = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_meta = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_field = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_getter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_setter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_field = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_getter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_setter = LuaAPI.lua_gettop(L); LuaCSFunction item_getter; LuaCSFunction item_setter; makeReflectionWrap(L, type, cls_field, cls_getter, cls_setter, obj_field, obj_getter, obj_setter, obj_meta, out item_getter, out item_setter); // init obj metatable LuaAPI.xlua_pushasciistring(L, "__gc"); LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.GcMeta); LuaAPI.lua_rawset(L, obj_meta); LuaAPI.xlua_pushasciistring(L, "__tostring"); LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.ToStringMeta); LuaAPI.lua_rawset(L, obj_meta); LuaAPI.xlua_pushasciistring(L, "__index"); LuaAPI.lua_pushvalue(L, obj_field); LuaAPI.lua_pushvalue(L, obj_getter); translator.PushFixCSFunction(L, item_getter); translator.PushAny(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.gen_obj_indexer(L); //store in lua indexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, obj_meta); // set __index LuaAPI.xlua_pushasciistring(L, "__newindex"); LuaAPI.lua_pushvalue(L, obj_setter); translator.PushFixCSFunction(L, item_setter); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.gen_obj_newindexer(L); //store in lua newindexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, obj_meta); // set __newindex //finish init obj metatable LuaAPI.xlua_pushasciistring(L, "UnderlyingSystemType"); translator.PushAny(L, type); LuaAPI.lua_rawset(L, cls_field); if (type != null && type.IsEnum) { LuaAPI.xlua_pushasciistring(L, "__CastFrom"); translator.PushFixCSFunction(L, genEnumCastFrom(type)); LuaAPI.lua_rawset(L, cls_field); } //set cls_field to namespace SetCSTable(L, type, cls_field); //finish set cls_field to namespace //init class meta LuaAPI.xlua_pushasciistring(L, "__index"); LuaAPI.lua_pushvalue(L, cls_getter); LuaAPI.lua_pushvalue(L, cls_field); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.gen_cls_indexer(L); //store in lua indexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, cls_meta); // set __index LuaAPI.xlua_pushasciistring(L, "__newindex"); LuaAPI.lua_pushvalue(L, cls_setter); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.gen_cls_newindexer(L); //store in lua newindexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, cls_meta); // set __newindex LuaCSFunction constructor = translator.methodWrapsCache.GetConstructorWrap(type); if (constructor == null) { constructor = (RealStatePtr LL) => { return(LuaAPI.luaL_error(LL, "No constructor for " + type)); }; } LuaAPI.xlua_pushasciistring(L, "__call"); translator.PushFixCSFunction(L, constructor); LuaAPI.lua_rawset(L, cls_meta); LuaAPI.lua_pushvalue(L, cls_meta); LuaAPI.lua_setmetatable(L, cls_field); LuaAPI.lua_pop(L, 8); System.Diagnostics.Debug.Assert(top_enter == LuaAPI.lua_gettop(L)); }
public static bool LoadField(RealStatePtr L, int idx, string field_name) { LuaAPI.xlua_pushasciistring(L, field_name); LuaAPI.lua_rawget(L, idx); return(!LuaAPI.lua_isnil(L, -1)); }
private ObjectCast genCaster(Type type) { ObjectCast fixTypeGetter = (RealStatePtr L, int idx, object target) => { if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA) { object obj = translator.SafeGetCSObj(L, idx); return((obj != null && type.IsAssignableFrom(obj.GetType())) ? obj : null); } return(null); }; if (typeof(Delegate).IsAssignableFrom(type)) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_isfunction(L, idx)) { return null; } return translator.CreateDelegateBridge(L, type, idx); }); } else if (type.IsInterface) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } return translator.CreateInterfaceBridge(L, type, idx); }); } else if (type.IsEnum) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } LuaTypes lua_type = LuaAPI.lua_type(L, idx); if (lua_type == LuaTypes.LUA_TSTRING) { return Enum.Parse(type, LuaAPI.lua_tostring(L, idx)); } else if (lua_type == LuaTypes.LUA_TNUMBER) { return Enum.ToObject(type, LuaAPI.xlua_tointeger(L, idx)); } throw new InvalidCastException("invalid value for enum " + type); }); } else if (type.IsArray) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } uint len = LuaAPI.xlua_objlen(L, idx); int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index Type et = type.GetElementType(); ObjectCast elementCaster = GetCaster(et); Array ary = target == null?Array.CreateInstance(et, len) : target as Array; if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to Array"); } for (int i = 0; i < len; ++i) { LuaAPI.lua_pushnumber(L, i + 1); LuaAPI.lua_rawget(L, idx); if (et.IsPrimitive) { if (!StaticLuaCallbacks.TryPrimitiveArraySet(type, L, ary, i, n + 1)) { ary.SetValue(elementCaster(L, n + 1, null), i); } } else { if (StaticLuaCallbacks.GenTryArraySetPtr == null || !StaticLuaCallbacks.GenTryArraySetPtr(type, L, translator, ary, i, n + 1)) { ary.SetValue(elementCaster(L, n + 1, null), i); } } LuaAPI.lua_pop(L, 1); } return ary; }); } else if (typeof(IList).IsAssignableFrom(type) && type.IsGenericType) { ObjectCast elementCaster = GetCaster(type.GetGenericArguments()[0]); return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } obj = target == null?Activator.CreateInstance(type) : target; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index IList list = obj as IList; uint len = LuaAPI.xlua_objlen(L, n); if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to IList"); } for (int i = 0; i < len; ++i) { LuaAPI.lua_pushnumber(L, i + 1); LuaAPI.lua_rawget(L, idx); if (i < list.Count && target != null) { var item = elementCaster(L, n + 1, list[i]); if (item != null) { list[i] = item; } } else { var item = elementCaster(L, n + 1, null); if (item != null) { list.Add(item); } } LuaAPI.lua_pop(L, 1); } return obj; }); } else if (typeof(IDictionary).IsAssignableFrom(type) && type.IsGenericType) { ObjectCast keyCaster = GetCaster(type.GetGenericArguments()[0]); ObjectCast valueCaster = GetCaster(type.GetGenericArguments()[1]); return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } IDictionary dic = (target == null ? Activator.CreateInstance(type) : target) as IDictionary; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index LuaAPI.lua_pushnil(L); if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to IDictionary"); } while (LuaAPI.lua_next(L, idx) != 0) { object k = keyCaster(L, n + 1, null); // -2:key if (k != null) { object v = valueCaster(L, n + 2, !dic.Contains(k) ? null : dic[k]); if (v != null) { dic[k] = v; // -1:value } } LuaAPI.lua_pop(L, 1); // removes value, keeps key for next iteration } return dic; }); } else if ((type.IsClass && type.GetConstructor(System.Type.EmptyTypes) != null) || (type.IsValueType && !type.IsEnum)) //class has default construtor { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } obj = target == null?Activator.CreateInstance(type) : target; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to " + type); } /*foreach (PropertyInfo prop in type.GetProperties()) * { * LuaAPI.xlua_pushasciistring(L, prop.Name); * LuaAPI.lua_rawget(L, idx); * if (!LuaAPI.lua_isnil(L, -1)) * { * try * { * prop.SetValue(obj, GetCaster(prop.PropertyType)(L, n + 1, * target == null || prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(string) ? null : prop.GetValue(obj, null)), null); * } * catch (Exception e) * { * throw new Exception("exception in tran " + prop.Name + ", msg=" + e.Message); * } * } * LuaAPI.lua_pop(L, 1); * }*/ foreach (FieldInfo field in type.GetFields()) { LuaAPI.xlua_pushasciistring(L, field.Name); LuaAPI.lua_rawget(L, idx); if (!LuaAPI.lua_isnil(L, -1)) { try { field.SetValue(obj, GetCaster(field.FieldType)(L, n + 1, target == null || field.FieldType.IsPrimitive || field.FieldType == typeof(string) ? null : field.GetValue(obj))); } catch (Exception e) { throw new Exception("exception in tran " + field.Name + ", msg=" + e.Message); } } LuaAPI.lua_pop(L, 1); } return obj; }); } else { return(fixTypeGetter); } }
public static void ReflectionWrap(RealStatePtr L, Type type) { int top_enter = LuaAPI.lua_gettop(L); ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); //create obj meta table LuaAPI.luaL_getmetatable(L, type.FullName); if (LuaAPI.lua_isnil(L, -1)) { LuaAPI.lua_pop(L, 1); LuaAPI.luaL_newmetatable(L, type.FullName); } LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag()); LuaAPI.lua_pushnumber(L, 1); LuaAPI.lua_rawset(L, -3); int obj_meta = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_meta = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_field = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_getter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int obj_setter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_field = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_getter = LuaAPI.lua_gettop(L); LuaAPI.lua_newtable(L); int cls_setter = LuaAPI.lua_gettop(L); BindingFlags flag = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; FieldInfo[] fields = type.GetFields(flag); for (int i = 0; i < fields.Length; ++i) { FieldInfo field = fields[i]; if (field.IsStatic && (field.IsInitOnly || field.IsLiteral)) { LuaAPI.xlua_pushasciistring(L, field.Name); translator.PushAny(L, field.GetValue(null)); LuaAPI.lua_rawset(L, cls_field); } else { LuaAPI.xlua_pushasciistring(L, field.Name); translator.PushFixCSFunction(L, genFieldGetter(type, field)); LuaAPI.lua_rawset(L, field.IsStatic ? cls_getter : obj_getter); LuaAPI.xlua_pushasciistring(L, field.Name); translator.PushFixCSFunction(L, genFieldSetter(type, field)); LuaAPI.lua_rawset(L, field.IsStatic ? cls_setter : obj_setter); } } EventInfo[] events = type.GetEvents(flag); for (int i = 0; i < events.Length; ++i) { EventInfo eventInfo = events[i]; LuaAPI.xlua_pushasciistring(L, eventInfo.Name); translator.PushFixCSFunction(L, translator.methodWrapsCache.GetEventWrap(type, eventInfo.Name)); bool is_static = (eventInfo.GetAddMethod() != null) ? eventInfo.GetAddMethod().IsStatic : eventInfo.GetRemoveMethod().IsStatic; LuaAPI.lua_rawset(L, is_static ? cls_field : obj_field); } Dictionary <string, PropertyInfo> prop_map = new Dictionary <string, PropertyInfo>(); List <PropertyInfo> items = new List <PropertyInfo>(); PropertyInfo[] props = type.GetProperties(flag); for (int i = 0; i < props.Length; ++i) { PropertyInfo prop = props[i]; if (prop.Name == "Item") { items.Add(prop); } else { prop_map.Add(prop.Name, prop); } } var item_array = items.ToArray(); LuaCSFunction item_getter = item_array.Length > 0 ? genItemGetter(type, item_array) : null; LuaCSFunction item_setter = item_array.Length > 0 ? genItemSetter(type, item_array) : null;; MethodInfo[] methods = type.GetMethods(flag); Dictionary <MethodKey, List <MemberInfo> > pending_methods = new Dictionary <MethodKey, List <MemberInfo> >(); for (int i = 0; i < methods.Length; ++i) { MethodInfo method = methods[i]; string method_name = method.Name; MethodKey method_key = new MethodKey { Name = method_name, IsStatic = method.IsStatic }; List <MemberInfo> overloads; if (pending_methods.TryGetValue(method_key, out overloads)) { overloads.Add(method); continue; } PropertyInfo prop = null; if (method_name.StartsWith("add_") || method_name.StartsWith("remove_") || method_name == "get_Item" || method_name == "set_Item") { continue; } if (method_name.StartsWith("op_")) // 操作符 { if (support_op.ContainsKey(method_name)) { if (overloads == null) { overloads = new List <MemberInfo>(); pending_methods.Add(method_key, overloads); } overloads.Add(method); } continue; } else if (method_name.StartsWith("get_") && prop_map.TryGetValue(method.Name.Substring(4), out prop)) // getter of property { LuaAPI.xlua_pushasciistring(L, prop.Name); translator.PushFixCSFunction(L, genPropGetter(type, prop, method.IsStatic)); LuaAPI.lua_rawset(L, method.IsStatic ? cls_getter : obj_getter); } else if (method_name.StartsWith("set_") && prop_map.TryGetValue(method.Name.Substring(4), out prop)) // setter of property { LuaAPI.xlua_pushasciistring(L, prop.Name); translator.PushFixCSFunction(L, genPropSetter(type, prop, method.IsStatic)); LuaAPI.lua_rawset(L, method.IsStatic ? cls_setter : obj_setter); } else if (method_name == ".ctor" && method.IsConstructor) { continue; } else { if (overloads == null) { overloads = new List <MemberInfo>(); pending_methods.Add(method_key, overloads); } overloads.Add(method); } } foreach (var kv in pending_methods) { if (kv.Key.Name.StartsWith("op_")) // 操作符 { LuaAPI.xlua_pushasciistring(L, support_op[kv.Key.Name]); translator.PushFixCSFunction(L, new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(type, kv.Key.Name, kv.Value.ToArray()).Call)); LuaAPI.lua_rawset(L, obj_meta); } else { LuaAPI.xlua_pushasciistring(L, kv.Key.Name); translator.PushFixCSFunction(L, new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(type, kv.Key.Name, kv.Value.ToArray()).Call)); LuaAPI.lua_rawset(L, kv.Key.IsStatic ? cls_field : obj_field); } } IEnumerable <MethodInfo> extend_methods = GetExtensionMethodsOf(type); if (extend_methods != null) { foreach (var kv in (from extend_method in extend_methods select(MemberInfo) extend_method into member group member by member.Name)) { LuaAPI.xlua_pushasciistring(L, kv.Key); translator.PushFixCSFunction(L, new LuaCSFunction(translator.methodWrapsCache._GenMethodWrap(type, kv.Key, kv).Call)); LuaAPI.lua_rawset(L, obj_field); } } // init obj metatable LuaAPI.xlua_pushasciistring(L, "__gc"); LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.GcMeta); LuaAPI.lua_rawset(L, obj_meta); LuaAPI.xlua_pushasciistring(L, "__tostring"); LuaAPI.lua_pushstdcallcfunction(L, translator.metaFunctions.ToStringMeta); LuaAPI.lua_rawset(L, obj_meta); LuaAPI.xlua_pushasciistring(L, "__index"); LuaAPI.lua_pushvalue(L, obj_field); LuaAPI.lua_pushvalue(L, obj_getter); translator.PushFixCSFunction(L, item_getter); translator.PushAny(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.gen_obj_indexer(L); //store in lua indexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, obj_meta); // set __index LuaAPI.xlua_pushasciistring(L, "__newindex"); LuaAPI.lua_pushvalue(L, obj_setter); translator.PushFixCSFunction(L, item_setter); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.gen_obj_newindexer(L); //store in lua newindexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, obj_meta); // set __newindex //finish init obj metatable LuaAPI.xlua_pushasciistring(L, "UnderlyingSystemType"); translator.PushAny(L, type); LuaAPI.lua_rawset(L, cls_field); if (type != null && type.IsEnum) { LuaAPI.xlua_pushasciistring(L, "__CastFrom"); translator.PushFixCSFunction(L, genEnumCastFrom(type)); LuaAPI.lua_rawset(L, cls_field); } //set cls_field to namespace SetCSTable(L, type, cls_field); //finish set cls_field to namespace //init class meta LuaAPI.xlua_pushasciistring(L, "__index"); LuaAPI.lua_pushvalue(L, cls_getter); LuaAPI.lua_pushvalue(L, cls_field); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.gen_cls_indexer(L); //store in lua indexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaClassIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, cls_meta); // set __index LuaAPI.xlua_pushasciistring(L, "__newindex"); LuaAPI.lua_pushvalue(L, cls_setter); translator.Push(L, type.BaseType); LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.gen_cls_newindexer(L); //store in lua newindexs function tables LuaAPI.xlua_pushasciistring(L, Utils.LuaClassNewIndexsFieldName); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); translator.Push(L, type); LuaAPI.lua_pushvalue(L, -3); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_rawset(L, cls_meta); // set __newindex LuaCSFunction constructor = translator.methodWrapsCache.GetConstructorWrap(type); if (constructor == null) { constructor = (RealStatePtr LL) => { return(LuaAPI.luaL_error(LL, "No constructor for " + type)); }; } LuaAPI.xlua_pushasciistring(L, "__call"); translator.PushFixCSFunction(L, constructor); LuaAPI.lua_rawset(L, cls_meta); LuaAPI.lua_pushvalue(L, cls_meta); LuaAPI.lua_setmetatable(L, cls_field); LuaAPI.lua_pop(L, 8); System.Diagnostics.Debug.Assert(top_enter == LuaAPI.lua_gettop(L)); }