[tarantool-patches] [PATCH tarantool 2/2] Using luaT_tolstring in conversion function, instead lua_tostring

Eugene Blikh bigbes at gmail.com
Mon Aug 6 10:17:00 MSK 2018


From: Eugine Blikh <bigbes at gmail.com>

We can throw any Lua object as Lua error, but current behaviour won't
convert it to string. So diag error's object will have NULL, instead of
string.

luaT_tolstring honors __tostring metamethod and thus can convert table
to it's string representation.

For example, old behaviour is:
```
tarantool> fiber.create(error, 'help')
LuajitError: help
tarantool> fiber.create(error, { message = 'help' })
LuajitError:
tarantool> fiber.create(error, setmetatable({ message = 'help' }, {
                          __tostring = function(self) return self.message end
                        }))
LuajitError:
```

New behaviour is:
```
tarantool> fiber.create(error, 'help')
LuajitError: help
tarantool> fiber.create(error, { 'help' })
LuajitError: table: 0x0108fa2790
tarantool> fiber.create(error, setmetatable({ message = 'help' }, {
                          __tostring = function(self) return self.message end
                        }))
LuajitError: help
```

It won't break anything, but'll add new behaviour
---
 src/lua/utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/lua/utils.c b/src/lua/utils.c
index afc44b581..56df2bfb6 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -920,7 +920,7 @@ luaT_toerror(lua_State *L)
 		diag_add_error(&fiber()->diag, e);
 	} else {
 		/* Convert Lua error to a Tarantool exception. */
-		diag_set(LuajitError, lua_tostring(L, -1));
+		diag_set(LuajitError, luaT_tolstring(L, -1, NULL));
 	}
 	return 1;
 }
-- 
2.16.2





More information about the Tarantool-patches mailing list