From ad46eb01748c1bd71e6f9dd5ec665c4aefe90185 Mon Sep 17 00:00:00 2001 From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> Date: Wed, 4 Sep 2019 00:01:53 +0200 Subject: [PATCH] app: serializers update now is reflected in Lua There are some objects called serializers - msgpack, cjson, yaml, maybe more. They are global objects affecting both Lua and C modules. A serializer have settings which can be updated. But before the patch an update changed only C structure of the serializer. It made impossible to use settings of the serializers from Lua. Now any update of any serializer is reflected both in its C and Lua structures. Part of #4434 (cherry picked from commit fe4a8047a8aa1d2d9927cb097ca4581552849601) --- src/lua/utils.c | 29 ++++++++++++++++++---------- test/app-tap/json.test.lua | 3 ++- test/app-tap/lua/serializer_test.lua | 13 +++++++++++++ test/app-tap/msgpack.test.lua | 3 ++- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/lua/utils.c b/src/lua/utils.c index 0a4bcf517b..309bf005cc 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -256,7 +256,8 @@ luaL_serializer_create(struct luaL_serializer *cfg) } /** - * Configure one field in @a cfg. + * Configure one field in @a cfg. Value of the field is kept on + * Lua stack after this function, and should be popped manually. * @param L Lua stack. * @param i Index of option in OPTIONS[]. * @param cfg Serializer to inherit configuration. @@ -268,10 +269,8 @@ luaL_serializer_parse_option(struct lua_State *L, int i, struct luaL_serializer *cfg) { lua_getfield(L, 2, OPTIONS[i].name); - if (lua_isnil(L, -1)) { - lua_pop(L, 1); + if (lua_isnil(L, -1)) return NULL; - } /* * Update struct luaL_serializer using pointer to a * configuration value (all values must be `int` for that). @@ -287,7 +286,6 @@ luaL_serializer_parse_option(struct lua_State *L, int i, default: unreachable(); } - lua_pop(L, 1); return pval; } @@ -295,8 +293,10 @@ void luaL_serializer_parse_options(struct lua_State *L, struct luaL_serializer *cfg) { - for (int i = 0; OPTIONS[i].name != NULL; i++) + for (int i = 0; OPTIONS[i].name != NULL; ++i) { luaL_serializer_parse_option(L, i, cfg); + lua_pop(L, 1); + } } /** @@ -305,16 +305,25 @@ luaL_serializer_parse_options(struct lua_State *L, * luaL_serializer structure. serializer.cfg has overriden __call() method * to change configuration keys in internal userdata (like box.cfg{}). * Please note that direct change in serializer.cfg.key will not affect - * internal state of userdata. + * internal state of userdata. Changes via cfg() are reflected in + * both Lua cfg table, and C serializer structure. * @param L lua stack * @return 0 */ static int luaL_serializer_cfg(struct lua_State *L) { - luaL_checktype(L, 1, LUA_TTABLE); /* serializer */ - luaL_checktype(L, 2, LUA_TTABLE); /* serializer.cfg */ - luaL_serializer_parse_options(L, luaL_checkserializer(L)); + /* Serializer.cfg */ + luaL_checktype(L, 1, LUA_TTABLE); + /* Updated parameters. */ + luaL_checktype(L, 2, LUA_TTABLE); + struct luaL_serializer *cfg = luaL_checkserializer(L); + for (int i = 0; OPTIONS[i].name != NULL; ++i) { + if (luaL_serializer_parse_option(L, i, cfg) == NULL) + lua_pop(L, 1); + else + lua_setfield(L, 1, OPTIONS[i].name); + } return 0; } diff --git a/test/app-tap/json.test.lua b/test/app-tap/json.test.lua index 10f6f4ab2f..0a89668665 100755 --- a/test/app-tap/json.test.lua +++ b/test/app-tap/json.test.lua @@ -22,7 +22,7 @@ end tap.test("json", function(test) local serializer = require('json') - test:plan(32) + test:plan(33) test:test("unsigned", common.test_unsigned, serializer) test:test("signed", common.test_signed, serializer) @@ -32,6 +32,7 @@ tap.test("json", function(test) test:test("nil", common.test_nil, serializer) test:test("table", common.test_table, serializer, is_array, is_map) test:test("ucdata", common.test_ucdata, serializer) + test:test("depth", common.test_depth, serializer) test:test("misc", test_misc, serializer) -- diff --git a/test/app-tap/lua/serializer_test.lua b/test/app-tap/lua/serializer_test.lua index a48e557a3e..bedbf95a58 100644 --- a/test/app-tap/lua/serializer_test.lua +++ b/test/app-tap/lua/serializer_test.lua @@ -389,6 +389,18 @@ local function test_ucdata(test, s) ss = nil end +local function test_depth(test, s) + test:plan(1) + -- + -- gh-4434: serializer update should be reflected in Lua. + -- + local max_depth = s.cfg.encode_max_depth + s.cfg({encode_max_depth = max_depth + 5}) + test:is(s.cfg.encode_max_depth, max_depth + 5, + "cfg({<name> = value}) is reflected in cfg.<name>") + s.cfg({encode_max_depth = max_depth}) +end + return { test_unsigned = test_unsigned; test_signed = test_signed; @@ -398,4 +410,5 @@ return { test_nil = test_nil; test_table = test_table; test_ucdata = test_ucdata; + test_depth = test_depth; } diff --git a/test/app-tap/msgpack.test.lua b/test/app-tap/msgpack.test.lua index bd095e5ae5..752f107a85 100755 --- a/test/app-tap/msgpack.test.lua +++ b/test/app-tap/msgpack.test.lua @@ -231,7 +231,7 @@ end tap.test("msgpack", function(test) local serializer = require('msgpack') - test:plan(11) + test:plan(12) test:test("unsigned", common.test_unsigned, serializer) test:test("signed", common.test_signed, serializer) test:test("double", common.test_double, serializer) @@ -240,6 +240,7 @@ tap.test("msgpack", function(test) test:test("nil", common.test_nil, serializer) test:test("table", common.test_table, serializer, is_array, is_map) test:test("ucdata", common.test_ucdata, serializer) + test:test("depth", common.test_depth, serializer) test:test("offsets", test_offsets, serializer) test:test("misc", test_misc, serializer) test:test("decode_array_map", test_decode_array_map_header, serializer) -- GitLab