Use lua_tointeger() instead of casting the result of lua_tonumber().