Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: "v.shpilevoy@tarantool.org" <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: Re: [PATCH 2/5] lua: port console yaml formatting to C
Date: Wed, 21 Mar 2018 12:14:55 +0300	[thread overview]
Message-ID: <20180321091455.3td6h42oe3l2omlg@esperanza> (raw)
In-Reply-To: <D930A4A6-F403-4442-93CF-201FFF2114C0@tarantool.org>

On Tue, Mar 20, 2018 at 09:04:09PM +0300, v.shpilevoy@tarantool.org wrote:
> 
> 
> > 20 марта 2018 г., в 20:51, Vladimir Davydov <vdavydov.dev@gmail.com> написал(а):
> > 
> > On Mon, Mar 19, 2018 at 04:34:49PM +0300, Vladislav Shpilevoy wrote:
> >> Box.session.push() will be implemented in C lbox_session_push()
> >> function, which will use port to encapsulate different session
> >> types (binary, text) push() logic. And push() must be able to
> >> either encode an argument into message pack, or format it as a
> >> string using yaml. This formatting can not be done in Lua before
> >> push() call, since it breaks push() virtualization.
> >> 
> >> Needed for #2677
> >> 
> >> Signed-off-by: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
> >> ---
> >> src/box/lua/console.c         | 42 ++++++++++++++++++++++++++++++++++++++++++
> >> src/box/lua/console.lua       | 34 ++++++++--------------------------
> >> third_party/lua-yaml/lyaml.cc |  9 +++------
> >> third_party/lua-yaml/lyaml.h  |  3 +++
> >> 4 files changed, 56 insertions(+), 32 deletions(-)
> >> 
> >> diff --git a/src/box/lua/console.c b/src/box/lua/console.c
> > 
> >> @@ -328,6 +329,32 @@ lbox_console_add_history(struct lua_State *L)
> >> 	return 0;
> >> }
> >> 
> >> +static int
> >> +lbox_console_format(struct lua_State *L)
> >> +{
> >> +	int arg_count = lua_gettop(L);
> >> +	if (arg_count == 0) {
> >> +		lua_pushstring(L, "---\n...\n");
> >> +		return 1;
> >> +	}
> >> +	lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
> >> +	lua_getfield(L, -1, "console");
> >> +	lua_getfield(L, -1, "formatter");
> >> +	lua_getfield(L, -1, "encode");
> >> +	lua_createtable(L, arg_count, 0);
> >> +	for (int i = 0; i < arg_count; ++i) {
> >> +		if (lua_isnil(L, i + 1))
> >> +			lua_getfield(L, -3, "NULL");
> >> +		else
> >> +			lua_pushvalue(L, i + 1);
> >> +		lua_rawseti(L, -2, i + 1);
> >> +	}
> >> +	lua_call(L, 1, 1);
> >> +	lua_insert(L, -4);
> >> +	lua_pop(L, 3);
> >> +	return 1;
> >> +}
> >> +
> >> void
> >> tarantool_lua_console_init(struct lua_State *L)
> >> {
> > 
> >> @@ -344,6 +372,20 @@ tarantool_lua_console_init(struct lua_State *L)
> >> 	lua_getfield(L, -1, "completion_handler");
> >> 	lua_pushcclosure(L, lbox_console_readline, 1);
> >> 	lua_setfield(L, -2, "readline");
> >> +
> >> +	lua_yaml_new_formatter(L);
> >> +	lua_getfield(L, -1, "cfg");
> >> +	lua_createtable(L, 0, 4);
> >> +	lua_pushboolean(L, true);
> >> +	lua_setfield(L, -2, "encode_invalid_numbers");
> >> +	lua_pushboolean(L, true);
> >> +	lua_setfield(L, -2, "encode_load_metatables");
> >> +	lua_pushboolean(L, true);
> >> +	lua_setfield(L, -2, "encode_use_tostring");
> >> +	lua_pushboolean(L, true);
> >> +	lua_setfield(L, -2, "encode_invalid_as_nil");
> >> +	lua_call(L, 1, 0);
> >> +	lua_setfield(L, -2, "formatter");
> > 
> >> diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua
> >> @@ -395,4 +375,6 @@ package.loaded['console'] = {
> >>     on_start = on_start;
> >>     on_client_disconnect = on_client_disconnect;
> >>     completion_handler = internal.completion_handler;
> >> +    formatter = internal.formatter;
> > 
> > I don't like that we export the serializer to Lua, but never use it in
> > Lua code, only in C. Can't we create a serializer in C and pass it to
> > the encoder function explicitly?
> 
> The problem of Lua formatter is that it is Lua table, not cdata. And
> to fully port it on C it is necessary to rewrite luaL_serializer and
> l_dump in lyaml.cc <http://lyaml.cc/>, that seems to be too
> complicated for a single propose - format push message. But if you
> insist, I can do full port.

I'm not sure... Obviously, I don't want you to rewrite the yaml encode.
I just want to understand if there are alternatives to exporting the
yaml encoder to the Lua console module, just to use it in C. Please take
a look at how src/lua/msgpack.c is implemented - may be, we could borrow
some ideas from it.

  reply	other threads:[~2018-03-21  9:14 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-19 13:34 [PATCH 0/5] session: introduce box.session.push Vladislav Shpilevoy
2018-03-19 13:34 ` [PATCH 1/5] session: forbid creation from Lua binary and applier sessions Vladislav Shpilevoy
2018-03-20 13:20   ` Vladimir Davydov
2018-03-20 13:46     ` v.shpilevoy
2018-03-19 13:34 ` [PATCH 2/5] lua: port console yaml formatting to C Vladislav Shpilevoy
2018-03-20 17:51   ` Vladimir Davydov
2018-03-20 18:04     ` v.shpilevoy
2018-03-21  9:14       ` Vladimir Davydov [this message]
2018-03-21  9:30         ` v.shpilevoy
2018-03-19 13:34 ` [PATCH 3/5] Remove empty function declaration Vladislav Shpilevoy
2018-03-20 17:55   ` Vladimir Davydov
2018-03-20 17:57     ` [tarantool-patches] " v.shpilevoy
2018-03-21  9:16       ` Vladimir Davydov
2018-03-19 13:34 ` [PATCH 4/5] session: introduce session_owner Vladislav Shpilevoy
2018-03-20 18:29   ` Vladimir Davydov
2018-03-19 13:34 ` [PATCH 5/5] session: introduce box.session.push Vladislav Shpilevoy
2018-03-21  9:10   ` Vladimir Davydov
2018-03-21  9:30     ` [tarantool-patches] " v.shpilevoy
2018-03-21 12:25       ` Vladimir Davydov
2018-03-19 13:41 ` [tarantool-patches] [PATCH 0/5] " v.shpilevoy

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=20180321091455.3td6h42oe3l2omlg@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [PATCH 2/5] lua: port console yaml formatting to 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