Exemple #1
0
        internal void PushObject(object obj, IntPtr?stateOverride = null)
        {
            var state = stateOverride ?? MainState;

            Debug.Assert(LuaApi.GetMainState(state) == MainState, "State override did not match main state.");

            if (obj == null)
            {
                LuaApi.PushNil(state);
                return;
            }

            if (obj is IConvertible conv)
            {
                switch (conv.GetTypeCode())
                {
                case TypeCode.Boolean:
                    LuaApi.PushBoolean(state, (bool)obj);
                    return;

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    LuaApi.PushInteger(state, conv.ToInt64(null));
                    return;

                case TypeCode.UInt64:
                    LuaApi.PushInteger(state, (long)((ulong)conv));
                    return;

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    LuaApi.PushNumber(state, conv.ToDouble(null));
                    return;

                case TypeCode.Char:
                case TypeCode.String:
                    LuaApi.PushString(state, conv.ToString());
                    return;

                default:
                    ObjectBinder.PushNetObject(state, conv);
                    return;
                }
            }
            else if (obj is LuaReference luaReference)
            {
                luaReference.PushOnto(state);
            }
            else
            {
                ObjectBinder.PushNetObject(state, obj);
            }
        }