aboutsummaryrefslogtreecommitdiff
path: root/src/preload.lua
diff options
context:
space:
mode:
authorAlex Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
committerAlexander M Pickering <alex@cogarr.net>2026-02-01 13:14:32 -0600
commit3a975db66a3711f34e8b64bb27a8eaca79fdeca9 (patch)
treefcc12f8f9d638ff575c1963796de76b7628854b4 /src/preload.lua
downloadggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.gz
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.tar.bz2
ggj26-3a975db66a3711f34e8b64bb27a8eaca79fdeca9.zip
Initial commitHEADmaster
Diffstat (limited to 'src/preload.lua')
-rw-r--r--src/preload.lua107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/preload.lua b/src/preload.lua
new file mode 100644
index 0000000..1085f63
--- /dev/null
+++ b/src/preload.lua
@@ -0,0 +1,107 @@
+-- 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
+
+-- Override global error function to log errors through the log system
+local old_error = error
+function error(message, level)
+ -- Get the log module if available
+ local log_available, log = pcall(require, "log")
+ if log_available and log and log.error then
+ -- Get full traceback
+ local traceback = debug.traceback(tostring(message), 2)
+ log.error(traceback, {"error"})
+ end
+ -- Call the original error function
+ old_error(message, level or 2)
+end
+