diff options
| author | Alexander M Pickering <alex@cogarr.net> | 2025-01-09 18:11:46 -0600 |
|---|---|---|
| committer | Alexander M Pickering <alex@cogarr.net> | 2025-01-09 18:11:46 -0600 |
| commit | decb72f936060a65bff18e9cbf33642ea3a71cd0 (patch) | |
| tree | 3b07bb1bfc1e4f0e39ec4ff8e0c243cd4fab0d61 /src/preload.lua | |
| parent | 726876d42270f8974fd495be91127ea7585472ff (diff) | |
| download | ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.tar.gz ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.tar.bz2 ggj25-decb72f936060a65bff18e9cbf33642ea3a71cd0.zip | |
Work
Diffstat (limited to 'src/preload.lua')
| -rw-r--r-- | src/preload.lua | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/preload.lua b/src/preload.lua new file mode 100644 index 0000000..8610cbc --- /dev/null +++ b/src/preload.lua @@ -0,0 +1,71 @@ +-- Stuff to load before everything else + +--[[ +rewrite traceback function to map file names and line numbers to moonscript +]] +local old_traceback = debug.traceback +debug.traceback = function(...) + local orig_traceback = old_traceback(...) + local noprint = {} + return orig_traceback:gsub("([ \t]*)([%w/_]+%.lua):(%d+):([^\n]*)",function(spaces, filename, linenum, rest) + --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 spaces .. filename .. ":" .. linenum .. ":" .. rest + end + debugfile = debugfile .. "\n" + for line in debugfile:gmatch("([^\n]+)\n") do + _,_,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%s:%d: %s", spaces, filename, linenum, rest) + end) +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 + |
