From: Alexander Turenko <alexander.turenko@tarantool.org> To: Vladimir Davydov <vdavydov.dev@gmail.com> Cc: Alexander Turenko <alexander.turenko@tarantool.org>, tarantool-patches@freelists.org Subject: [PATCH v2 2/6] Add functions to ease using Lua iterators from C Date: Wed, 9 Jan 2019 23:20:10 +0300 [thread overview] Message-ID: <2c8049118f86934d6088636dba44f92f381ef8ff.1547064388.git.alexander.turenko@tarantool.org> (raw) In-Reply-To: <cover.1547064388.git.alexander.turenko@tarantool.org> Needed for #3276. --- src/lua/utils.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lua/utils.h | 28 +++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/lua/utils.c b/src/lua/utils.c index eefb860ee..4d1eee6ab 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -969,6 +969,72 @@ luaT_state(void) return tarantool_L; } +/* {{{ Helper functions to interact with a Lua iterator from C */ + +struct luaL_iterator { + int gen; + int param; + int state; +}; + +struct luaL_iterator * +luaL_iterator_new_fromtable(lua_State *L, int idx) +{ + struct luaL_iterator *it = (struct luaL_iterator *) malloc( + sizeof(struct luaL_iterator)); + + lua_rawgeti(L, idx, 1); /* Popped by luaL_ref(). */ + it->gen = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, idx, 2); /* Popped by luaL_ref(). */ + it->param = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, idx, 3); /* Popped by luaL_ref(). */ + it->state = luaL_ref(L, LUA_REGISTRYINDEX); + + return it; +} + +int +luaL_iterator_next(lua_State *L, struct luaL_iterator *it) +{ + int frame_start = lua_gettop(L); + + /* Call gen(param, state). */ + lua_rawgeti(L, LUA_REGISTRYINDEX, it->gen); + lua_rawgeti(L, LUA_REGISTRYINDEX, it->param); + lua_rawgeti(L, LUA_REGISTRYINDEX, it->state); + lua_call(L, 2, LUA_MULTRET); + int nresults = lua_gettop(L) - frame_start; + if (nresults == 0) { + luaL_error(L, "luaL_iterator_next: gen(param, state) must " + "return at least one result"); + unreachable(); + return 0; + } + + /* The call above returns nil as the first result. */ + if (lua_isnil(L, frame_start + 1)) { + lua_settop(L, frame_start); + return 0; + } + + /* Save the first result to it->state. */ + luaL_unref(L, LUA_REGISTRYINDEX, it->state); + lua_pushvalue(L, frame_start + 1); /* Popped by luaL_ref(). */ + it->state = luaL_ref(L, LUA_REGISTRYINDEX); + + return nresults; +} + +void luaL_iterator_free(lua_State *L, struct luaL_iterator *it) +{ + luaL_unref(L, LUA_REGISTRYINDEX, it->gen); + luaL_unref(L, LUA_REGISTRYINDEX, it->param); + luaL_unref(L, LUA_REGISTRYINDEX, it->state); + free(it); +} + +/* }}} */ + int tarantool_lua_utils_init(struct lua_State *L) { diff --git a/src/lua/utils.h b/src/lua/utils.h index bd302d8e9..6ba2e4767 100644 --- a/src/lua/utils.h +++ b/src/lua/utils.h @@ -525,6 +525,34 @@ luaL_checkfinite(struct lua_State *L, struct luaL_serializer *cfg, luaL_error(L, "number must not be NaN or Inf"); } +/* {{{ Helper functions to interact with a Lua iterator from C */ + +/** + * Holds iterator state (references to Lua objects). + */ +struct luaL_iterator; + +/** + * Create a Lua iterator from {gen, param, state}. + */ +struct luaL_iterator * +luaL_iterator_new_fromtable(lua_State *L, int idx); + +/** + * Move iterator to the next value. Push values returned by + * gen(param, state) and return its count. Zero means no more + * results available. + */ +int +luaL_iterator_next(lua_State *L, struct luaL_iterator *it); + +/** + * Free all resources held by the iterator. + */ +void luaL_iterator_free(lua_State *L, struct luaL_iterator *it); + +/* }}} */ + int tarantool_lua_utils_init(struct lua_State *L); -- 2.20.1
next prev parent reply other threads:[~2019-01-09 20:20 UTC|newest] Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-01-09 20:20 [PATCH v2 0/6] Merger Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 1/6] Add luaL_iscallable with support of cdata metatype Alexander Turenko 2019-01-10 12:21 ` Vladimir Davydov 2019-01-09 20:20 ` Alexander Turenko [this message] 2019-01-10 12:29 ` [PATCH v2 2/6] Add functions to ease using Lua iterators from C Vladimir Davydov 2019-01-15 23:26 ` Alexander Turenko 2019-01-16 8:18 ` Vladimir Davydov 2019-01-16 11:40 ` Alexander Turenko 2019-01-16 12:20 ` Vladimir Davydov 2019-01-17 1:20 ` Alexander Turenko 2019-01-28 18:17 ` Alexander Turenko 2019-03-01 4:04 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 3/6] lua: add luaT_newtuple() Alexander Turenko 2019-01-10 12:44 ` Vladimir Davydov 2019-01-18 21:58 ` Alexander Turenko 2019-01-23 16:12 ` Vladimir Davydov 2019-01-28 16:51 ` Alexander Turenko 2019-03-01 4:08 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 4/6] lua: add luaT_new_key_def() Alexander Turenko 2019-01-10 13:07 ` Vladimir Davydov 2019-01-29 18:52 ` Alexander Turenko 2019-01-30 10:58 ` Alexander Turenko 2019-03-01 4:10 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 5/6] net.box: add helpers to decode msgpack headers Alexander Turenko 2019-01-10 17:29 ` Vladimir Davydov 2019-02-01 15:11 ` Alexander Turenko 2019-03-05 12:00 ` Alexander Turenko 2019-01-09 20:20 ` [PATCH v2 6/6] Add merger for tuple streams Alexander Turenko
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=2c8049118f86934d6088636dba44f92f381ef8ff.1547064388.git.alexander.turenko@tarantool.org \ --to=alexander.turenko@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH v2 2/6] Add functions to ease using Lua iterators from C' \ /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