summaryrefslogtreecommitdiff
path: root/src/preload.lua
blob: 64b5314c4d8ed72bac4c7c4289fb75587fad9f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- Stuff to load before everything else

--[[
rewrite traceback function to map file names and line numbers to moonscript
]]
local require_order = {}
local old_traceback = debug.traceback
debug.traceback = function(...)
	local orig_traceback = old_traceback(...)
	local noprint = {}
	return orig_traceback:gsub("([%w/_]+%.lua):(%d+):",function(filename, linenum)
		--If our file is not moonscript, we won't have a debug file
		local debugfile = am.load_string(filename .. ".X")
		if not debugfile then
			return filename .. ":" .. linenum .. ":"
		end
		debugfile = debugfile .. "\n"
		for line in debugfile:gmatch("([^\n]+)\n") do
			local _,_,pos,lua,moon = line:find("(%d+)%s+(%d+):%b[] >> (%d+)")
			if pos and lua and moon and tonumber(linenum) == tonumber(lua) then
				filename = filename:gsub(".lua$",".moon")
				linenum = moon
				break
			end
		end
		return string.format("%s:%d:", filename, linenum)
	end) .. "\nRequire order: [" .. table.concat(require_order, ",\n") .. "]"
end

local old_require = require
local required = {}
require = function(...)
	args = {...}
	if not required[args[1]] then
		required[args[1]] = true
		table.insert(require_order, args[1])
	end
	return old_require(...)
end

--[[
Display where print statements are comming from

local oldprint = print
print = function(...)
	error("Print")
	oldprint(debug.traceback())
end
]]

-- Override tostring to display more info about the table
local old_tostring = tostring
local numtabs = 0
local printed_tables = {}
local function tostring_helper(el)
        assert(type(el) == "table", "Tried to call helper with something that was not a table, it was a " .. type(el))
        local mt = getmetatable(el)
        if mt and mt.__tostring then
                return mt.__tostring(el)
        elseif printed_tables[el] == true then
                return old_tostring(el)
        else
                printed_tables[el] = true
                numtabs = numtabs + 1
                local strbuilder = {"{"}
                for k,v in pairs(el) do
                        local key,value
                        if type(k) == "table" then
                                key = tostring_helper(k)
                        else
                                key = old_tostring(k)
                        end
                        if type(v) == "table" then
                                value = tostring_helper(v)
                        else
                                value = old_tostring(v)
                        end
                        strbuilder[#strbuilder + 1] = string.format("%s%s : %s", string.rep("\t",numtabs), key, value)
                end
                strbuilder[#strbuilder + 1] = string.rep("\t",numtabs - 1) .. "}"
                numtabs = numtabs - 1
                return table.concat(strbuilder,"\n")
        end

end
function tostring(el)
        printed_tables = {}
        if type(el) == "table" then
                return tostring_helper(el)
        end
        return old_tostring(el)
end