From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Alexander Turenko <alexander.turenko@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: Re: [PATCH v2 2/6] Add functions to ease using Lua iterators from C
Date: Thu, 10 Jan 2019 15:29:09 +0300 [thread overview]
Message-ID: <20190110122909.novsa6o6duykot6a@esperanza> (raw)
In-Reply-To: <2c8049118f86934d6088636dba44f92f381ef8ff.1547064388.git.alexander.turenko@tarantool.org>
On Wed, Jan 09, 2019 at 11:20:10PM +0300, Alexander Turenko wrote:
> Needed for #3276.
Again, I'm not quite sure that you'll need this patch after you
rework the merger API so I'm not applying it until you send the
new API proposal.
> ---
> src/lua/utils.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++
> src/lua/utils.h | 28 +++++++++++++++++++++
Some tests would be nice to have.
> 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(
Nit: no need to convert void * to struct luaL_iterator *.
> + 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;
I'd make luaL_iterator struct transparent so that one could define it
on stack.
> +
> +/**
> + * Create a Lua iterator from {gen, param, state}.
May be, we could pass idx == 0 to create an iterator from
gen, param, state (without a table)? Would it be worthwhile?
> + */
> +struct luaL_iterator *
> +luaL_iterator_new_fromtable(lua_State *L, int idx);
I don't think that _fromtable suffix is really necessary.
> +
> +/**
> + * 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);
We usually match _new with _delete.
> +
> +/* }}} */
> +
> int
> tarantool_lua_utils_init(struct lua_State *L);
next prev parent reply other threads:[~2019-01-10 12:29 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 ` [PATCH v2 2/6] Add functions to ease using Lua iterators from C Alexander Turenko
2019-01-10 12:29 ` Vladimir Davydov [this message]
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=20190110122909.novsa6o6duykot6a@esperanza \
--to=vdavydov.dev@gmail.com \
--cc=alexander.turenko@tarantool.org \
--cc=tarantool-patches@freelists.org \
--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