util/debug.lua
changeset 4777 74ae0433f8dd
parent 4699 c66179261551
child 4778 127de6eec433
--- a/util/debug.lua	Sat Apr 28 15:47:43 2012 +0100
+++ b/util/debug.lua	Sat Apr 28 16:20:26 2012 +0100
@@ -119,21 +119,26 @@
 
 function _traceback(thread, message, level)
 
-	if type(thread) ~= "thread" then
+	-- Lua manual says: debug.traceback ([thread,] [message [, level]])
+	-- I fathom this to mean one of:
+	-- ()
+	-- (thread)
+	-- (message, level)
+	-- (thread, message, level)
+
+	if thread == nil then -- Defaults
+		thread, message, level = coroutine.running(), message, level;
+	elseif type(thread) == "string" then
 		thread, message, level = coroutine.running(), thread, message;
+	elseif type(thread) ~= "thread" then
+		return nil; -- debug.traceback() does this
 	end
-	if level and type(message) ~= "string" then
-		return nil, "invalid message";
-	elseif not level then
-		if type(message) == "number" then
-			level, message = message, nil;
-		else
-			level = 1;
-		end
-	end
-	
+
+	level = level or 1;
+
 	message = message and (message.."\n") or "";
 	
+	-- +3 counts for this function, and the pcall() and wrapper above us
 	local levels = get_traceback_table(thread, level+3);
 	
 	local last_source_desc;