From cfc2621c12480cc5311d4fc1497b3bee9dd7c6d1 Mon Sep 17 00:00:00 2001 From: George Zhao Date: Sat, 19 Aug 2023 18:10:04 +0800 Subject: [PATCH] chore: update time callback error non-strings --- src/luv.c | 6 ++++-- tests/test-timer.lua | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/luv.c b/src/luv.c index 105e4486..a55f917c 100644 --- a/src/luv.c +++ b/src/luv.c @@ -706,7 +706,8 @@ LUALIB_API int luv_cfpcall(lua_State* L, int nargs, int nresult, int flags) { break; case LUA_ERRMEM: if ((flags & LUVF_CALLBACK_NOERRMSG) == 0) - fprintf(stderr, "System Error: %s\n", lua_tostring(L, -1)); + fprintf(stderr, "System Error: %s\n", + luaL_tolstring(L, lua_absindex(L, -1), NULL)); if ((flags & LUVF_CALLBACK_NOEXIT) == 0) exit(-1); lua_pop(L, 1); @@ -716,7 +717,8 @@ LUALIB_API int luv_cfpcall(lua_State* L, int nargs, int nresult, int flags) { case LUA_ERRERR: default: if ((flags & LUVF_CALLBACK_NOERRMSG) == 0) - fprintf(stderr, "Uncaught Error: %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Uncaught Error: %s\n", + luaL_tolstring(L, lua_absindex(L, -1), NULL)); if ((flags & LUVF_CALLBACK_NOEXIT) == 0) exit(-1); lua_pop(L, 1); diff --git a/tests/test-timer.lua b/tests/test-timer.lua index 83d3865c..c2657284 100644 --- a/tests/test-timer.lua +++ b/tests/test-timer.lua @@ -115,12 +115,26 @@ return require('lib/tap')(function (test) assert(huge_timer:get_due_in()==0xffff) end, "1.40.0") - test("timer init", function(print, p, expect, uv) + test("timer callback with errors", function(print, p, expect, uv) + local Error = {} + Error.__index = Error + + function Error.new(msg) + local o = setmetatable({}, Error) + o.msg = assert(msg) + return o + end + + function Error:__tostring() + return assert(tostring(self.msg)) + end + local timer = uv.new_timer() timer:start(10, 0, function() timer:stop() timer:close() - error('Error in timeout callback') + local e = Error.new('Error in timeout callback') + error(e) end) end) end)