Tarantool development patches archive
 help / color / mirror / Atom feed
From: Konstantin Osipov <kostja@tarantool.org>
To: tarantool-patches@freelists.org
Cc: vdavydov.dev@gmail.com
Subject: Re: [tarantool-patches] [PATCH v2 04/10] console: use Lua C API to do formatting for console
Date: Thu, 10 May 2018 21:46:48 +0300	[thread overview]
Message-ID: <20180510184648.GD30593@atlas> (raw)
In-Reply-To: <be57b8cd5640d3ed0756f00ba09d8fb9a682146a.1524228894.git.v.shpilevoy@tarantool.org>

* Vladislav Shpilevoy <v.shpilevoy@tarantool.org> [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 <lua.h>
>  #include <lauxlib.h>
>  #include <lualib.h>
> @@ -42,6 +43,8 @@
>  #include <stdlib.h>
>  #include <ctype.h>
>  
> +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

  reply	other threads:[~2018-05-10 18:46 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-20 13:24 [PATCH v2 00/10] session: introduce box.session.push Vladislav Shpilevoy
2018-04-20 13:24 ` [PATCH v2 01/10] yaml: don't throw OOM on any error in yaml encoding Vladislav Shpilevoy
2018-05-10 18:10   ` [tarantool-patches] " Konstantin Osipov
2018-04-20 13:24 ` [tarantool-patches] [PATCH v2 10/10] session: introduce binary box.session.push Vladislav Shpilevoy
2018-05-10 19:50   ` Konstantin Osipov
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-20 13:24 ` [PATCH v2 02/10] yaml: introduce yaml.encode_tagged Vladislav Shpilevoy
2018-05-10 18:22   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-05-30 19:15       ` Konstantin Osipov
2018-05-30 20:49         ` Vladislav Shpilevoy
2018-05-31 10:46           ` Konstantin Osipov
2018-04-20 13:24 ` [PATCH v2 03/10] yaml: introduce yaml.decode_tag Vladislav Shpilevoy
2018-05-10 18:41   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-05-31 10:54       ` Konstantin Osipov
2018-05-31 11:36       ` Konstantin Osipov
2018-04-20 13:24 ` [PATCH v2 04/10] console: use Lua C API to do formatting for console Vladislav Shpilevoy
2018-05-10 18:46   ` Konstantin Osipov [this message]
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-20 13:24 ` [PATCH v2 05/10] session: move salt into iproto connection Vladislav Shpilevoy
2018-05-10 18:47   ` [tarantool-patches] " Konstantin Osipov
2018-04-20 13:24 ` [PATCH v2 06/10] session: introduce session vtab and meta Vladislav Shpilevoy
2018-05-10 19:20   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-20 13:24 ` [PATCH v2 07/10] port: rename dump() into dump_msgpack() Vladislav Shpilevoy
2018-05-10 19:21   ` [tarantool-patches] " Konstantin Osipov
2018-04-20 13:24 ` [PATCH v2 08/10] session: introduce text box.session.push Vladislav Shpilevoy
2018-05-10 19:27   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 20:50     ` [tarantool-patches] " Vladislav Shpilevoy
2018-04-20 13:24 ` [PATCH v2 09/10] session: enable box.session.push in local console Vladislav Shpilevoy
2018-05-10 19:28   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 20:50 ` [tarantool-patches] [PATCH 1/1] netbox: introduce iterable future objects Vladislav Shpilevoy
2018-06-04 22:17   ` [tarantool-patches] " Vladislav 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=20180510184648.GD30593@atlas \
    --to=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [tarantool-patches] [PATCH v2 04/10] console: use Lua C API to do formatting for console' \
    /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