我有以下3个功能:
function getName() -- returns filename without ext
local files = scandir(currentDir())
local name = nil
for i = 1, #files do
if isValidExt( getExt(files[i]) ) then
name = getFilename(files[i])
break
end
end
return name
end
function currentDir()
local string = debug.getinfo(1).source
local endPoint = getLastOcurrence(string, '/')
local dir = string.sub(string, 2, endPoint)
return dir
end
function getLastOcurrence(str, char)
local last = string.find(string.reverse(str), char, 1, true)
return #str - last
end
奇怪的是调试结束时没有问题,而运行时出现错误:
...\Downloader.lua:22: attempt to perform arithmetic on local 'last' (a nil value)
stack traceback:
...\Downloader.lua:22: in function 'getLastOcurrence'
...\Downloader.lua:39: in function 'currentDir'
...\Downloader.lua:50: in function 'getName'
什么可能导致调试和运行之间存在这种差异?