Beispiel #1
0
        public object getAsObject(KopiLua.LuaState luaState, int stackPos)
        {
            if (LuaDll.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaDll.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    if (LuaDll.luaL_checkmetatable(luaState, -1))
                    {
                        LuaDll.lua_insert(luaState, stackPos);
                        LuaDll.lua_remove(luaState, stackPos + 1);
                    }
                    else
                    {
                        LuaDll.lua_settop(luaState, -2);
                    }
                }
            }
            object obj = translator.getObject(luaState, stackPos);

            return(obj);
        }
Beispiel #2
0
        internal ExtractValue checkType(KopiLua.LuaState 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);
        }