Tarantool development patches archive
 help / color / mirror / Atom feed
From: Maria <maria.khaydich@tarantool.org>
To: tarantool-patches@dev.tarantool.org,
	alexander.turenko@tarantool.org, georgy@tarantool.org
Subject: [Tarantool-patches] [PATCH] Msgpackffi considers msgpack.cfg options now
Date: Thu,  7 Nov 2019 13:36:42 +0300	[thread overview]
Message-ID: <20191107103642.42377-1-maria.khaydich@tarantool.org> (raw)

Lua serializers have common configuration options
that mostly were not respected in msgpackffi module.
This patch fixes that for several configurations:
encode_use_tostring, encode_invalid_as_nil and
encode_load_metatables.
Options encode/decode_invalid_numbers are suggested
to not be considered in msgpackffi module since lua
processed them properly without this check so adding
it would mean cutting functionality provided by the
language in case this field was set to be false.

Closes #4499
Issue:
https://github.com/tarantool/tarantool/issues/4499
Branch:
https://github.com/tarantool/tarantool/compare/eljashm/gh-4499-msgpackffi-ignores-msgpack.cfg-options
---
 src/lua/msgpack.c      |  1 -
 src/lua/msgpack.h      |  2 +-
 src/lua/msgpackffi.lua | 22 ++++++++++++++++++----
 src/lua/utils.h        |  6 +++---
 4 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/lua/msgpack.c b/src/lua/msgpack.c
index c2be0b3e8..d3df1a8c1 100644
--- a/src/lua/msgpack.c
+++ b/src/lua/msgpack.c
@@ -95,7 +95,6 @@ luamp_decode_extension_default(struct lua_State *L, const char **data)
 		   (unsigned char) **data);
 }
 
-
 void
 luamp_set_decode_extension(luamp_decode_extension_f handler)
 {
diff --git a/src/lua/msgpack.h b/src/lua/msgpack.h
index d0ade303f..1e60f0a5d 100644
--- a/src/lua/msgpack.h
+++ b/src/lua/msgpack.h
@@ -47,7 +47,7 @@ struct mpstream;
 /**
  * Default instance of msgpack serializer (msgpack = require('msgpack')).
  * This instance is used by all box's Lua/C API bindings (e.g. space:replace()).
- * All changes made by msgpack.cfg{} function are also affect box's bindings
+ * All changes made by msgpack.cfg{} function also affect box's bindings
  * (this is a feature).
  */
 extern struct luaL_serializer *luaL_msgpack_default;
diff --git a/src/lua/msgpackffi.lua b/src/lua/msgpackffi.lua
index f7ee44291..43f5d6df1 100644
--- a/src/lua/msgpackffi.lua
+++ b/src/lua/msgpackffi.lua
@@ -224,9 +224,11 @@ local function encode_r(buf, obj, level)
             return
         end
         local serialize = nil
-        local mt = getmetatable(obj)
-        if mt ~= nil then
-            serialize = mt.__serialize
+        if msgpack.cfg.encode_load_metatables then
+            local mt = getmetatable(obj)
+            if mt ~= nil then
+                serialize = mt.__serialize
+            end
         end
         -- calculate the number of array and map elements in the table
         -- TODO: pairs() aborts JIT
@@ -275,7 +277,19 @@ local function encode_r(buf, obj, level)
             error("can not encode FFI type: '"..ffi.typeof(obj).."'")
         end
     else
-        error("can not encode Lua type: '"..type(obj).."'")
+        if msgpack.cfg.encode_use_tostring then
+            obj = tostring(obj)
+            if obj then
+                goto restart
+            else
+                error("can not encode Lua type: '"..type(obj).."'")
+            end
+        else if msgpack.cfg.encode_invalid_as_nil then
+            encode_nil(buf)
+        else
+            error("can not encode Lua type: '"..type(obj).."'")
+        end
+    end
     end
 end
 
diff --git a/src/lua/utils.h b/src/lua/utils.h
index d42cc3992..425b9ebf1 100644
--- a/src/lua/utils.h
+++ b/src/lua/utils.h
@@ -185,7 +185,7 @@ struct luaL_serializer {
 	 *  + map - at least one table index is not unsigned integer.
 	 *  + regular array - all array indexes are available.
 	 *  + sparse array - at least one array index is missing.
-	 *  + excessively sparse arrat - the number of values missing
+	 *  + excessively sparse array - the number of values missing
 	 * exceeds the configured ratio.
 	 *
 	 * An array is excessively sparse when **all** the following
@@ -212,7 +212,7 @@ struct luaL_serializer {
 	int encode_sparse_safe;
 	/** Max recursion depth for encoding (MsgPack, CJSON only) */
 	int encode_max_depth;
-	/** Enables encoding of NaN and Inf numbers */
+	/** Enables encoding of NaN and Inf numbers (YAML, CJSON only) */
 	int encode_invalid_numbers;
 	/** Floating point numbers precision (YAML, CJSON only) */
 	int encode_number_precision;
@@ -233,7 +233,7 @@ struct luaL_serializer {
 	/** Use NULL for all unrecognizable types */
 	int encode_invalid_as_nil;
 
-	/** Enables decoding NaN and Inf numbers */
+	/** Enables decoding NaN and Inf numbers (YAML, CJSON only) */
 	int decode_invalid_numbers;
 	/** Save __serialize meta-value for decoded arrays and maps */
 	int decode_save_metatables;
-- 
2.20.1 (Apple Git-117)

             reply	other threads:[~2019-11-07 10:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-07 10:36 Maria [this message]
2019-11-11 23:21 ` Alexander Turenko
2019-11-29 21:09   ` Maria Khaydich
2019-12-17 23:26     ` Alexander Turenko
2020-01-24 17:37       ` [Tarantool-patches] [PATCH] lua: msgpackffi " Maria Khaydich
2020-01-25 15:01         ` Konstantin Osipov

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=20191107103642.42377-1-maria.khaydich@tarantool.org \
    --to=maria.khaydich@tarantool.org \
    --cc=alexander.turenko@tarantool.org \
    --cc=georgy@tarantool.org \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH] Msgpackffi considers msgpack.cfg options now' \
    /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