Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Kirill Shcherbatov <kshcherbatov@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: Re: [PATCH v3 5/6] box: refactor box_lua_find helper
Date: Tue, 18 Jun 2019 17:22:04 +0300	[thread overview]
Message-ID: <20190618142204.enm7ey52ts6djpx3@esperanza> (raw)
In-Reply-To: <f169ad138c2376699a5f4bde61a0bdc5b2ffb63b.1560433806.git.kshcherbatov@tarantool.org>

On Thu, Jun 13, 2019 at 05:08:25PM +0300, Kirill Shcherbatov wrote:
> The box_lua_find routine used to work with an empty stack only.
> It is unacceptable in following patches because this helper
> need to be reused in following patches for environment table
> construction.
> 
> Refactored function is renamed to luaT_func_find.
> 
> Needed for #4182, #1260
> ---
>  src/box/lua/call.c | 54 +++++++++++++++++++++++++---------------------
>  1 file changed, 30 insertions(+), 24 deletions(-)
> 
> diff --git a/src/box/lua/call.c b/src/box/lua/call.c
> index 23eb29eb8..90c28a160 100644
> --- a/src/box/lua/call.c
> +++ b/src/box/lua/call.c
> @@ -54,21 +54,19 @@
>   * on top of the stack.
>   */
>  static int
> -box_lua_find(lua_State *L, const char *name, const char *name_end)
> +luaT_func_find(lua_State *L, const char *name, const char *name_end,
> +	       int *count)

Don't see any point in renaming it or using luaT_ prefix for this and
other helper functions in this compilation unit - other functions in the
same file have box_ prefix, e.g. box_process_lua. I may be wrong, but it
looks like luaT_ prefix is primarily used for generic helper functions
from util.c.

>  {
>  	int index = LUA_GLOBALSINDEX;
> -	int objstack = 0;
> +	int objstack = 0, top = lua_gettop(L);
>  	const char *start = name, *end;
>  
>  	while ((end = (const char *) memchr(start, '.', name_end - start))) {
>  		lua_checkstack(L, 3);
>  		lua_pushlstring(L, start, end - start);
>  		lua_gettable(L, index);
> -		if (! lua_istable(L, -1)) {
> -			diag_set(ClientError, ER_NO_SUCH_PROC,
> -				 name_end - name, name);

Why drop diag_set? Looking at the following patch, I see that you want
to set another error when you create a sandbox. Well, it's okay to
overwrite a diag. Moreover, we have plans to implement stacked diag so
that instead of overwriting the error, subsequent diag_set would append
a new error to the diag and we could print something like a stack trace
leading to the error.

> -			luaT_error(L);
> -		}
> +		if (! lua_istable(L, -1))
> +			return -1;
>  		start = end + 1; /* next piece of a.b.c */
>  		index = lua_gettop(L); /* top of the stack */
>  	}
> @@ -95,25 +90,27 @@ box_lua_find(lua_State *L, const char *name, const char *name_end)
>  	if (!lua_isfunction(L, -1) && !lua_istable(L, -1)) {
>  		/* lua_call or lua_gettable would raise a type error
>  		 * for us, but our own message is more verbose. */
> -		diag_set(ClientError, ER_NO_SUCH_PROC,
> -			  name_end - name, name);
> -		luaT_error(L);
> +		return -1;
>  	}
> +
>  	/* setting stack that it would contain only
>  	 * the function pointer. */
>  	if (index != LUA_GLOBALSINDEX) {
>  		if (objstack == 0) {        /* no object, only a function */
> -			lua_replace(L, 1);
> +			lua_replace(L, top + 1);
> +			lua_pop(L, lua_gettop(L) - top - 1);
>  		} else if (objstack == 1) { /* just two values, swap them */
>  			lua_insert(L, -2);
> +			lua_pop(L, lua_gettop(L) - top - 2);
>  		} else {		    /* long path */
> -			lua_insert(L, 1);
> -			lua_insert(L, 2);
> +			lua_insert(L, top + 1);
> +			lua_insert(L, top + 2);
> +			lua_pop(L, objstack - 1);
>  			objstack = 1;
>  		}
> -		lua_settop(L, 1 + objstack);
>  	}
> -	return 1 + objstack;
> +	*count = 1 + objstack;
> +	return 0;

Why change the return value? We can return -1 on error, >= 0 on success.
IMHO better than returning the value by pointer.

  reply	other threads:[~2019-06-18 14:22 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-13 14:08 [PATCH v3 0/6] box: rework functions machinery Kirill Shcherbatov
2019-06-13 14:08 ` [PATCH v3 1/6] box: rework func cache update machinery Kirill Shcherbatov
2019-06-18 10:52   ` Vladimir Davydov
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-13 14:08 ` [PATCH v3 2/6] box: rework box_lua_{call, eval} to use input port Kirill Shcherbatov
2019-06-17  9:35   ` [tarantool-patches] " Konstantin Osipov
2019-06-17 10:27     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-18 12:12   ` Vladimir Davydov
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-19 16:11       ` Vladimir Davydov
2019-06-18 13:58   ` Vladimir Davydov
2019-06-19 18:28   ` [tarantool-patches] " Konstantin Osipov
2019-06-20  7:53     ` Kirill Shcherbatov
2019-06-20  8:09       ` Konstantin Osipov
2019-06-20  8:44         ` [tarantool-patches] " Kirill Shcherbatov
2019-06-19 18:30   ` [tarantool-patches] " Konstantin Osipov
2019-06-13 14:08 ` [PATCH v3 3/6] box: rework func object as a function frontend Kirill Shcherbatov
2019-06-18 13:23   ` Vladimir Davydov
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-13 14:08 ` [PATCH v3 4/6] box: export registered functions in box.func folder Kirill Shcherbatov
2019-06-18 14:06   ` Vladimir Davydov
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-18 16:11   ` Vladimir Davydov
2019-06-13 14:08 ` [PATCH v3 5/6] box: refactor box_lua_find helper Kirill Shcherbatov
2019-06-18 14:22   ` Vladimir Davydov [this message]
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov
2019-06-13 14:08 ` [PATCH v3 6/6] box: introduce Lua persistent functions Kirill Shcherbatov
2019-06-18 16:23   ` Vladimir Davydov
2019-06-19 15:51     ` [tarantool-patches] " Kirill Shcherbatov

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=20190618142204.enm7ey52ts6djpx3@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH v3 5/6] box: refactor box_lua_find helper' \
    /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