MCPcopy Create free account
hub / github.com/Tencent/xLua / XLuaAccess

Function XLuaAccess

Assets/XLua/Src/StaticLuaCallbacks.cs:969–1032  ·  view source on GitHub ↗
(RealStatePtr L)

Source from the content-addressed store, hash-verified

967 }
968
969 [MonoPInvokeCallback(typeof(LuaCSFunction))]
970 public static int XLuaAccess(RealStatePtr L)
971 {
972 try
973 {
974 ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
975 Type type = getType(L, translator, 1);
976 object obj = null;
977 if (type == null && LuaAPI.lua_type(L, 1) == LuaTypes.LUA_TUSERDATA)
978 {
979 obj = translator.SafeGetCSObj(L, 1);
980 if (obj == null)
981 {
982 return LuaAPI.luaL_error(L, "xlua.access, #1 parameter must a type/c# object/string");
983 }
984 type = obj.GetType();
985 }
986
987 if (type == null)
988 {
989 return LuaAPI.luaL_error(L, "xlua.access, can not find c# type");
990 }
991
992 string fieldName = LuaAPI.lua_tostring(L, 2);
993
994 BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
995
996 if (LuaAPI.lua_gettop(L) > 2) // set
997 {
998 var field = type.GetField(fieldName, bindingFlags);
999 if (field != null)
1000 {
1001 field.SetValue(obj, translator.GetObject(L, 3, field.FieldType));
1002 return 0;
1003 }
1004 var prop = type.GetProperty(fieldName, bindingFlags);
1005 if (prop != null)
1006 {
1007 prop.SetValue(obj, translator.GetObject(L, 3, prop.PropertyType), null);
1008 return 0;
1009 }
1010 }
1011 else
1012 {
1013 var field = type.GetField(fieldName, bindingFlags);
1014 if (field != null)
1015 {
1016 translator.PushAny(L, field.GetValue(obj));
1017 return 1;
1018 }
1019 var prop = type.GetProperty(fieldName, bindingFlags);
1020 if (prop != null)
1021 {
1022 translator.PushAny(L, prop.GetValue(obj, null));
1023 return 1;
1024 }
1025 }
1026 return LuaAPI.luaL_error(L, "xlua.access, no field " + fieldName);

Callers

nothing calls this directly

Calls 9

FindMethod · 0.80
lua_typeMethod · 0.80
SafeGetCSObjMethod · 0.80
luaL_errorMethod · 0.80
lua_tostringMethod · 0.80
lua_gettopMethod · 0.80
GetObjectMethod · 0.80
PushAnyMethod · 0.80
getTypeFunction · 0.70

Tested by

no test coverage detected