From: Kirill Shcherbatov <kshcherbatov@tarantool.org> To: tarantool-patches@freelists.org, vdavydov.dev@gmail.com Cc: Kirill Shcherbatov <kshcherbatov@tarantool.org> Subject: [PATCH v4 5/6] box: refactor box_lua_find helper Date: Sun, 23 Jun 2019 16:57:56 +0300 [thread overview] Message-ID: <c3608f451322f9cd4626bbeb5fd548fc5c322ab6.1561298197.git.kshcherbatov@tarantool.org> (raw) In-Reply-To: <cover.1561298197.git.kshcherbatov@tarantool.org> 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. Needed for #4182, #1260 --- src/box/lua/call.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/box/lua/call.c b/src/box/lua/call.c index 8c70c1088..f98ab42ac 100644 --- a/src/box/lua/call.c +++ b/src/box/lua/call.c @@ -56,7 +56,7 @@ static int box_lua_find(lua_State *L, const char *name, const char *name_end) { 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))) { @@ -66,7 +66,7 @@ box_lua_find(lua_State *L, const char *name, const char *name_end) if (! lua_istable(L, -1)) { diag_set(ClientError, ER_NO_SUCH_PROC, name_end - name, name); - luaT_error(L); + return -1; } start = end + 1; /* next piece of a.b.c */ index = lua_gettop(L); /* top of the stack */ @@ -81,11 +81,12 @@ box_lua_find(lua_State *L, const char *name, const char *name_end) lua_islightuserdata(L, -1) || lua_isuserdata(L, -1) )) { diag_set(ClientError, ER_NO_SUCH_PROC, name_end - name, name); - luaT_error(L); + return -1; } + start = end + 1; /* next piece of a.b.c */ index = lua_gettop(L); /* top of the stack */ - objstack = index; + objstack = index - top; } @@ -96,21 +97,24 @@ box_lua_find(lua_State *L, const char *name, const char *name_end) * 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; } @@ -128,7 +132,10 @@ lbox_call_loadproc(struct lua_State *L) const char *name; size_t name_len; name = lua_tolstring(L, 1, &name_len); - return box_lua_find(L, name, name + name_len); + int count = box_lua_find(L, name, name + name_len); + if (count < 0) + return luaT_error(L); + return count; } /* @@ -307,9 +314,10 @@ execute_lua_call(lua_State *L) const char *name = ctx->name; uint32_t name_len = ctx->name_len; - int oc = 0; /* how many objects are on stack after box_lua_find */ - /* Try to find a function by name in Lua */ - oc = box_lua_find(L, name, name + name_len); + /* How many objects are on stack after box_lua_find. */ + int oc = box_lua_find(L, name, name + name_len); + if (oc < 0) + return luaT_error(L); /* Push the rest of args (a tuple). */ int top = lua_gettop(L); -- 2.21.0
next prev parent reply other threads:[~2019-06-23 13:57 UTC|newest] Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-06-23 13:57 [PATCH v4 0/6] box: rework functions machinery Kirill Shcherbatov 2019-06-23 13:57 ` [PATCH v4 1/6] box: rework func cache update machinery Kirill Shcherbatov 2019-06-24 10:22 ` Vladimir Davydov 2019-06-23 13:57 ` [PATCH v4 2/6] box: rework box_lua_{call, eval} to use input port Kirill Shcherbatov 2019-06-24 10:23 ` Vladimir Davydov 2019-06-23 13:57 ` [PATCH v4 3/6] box: rework func object as a function frontend Kirill Shcherbatov 2019-06-24 10:23 ` Vladimir Davydov 2019-06-23 13:57 ` [PATCH v4 4/6] box: export registered functions in box.func folder Kirill Shcherbatov 2019-06-24 10:25 ` Vladimir Davydov 2019-06-23 13:57 ` Kirill Shcherbatov [this message] 2019-06-24 12:35 ` [PATCH v4 5/6] box: refactor box_lua_find helper Vladimir Davydov 2019-06-23 13:57 ` [PATCH v4 6/6] box: introduce Lua persistent functions Kirill Shcherbatov 2019-06-24 12:38 ` Vladimir Davydov 2019-06-25 8:22 ` [tarantool-patches] " Konstantin Osipov
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=c3608f451322f9cd4626bbeb5fd548fc5c322ab6.1561298197.git.kshcherbatov@tarantool.org \ --to=kshcherbatov@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH v4 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