Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org, imeevma@tarantool.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] Re: [PATCH v2 1/2] lua: new function luaT_return_error()
Date: Wed, 31 Jul 2019 19:15:13 +0200	[thread overview]
Message-ID: <2d0ba195-f5cb-bd23-6340-1426b506c82f@tarantool.org> (raw)
In-Reply-To: <f01ecd3a3b0e8d5af80767fa3753bd7ad985191f.1564568388.git.imeevma@gmail.com>

Hi! Thanks for the patch!

> diff --git a/src/lua/fio.c b/src/lua/fio.c
> index 55fa667..cb7e31e 100644
> --- a/src/lua/fio.c
> +++ b/src/lua/fio.c
> @@ -47,11 +47,9 @@
>  #include "lua/utils.h"
>  #include "coio_file.h"
>  
> -static inline void
> -lbox_fio_pushsyserror(struct lua_State *L)
> -{
> -	diag_set(SystemError, "fio: %s", strerror(errno));
> -	luaT_pusherror(L, diag_get()->last);
> +#define lbox_fio_pushsyserror(L) {				\
> +	diag_set(SystemError, "fio: %s", strerror(errno));	\
> +	return luaT_return_error(L);				\
>  }

Please, don't return here. It makes the code below
confusing, when you call 'pushsyserror, pushinteger', and
return 1. Make this macro return value of the last block,
and surround it with '()' or with 'do {...} while(false)'
to be force ';' after it, and be able to use it in
'if ... else ...' constructions. It is a standard practice.

#define lbox_fio_pushsyserror(L) ({				\
	diag_set(SystemError, "fio: %s", strerror(errno));	\
	luaT_return_error(L);					\
})

and use 'return lbox_fio_pushsyserror(L);' in the code
below.

>  
>  static int
> @@ -69,11 +67,8 @@ usage:
>  	int mode = lua_tointeger(L, 3);
>  
>  	int fh = coio_file_open(pathname, flags, mode);
> -	if (fh < 0) {
> -		lua_pushnil(L);
> +	if (fh < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushinteger(L, fh);
>  	return 1;
>  }
> @@ -90,11 +85,8 @@ lbox_fio_pwrite(struct lua_State *L)
>  	size_t offset = lua_tonumber(L, 4);
>  
>  	int res = coio_pwrite(fh, buf, len, offset);
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushinteger(L, res);
>  	return 1;
>  }
> @@ -115,11 +107,8 @@ lbox_fio_pread(struct lua_State *L)
>  
>  	int res = coio_pread(fh, buf, len, offset);
>  
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushinteger(L, res);
>  	return 1;
>  }
> @@ -129,7 +118,10 @@ lbox_fio_pushbool(struct lua_State *L, bool res)
>  {
>  	lua_pushboolean(L, res);
>  	if (!res) {
> -		lbox_fio_pushsyserror(L);
> +		diag_set(SystemError, "fio: %s", strerror(errno));
> +		struct error *e = diag_last_error(diag_get());
> +		assert(e != NULL);
> +		luaT_pusherror(L, e);
>  		return 2;
>  	}
>  	return 1;
> @@ -211,11 +203,8 @@ lbox_fio_write(struct lua_State *L)
>  	size_t len = lua_tonumber(L, 3);
>  
>  	int res = coio_write(fh, buf, len);
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushinteger(L, res);
>  	return 1;
>  }
> @@ -298,11 +287,8 @@ lbox_fio_read(struct lua_State *L)
>  
>  	int res = coio_read(fh, buf, len);
>  
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushinteger(L, res);
>  	return 1;
>  }
> @@ -371,11 +357,8 @@ DEF_STAT_METHOD(is_sock, S_ISSOCK);
>  static int
>  lbox_fio_pushstat(struct lua_State *L, int res, const struct stat *stat)
>  {
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_newtable(L);
>  
>  	PUSHTABLE("dev", lua_pushinteger, stat->st_dev);
> @@ -518,11 +501,8 @@ lbox_fio_listdir(struct lua_State *L)
>  	}
>  	pathname = lua_tostring(L, 1);
>  	char *buf;
> -	if (coio_readdir(pathname, &buf) < 0) {
> -		lua_pushnil(L);
> +	if (coio_readdir(pathname, &buf) < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushstring(L, buf);
>  	free(buf);
>  	return 1;
> @@ -613,11 +593,8 @@ usage:
>  		goto usage;
>  	char *path = (char *)lua_newuserdata(L, PATH_MAX);
>  	int res = coio_readlink(pathname, path, PATH_MAX);
> -	if (res < 0) {
> -		lua_pushnil(L);
> +	if (res < 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushlstring(L, path, res);
>  	lua_remove(L, -2);
>  	return 1;
> @@ -629,16 +606,11 @@ lbox_fio_tempdir(struct lua_State *L)
>  	char *buf = (char *)lua_newuserdata(L, PATH_MAX);
>  	if (!buf) {
>  		errno = ENOMEM;
> -		lua_pushnil(L);
>  		lbox_fio_pushsyserror(L);
> -		return 2;
>  	}
>  
> -	if (coio_tempdir(buf, PATH_MAX) != 0) {
> -		lua_pushnil(L);
> +	if (coio_tempdir(buf, PATH_MAX) != 0)
>  		lbox_fio_pushsyserror(L);
> -		return 2;
> -	}
>  	lua_pushstring(L, buf);
>  	lua_remove(L, -2);
>  	return 1;
> @@ -650,20 +622,12 @@ lbox_fio_cwd(struct lua_State *L)
>  	char *buf = (char *)lua_newuserdata(L, PATH_MAX);
>  	if (!buf) {
>  		errno = ENOMEM;
> -		lua_pushnil(L);
>  		lbox_fio_pushsyserror(L);
> -		return 2;
>  	}
> -
> -
> -	if (getcwd(buf, PATH_MAX)) {
> -		lua_pushstring(L, buf);
> -		lua_remove(L, -2);
> -	} else {
> +	if (getcwd(buf, PATH_MAX) == NULL)
>  		lbox_fio_pushsyserror(L);
> -		lua_pushnil(L);
> -		return 2;
> -	}
> +	lua_pushstring(L, buf);
> +	lua_remove(L, -2);
>  	return 1;
>  }

  parent reply	other threads:[~2019-07-31 17:13 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-31 10:32 [tarantool-patches] [PATCH v2 0/2] sql: rework error handling in box.execute() imeevma
2019-07-31 10:32 ` [tarantool-patches] [PATCH v2 1/2] lua: new function luaT_return_error() imeevma
2019-07-31 15:23   ` [tarantool-patches] " Alexander Turenko
2019-07-31 15:39     ` Konstantin Osipov
2019-07-31 15:40     ` Vladislav Shpilevoy
2019-07-31 16:20       ` Imeev Mergen
2019-07-31 17:00         ` Vladislav Shpilevoy
2019-07-31 19:33           ` Konstantin Osipov
2019-08-01  8:35           ` Alexander Turenko
2019-07-31 19:32         ` Konstantin Osipov
2019-07-31 17:15   ` Vladislav Shpilevoy [this message]
2019-07-31 22:16     ` Mergen Imeev
2019-08-01 20:03       ` Vladislav Shpilevoy
2019-08-01  8:59     ` Mergen Imeev
2019-07-31 10:32 ` [tarantool-patches] [PATCH v2 2/2] sql: rework error handling in box.execute() imeevma
2019-07-31 22:23   ` [tarantool-patches] " Mergen Imeev
2019-08-02  5:39 ` [tarantool-patches] Re: [PATCH v2 0/2] " Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2d0ba195-f5cb-bd23-6340-1426b506c82f@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH v2 1/2] lua: new function luaT_return_error()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox