db.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. -- testing debug library
  2. debug = require "debug"
  3. local function dostring(s) return assert(load(s))() end
  4. print"testing debug library and debug information"
  5. do
  6. local a=1
  7. end
  8. function test (s, l, p)
  9. collectgarbage() -- avoid gc during trace
  10. local function f (event, line)
  11. assert(event == 'line')
  12. local l = table.remove(l, 1)
  13. if p then print(l, line) end
  14. assert(l == line, "wrong trace!!")
  15. end
  16. debug.sethook(f,"l"); load(s)(); debug.sethook()
  17. assert(#l == 0)
  18. end
  19. do
  20. assert(not pcall(debug.getinfo, print, "X")) -- invalid option
  21. assert(debug.getinfo(1000) == nil) -- out of range level
  22. assert(debug.getinfo(-1) == nil) -- out of range level
  23. local a = debug.getinfo(print)
  24. assert(a.what == "C#" and a.short_src == "[C#]") -- changed C to C#
  25. a = debug.getinfo(print, "L")
  26. assert(a.activelines == nil)
  27. local b = debug.getinfo(test, "SfL")
  28. assert(b.name == nil and b.what == "Lua" and b.linedefined == 13 and
  29. b.lastlinedefined == b.linedefined + 10 and
  30. b.func == test and not string.find(b.short_src, "%["))
  31. assert(b.activelines[b.linedefined + 1] and
  32. b.activelines[b.lastlinedefined])
  33. assert(not b.activelines[b.linedefined] and
  34. not b.activelines[b.lastlinedefined + 1])
  35. end
  36. -- test file and string names truncation
  37. a = "function f () end"
  38. local function dostring (s, x) return load(s, x)() end
  39. dostring(a)
  40. assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
  41. dostring(a..string.format("; %s\n=1", string.rep('p', 400)))
  42. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  43. dostring(a..string.format("; %s=1", string.rep('p', 400)))
  44. assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
  45. dostring("\n"..a)
  46. assert(debug.getinfo(f).short_src == '[string "..."]')
  47. dostring(a, "")
  48. assert(debug.getinfo(f).short_src == '[string ""]')
  49. dostring(a, "@xuxu")
  50. assert(debug.getinfo(f).short_src == "xuxu")
  51. dostring(a, "@"..string.rep('p', 1000)..'t')
  52. assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
  53. dostring(a, "=xuxu")
  54. assert(debug.getinfo(f).short_src == "xuxu")
  55. dostring(a, string.format("=%s", string.rep('x', 500)))
  56. assert(string.find(debug.getinfo(f).short_src, "^x*$"))
  57. dostring(a, "=")
  58. assert(debug.getinfo(f).short_src == "")
  59. a = nil; f = nil;
  60. repeat
  61. local g = {x = function ()
  62. local a = debug.getinfo(2)
  63. assert(a.name == 'f' and a.namewhat == 'local')
  64. a = debug.getinfo(1)
  65. assert(a.name == 'x' and a.namewhat == 'field')
  66. return 'xixi'
  67. end}
  68. local f = function () return 1+1 and (not 1 or g.x()) end
  69. assert(f() == 'xixi')
  70. g = debug.getinfo(f)
  71. assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)
  72. function f (x, name) -- local!
  73. name = name or 'f'
  74. local a = debug.getinfo(1)
  75. assert(a.name == name and a.namewhat == 'local')
  76. return x
  77. end
  78. -- breaks in different conditions
  79. if 3>4 then break end; f()
  80. if 3<4 then a=1 else break end; f()
  81. while 1 do local x=10; break end; f()
  82. local b = 1
  83. if 3>4 then return math.sin(1) end; f()
  84. a = 3<4; f()
  85. a = 3<4 or 1; f()
  86. repeat local x=20; if 4>3 then f() else break end; f() until 1
  87. g = {}
  88. f(g).x = f(2) and f(10)+f(9)
  89. assert(g.x == f(19))
  90. function g(x) if not x then return 3 end return (x('a', 'x')) end
  91. assert(g(f) == 'a')
  92. until 1
  93. test([[if
  94. math.sin(1)
  95. then
  96. a=1
  97. else
  98. a=2
  99. end
  100. ]], {2,3,4,7})
  101. test([[--
  102. if nil then
  103. a=1
  104. else
  105. a=2
  106. end
  107. ]], {2,5,6})
  108. test([[a=1
  109. repeat
  110. a=a+1
  111. until a==3
  112. ]], {1,3,4,3,4})
  113. test([[ do
  114. return
  115. end
  116. ]], {2})
  117. test([[local a
  118. a=1
  119. while a<=3 do
  120. a=a+1
  121. end
  122. ]], {1,2,3,4,3,4,3,4,3,5})
  123. test([[while math.sin(1) do
  124. if math.sin(1)
  125. then break
  126. end
  127. end
  128. a=1]], {1,2,3,6})
  129. test([[for i=1,3 do
  130. a=i
  131. end
  132. ]], {1,2,1,2,1,2,1,3})
  133. test([[for i,v in pairs{'a','b'} do
  134. a=i..v
  135. end
  136. ]], {1,2,1,2,1,3})
  137. test([[for i=1,4 do a=1 end]], {1,1,1,1,1})
  138. print'+'
  139. -- invalid levels in [gs]etlocal
  140. assert(not pcall(debug.getlocal, 20, 1))
  141. assert(not pcall(debug.setlocal, -1, 1, 10))
  142. -- parameter names
  143. local function foo (a,b,...) local d, e end
  144. local co = coroutine.create(foo)
  145. assert(debug.getlocal(foo, 1) == 'a')
  146. assert(debug.getlocal(foo, 2) == 'b')
  147. assert(debug.getlocal(foo, 3) == nil)
  148. assert(debug.getlocal(co, foo, 1) == 'a')
  149. assert(debug.getlocal(co, foo, 2) == 'b')
  150. assert(debug.getlocal(co, foo, 3) == nil)
  151. assert(debug.getlocal(print, 1) == nil)
  152. -- varargs
  153. local function foo (a, ...)
  154. local t = table.pack(...)
  155. for i = 1, t.n do
  156. local n, v = debug.getlocal(1, -i)
  157. assert(n == "(*vararg)" and v == t[i])
  158. end
  159. assert(not debug.getlocal(1, -(t.n + 1)))
  160. assert(not debug.setlocal(1, -(t.n + 1), 30))
  161. if t.n > 0 then
  162. (function (x)
  163. assert(debug.setlocal(2, -1, x) == "(*vararg)")
  164. assert(debug.setlocal(2, -t.n, x) == "(*vararg)")
  165. end)(430)
  166. assert(... == 430)
  167. end
  168. end
  169. foo()
  170. foo(print)
  171. foo(200, 3, 4)
  172. local a = {}
  173. for i = 1,1000 do a[i] = i end
  174. foo(table.unpack(a))
  175. a = nil
  176. -- access to vararg in non-vararg function
  177. local function foo () return debug.getlocal(1, -1) end
  178. assert(foo(10) == nil)
  179. a = {}; L = nil
  180. local glob = 1
  181. local oldglob = glob
  182. debug.sethook(function (e,l)
  183. collectgarbage() -- force GC during a hook
  184. local f, m, c = debug.gethook()
  185. assert(m == 'crl' and c == 0)
  186. if e == "line" then
  187. if glob ~= oldglob then
  188. L = l-1 -- get the first line where "glob" has changed
  189. oldglob = glob
  190. end
  191. elseif e == "call" then
  192. local f = debug.getinfo(2, "f").func
  193. a[f] = 1
  194. else assert(e == "return")
  195. end
  196. end, "crl")
  197. function f(a,b)
  198. collectgarbage()
  199. local _, x = debug.getlocal(1, 1)
  200. local _, y = debug.getlocal(1, 2)
  201. assert(x == a and y == b)
  202. assert(debug.setlocal(2, 3, "pera") == "AA".."AA")
  203. assert(debug.setlocal(2, 4, "ma��") == "B")
  204. x = debug.getinfo(2)
  205. assert(x.func == g and x.what == "Lua" and x.name == 'g' and
  206. x.nups == 1 and string.find(x.source, "^@.*db%.lua$"))
  207. glob = glob+1
  208. assert(debug.getinfo(1, "l").currentline == L+1)
  209. assert(debug.getinfo(1, "l").currentline == L+2)
  210. end
  211. function foo()
  212. glob = glob+1
  213. assert(debug.getinfo(1, "l").currentline == L+1)
  214. end; foo() -- set L
  215. -- check line counting inside strings and empty lines
  216. _ = 'alo\
  217. alo' .. [[
  218. ]]
  219. --[[
  220. ]]
  221. assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
  222. function g(...)
  223. local arg = {...}
  224. do local a,b,c; a=math.sin(40); end
  225. local feijao
  226. local AAAA,B = "xuxu", "mam�o"
  227. f(AAAA,B)
  228. assert(AAAA == "pera" and B == "ma��")
  229. do
  230. local B = 13
  231. local x,y = debug.getlocal(1,5)
  232. assert(x == 'B' and y == 13)
  233. end
  234. end
  235. g()
  236. assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print])
  237. -- tests for manipulating non-registered locals (C and Lua temporaries)
  238. local n, v = debug.getlocal(0, 1)
  239. assert(v == 0 and n == "(*temporary)")
  240. local n, v = debug.getlocal(0, 2)
  241. assert(v == 2 and n == "(*temporary)")
  242. assert(not debug.getlocal(0, 3))
  243. assert(not debug.getlocal(0, 0))
  244. function f()
  245. assert(select(2, debug.getlocal(2,3)) == 1)
  246. assert(not debug.getlocal(2,4))
  247. debug.setlocal(2, 3, 10)
  248. return 20
  249. end
  250. function g(a,b) return (a+1) + f() end
  251. assert(g(0,0) == 30)
  252. debug.sethook(nil);
  253. assert(debug.gethook() == nil)
  254. -- testing access to function arguments
  255. X = nil
  256. a = {}
  257. function a:f (a, b, ...) local arg = {...}; local c = 13 end
  258. debug.sethook(function (e)
  259. assert(e == "call")
  260. dostring("XX = 12") -- test dostring inside hooks
  261. -- testing errors inside hooks
  262. assert(not pcall(load("a='joao'+1")))
  263. debug.sethook(function (e, l)
  264. assert(debug.getinfo(2, "l").currentline == l)
  265. local f,m,c = debug.gethook()
  266. assert(e == "line")
  267. assert(m == 'l' and c == 0)
  268. debug.sethook(nil) -- hook is called only once
  269. assert(not X) -- check that
  270. X = {}; local i = 1
  271. local x,y
  272. while 1 do
  273. x,y = debug.getlocal(2, i)
  274. if x==nil then break end
  275. X[x] = y
  276. i = i+1
  277. end
  278. end, "l")
  279. end, "c")
  280. a:f(1,2,3,4,5)
  281. assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
  282. assert(XX == 12)
  283. assert(debug.gethook() == nil)
  284. -- testing upvalue access
  285. local function getupvalues (f)
  286. local t = {}
  287. local i = 1
  288. while true do
  289. local name, value = debug.getupvalue(f, i)
  290. if not name then break end
  291. assert(not t[name])
  292. t[name] = value
  293. i = i + 1
  294. end
  295. return t
  296. end
  297. local a,b,c = 1,2,3
  298. local function foo1 (a) b = a; return c end
  299. local function foo2 (x) a = x; return c+b end
  300. assert(debug.getupvalue(foo1, 3) == nil)
  301. assert(debug.getupvalue(foo1, 0) == nil)
  302. assert(debug.setupvalue(foo1, 3, "xuxu") == nil)
  303. local t = getupvalues(foo1)
  304. assert(t.a == nil and t.b == 2 and t.c == 3)
  305. t = getupvalues(foo2)
  306. assert(t.a == 1 and t.b == 2 and t.c == 3)
  307. assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
  308. assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
  309. -- upvalues of C functions are allways "called" "" (the empty string)
  310. assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
  311. -- testing count hooks
  312. local a=0
  313. debug.sethook(function (e) a=a+1 end, "", 1)
  314. a=0; for i=1,1000 do end; assert(1000 < a and a < 1012)
  315. debug.sethook(function (e) a=a+1 end, "", 4)
  316. a=0; for i=1,1000 do end; assert(250 < a and a < 255)
  317. local f,m,c = debug.gethook()
  318. assert(m == "" and c == 4)
  319. debug.sethook(function (e) a=a+1 end, "", 4000)
  320. a=0; for i=1,1000 do end; assert(a == 0)
  321. if not _no32 then
  322. debug.sethook(print, "", 2^24 - 1) -- count upperbound
  323. local f,m,c = debug.gethook()
  324. assert(({debug.gethook()})[3] == 2^24 - 1)
  325. end
  326. debug.sethook()
  327. -- tests for tail calls
  328. local function f (x)
  329. if x then
  330. assert(debug.getinfo(1, "S").what == "Lua")
  331. assert(debug.getinfo(1, "t").istailcall == true)
  332. local tail = debug.getinfo(2)
  333. assert(tail.func == g1 and tail.istailcall == true)
  334. assert(debug.getinfo(3, "S").what == "main")
  335. print"+"
  336. end
  337. end
  338. function g(x) return f(x) end
  339. function g1(x) g(x) end
  340. local function h (x) local f=g1; return f(x) end
  341. h(true)
  342. local b = {}
  343. debug.sethook(function (e) table.insert(b, e) end, "cr")
  344. h(false)
  345. debug.sethook()
  346. local res = {"return", -- first return (from sethook)
  347. "call", "tail call", "call", "tail call",
  348. "return", "return",
  349. "call", -- last call (to sethook)
  350. }
  351. for i = 1, #res do assert(res[i] == table.remove(b, 1)) end
  352. b = 0
  353. debug.sethook(function (e)
  354. if e == "tail call" then
  355. b = b + 1
  356. assert(debug.getinfo(2, "t").istailcall == true)
  357. else
  358. assert(debug.getinfo(2, "t").istailcall == false)
  359. end
  360. end, "c")
  361. h(false)
  362. debug.sethook()
  363. assert(b == 2) -- two tail calls
  364. lim = 30000
  365. if _soft then limit = 3000 end
  366. local function foo (x)
  367. if x==0 then
  368. assert(debug.getinfo(2).what == "main")
  369. local info = debug.getinfo(1)
  370. assert(info.istailcall == true and info.func == foo)
  371. else return foo(x-1)
  372. end
  373. end
  374. foo(lim)
  375. print"+"
  376. -- testing local function information
  377. co = load[[
  378. local A = function ()
  379. return x
  380. end
  381. return
  382. ]]
  383. local a = 0
  384. -- 'A' should be visible to debugger only after its complete definition
  385. debug.sethook(function (e, l)
  386. if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == nil)-- assert(debug.getlocal(2, 1) == "(*temporary)") --changed behavior Lua-CSharp
  387. elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
  388. end
  389. end, "l")
  390. co() -- run local function definition
  391. debug.sethook() -- turn off hook
  392. assert(a == 2) -- ensure all two lines where hooked
  393. -- testing traceback
  394. assert(debug.traceback(print) == print)
  395. assert(debug.traceback(print, 4) == print)
  396. assert(string.find(debug.traceback("hi", 4), "^hi\n"))
  397. assert(string.find(debug.traceback("hi"), "^hi\n"))
  398. assert(not string.find(debug.traceback("hi"), "'traceback'"))
  399. assert(string.find(debug.traceback("hi", 0), "'traceback'"))
  400. assert(string.find(debug.traceback(), "^stack traceback:\n"))
  401. -- testing nparams, nups e isvararg
  402. local t = debug.getinfo(print, "u")
  403. assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
  404. t = debug.getinfo(function (a,b,c) end, "u")
  405. assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
  406. t = debug.getinfo(function (a,b,...) return t[a] end, "u")
  407. assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
  408. t = debug.getinfo(1) -- main
  409. assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
  410. debug.getupvalue(t.func, 1) == "_ENV")
  411. -- testing debugging of coroutines
  412. local function checktraceback (co, p, level)
  413. local tb = debug.traceback(co, nil, level)
  414. local i = 0
  415. for l in string.gmatch(tb, "[^\n]+\n?") do
  416. assert(i == 0 or string.find(l, p[i]))
  417. i = i+1
  418. end
  419. assert(p[i] == nil)
  420. end
  421. local function f (n)
  422. if n > 0 then f(n-1)
  423. else coroutine.yield() end
  424. end
  425. local co = coroutine.create(f)
  426. coroutine.resume(co, 3)
  427. checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"})
  428. checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1)
  429. checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2)
  430. checktraceback(co, {"db.lua"}, 4)
  431. checktraceback(co, {}, 40)
  432. co = coroutine.create(function (x)
  433. local a = 1
  434. coroutine.yield(debug.getinfo(1, "l"))
  435. coroutine.yield(debug.getinfo(1, "l").currentline)
  436. return a
  437. end)
  438. local tr = {}
  439. local foo = function (e, l) if l then table.insert(tr, l) end end
  440. debug.sethook(co, foo, "lcr")
  441. local _, l = coroutine.resume(co, 10)
  442. local x = debug.getinfo(co, 1, "lfLS")
  443. assert(x.currentline == l.currentline and x.activelines[x.currentline])
  444. assert(type(x.func) == "function")
  445. for i=x.linedefined + 1, x.lastlinedefined do
  446. assert(x.activelines[i])
  447. x.activelines[i] = nil
  448. end
  449. assert(next(x.activelines) == nil) -- no 'extra' elements
  450. assert(debug.getinfo(co, 2) == nil)
  451. local a,b = debug.getlocal(co, 1, 1)
  452. assert(a == "x" and b == 10)
  453. a,b = debug.getlocal(co, 1, 2)
  454. assert(a == "a" and b == 1)
  455. debug.setlocal(co, 1, 2, "hi")
  456. assert(debug.gethook(co) == foo)
  457. assert(#tr == 2 and
  458. tr[1] == l.currentline-1 and tr[2] == l.currentline)
  459. a,b,c = pcall(coroutine.resume, co)
  460. assert(a and b and c == l.currentline+1)
  461. checktraceback(co, {"yield", "in function <"})
  462. a,b = coroutine.resume(co)
  463. assert(a and b == "hi")
  464. assert(#tr == 4 and tr[4] == l.currentline+2)
  465. assert(debug.gethook(co) == foo)
  466. assert(debug.gethook() == nil)
  467. checktraceback(co, {})
  468. -- check traceback of suspended (or dead with error) coroutines
  469. function f(i) if i==0 then error(i) else coroutine.yield(); f(i-1) end end
  470. co = coroutine.create(function (x) f(x) end)
  471. a, b = coroutine.resume(co, 3)
  472. t = {"'yield'", "'f'", "in function <"}
  473. while coroutine.status(co) == "suspended" do
  474. checktraceback(co, t)
  475. a, b = coroutine.resume(co)
  476. table.insert(t, 2, "'f'") -- one more recursive call to 'f'
  477. end
  478. t[1] = "'error'"
  479. checktraceback(co, t)
  480. -- test acessing line numbers of a coroutine from a resume inside
  481. -- a C function (this is a known bug in Lua 5.0)
  482. local function g(x)
  483. coroutine.yield(x)
  484. end
  485. local function f (i)
  486. debug.sethook(function () end, "l")
  487. for j=1,1000 do
  488. g(i+j)
  489. end
  490. end
  491. local co = coroutine.wrap(f)
  492. co(10)
  493. pcall(co)
  494. pcall(co)
  495. assert(type(debug.getregistry()) == "table")
  496. -- test tagmethod information
  497. local a = {}
  498. local function f (t)
  499. local info = debug.getinfo(1);
  500. assert(info.namewhat == "metamethod")
  501. a.op = info.name
  502. return info.name
  503. end
  504. setmetatable(a, {
  505. __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f;
  506. __eq = f; __le = f; __lt = f;
  507. })
  508. local b = setmetatable({}, getmetatable(a))
  509. assert(a[3] == "index" and a^3 == "pow" and a..a == "concat")
  510. assert(a/3 == "div" and 3%a == "mod")
  511. assert (a==b and a.op == "eq")
  512. assert (a>=b and a.op == "le")
  513. assert (a>b and a.op == "lt")
  514. print"OK"