/*
  * Pushes the object into the Lua stack according to its type.
  */
 internal void push(LuaCore.lua_State luaState, object o)
 {
     if (o.IsNull())
     {
         LuaLib.lua_pushnil(luaState);
     }
     else if (o is sbyte || o is byte || o is short || o is ushort ||
              o is int || o is uint || o is long || o is float ||
              o is ulong || o is decimal || o is double)
     {
         double d = Convert.ToDouble(o);
         LuaLib.lua_pushnumber(luaState, d);
     }
     else if (o is char)
     {
         double d = (char)o;
         LuaLib.lua_pushnumber(luaState, d);
     }
     else if (o is string)
     {
         string str = (string)o;
         LuaLib.lua_pushstring(luaState, str);
     }
     else if (o is bool)
     {
         bool b = (bool)o;
         LuaLib.lua_pushboolean(luaState, b);
     }
     else if (IsILua(o))
     {
         (((ILuaGeneratedType)o).__luaInterface_getLuaTable()).push(luaState);
     }
     else if (o is LuaTable)
     {
         ((LuaTable)o).push(luaState);
     }
     else if (o is LuaCore.lua_CFunction)
     {
         pushFunction(luaState, (LuaCore.lua_CFunction)o);
     }
     else if (o is LuaFunction)
     {
         ((LuaFunction)o).push(luaState);
     }
     else
     {
         pushObject(luaState, o, "luaNet_metatable");
     }
 }
        /*
         * Pushes a new object into the Lua stack with the provided
         * metatable
         */
        private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable)
        {
            if (metatable == "luaNet_metatable")
            {
                // Gets or creates the metatable for the object's type
                LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName);

                if (LuaLib.lua_isnil(luaState, -1))
                {
                    LuaLib.lua_settop(luaState, -2);
                    LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName);
                    LuaLib.lua_pushstring(luaState, "cache");
                    LuaLib.lua_newtable(luaState);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag());
                    LuaLib.lua_pushnumber(luaState, 1);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__index");
                    LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
                    LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__gc");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__tostring");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
                    LuaLib.lua_rawset(luaState, -3);
                    LuaLib.lua_pushstring(luaState, "__newindex");
                    LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
                    LuaLib.lua_rawset(luaState, -3);
                }
            }
            else
            {
                LuaLib.luaL_getmetatable(luaState, metatable);
            }

            // Stores the object index in the Lua list and pushes the
            // index into the Lua stack
            LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
            LuaLib.luanet_newudata(luaState, index);
            LuaLib.lua_pushvalue(luaState, -3);
            LuaLib.lua_remove(luaState, -4);
            LuaLib.lua_setmetatable(luaState, -2);
            LuaLib.lua_pushvalue(luaState, -1);
            LuaLib.lua_rawseti(luaState, -3, index);
            LuaLib.lua_remove(luaState, -2);
        }