diff --git a/src/box/lua/console.c b/src/box/lua/console.c index 7a5ac5550dcbf6606894f0955907671788b5d2b6..74434f992518c75aa414a7a57101a4ecbe24bff1 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 @@ -329,6 +332,33 @@ lbox_console_add_history(struct lua_State *L) return 0; } +/** + * Encode Lua object into YAML documents. Gets variable count of + * parameters. + * @retval 1 Pushes 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, NULL, NULL); +} + void tarantool_lua_console_init(struct lua_State *L) { @@ -337,6 +367,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); @@ -345,6 +376,22 @@ 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 the formatter in module local + * variable. + * + * This member is not visible to a user, because + * console.lua modifies itself, holding the formatter in + * module local variable. Add_history, save_history, + * load_history work the same way. + */ + lua_setfield(L, -2, "formatter"); } /* diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index f1a8f4915bc0b126fd26e6adfd83b28fad3333b1..bc4e02bfccf67127618d9fa1527da742ad1e65d4 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -12,34 +12,11 @@ local net_box = require('net.box') local YAML_TERM = '\n...\n' --- admin formatter must be able to encode any Lua variable -local formatter = yaml.new() -formatter.cfg{ - encode_invalid_numbers = true; - encode_load_metatables = true; - encode_use_tostring = true; - encode_invalid_as_nil = true; -} - local function format(status, ...) - -- When storing a nil in a Lua table, there is no way to - -- distinguish nil value from no value. This is a trick to - -- make sure yaml converter correctly - local function wrapnull(v) - return v == nil and formatter.NULL or v - end local err if status then - local count = select('#', ...) - if count == 0 then - return "---\n...\n" - end - local res = {} - for i=1,count,1 do - table.insert(res, wrapnull(select(i, ...))) - end -- serializer can raise an exception - status, err = pcall(formatter.encode, res) + status, err = pcall(internal.format, ...) if status then return err else @@ -47,9 +24,12 @@ local function format(status, ...) tostring(err) end else - err = wrapnull(...) + err = ... + if err == nil then + err = box.NULL + end end - return formatter.encode({{error = err }}) + return internal.format({ error = err }) end -- diff --git a/third_party/lua-yaml/lyaml.cc b/third_party/lua-yaml/lyaml.cc index bf60c5ca00fd4c459bdd1b0a3cf4fd16608d5d7f..14eabca7729e725d855b54a822171c3817c134c9 100644 --- a/third_party/lua-yaml/lyaml.cc +++ b/third_party/lua-yaml/lyaml.cc @@ -702,27 +702,7 @@ static void find_references(struct lua_yaml_dumper *dumper) { } } -/** - * Encode an object on Lua stack into YAML stream. - * @param L Lua stack to get an argument and push the result. - * @param serializer Lua YAML serializer. - * @param tag_handle NULL, or a global tag handle. For global tags - * details see the standard: - * http://yaml.org/spec/1.2/spec.html#tag/shorthand/. - * @param tag_prefix NULL, or a global tag prefix, to which @a - * handle is expanded. Example of a tagged document: - * handle prefix - * ____ ________________________ - * / \ / \ - * '%TAG !push! tag:tarantool.io/push,2018 - * --- value - * ... - * - * @retval 2 Pushes two values on error: nil, error description. - * @retval 1 Pushes one value on success: string with dumped - * object. - */ -static int +int lua_yaml_encode(lua_State *L, struct luaL_serializer *serializer, const char *tag_handle, const char *tag_prefix) { @@ -839,7 +819,11 @@ static int l_dump(lua_State *L) { } static int -l_new(lua_State *L); +l_new(lua_State *L) +{ + lua_yaml_new_serializer(L); + return 1; +} static const luaL_Reg yamllib[] = { { "encode", l_dump }, @@ -848,12 +832,12 @@ static const luaL_Reg yamllib[] = { { NULL, NULL} }; -static int -l_new(lua_State *L) +struct luaL_serializer * +lua_yaml_new_serializer(lua_State *L) { struct luaL_serializer *s = luaL_newserializer(L, NULL, yamllib); s->has_compact = 1; - return 1; + return s; } int diff --git a/third_party/lua-yaml/lyaml.h b/third_party/lua-yaml/lyaml.h index 650a2d7474162f6252892ee556bcd3f611e55371..335792d245150eb5375819a8c6c92d6ce9cbdbb3 100644 --- a/third_party/lua-yaml/lyaml.h +++ b/third_party/lua-yaml/lyaml.h @@ -7,9 +7,39 @@ extern "C" { #include <lua.h> +struct luaL_serializer; + LUALIB_API int luaopen_yaml(lua_State *L); +/** @Sa luaL_newserializer(). */ +struct luaL_serializer * +lua_yaml_new_serializer(lua_State *L); + +/** + * Encode an object on Lua stack into YAML stream. + * @param L Lua stack to get an argument and push the result. + * @param serializer Lua YAML serializer. + * @param tag_handle NULL, or a global tag handle. For global tags + * details see the standard: + * http://yaml.org/spec/1.2/spec.html#tag/shorthand/. + * @param tag_prefix NULL, or a global tag prefix, to which @a + * handle is expanded. Example of a tagged document: + * handle prefix + * ____ ________________________ + * / \ / \ + * '%TAG !push! tag:tarantool.io/push,2018 + * --- value + * ... + * + * @retval 2 Pushes two values on error: nil, error description. + * @retval 1 Pushes one value on success: string with dumped + * object. + */ +int +lua_yaml_encode(lua_State *L, struct luaL_serializer *serializer, + const char *tag_handle, const char *tag_prefix); + #ifdef __cplusplus } #endif