lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/db.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | -- testing debug library |
| 5 | |
| 6 | local debug = require "debug" |
| 7 | |
| 8 | local function dostring(s) return assert(load(s))() end |
| 9 | |
| 10 | print"testing debug library and debug information" |
| 11 | |
| 12 | do |
| 13 | local a=1 |
| 14 | end |
| 15 | |
| 16 | assert(not debug.gethook()) |
| 17 | |
| 18 | local testline = 19 -- line where 'test' is defined |
| 19 | local function test (s, l, p) -- this must be line 19 |
| 20 | collectgarbage() -- avoid gc during trace |
| 21 | local function f (event, line) |
| 22 | assert(event == 'line') |
| 23 | local l = table.remove(l, 1) |
| 24 | if p then print(l, line) end |
| 25 | assert(l == line, "wrong trace!!") |
| 26 | end |
| 27 | debug.sethook(f,"l"); load(s)(); debug.sethook() |
| 28 | assert(#l == 0) |
| 29 | end |
| 30 | |
| 31 | |
| 32 | do |
| 33 | assert(not pcall(debug.getinfo, print, "X")) -- invalid option |
| 34 | assert(not pcall(debug.getinfo, 0, ">")) -- invalid option |
| 35 | assert(not debug.getinfo(1000)) -- out of range level |
| 36 | assert(not debug.getinfo(-1)) -- out of range level |
| 37 | local a = debug.getinfo(print) |
| 38 | assert(a.what == "C" and a.short_src == "[C]") |
| 39 | a = debug.getinfo(print, "L") |
| 40 | assert(a.activelines == nil) |
| 41 | local b = debug.getinfo(test, "SfL") |
| 42 | assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and |
| 43 | b.lastlinedefined == b.linedefined + 10 and |
| 44 | b.func == test and not string.find(b.short_src, "%[")) |
| 45 | assert(b.activelines[b.linedefined + 1] and |
| 46 | b.activelines[b.lastlinedefined]) |
| 47 | assert(not b.activelines[b.linedefined] and |
| 48 | not b.activelines[b.lastlinedefined + 1]) |
| 49 | end |
| 50 | |
| 51 | |
| 52 | -- bug in 5.4.4-5.4.6: activelines in vararg functions |
| 53 | -- without debug information |
| 54 | do |
| 55 | local func = load(string.dump(load("print(10)"), true)) |
| 56 | local actl = debug.getinfo(func, "L").activelines |
| 57 | assert(#actl == 0) -- no line info |
| 58 | end |
| 59 | |
| 60 | |
| 61 | -- test file and string names truncation |
| 62 | local a = "function f () end" |
| 63 | local function dostring (s, x) return load(s, x)() end |
| 64 | dostring(a) |
| 65 | assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a)) |
| 66 | dostring(a..string.format("; %s\n=1", string.rep('p', 400))) |
| 67 | assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) |
| 68 | dostring(a..string.format("; %s=1", string.rep('p', 400))) |
| 69 | assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$')) |
| 70 | dostring("\n"..a) |
| 71 | assert(debug.getinfo(f).short_src == '[string "..."]') |
| 72 | dostring(a, "") |
| 73 | assert(debug.getinfo(f).short_src == '[string ""]') |
| 74 | dostring(a, "@xuxu") |
| 75 | assert(debug.getinfo(f).short_src == "xuxu") |
| 76 | dostring(a, "@"..string.rep('p', 1000)..'t') |
| 77 | assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$")) |
| 78 | dostring(a, "=xuxu") |
| 79 | assert(debug.getinfo(f).short_src == "xuxu") |
| 80 | dostring(a, string.format("=%s", string.rep('x', 500))) |
| 81 | assert(string.find(debug.getinfo(f).short_src, "^x*$")) |
| 82 | dostring(a, "=") |
| 83 | assert(debug.getinfo(f).short_src == "") |
| 84 | _G.a = nil; _G.f = nil; |
| 85 | _G[string.rep("p", 400)] = nil |
| 86 | |
| 87 | |
| 88 | repeat |
| 89 | local g = {x = function () |
| 90 | local a = debug.getinfo(2) |
| 91 | assert(a.name == 'f' and a.namewhat == 'local') |
| 92 | a = debug.getinfo(1) |
| 93 | assert(a.name == 'x' and a.namewhat == 'field') |
| 94 | return 'xixi' |
| 95 | end} |
| 96 | local f = function () return 1+1 and (not 1 or g.x()) end |
| 97 | assert(f() == 'xixi') |
| 98 | g = debug.getinfo(f) |
| 99 | assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name) |
| 100 | |
| 101 | function f (x, name) -- local! |
| 102 | name = name or 'f' |
| 103 | local a = debug.getinfo(1) |
| 104 | assert(a.name == name and a.namewhat == 'local') |
| 105 | return x |
| 106 | end |
| 107 | |
| 108 | -- breaks in different conditions |
| 109 | if 3>4 then break end; f() |
| 110 | if 3<4 then a=1 else break end; f() |
| 111 | while 1 do local x=10; break end; f() |
| 112 | local b = 1 |
| 113 | if 3>4 then return math.sin(1) end; f() |
| 114 | a = 3<4; f() |
| 115 | a = 3<4 or 1; f() |
| 116 | repeat local x=20; if 4>3 then f() else break end; f() until 1 |
| 117 | g = {} |
| 118 | f(g).x = f(2) and f(10)+f(9) |
| 119 | assert(g.x == f(19)) |
| 120 | function g(x) if not x then return 3 end return (x('a', 'x')) end |
| 121 | assert(g(f) == 'a') |
| 122 | until 1 |
| 123 | |
| 124 | test([[if |
| 125 | math.sin(1) |
| 126 | then |
| 127 | a=1 |
| 128 | else |
| 129 | a=2 |
| 130 | end |
| 131 | ]], {2,4,7}) |
| 132 | |
| 133 | |
| 134 | test([[ |
| 135 | local function foo() |
| 136 | end |
| 137 | foo() |
| 138 | A = 1 |
| 139 | A = 2 |
| 140 | A = 3 |
| 141 | ]], {2, 3, 2, 4, 5, 6}) |
| 142 | _G.A = nil |
| 143 | |
| 144 | |
| 145 | test([[-- |
| 146 | if nil then |
| 147 | a=1 |
| 148 | else |
| 149 | a=2 |
| 150 | end |
| 151 | ]], {2,5,6}) |
| 152 | |
| 153 | test([[a=1 |
| 154 | repeat |
| 155 | a=a+1 |
| 156 | until a==3 |
| 157 | ]], {1,3,4,3,4}) |
| 158 | |
| 159 | test([[ do |
| 160 | return |
| 161 | end |
| 162 | ]], {2}) |
| 163 | |
| 164 | test([[local a |
| 165 | a=1 |
| 166 | while a<=3 do |
| 167 | a=a+1 |
| 168 | end |
| 169 | ]], {1,2,3,4,3,4,3,4,3,5}) |
| 170 | |
| 171 | test([[while math.sin(1) do |
| 172 | if math.sin(1) |
| 173 | then break |
| 174 | end |
| 175 | end |
| 176 | a=1]], {1,2,3,6}) |
| 177 | |
| 178 | test([[for i=1,3 do |
| 179 | a=i |
| 180 | end |
| 181 | ]], {1,2,1,2,1,2,1,3}) |
| 182 | |
| 183 | test([[for i,v in pairs{'a','b'} do |
| 184 | a=tostring(i) .. v |
| 185 | end |
| 186 | ]], {1,2,1,2,1,3}) |
| 187 | |
| 188 | test([[for i=1,4 do a=1 end]], {1,1,1,1}) |
| 189 | |
| 190 | _G.a = nil |
| 191 | |
| 192 | |
| 193 | do -- testing line info/trace with large gaps in source |
| 194 | |
| 195 | local a = {1, 2, 3, 10, 124, 125, 126, 127, 128, 129, 130, |
| 196 | 255, 256, 257, 500, 1000} |
| 197 | local s = [[ |
| 198 | local b = {10} |
| 199 | a = b[1] X + Y b[1] |
| 200 | b = 4 |
| 201 | ]] |
| 202 | for _, i in ipairs(a) do |
| 203 | local subs = {X = string.rep("\n", i)} |
| 204 | for _, j in ipairs(a) do |
| 205 | subs.Y = string.rep("\n", j) |
| 206 | local s = string.gsub(s, "[XY]", subs) |
| 207 | test(s, {1, 2 + i, 2 + i + j, 2 + i, 2 + i + j, 3 + i + j}) |
| 208 | end |
| 209 | end |
| 210 | end |
| 211 | _G.a = nil |
| 212 | |
| 213 | |
| 214 | do -- testing active lines |
| 215 | local function checkactivelines (f, lines) |
| 216 | local t = debug.getinfo(f, "SL") |
| 217 | for _, l in pairs(lines) do |
| 218 | l = l + t.linedefined |
| 219 | assert(t.activelines[l]) |
| 220 | t.activelines[l] = undef |
| 221 | end |
| 222 | assert(next(t.activelines) == nil) -- no extra lines |
| 223 | end |
| 224 | |
| 225 | checkactivelines(function (...) -- vararg function |
| 226 | -- 1st line is empty |
| 227 | -- 2nd line is empty |
| 228 | -- 3th line is empty |
| 229 | local a = 20 |
| 230 | -- 5th line is empty |
| 231 | local b = 30 |
| 232 | -- 7th line is empty |
| 233 | end, {4, 6, 8}) |
| 234 | |
| 235 | checkactivelines(function (a) |
| 236 | -- 1st line is empty |
| 237 | -- 2nd line is empty |
| 238 | local a = 20 |
| 239 | local b = 30 |
| 240 | -- 5th line is empty |
| 241 | end, {3, 4, 6}) |
| 242 | |
| 243 | checkactivelines(function (a, b, ...) end, {0}) |
| 244 | |
| 245 | checkactivelines(function (a, b) |
| 246 | end, {1}) |
| 247 | |
| 248 | for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do |
| 249 | checkactivelines( |
| 250 | load(string.format("%s return 1", string.rep("\n", n))), |
| 251 | {n + 1}) |
| 252 | end |
| 253 | |
| 254 | end |
| 255 | |
| 256 | print'+' |
| 257 | |
| 258 | -- invalid levels in [gs]etlocal |
| 259 | assert(not pcall(debug.getlocal, 20, 1)) |
| 260 | assert(not pcall(debug.setlocal, -1, 1, 10)) |
| 261 | assert(not debug.getlocal(test, -2147483648)) |
| 262 | |
| 263 | |
| 264 | -- parameter names |
| 265 | local function foo (a,b,...) local d, e end |
| 266 | local co = coroutine.create(foo) |
| 267 | |
| 268 | assert(debug.getlocal(foo, 1) == 'a') |
| 269 | assert(debug.getlocal(foo, 2) == 'b') |
| 270 | assert(not debug.getlocal(foo, 3)) |
| 271 | assert(debug.getlocal(co, foo, 1) == 'a') |
| 272 | assert(debug.getlocal(co, foo, 2) == 'b') |
| 273 | assert(not debug.getlocal(co, foo, 3)) |
| 274 | |
| 275 | assert(not debug.getlocal(print, 1)) |
| 276 | |
| 277 | |
| 278 | local function foo () return (debug.getlocal(1, -1)) end |
| 279 | assert(not foo(10)) |
| 280 | |
| 281 | |
| 282 | -- varargs |
| 283 | local function foo (a, ...) |
| 284 | local t = table.pack(...) |
| 285 | for i = 1, t.n do |
| 286 | local n, v = debug.getlocal(1, -i) |
| 287 | assert(n == "(vararg)" and v == t[i]) |
| 288 | end |
| 289 | assert(not debug.getlocal(1, -(t.n + 1))) |
| 290 | assert(not debug.setlocal(1, -(t.n + 1), 30)) |
| 291 | if t.n > 0 then |
| 292 | (function (x) |
| 293 | assert(debug.setlocal(2, -1, x) == "(vararg)") |
| 294 | assert(debug.setlocal(2, -t.n, x) == "(vararg)") |
| 295 | end)(430) |
| 296 | assert(... == 430) |
| 297 | end |
| 298 | end |
| 299 | |
| 300 | foo() |
| 301 | foo(print) |
| 302 | foo(200, 3, 4) |
| 303 | local a = {} |
| 304 | for i = 1, (_soft and 100 or 1000) do a[i] = i end |
| 305 | foo(table.unpack(a)) |
| 306 | |
| 307 | |
| 308 | |
| 309 | do -- test hook presence in debug info |
| 310 | assert(not debug.gethook()) |
| 311 | local count = 0 |
| 312 | local function f () |
| 313 | assert(debug.getinfo(1).namewhat == "hook") |
| 314 | local sndline = string.match(debug.traceback(), "\n(.-)\n") |
| 315 | assert(string.find(sndline, "hook")) |
| 316 | count = count + 1 |
| 317 | end |
| 318 | debug.sethook(f, "l") |
| 319 | local a = 0 |
| 320 | _ENV.a = a |
| 321 | a = 1 |
| 322 | debug.sethook() |
| 323 | assert(count == 4) |
| 324 | end |
| 325 | _ENV.a = nil |
| 326 | |
| 327 | |
| 328 | -- hook table has weak keys |
| 329 | assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k') |
| 330 | |
| 331 | |
| 332 | a = {}; local L = nil |
| 333 | local glob = 1 |
| 334 | local oldglob = glob |
| 335 | debug.sethook(function (e,l) |
| 336 | collectgarbage() -- force GC during a hook |
| 337 | local f, m, c = debug.gethook() |
| 338 | assert(m == 'crl' and c == 0) |
| 339 | if e == "line" then |
| 340 | if glob ~= oldglob then |
| 341 | L = l-1 -- get the first line where "glob" has changed |
| 342 | oldglob = glob |
| 343 | end |
| 344 | elseif e == "call" then |
| 345 | local f = debug.getinfo(2, "f").func |
| 346 | a[f] = 1 |
| 347 | else assert(e == "return") |
| 348 | end |
| 349 | end, "crl") |
| 350 | |
| 351 | |
| 352 | function f(a,b) |
| 353 | -- declare some globals to check that they don't interfere with 'getlocal' |
| 354 | global collectgarbage |
| 355 | collectgarbage() |
| 356 | local _, x = debug.getlocal(1, 1) |
| 357 | global assert, g, string |
| 358 | local _, y = debug.getlocal(1, 2) |
| 359 | assert(x == a and y == b) |
| 360 | assert(debug.setlocal(2, 4, "pera") == "AA".."AA") |
| 361 | assert(debug.setlocal(2, 5, "manga") == "B") |
| 362 | x = debug.getinfo(2) |
| 363 | assert(x.func == g and x.what == "Lua" and x.name == 'g' and |
| 364 | x.nups == 2 and string.find(x.source, "^@.*db%.lua$")) |
| 365 | glob = glob+1 |
| 366 | assert(debug.getinfo(1, "l").currentline == L+1) |
| 367 | assert(debug.getinfo(1, "l").currentline == L+2) |
| 368 | end |
| 369 | |
| 370 | function foo() |
| 371 | glob = glob+1 |
| 372 | assert(debug.getinfo(1, "l").currentline == L+1) |
| 373 | end; foo() -- set L |
| 374 | -- check line counting inside strings and empty lines |
| 375 | |
| 376 | local _ = 'alo\ |
| 377 | alo' .. [[ |
| 378 | |
| 379 | ]] |
| 380 | --[[ |
| 381 | ]] |
| 382 | assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines |
| 383 | |
| 384 | |
| 385 | function g (...) |
| 386 | local arg = {...} |
| 387 | do local a,b,c; a=math.sin(40); end |
| 388 | local feijao |
| 389 | local AAAA,B = "xuxu", "abacate" |
| 390 | f(AAAA,B) |
| 391 | assert(AAAA == "pera" and B == "manga") |
| 392 | do |
| 393 | global * |
| 394 | local B = 13 |
| 395 | global<const> assert |
| 396 | local x,y = debug.getlocal(1,6) |
| 397 | assert(x == 'B' and y == 13) |
| 398 | end |
| 399 | end |
| 400 | |
| 401 | g() |
| 402 | |
| 403 | |
| 404 | assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print]) |
| 405 | |
| 406 | |
| 407 | -- tests for manipulating non-registered locals (C and Lua temporaries) |
| 408 | |
| 409 | local n, v = debug.getlocal(0, 1) |
| 410 | assert(v == 0 and n == "(C temporary)") |
| 411 | local n, v = debug.getlocal(0, 2) |
| 412 | assert(v == 2 and n == "(C temporary)") |
| 413 | assert(not debug.getlocal(0, 3)) |
| 414 | assert(not debug.getlocal(0, 0)) |
| 415 | |
| 416 | function f() |
| 417 | assert(select(2, debug.getlocal(2,3)) == 1) |
| 418 | assert(not debug.getlocal(2,4)) |
| 419 | debug.setlocal(2, 3, 10) |
| 420 | return 20 |
| 421 | end |
| 422 | |
| 423 | function g(a,b) return (a+1) + f() end |
| 424 | |
| 425 | assert(g(0,0) == 30) |
| 426 | |
| 427 | _G.f, _G.g = nil |
| 428 | |
| 429 | debug.sethook(nil); |
| 430 | assert(not debug.gethook()) |
| 431 | |
| 432 | |
| 433 | -- minimal tests for setuservalue/getuservalue |
| 434 | do |
| 435 | assert(not debug.setuservalue(io.stdin, 10)) |
| 436 | local a, b = debug.getuservalue(io.stdin, 10) |
| 437 | assert(a == nil and not b) |
| 438 | end |
| 439 | |
| 440 | -- testing interaction between multiple values x hooks |
| 441 | do |
| 442 | local function f(...) return 3, ... end |
| 443 | local count = 0 |
| 444 | local a = {} |
| 445 | for i = 1, 100 do a[i] = i end |
| 446 | debug.sethook(function () count = count + 1 end, "", 1) |
| 447 | local t = {table.unpack(a)} |
| 448 | assert(#t == 100) |
| 449 | t = {table.unpack(a, 1, 3)} |
| 450 | assert(#t == 3) |
| 451 | t = {f(table.unpack(a, 1, 30))} |
| 452 | assert(#t == 31) |
| 453 | end |
| 454 | |
| 455 | |
| 456 | -- testing access to function arguments |
| 457 | |
| 458 | local function collectlocals (level) |
| 459 | local tab = {} |
| 460 | for i = 1, math.huge do |
| 461 | local n, v = debug.getlocal(level + 1, i) |
| 462 | if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or |
| 463 | n == "(vararg table)") then |
| 464 | break -- consider only real variables |
| 465 | end |
| 466 | tab[n] = v |
| 467 | end |
| 468 | return tab |
| 469 | end |
| 470 | |
| 471 | |
| 472 | local X = nil |
| 473 | a = {} |
| 474 | function a:f (a, b, ...) local arg = {...}; local c = 13 end |
| 475 | debug.sethook(function (e) |
| 476 | assert(e == "call") |
| 477 | dostring("XX = 12") -- test dostring inside hooks |
| 478 | -- testing errors inside hooks |
| 479 | assert(not pcall(load("a='joao'+1"))) |
| 480 | debug.sethook(function (e, l) |
| 481 | assert(debug.getinfo(2, "l").currentline == l) |
| 482 | local f,m,c = debug.gethook() |
| 483 | assert(e == "line") |
| 484 | assert(m == 'l' and c == 0) |
| 485 | debug.sethook(nil) -- hook is called only once |
| 486 | assert(not X) -- check that |
| 487 | X = collectlocals(2) |
| 488 | end, "l") |
| 489 | end, "c") |
| 490 | |
| 491 | a:f(1,2,3,4,5) |
| 492 | assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil) |
| 493 | assert(XX == 12) |
| 494 | assert(not debug.gethook()) |
| 495 | _G.XX = nil |
| 496 | |
| 497 | |
| 498 | -- testing access to local variables in return hook (bug in 5.2) |
| 499 | do |
| 500 | local X = false |
| 501 | |
| 502 | local function foo (a, b, ...) |
| 503 | do local x,y,z end |
| 504 | local c, d = 10, 20 |
| 505 | return |
| 506 | end |
| 507 | |
| 508 | local function aux () |
| 509 | if debug.getinfo(2).name == "foo" then |
| 510 | X = true -- to signal that it found 'foo' |
| 511 | local tab = {a = 100, b = 200, c = 10, d = 20} |
| 512 | for n, v in pairs(collectlocals(2)) do |
| 513 | assert(tab[n] == v) |
| 514 | tab[n] = undef |
| 515 | end |
| 516 | assert(next(tab) == nil) -- 'tab' must be empty |
| 517 | end |
| 518 | end |
| 519 | |
| 520 | debug.sethook(aux, "r"); foo(100, 200); debug.sethook() |
| 521 | assert(X) |
| 522 | |
| 523 | end |
| 524 | |
| 525 | |
| 526 | local function eqseq (t1, t2) |
| 527 | assert(#t1 == #t2) |
| 528 | for i = 1, #t1 do |
| 529 | assert(t1[i] == t2[i]) |
| 530 | end |
| 531 | end |
| 532 | |
| 533 | |
| 534 | do print("testing inspection of parameters/returned values") |
| 535 | local on = false |
| 536 | local inp, out |
| 537 | |
| 538 | local function hook (event) |
| 539 | if not on then return end |
| 540 | local ar = debug.getinfo(2, "ruS") |
| 541 | local t = {} |
| 542 | for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do |
| 543 | local _, v = debug.getlocal(2, i) |
| 544 | t[#t + 1] = v |
| 545 | end |
| 546 | if event == "return" then |
| 547 | out = t |
| 548 | else |
| 549 | inp = t |
| 550 | end |
| 551 | end |
| 552 | |
| 553 | debug.sethook(hook, "cr") |
| 554 | |
| 555 | on = true; math.sin(3); on = false |
| 556 | eqseq(inp, {3}); eqseq(out, {math.sin(3)}) |
| 557 | |
| 558 | on = true; select(2, 10, 20, 30, 40); on = false |
| 559 | eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40}) |
| 560 | |
| 561 | local function foo (a, ...) return ... end |
| 562 | local function foo1 () on = not on; return foo(20, 10, 0) end |
| 563 | foo1(); on = false |
| 564 | eqseq(inp, {20}); eqseq(out, {10, 0}) |
| 565 | |
| 566 | debug.sethook() |
| 567 | end |
| 568 | |
| 569 | |
| 570 | |
| 571 | -- testing upvalue access |
| 572 | local function getupvalues (f) |
| 573 | local t = {} |
| 574 | local i = 1 |
| 575 | while true do |
| 576 | local name, value = debug.getupvalue(f, i) |
| 577 | if not name then break end |
| 578 | assert(not t[name]) |
| 579 | t[name] = value |
| 580 | i = i + 1 |
| 581 | end |
| 582 | return t |
| 583 | end |
| 584 | |
| 585 | local a,b,c = 1,2,3 |
| 586 | local function foo1 (a) b = a; return c end |
| 587 | local function foo2 (x) a = x; return c+b end |
| 588 | assert(not debug.getupvalue(foo1, 3)) |
| 589 | assert(not debug.getupvalue(foo1, 0)) |
| 590 | assert(not debug.setupvalue(foo1, 3, "xuxu")) |
| 591 | local t = getupvalues(foo1) |
| 592 | assert(t.a == nil and t.b == 2 and t.c == 3) |
| 593 | t = getupvalues(foo2) |
| 594 | assert(t.a == 1 and t.b == 2 and t.c == 3) |
| 595 | assert(debug.setupvalue(foo1, 1, "xuxu") == "b") |
| 596 | assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu") |
| 597 | -- upvalues of C functions are always named "" (the empty string) |
| 598 | assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "") |
| 599 | |
| 600 | |
| 601 | -- testing count hooks |
| 602 | local a=0 |
| 603 | debug.sethook(function (e) a=a+1 end, "", 1) |
| 604 | a=0; for i=1,1000 do end; assert(1000 < a and a < 1012) |
| 605 | debug.sethook(function (e) a=a+1 end, "", 4) |
| 606 | a=0; for i=1,1000 do end; assert(250 < a and a < 255) |
| 607 | local f,m,c = debug.gethook() |
| 608 | assert(m == "" and c == 4) |
| 609 | debug.sethook(function (e) a=a+1 end, "", 4000) |
| 610 | a=0; for i=1,1000 do end; assert(a == 0) |
| 611 | |
| 612 | do |
| 613 | debug.sethook(print, "", 2^24 - 1) -- count upperbound |
| 614 | local f,m,c = debug.gethook() |
| 615 | assert(({debug.gethook()})[3] == 2^24 - 1) |
| 616 | end |
| 617 | |
| 618 | debug.sethook() |
| 619 | |
| 620 | local g, g1 |
| 621 | |
| 622 | -- tests for tail calls |
| 623 | local function f (x) |
| 624 | if x then |
| 625 | assert(debug.getinfo(1, "S").what == "Lua") |
| 626 | assert(debug.getinfo(1, "t").istailcall == true) |
| 627 | local tail = debug.getinfo(2) |
| 628 | assert(tail.func == g1 and tail.istailcall == true) |
| 629 | assert(debug.getinfo(3, "S").what == "main") |
| 630 | print"+" |
| 631 | end |
| 632 | end |
| 633 | |
| 634 | assert(debug.getinfo(print, 't').istailcall == false) |
| 635 | assert(debug.getinfo(print, 't').extraargs == 0) |
| 636 | |
| 637 | function g(x) return f(x) end |
| 638 | |
| 639 | function g1(x) g(x) end |
| 640 | |
| 641 | local function h (x) local f=g1; return f(x) end |
| 642 | |
| 643 | h(true) |
| 644 | |
| 645 | local b = {} |
| 646 | debug.sethook(function (e) table.insert(b, e) end, "cr") |
| 647 | h(false) |
| 648 | debug.sethook() |
| 649 | local res = {"return", -- first return (from sethook) |
| 650 | "call", "tail call", "call", "tail call", |
| 651 | "return", "return", |
| 652 | "call", -- last call (to sethook) |
| 653 | } |
| 654 | for i = 1, #res do assert(res[i] == table.remove(b, 1)) end |
| 655 | |
| 656 | b = 0 |
| 657 | debug.sethook(function (e) |
| 658 | if e == "tail call" then |
| 659 | b = b + 1 |
| 660 | assert(debug.getinfo(2, "t").istailcall == true) |
| 661 | else |
| 662 | assert(debug.getinfo(2, "t").istailcall == false) |
| 663 | end |
| 664 | end, "c") |
| 665 | h(false) |
| 666 | debug.sethook() |
| 667 | assert(b == 2) -- two tail calls |
| 668 | |
| 669 | local lim = _soft and 3000 or 30000 |
| 670 | local function foo (x) |
| 671 | if x==0 then |
| 672 | assert(debug.getinfo(2).what == "main") |
| 673 | local info = debug.getinfo(1) |
| 674 | assert(info.istailcall == true and info.func == foo) |
| 675 | else return foo(x-1) |
| 676 | end |
| 677 | end |
| 678 | |
| 679 | foo(lim) |
| 680 | |
| 681 | |
| 682 | print"+" |
| 683 | |
| 684 | |
| 685 | -- testing local function information |
| 686 | co = load[[ |
| 687 | local A = function () |
| 688 | return x |
| 689 | end |
| 690 | return |
| 691 | ]] |
| 692 | |
| 693 | local a = 0 |
| 694 | -- 'A' should be visible to debugger only after its complete definition |
| 695 | debug.sethook(function (e, l) |
| 696 | if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)") |
| 697 | elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A") |
| 698 | end |
| 699 | end, "l") |
| 700 | co() -- run local function definition |
| 701 | debug.sethook() -- turn off hook |
| 702 | assert(a == 2) -- ensure all two lines where hooked |
| 703 | |
| 704 | -- testing traceback |
| 705 | |
| 706 | assert(debug.traceback(print) == print) |
| 707 | assert(debug.traceback(print, 4) == print) |
| 708 | assert(string.find(debug.traceback("hi", 4), "^hi\n")) |
| 709 | assert(string.find(debug.traceback("hi"), "^hi\n")) |
| 710 | assert(not string.find(debug.traceback("hi"), "'debug.traceback'")) |
| 711 | assert(string.find(debug.traceback("hi", 0), "'traceback'")) |
| 712 | assert(string.find(debug.traceback(), "^stack traceback:\n")) |
| 713 | |
| 714 | do -- C-function names in traceback |
| 715 | local st, msg = (function () return pcall end)()(debug.traceback) |
| 716 | assert(st == true and string.find(msg, "pcall")) |
| 717 | end |
| 718 | |
| 719 | |
| 720 | -- testing nparams, nups e isvararg |
| 721 | local t = debug.getinfo(print, "u") |
| 722 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) |
| 723 | |
| 724 | t = debug.getinfo(function (a,b,c) end, "u") |
| 725 | assert(t.isvararg == false and t.nparams == 3 and t.nups == 0) |
| 726 | |
| 727 | t = debug.getinfo(function (a,b,...) return t[a] end, "u") |
| 728 | assert(t.isvararg == true and t.nparams == 2 and t.nups == 1) |
| 729 | |
| 730 | t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u") |
| 731 | assert(t.isvararg == true and t.nparams == 2 and t.nups == 0) |
| 732 | |
| 733 | t = debug.getinfo(1) -- main |
| 734 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and |
| 735 | debug.getupvalue(t.func, 1) == "_ENV") |
| 736 | |
| 737 | t = debug.getinfo(math.sin) -- C function |
| 738 | assert(t.isvararg == true and t.nparams == 0 and t.nups == 0) |
| 739 | |
| 740 | t = debug.getinfo(string.gmatch("abc", "a")) -- C closure |
| 741 | assert(t.isvararg == true and t.nparams == 0 and t.nups > 0) |
| 742 | |
| 743 | |
| 744 | |
| 745 | -- testing debugging of coroutines |
| 746 | |
| 747 | local function checktraceback (co, p, level) |
| 748 | local tb = debug.traceback(co, nil, level) |
| 749 | local i = 0 |
| 750 | for l in string.gmatch(tb, "[^\n]+\n?") do |
| 751 | assert(i == 0 or string.find(l, p[i])) |
| 752 | i = i+1 |
| 753 | end |
| 754 | assert(p[i] == undef) |
| 755 | end |
| 756 | |
| 757 | |
| 758 | local function f (n) |
| 759 | if n > 0 then f(n-1) |
| 760 | else coroutine.yield() end |
| 761 | end |
| 762 | |
| 763 | local co = coroutine.create(f) |
| 764 | coroutine.resume(co, 3) |
| 765 | checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"}) |
| 766 | checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1) |
| 767 | checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2) |
| 768 | checktraceback(co, {"db.lua"}, 4) |
| 769 | checktraceback(co, {}, 40) |
| 770 | |
| 771 | |
| 772 | co = coroutine.create(function (x) |
| 773 | local a = 1 |
| 774 | coroutine.yield(debug.getinfo(1, "l")) |
| 775 | coroutine.yield(debug.getinfo(1, "l").currentline) |
| 776 | return a |
| 777 | end) |
| 778 | |
| 779 | local tr = {} |
| 780 | local foo = function (e, l) if l then table.insert(tr, l) end end |
| 781 | debug.sethook(co, foo, "lcr") |
| 782 | |
| 783 | local _, l = coroutine.resume(co, 10) |
| 784 | local x = debug.getinfo(co, 1, "lfLS") |
| 785 | assert(x.currentline == l.currentline and x.activelines[x.currentline]) |
| 786 | assert(type(x.func) == "function") |
| 787 | for i=x.linedefined + 1, x.lastlinedefined do |
| 788 | assert(x.activelines[i]) |
| 789 | x.activelines[i] = undef |
| 790 | end |
| 791 | assert(next(x.activelines) == nil) -- no 'extra' elements |
| 792 | assert(not debug.getinfo(co, 2)) |
| 793 | local a,b = debug.getlocal(co, 1, 1) |
| 794 | assert(a == "x" and b == 10) |
| 795 | a,b = debug.getlocal(co, 1, 2) |
| 796 | assert(a == "a" and b == 1) |
| 797 | debug.setlocal(co, 1, 2, "hi") |
| 798 | assert(debug.gethook(co) == foo) |
| 799 | assert(#tr == 2 and |
| 800 | tr[1] == l.currentline-1 and tr[2] == l.currentline) |
| 801 | |
| 802 | a,b,c = pcall(coroutine.resume, co) |
| 803 | assert(a and b and c == l.currentline+1) |
| 804 | checktraceback(co, {"yield", "in function <"}) |
| 805 | |
| 806 | a,b = coroutine.resume(co) |
| 807 | assert(a and b == "hi") |
| 808 | assert(#tr == 4 and tr[4] == l.currentline+2) |
| 809 | assert(debug.gethook(co) == foo) |
| 810 | assert(not debug.gethook()) |
| 811 | checktraceback(co, {}) |
| 812 | |
| 813 | |
| 814 | -- check get/setlocal in coroutines |
| 815 | co = coroutine.create(function (x) |
| 816 | local a, b = coroutine.yield(x) |
| 817 | assert(a == 100 and b == nil) |
| 818 | return x |
| 819 | end) |
| 820 | a, b = coroutine.resume(co, 10) |
| 821 | assert(a and b == 10) |
| 822 | a, b = debug.getlocal(co, 1, 1) |
| 823 | assert(a == "x" and b == 10) |
| 824 | assert(not debug.getlocal(co, 1, 5)) |
| 825 | assert(debug.setlocal(co, 1, 1, 30) == "x") |
| 826 | assert(not debug.setlocal(co, 1, 5, 40)) |
| 827 | a, b = coroutine.resume(co, 100) |
| 828 | assert(a and b == 30) |
| 829 | |
| 830 | |
| 831 | -- check traceback of suspended (or dead with error) coroutines |
| 832 | |
| 833 | function f(i) |
| 834 | if i == 0 then error(i) |
| 835 | else coroutine.yield(); f(i-1) |
| 836 | end |
| 837 | end |
| 838 | |
| 839 | |
| 840 | co = coroutine.create(function (x) f(x) end) |
| 841 | a, b = coroutine.resume(co, 3) |
| 842 | t = {"'yield'", "'f'", "in function <"} |
| 843 | while coroutine.status(co) == "suspended" do |
| 844 | checktraceback(co, t) |
| 845 | a, b = coroutine.resume(co) |
| 846 | table.insert(t, 2, "'f'") -- one more recursive call to 'f' |
| 847 | end |
| 848 | t[1] = "'error'" |
| 849 | checktraceback(co, t) |
| 850 | |
| 851 | |
| 852 | -- test accessing line numbers of a coroutine from a resume inside |
| 853 | -- a C function (this is a known bug in Lua 5.0) |
| 854 | |
| 855 | local function g(x) |
| 856 | coroutine.yield(x) |
| 857 | end |
| 858 | |
| 859 | local function f (i) |
| 860 | debug.sethook(function () end, "l") |
| 861 | for j=1,1000 do |
| 862 | g(i+j) |
| 863 | end |
| 864 | end |
| 865 | |
| 866 | local co = coroutine.wrap(f) |
| 867 | co(10) |
| 868 | pcall(co) |
| 869 | pcall(co) |
| 870 | |
| 871 | |
| 872 | assert(type(debug.getregistry()) == "table") |
| 873 | |
| 874 | |
| 875 | -- test tagmethod information |
| 876 | local a = {} |
| 877 | local function f (t) |
| 878 | local info = debug.getinfo(1); |
| 879 | assert(info.namewhat == "metamethod") |
| 880 | a.op = info.name |
| 881 | return info.name |
| 882 | end |
| 883 | setmetatable(a, { |
| 884 | __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f; |
| 885 | __mul = f; __idiv = f; __unm = f; __len = f; __sub = f; |
| 886 | __shl = f; __shr = f; __bor = f; __bxor = f; |
| 887 | __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f; |
| 888 | __bnot = f; |
| 889 | }) |
| 890 | |
| 891 | local b = setmetatable({}, getmetatable(a)) |
| 892 | |
| 893 | assert(a[3] == "index" and a^3 == "pow" and a..a == "concat") |
| 894 | assert(a/3 == "div" and 3%a == "mod") |
| 895 | assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and |
| 896 | -a == "unm" and #a == "len" and a&3 == "band") |
| 897 | assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and |
| 898 | -a == "unm" and #a == "len" and a & 3 == "band") |
| 899 | assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr") |
| 900 | assert (a==b and a.op == "eq") |
| 901 | assert (a>=b and a.op == "le") |
| 902 | assert ("x">=a and a.op == "le") |
| 903 | assert (a>b and a.op == "lt") |
| 904 | assert (a>10 and a.op == "lt") |
| 905 | assert(~a == "bnot") |
| 906 | |
| 907 | do -- testing for-iterator name |
| 908 | local function f() |
| 909 | assert(debug.getinfo(1).name == "for iterator") |
| 910 | end |
| 911 | |
| 912 | for i in f do end |
| 913 | end |
| 914 | |
| 915 | |
| 916 | do -- testing debug info for finalizers |
| 917 | local name = nil |
| 918 | |
| 919 | -- create a piece of garbage with a finalizer |
| 920 | setmetatable({}, {__gc = function () |
| 921 | local t = debug.getinfo(1) -- get function information |
| 922 | assert(t.namewhat == "metamethod") |
| 923 | name = t.name |
| 924 | end}) |
| 925 | |
| 926 | -- repeat until previous finalizer runs (setting 'name') |
| 927 | repeat local a = {} until name |
| 928 | assert(name == "__gc") |
| 929 | end |
| 930 | |
| 931 | |
| 932 | do |
| 933 | print("testing traceback sizes") |
| 934 | |
| 935 | local function countlines (s) |
| 936 | return select(2, string.gsub(s, "\n", "")) |
| 937 | end |
| 938 | |
| 939 | local function deep (lvl, n) |
| 940 | if lvl == 0 then |
| 941 | return (debug.traceback("message", n)) |
| 942 | else |
| 943 | return (deep(lvl-1, n)) |
| 944 | end |
| 945 | end |
| 946 | |
| 947 | local function checkdeep (total, start) |
| 948 | local s = deep(total, start) |
| 949 | local rest = string.match(s, "^message\nstack traceback:\n(.*)$") |
| 950 | local cl = countlines(rest) |
| 951 | -- at most 10 lines in first part, 11 in second, plus '...' |
| 952 | assert(cl <= 10 + 11 + 1) |
| 953 | local brk = string.find(rest, "%.%.%.\t%(skip") |
| 954 | if brk then -- does message have '...'? |
| 955 | local rest1 = string.sub(rest, 1, brk) |
| 956 | local rest2 = string.sub(rest, brk, #rest) |
| 957 | assert(countlines(rest1) == 10 and countlines(rest2) == 11) |
| 958 | else |
| 959 | assert(cl == total - start + 2) |
| 960 | end |
| 961 | end |
| 962 | |
| 963 | for d = 1, 51, 10 do |
| 964 | for l = 1, d do |
| 965 | -- use coroutines to ensure complete control of the stack |
| 966 | coroutine.wrap(checkdeep)(d, l) |
| 967 | end |
| 968 | end |
| 969 | |
| 970 | end |
| 971 | |
| 972 | |
| 973 | print("testing debug functions on chunk without debug info") |
| 974 | local prog = [[-- program to be loaded without debug information (strip) |
| 975 | local debug = require'debug' |
| 976 | local a = 12 -- a local variable |
| 977 | |
| 978 | local n, v = debug.getlocal(1, 1) |
| 979 | assert(n == "(temporary)" and v == debug) -- unknown name but known value |
| 980 | n, v = debug.getlocal(1, 2) |
| 981 | assert(n == "(temporary)" and v == 12) -- unknown name but known value |
| 982 | |
| 983 | -- a function with an upvalue |
| 984 | local f = function () local x; return a end |
| 985 | n, v = debug.getupvalue(f, 1) |
| 986 | assert(n == "(no name)" and v == 12) |
| 987 | assert(debug.setupvalue(f, 1, 13) == "(no name)") |
| 988 | assert(a == 13) |
| 989 | |
| 990 | local t = debug.getinfo(f) |
| 991 | assert(t.name == nil and t.linedefined > 0 and |
| 992 | t.lastlinedefined == t.linedefined and |
| 993 | t.short_src == "?") |
| 994 | assert(debug.getinfo(1).currentline == -1) |
| 995 | |
| 996 | t = debug.getinfo(f, "L").activelines |
| 997 | assert(next(t) == nil) -- active lines are empty |
| 998 | |
| 999 | -- dump/load a function without debug info |
| 1000 | f = load(string.dump(f)) |
| 1001 | |
| 1002 | t = debug.getinfo(f) |
| 1003 | assert(t.name == nil and t.linedefined > 0 and |
| 1004 | t.lastlinedefined == t.linedefined and |
| 1005 | t.short_src == "?") |
| 1006 | assert(debug.getinfo(1).currentline == -1) |
| 1007 | |
| 1008 | return a |
| 1009 | ]] |
| 1010 | |
| 1011 | |
| 1012 | -- load 'prog' without debug info |
| 1013 | local f = assert(load(string.dump(load(prog), true))) |
| 1014 | |
| 1015 | assert(f() == 13) |
| 1016 | |
| 1017 | do -- bug in 5.4.0: line hooks in stripped code |
| 1018 | local function foo () |
| 1019 | local a = 1 |
| 1020 | local b = 2 |
| 1021 | return b |
| 1022 | end |
| 1023 | |
| 1024 | local s = load(string.dump(foo, true)) |
| 1025 | local line = true |
| 1026 | debug.sethook(function (e, l) |
| 1027 | assert(e == "line") |
| 1028 | line = l |
| 1029 | end, "l") |
| 1030 | assert(s() == 2); debug.sethook(nil) |
| 1031 | assert(line == nil) -- hook called without debug info for 1st instruction |
| 1032 | end |
| 1033 | |
| 1034 | do -- tests for 'source' in binary dumps |
| 1035 | local prog = [[ |
| 1036 | return function (x) |
| 1037 | return function (y) |
| 1038 | return x + y |
| 1039 | end |
| 1040 | end |
| 1041 | ]] |
| 1042 | local name = string.rep("x", 1000) |
| 1043 | local p = assert(load(prog, name)) |
| 1044 | -- load 'p' as a binary chunk with debug information |
| 1045 | local c = string.dump(p) |
| 1046 | assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump |
| 1047 | local f = assert(load(c)) |
| 1048 | local g = f() |
| 1049 | local h = g(3) |
| 1050 | assert(h(5) == 8) |
| 1051 | assert(debug.getinfo(f).source == name and -- all functions have 'source' |
| 1052 | debug.getinfo(g).source == name and |
| 1053 | debug.getinfo(h).source == name) |
| 1054 | -- again, without debug info |
| 1055 | local c = string.dump(p, true) |
| 1056 | assert(#c < 500) -- no 'source' in dump |
| 1057 | local f = assert(load(c)) |
| 1058 | local g = f() |
| 1059 | local h = g(30) |
| 1060 | assert(h(50) == 80) |
| 1061 | assert(debug.getinfo(f).source == '=?' and -- no function has 'source' |
| 1062 | debug.getinfo(g).source == '=?' and |
| 1063 | debug.getinfo(h).source == '=?') |
| 1064 | end |
| 1065 | |
| 1066 | print"OK" |
| 1067 |