Exemple #1
0
 public static void Push <T>(lua_State L, T v) => SharpObject.PushObject(L, v);
Exemple #2
0
        public static void PushT <T>(lua_State L, T v)
        {
            switch (v)
            {
            case bool bval:
                lua_pushboolean(L, bval ? 1 : 0);
                break;

            case long lval:
                lua_pushinteger(L, lval);
                break;

            case ulong lval:
                lua_pushinteger(L, (long)lval);
                break;

            case IntPtr lval:
                lua_pushinteger(L, (long)lval);
                break;

            case UIntPtr lval:
                lua_pushinteger(L, (long)lval);
                break;

            case sbyte ival:
                lua_pushnumber(L, ival);
                break;

            case byte ival:
                lua_pushnumber(L, ival);
                break;

            case short ival:
                lua_pushnumber(L, ival);
                break;

            case ushort ival:
                lua_pushnumber(L, ival);
                break;

            case int ival:
                lua_pushnumber(L, ival);
                break;

            case uint ival:
                lua_pushnumber(L, ival);
                break;

            case float fval:
                lua_pushnumber(L, fval);
                break;

            case double dval:
                lua_pushnumber(L, dval);
                break;

            case string strval:
                lua_pushstring(L, strval);
                break;

            case LuaNativeFunction fn:
                lua_pushcfunction(L, fn);
                break;

            case LuaRef luaRef:
                if (luaRef.IsValid)
                {
                    luaRef.PushToStack();
                }
                else
                {
                    lua_pushnil(L);
                }
                break;

            case byte[] bytes:
                Push(L, bytes);
                break;

            case LuaByteBuffer bytes:
                Push(L, bytes);
                break;

            default:
                if (v == null)
                {
                    lua_pushnil(L);
                    break;
                }
                Type t = v.GetType();
                if (t.IsEnum)
                {
                    //SharpObject.PushToStack(L, v);
                    lua_pushinteger(L, (int)(object)v);
                    return;
                }
                else if (t.IsValueType)
                {
                    SharpObject.PushValueType(L, v);
                }
                else
                {
                    SharpObject.PushObject(L, v);
                }
                break;
            }
        }
Exemple #3
0
        public static void Push(lua_State L, object obj)
        {
            Type t = obj.GetType();

            if (t.IsPrimitive)
            {
                if (t == typeof(bool))
                {
                    lua_pushboolean(L, ((bool)obj) ? 1 : 0);
                }
                else if (t == typeof(long))
                {
                    lua_pushinteger(L, (long)obj);
                }
                else if (t == typeof(ulong))
                {
                    lua_pushinteger(L, (long)(ulong)obj);
                }
                else if (t == typeof(sbyte))
                {
                    lua_pushinteger(L, (long)(sbyte)obj);
                }
                else if (t == typeof(byte))
                {
                    lua_pushnumber(L, (double)(byte)obj);
                }
                else if (t == typeof(short))
                {
                    lua_pushnumber(L, (double)(short)obj);
                }
                else if (t == typeof(ushort))
                {
                    lua_pushnumber(L, (double)(ushort)obj);
                }
                else if (t == typeof(char))
                {
                    lua_pushnumber(L, (double)(char)obj);
                }
                else if (t == typeof(int))
                {
                    lua_pushnumber(L, (double)(int)obj);
                }
                else if (t == typeof(uint))
                {
                    lua_pushnumber(L, (double)(uint)obj);
                }
                else if (t == typeof(float))
                {
                    lua_pushnumber(L, (double)(float)obj);
                }
                else if (t == typeof(double))
                {
                    lua_pushnumber(L, (double)obj);
                }
                else
                {
                    throw new Exception("未知类型");
                }
            }
            else if (t == typeof(IntPtr))
            {
                lua_pushinteger(L, (long)(IntPtr)obj);
            }
            else if (t == typeof(UIntPtr))
            {
                lua_pushinteger(L, (long)(UIntPtr)obj);
            }
            else if (t == typeof(string))
            {
                lua_pushstring(L, (string)obj);
            }
            else if (t == typeof(LuaNativeFunction))
            {
                lua_pushcfunction(L, (LuaNativeFunction)obj);
            }
            else if (t == typeof(byte[]))
            {
                Push(L, (byte[])obj);
            }
            else if (t == typeof(LuaByteBuffer))
            {
                Push(L, (LuaByteBuffer)obj);
            }
            else if (t == typeof(LuaRef))
            {
                var luaRef = (LuaRef)obj;
                if (luaRef.IsValid)
                {
                    luaRef.PushToStack();
                }
                else
                {
                    lua_pushnil(L);
                }
            }
            else
            {
                if (t.IsEnum)
                {
                    lua_pushinteger(L, (int)(object)obj);
                    //SharpObject.PushToStack(L, obj);
                }
                else if (t.IsValueType)
                {
                    SharpObject.PushValueType(L, obj);
                }
                else
                {
                    SharpObject.PushObject(L, obj);
                }
            }
        }