From 3a975db66a3711f34e8b64bb27a8eaca79fdeca9 Mon Sep 17 00:00:00 2001 From: Alex Pickering Date: Sun, 1 Feb 2026 13:14:32 -0600 Subject: Initial commit --- src/preload.lua | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/preload.lua (limited to 'src/preload.lua') 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 + -- cgit v1.2.3-70-g09d2