/// <summary> /// TODO 未完成 有GC的接口尽量少用 /// </summary> /// <param name="L"></param> /// <param name="stackPos"></param> /// <returns></returns> public static object ToVarObject(IntPtr L, int stackPos) { LuaTypes luaTypes = LuaDLL.lua_type(L, stackPos); switch (luaTypes) { case LuaTypes.LUA_TNUMBER: return(LuaDLL.lua_tonumber(L, stackPos)); case LuaTypes.LUA_TSTRING: return(LuaDLL.lua_tostring(L, stackPos)); case LuaTypes.LUA_TUSERDATA: return(null); case LuaTypes.LUA_TBOOLEAN: return(LuaDLL.lua_toboolean(L, stackPos)); case LuaTypes.LUA_TFUNCTION: return(ToLuaFunction(L, stackPos)); case LuaTypes.LUA_TTABLE: return(ToVarTable(L, stackPos)); case LuaTypes.LUA_TNIL: return(null); case LuaTypes.LUA_TLIGHTUSERDATA: return(null); case LuaTypes.LUA_TTHREAD: return(null); default: return(null); } }
public static bool CheckType(IntPtr L, Type t, int pos) { if (t == typeof(object)) { return(true); } t = TypeChecker.GetNullableType(t); switch (LuaDLL.lua_type(L, pos)) { case LuaTypes.LUA_TNIL: return(TypeChecker.IsNilType(t)); case LuaTypes.LUA_TBOOLEAN: return(t == typeof(bool)); case LuaTypes.LUA_TLIGHTUSERDATA: return(t == typeof(IntPtr) || t == typeof(UIntPtr)); case LuaTypes.LUA_TNUMBER: return(TypeChecker.IsNumberType(t)); case LuaTypes.LUA_TSTRING: return(t == typeof(string) || t == typeof(byte[]) || t == typeof(char[])); case LuaTypes.LUA_TTABLE: return(TypeChecker.IsUserTable(L, t, pos)); case LuaTypes.LUA_TFUNCTION: return(t == typeof(LuaFunction)); case LuaTypes.LUA_TUSERDATA: return(TypeChecker.IsMatchUserData(L, t, pos)); default: throw new LuaException("undefined type to check" + LuaDLL.luaL_typename(L, pos), null, 1); } }
static public bool CheckDelegateType(Type type, IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); switch (luaType) { case LuaTypes.LUA_TNIL: return(true); case LuaTypes.LUA_TUSERDATA: int udata = LuaDLL.tolua_rawnetobj(L, pos); if (udata != -1) { ObjectTranslator translator = ObjectTranslator.Get(L); object obj = translator.GetObject(udata); return(obj == null ? true : type == obj.GetType()); } return(false); default: return(false); } }
public static bool lua_isnil(IntPtr luaState, int index) { return(LuaDLL.lua_type(luaState, index) == LuaTypes.LUA_TNIL); }
public bool CheckBool(IntPtr L, int pos) { return(LuaDLL.lua_type(L, pos) == LuaTypes.LUA_TBOOLEAN); }
public static string luaL_typename(IntPtr luaState, int stackPos) { return(LuaDLL.lua_typenamestr(luaState, LuaDLL.lua_type(luaState, stackPos))); }
public bool CheckNumber(IntPtr L, int pos) { return(LuaDLL.lua_type(L, pos) == LuaTypes.LUA_TNUMBER); }
/// <summary> /// Tries to set a named property or field /// </summary> /// <param name="luaState"></param> /// <param name="targetType"></param> /// <param name="target"></param> /// <param name="bindingType"></param> /// <returns>false if unable to find the named member, true for success</returns> private bool trySetMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage) { detailMessage = null; // No error yet // If not already a string just return - we don't want to call tostring - which has the side effect of // changing the lua typecode to string // Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to // be true for isstring. if (LuaDLL.lua_type(luaState, 2) != LuaTypes.LUA_TSTRING) { detailMessage = "property names must be strings"; return(false); } // We only look up property names by string string fieldName = LuaDLL.lua_tostring(luaState, 2); if (fieldName == null || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_')) { detailMessage = "invalid property name"; return(false); } // Find our member via reflection or the cache MemberInfo member = (MemberInfo)checkMemberCache(memberCache, targetType, fieldName); if (member == null) { //CP: Removed NonPublic binding search and made case insensitive MemberInfo[] members = targetType.GetMember(fieldName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; setMemberCache(memberCache, targetType, fieldName, member); } else { detailMessage = "field or property '" + fieldName + "' does not exist"; return(false); } } if (member.MemberType == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; object val = translator.getAsType(luaState, 3, field.FieldType); try { field.SetValue(target, val); } catch (Exception e) { ThrowError(luaState, e); } // We did a call return(true); } else if (member.MemberType == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; object val = translator.getAsType(luaState, 3, property.PropertyType); try { property.SetValue(target, val, null); } catch (Exception e) { ThrowError(luaState, e); } // We did a call return(true); } detailMessage = "'" + fieldName + "' is not a .net field or property"; return(false); }
public bool CheckLuaFunc(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TFUNCTION || luaType == LuaTypes.LUA_TNIL); }
public bool lua_isnoneornil(int n) { return(LuaDLL.lua_type(L, n) <= LuaTypes.LUA_TNIL); }
public bool lua_isfunction(int n) { return(LuaDLL.lua_type(L, n) == LuaTypes.LUA_TFUNCTION); }
public bool lua_islightuserdata(int n) { return(LuaDLL.lua_type(L, n) == LuaTypes.LUA_TLIGHTUSERDATA); }
public bool CheckNullNumber(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TNUMBER || luaType == LuaTypes.LUA_TNIL); }
public bool CheckNullBool(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TBOOLEAN || luaType == LuaTypes.LUA_TNIL); }
public bool CheckByteBuffer(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TSTRING || luaType == LuaTypes.LUA_TNIL); }
public bool CheckLuaThread(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TTHREAD || luaType == LuaTypes.LUA_TNIL); }
public bool CheckLuaTable(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TTABLE || luaType == LuaTypes.LUA_TNIL); }
public static bool lua_isboolean(IntPtr luaState, int index) { return(LuaDLL.lua_type(luaState, index) == LuaTypes.LUA_TBOOLEAN); }
public bool lua_isthread(int n) { return(LuaDLL.lua_type(L, n) == LuaTypes.LUA_TTHREAD); }
public LuaTypes LuaType(int index) { return(LuaDLL.lua_type(L, index)); }
public bool CheckPtr(IntPtr L, int pos) { LuaTypes luaType = LuaDLL.lua_type(L, pos); return(luaType == LuaTypes.LUA_TLIGHTUSERDATA || luaType == LuaTypes.LUA_TNIL); }
public bool lua_istable(int n) { return(LuaDLL.lua_type(L, n) == LuaTypes.LUA_TTABLE); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; // Silently convert nullable types to their non null requics } if (paramType.Equals(typeof(object))) { return(extractObject); } else if (paramType.Equals(typeof(sbyte))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractSbyte); } } else if (paramType.Equals(typeof(byte))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractByte); } } else if (paramType.Equals(typeof(short))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractShort); } } else if (paramType.Equals(typeof(ushort))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractUshort); } } else if (paramType.Equals(typeof(int))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractInt); } } else if (paramType.Equals(typeof(uint))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractUint); } } else if (paramType.Equals(typeof(long))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractLong); } } else if (paramType.Equals(typeof(ulong))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractUlong); } } else if (paramType.Equals(typeof(char))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractChar); } } else if (paramType.Equals(typeof(float))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractFloat); } } else if (paramType.Equals(typeof(decimal))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractDecimal); } } else if (paramType.Equals(typeof(double))) { if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractDouble); } } else if (paramType.Equals(typeof(bool))) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(extractBoolean); } } else if (paramType.Equals(typeof(string))) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(extractString); } else if (luatype == LuaTypes.LUA_TNIL) { return(extractNetObject); // kevinh - silently convert nil to a null string pointer } } else if (paramType.Equals(typeof(LuaTable))) { if (luatype == LuaTypes.LUA_TTABLE) { return(extractTable); } } else if (paramType.Equals(typeof(LuaUserData))) { if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractUserdata); } } else if (paramType.Equals(typeof(LuaFunction))) { if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractFunction); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) { return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated)); } else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) { return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated)); } else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) { // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found return(extractNetObject); } else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { object obj = translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } else { return(null); } } else { object obj = translator.getNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }
public bool lua_isboolean(int n) { LuaTypes type = LuaDLL.lua_type(L, n); return(type == LuaTypes.LUA_TBOOLEAN || type == LuaTypes.LUA_TNIL); }
/* * Indexer for global variables from the LuaInterpreter * Supports navigation of tables by using . operator */ public object this[string fullPath] { get { object returnValue = null; int oldTop = LuaDLL.lua_gettop(L); string[] path = fullPath.Split(new char[] { '.' }); LuaDLL.lua_getglobal(L, path[0]); returnValue = translator.getObject(L, -1); if (path.Length > 1) { string[] remainingPath = new string[path.Length - 1]; Array.Copy(path, 1, remainingPath, 0, path.Length - 1); returnValue = getObject(remainingPath); } LuaDLL.lua_settop(L, oldTop); return(returnValue); } set { int oldTop = LuaDLL.lua_gettop(L); string[] path = fullPath.Split(new char[] { '.' }); if (path.Length == 1) { translator.push(L, value); LuaDLL.lua_setglobal(L, fullPath); } else { //LuaDLL.lua_getglobal(L, path[0]); LuaDLL.lua_rawglobal(L, path[0]); LuaTypes type = LuaDLL.lua_type(L, -1); if (type == LuaTypes.LUA_TNIL) { uLuaDebugger.LogError("Table {0} not exists", path[0]); LuaDLL.lua_settop(L, oldTop); return; } string[] remainingPath = new string[path.Length - 1]; Array.Copy(path, 1, remainingPath, 0, path.Length - 1); setObject(remainingPath, value); } LuaDLL.lua_settop(L, oldTop); // Globals auto-complete // comment by topameng, too many time cost, you shound register you type in other position /*if (value == null) * { * // Remove now obsolete entries * globals.Remove(fullPath); * } * else * { * // Add new entries * if (!globals.Contains(fullPath)) * registerGlobal(fullPath, value.GetType(), 0); * }*/ } }
public bool lua_isnone(int n) { return(LuaDLL.lua_type(L, n) == LuaTypes.LUA_TNONE); }
internal ExtractValue checkType(KopiLua.Lua.lua_State luaState, int stackPos, Type paramType) { LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; // Silently convert nullable types to their non null requics } long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(extractValues[runtimeHandleValue]); } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } else if (luatype == LuaTypes.LUA_TNIL) { return(extractNetObject); // kevinh - silently convert nil to a null string pointer } } else if (paramType == typeof(LuaTable)) { if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaUserData)) { if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaFunction)) { if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[runtimeHandleValue]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) { return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated)); } else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) { return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated)); } else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) { // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found return(extractNetObject); } else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { object obj = translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } else { return(null); } } else { object obj = translator.getNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }
/* ** =============================================================== ** some useful functions ** =============================================================== */ public static string luaL_typename(IntPtr luaState, int stackPos) { LuaTypes type = LuaDLL.lua_type(luaState, stackPos); return(lua_typename(luaState, type)); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; // Silently convert nullable types to their non null requics } long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(extractValues[runtimeHandleValue]); } //CP: Added support for generic parameters if (paramType.IsGenericParameter) { if (luatype == LuaTypes.LUA_TBOOLEAN) { return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TSTRING) { return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TNUMBER) { return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]); } //else // suppress CS0642 ; //an unsupported type was encountered } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } else if (luatype == LuaTypes.LUA_TNIL) { return(extractNetObject); // kevinh - silently convert nil to a null string pointer } } else if (paramType == typeof(LuaTable)) { if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaUserData)) { if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaFunction)) { if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[runtimeHandleValue]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) { #if __NOGEN__ translator.throwError(luaState, "Delegates not implemnented"); #else return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated)); #endif } else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) { #if __NOGEN__ translator.throwError(luaState, "Interfaces not implemnented"); #else return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated)); #endif } else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) { // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found return(extractNetObject); } else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { object obj = translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } else { return(null); } } else { object obj = translator.getNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }