Esempio n. 1
0
        internal ExtractValue checkType(LuaCore.lua_State luaState, int stackPos, Type paramType)
        {
            var luatype = LuaLib.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            var underlyingType = Nullable.GetUnderlyingType(paramType);

            if (!underlyingType.IsNull())
            {
                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.Boolean)
                {
                    return(extractValues [typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.String)
                {
                    return(extractValues [typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Table)
                {
                    return(extractValues [typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.UserData)
                {
                    return(extractValues [typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Function)
                {
                    return(extractValues [typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.Number)
                {
                    return(extractValues [typeof(double).TypeHandle.Value.ToInt64()]);
                }
            }

            if (LuaLib.lua_isnumber(luaState, stackPos))
            {
                return(extractValues [runtimeHandleValue]);
            }

            if (paramType == typeof(bool))
            {
                if (LuaLib.lua_isboolean(luaState, stackPos))
                {
                    return(extractValues [runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaLib.lua_isstring(luaState, stackPos))
                {
                    return(extractValues [runtimeHandleValue]);
                }
                else if (luatype == LuaTypes.Nil)
                {
                    return(extractNetObject);                    // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.Table)
                {
                    return(extractValues [runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaUserData))
            {
                if (luatype == LuaTypes.UserData)
                {
                    return(extractValues [runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.Function)
                {
                    return(extractValues [runtimeHandleValue]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.Function)
            {
                return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated));
            }
            else if (paramType.IsInterface && luatype == LuaTypes.Table)
            {
                return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated));
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.Nil)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaLib.lua_type(luaState, stackPos) == LuaTypes.Table)
            {
                if (LuaLib.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaLib.lua_settop(luaState, -2);
                    if (!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                object obj = translator.getNetObject(luaState, stackPos);
                if (!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }
Esempio n. 2
0
        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
                {
                    ;//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(Color))
            {
                return(extractValues[runtimeHandleValue]);
            }
            else if (paramType == typeof(string) || paramType == typeof(char[]))
            {
                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);
        }