From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 10 May 2018 21:46:48 +0300 From: Konstantin Osipov Subject: Re: [tarantool-patches] [PATCH v2 04/10] console: use Lua C API to do formatting for console Message-ID: <20180510184648.GD30593@atlas> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com List-ID: * Vladislav Shpilevoy [18/04/20 16:25]: > YAML formatting C API is needed for #2677, where it will be used > to send text pushes and prints as tagged YAML documents. I don't understand why you need to move dump/format into C from this comment. What was wrong with keeping it in Lua implementation? > > Needed for #2677 > --- > src/box/lua/console.c | 48 +++++++++++++++++++++++++++++++++++++++++++ > src/box/lua/console.lua | 32 ++++++----------------------- > third_party/lua-yaml/lyaml.cc | 34 ++++++++++++------------------ > third_party/lua-yaml/lyaml.h | 29 ++++++++++++++++++++++++++ > 4 files changed, 96 insertions(+), 47 deletions(-) > > diff --git a/src/box/lua/console.c b/src/box/lua/console.c > index d27d7ecac..f6009d387 100644 > --- a/src/box/lua/console.c > +++ b/src/box/lua/console.c > @@ -34,6 +34,7 @@ > #include "lua/fiber.h" > #include "fiber.h" > #include "coio.h" > +#include "lua-yaml/lyaml.h" > #include > #include > #include > @@ -42,6 +43,8 @@ > #include > #include > > +static struct luaL_serializer *luaL_yaml_default = NULL; > + > /* > * Completion engine (Mike Paul's). > * Used internally when collecting completions locally. Also a Lua > @@ -328,6 +331,32 @@ lbox_console_add_history(struct lua_State *L) > return 0; > } > > +/** > + * Encode Lua object into YAML documents. Gets variable count of > + * parameters. > + * @retval String with YAML documents - one per parameter. > + */ > +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_createtable(L, arg_count, 0); > + for (int i = 0; i < arg_count; ++i) { > + if (lua_isnil(L, i + 1)) > + luaL_pushnull(L); > + else > + lua_pushvalue(L, i + 1); > + lua_rawseti(L, -2, i + 1); > + } > + lua_replace(L, 1); > + lua_settop(L, 1); > + return lua_yaml_encode(L, luaL_yaml_default); > +} > + > void > tarantool_lua_console_init(struct lua_State *L) > { > @@ -336,6 +365,7 @@ tarantool_lua_console_init(struct lua_State *L) > {"save_history", lbox_console_save_history}, > {"add_history", lbox_console_add_history}, > {"completion_handler", lbox_console_completion_handler}, > + {"format", lbox_console_format}, > {NULL, NULL} > }; > luaL_register_module(L, "console", consolelib); > @@ -344,6 +374,24 @@ 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"); > + > + luaL_yaml_default = lua_yaml_new_serializer(L); > + luaL_yaml_default->encode_invalid_numbers = 1; > + luaL_yaml_default->encode_load_metatables = 1; > + luaL_yaml_default->encode_use_tostring = 1; > + luaL_yaml_default->encode_invalid_as_nil = 1; > + /* > + * Hold reference to a formatter (the Lua representation > + * of luaL_yaml_default). > It is not visible to a user > + * here, because require('console') returns modified > + * package with no formatter. This problem is absent in > + * similar places like lua/msgpack.c, because they are > + * serializers and nothing more - they hold > + * luaL_serializer in LUA_REGISTRYINDEX. Console does the > + * same, but here a YAML serializer is just a part of > + * console. > + */ I don't understand the comment beyond the first sentence. > + lua_setfield(L, -2, "formatter"); > } > > /* > +/** > + * Encode a Lua object or objects into YAML documents onto Lua > + * stack. > + * @param L Lua stack to get arguments and push result. > + * @param serializer Lua YAML serializer. > + * @param tag_handle NULL, or a global tag handle. Handle becomes > + * a synonym for prefix. > + * @param tag_prefix NULL, or a global tag prefix, to which @a > + * handle is expanded. > + * @retval nil, error Error. > + * @retval not nil Lua string with dumped object. > + */ > +int > +lua_yaml_encode_tagged(lua_State *L, struct luaL_serializer *serializer, > + const char *tag_handle, const char *tag_prefix); No need to keep the comments in two places. We have decided to keep the comments around declarations, not definitions, a while ago (please make sure to update the server engineering guidelines if it is not reflected there or in the coding style). > + > +/** Same as lua_yaml_encode_tagged, but encode with no tags. */ > +static inline int > +lua_yaml_encode(lua_State *L, struct luaL_serializer *serializer) > +{ > + return lua_yaml_encode_tagged(L, serializer, NULL, NULL); > +} -- Konstantin Osipov, Moscow, Russia, +7 903 626 22 32 http://tarantool.io - www.twitter.com/kostja_osipov