--[[----------------------------------------------------------------------------

WFDebugTraceback.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2013 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--


--==============================================================================

local function describeFrame( inf )
	if inf.what == "Lua" then
		if inf.name then
			return string.format( "\t%s:%d: in function %s", inf.short_src, inf.currentline, inf.name )
		else
			return string.format( "\t%s: in function <%s:%d>", inf.short_src, inf.short_src, inf.linedefined )
		end
	elseif inf.what == "main" then
		return string.format( "\t%s:%d: in main chunk", inf.short_src, inf.currentline )
	end
end

--------------------------------------------------------------------------------

-- Create a string identifying a function based on the results of getInfo.

local function describeFunction( inf )
	if inf.what == "Lua" then
		if inf.name then
			return string.format( "function %s @ %s:%d", inf.name, inf.short_src, inf.linedefined )
		else
			return string.format( "function @ %s:%d", inf.short_src, inf.linedefined )
		end
	elseif inf.what == "main" then
		return string.format( "main chunk: %s", inf.short_src )
	elseif inf.what == "C" then
		return string.format( "native code" )
	end
end

--------------------------------------------------------------------------------

local mt = {
	__tostring = function( self )
		local chunks = self._chunks
		local frames = { "stack traceback:" }
		local skipped = 0
		local skipping = true
		for level, inf in ipairs( self._infs ) do
			if skipping and chunks[ inf.source ] then
				frames[ 2 ] = describeFrame( inf )
				skipped = skipped + 1
			else
				if skipping then
					if skipped > 1 then
						frames[ 2 ] = string.format( "\t(skipped %d frames)", skipped )
					end
					skipping = false
				end
			
				frames[ #frames + 1 ] = describeFrame( inf )
			end
		end
		return table.concat( frames, "\n" )
	end,
}

--------------------------------------------------------------------------------

local function tracebackBeforeSkippedChunks( chunkArray )
	local chunks = {}
	for k,v in ipairs( chunkArray ) do
		chunks[ v ] = true
	end
	
	local level = 1
	local infs = {}
	
	while true do
		local inf = debug.getinfo( level + 1, "nSl" )
		if not inf then
			break
		end
		infs[ level ] = inf
		level = level + 1
	end
	
	return setmetatable( { _infs = infs, _chunks = chunks }, mt )
end

--==============================================================================

return {
	traceback = tracebackBeforeSkippedChunks,
	describeFunction = describeFunction
}
