Skip to content

Instantly share code, notes, and snippets.

@Yonaba
Created June 29, 2011 14:24
Show Gist options
  • Save Yonaba/1053931 to your computer and use it in GitHub Desktop.
Save Yonaba/1053931 to your computer and use it in GitHub Desktop.
Custom Dofile Function (For PSP's LuaPlayer Debugging)
----------------------------
--LIB DOFILE
--Roland Yonaba (SeanPaul223)
--03/08/2010
----------------------------
-----------------------------------------------------------------------------------------
--The main purpose of this library is helping Lua coders to debug their apps bypassing the
--in-built error screen, which blocks the running process
--Usage : call this file using require
--Example: require 'libDofile'
--When error screen is displayed, press Start to escape.
--Note: Calling This Library overrides original Lua's dofile() function.
function spliterr(str)
local g,e = string.find(str,':%d+')
local TYPE = string.sub(str,1,g-1)
local LINENUM = tonumber(string.sub(str,g+1,e))
local DESC = string.sub(str,e+2,str:len())
return TYPE,LINENUM,DESC
end
function handleTraceBack()
local out,lastI = {},1
local str = debug.traceback()
for i=1,str:len() do
local char = str:sub(i,i)
if char=='\r' or char=='\n' then
table.insert(out,str:sub(lastI,i-1))
lastI = i
end
end
return out
end
--Custom Loop to display Error arguments.Escape this pressing Start key
_USB = false
function showLoop(err,filename)
while not Controls.read():start() do
screen:clear()
screen:print(10,10,"Byte - Compilation Error on file : "..filename,Color.new(255,255,255))
if Controls.read():r() then
if _USB then
System.usbDiskModeDeactivate()
_USB = false
else System.usbDiskModeActivate()
_USB = true
end
end
if err then
local TYPE,LINE,DESC = spliterr(err)
screen:print(10,20,'ERROR TYPE: '..TYPE,Color.new(255,0,0))
screen:print(10,30,'ERROR LINE: '..LINE,Color.new(255,255,0))
screen:print(10,40,'DESCRIPTION: '..DESC,Color.new(255,255,255))
screen:print(10,50,'R to activate/deactivate USB',Color.new(255,255,255))
screen:print(10,60,'Start to go back to the calling file',Color.new(255,255,255))
screen.waitVblankStart()
screen.flip()
end
end
end
--Main function
function dofile(filename)
local file = assert(io.open(filename,"r"))
if not file then
showLoop(false,filename)
return
end
local f_size = file:seek("end")
file:seek("set")
local new_chunk = file:read(f_size)
local callresult,callerror = loadstring(new_chunk)
if not callresult then
showLoop(callerror,filename)
else
local finalresult,err = pcall(callresult)
if not result then
showLoop(err,filename)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment