[PATCH v2 1/8] lua: fix decimal comparison with nil

Serge Petrenko sergepetrenko at tarantool.org
Thu Aug 8 14:55:52 MSK 2019


Previously decimal comparison with nil failed with following error:
`expected decimal, number or string as 2 argument`.
Fix this.

Follow-up #692
---
 src/lua/decimal.c         |  4 ++++
 test/app/decimal.result   | 26 ++++++++++++++++++++++++++
 test/app/decimal.test.lua |  8 ++++++++
 3 files changed, 38 insertions(+)

diff --git a/src/lua/decimal.c b/src/lua/decimal.c
index 330f4c3f8..e480ec7be 100644
--- a/src/lua/decimal.c
+++ b/src/lua/decimal.c
@@ -69,6 +69,10 @@ ldecimal_##name(struct lua_State *L) {						\
 static int									\
 ldecimal_##name(struct lua_State *L) {						\
 	assert(lua_gettop(L) == 2);						\
+	if (lua_isnil(L, 1) || lua_isnil(L, 2)) {				\
+		lua_pushboolean(L, false);					\
+		return 1;							\
+	}									\
 	decimal_t *lhs = lua_todecimal(L, 1);					\
 	decimal_t *rhs = lua_todecimal(L, 2);					\
 	lua_pushboolean(L, decimal_compare(lhs, rhs) cmp 0);			\
diff --git a/test/app/decimal.result b/test/app/decimal.result
index b53f4e75d..633f3681a 100644
--- a/test/app/decimal.result
+++ b/test/app/decimal.result
@@ -223,6 +223,32 @@ b
  | - '0.1'
  | ...
 
+-- check comparsion with nil
+a == nil
+ | ---
+ | - false
+ | ...
+a ~= nil
+ | ---
+ | - true
+ | ...
+a > nil
+ | ---
+ | - false
+ | ...
+a < nil
+ | ---
+ | - false
+ | ...
+a >= nil
+ | ---
+ | - false
+ | ...
+a <= nil
+ | ---
+ | - false
+ | ...
+
 decimal.sqrt(a)
  | ---
  | - '3.1622776601683793319988935444327185337'
diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua
index cee56d5e7..cb14dfb56 100644
--- a/test/app/decimal.test.lua
+++ b/test/app/decimal.test.lua
@@ -62,6 +62,14 @@ a ~= b
 a
 b
 
+-- check comparsion with nil
+a == nil
+a ~= nil
+a > nil
+a < nil
+a >= nil
+a <= nil
+
 decimal.sqrt(a)
 decimal.ln(a)
 decimal.log10(a)
-- 
2.20.1 (Apple Git-117)




More information about the Tarantool-patches mailing list