github.com/rhettli/gopher-lua@v0.0.0-20200830072439-712e2f816099/_glua-tests/db.lua (about)

     1  -- debug lib tests
     2  -- debug stuff are  partially implemented; hooks are not supported.
     3  
     4  local function f1()
     5  end
     6  local env = {}
     7  local mt = {}
     8  debug.setfenv(f1, env)
     9  assert(debug.getfenv(f1) == env)
    10  debug.setmetatable(f1, mt)
    11  assert(debug.getmetatable(f1) == mt)
    12  
    13  local function f2()
    14    local info = debug.getinfo(1, "Slunf")
    15    assert(info.currentline == 14)
    16    assert(info.linedefined == 13)
    17    assert(info.func == f2)
    18    assert(info.lastlinedefined == 25)
    19    assert(info.nups == 1)
    20    assert(info.name == "f2")
    21    assert(info.what == "Lua")
    22    if string.find(_VERSION, "GopherLua") then
    23      assert(info.source == "db.lua")
    24    end
    25  end
    26  f2()
    27  
    28  local function f3()
    29  end
    30  local info = debug.getinfo(f3)
    31  assert(info.currentline == -1)
    32  assert(info.linedefined == 28)
    33  assert(info.func == f3)
    34  assert(info.lastlinedefined == 29)
    35  assert(info.nups == 0)
    36  assert(info.name == nil)
    37  assert(info.what == "Lua")
    38  if string.find(_VERSION, "GopherLua") then
    39    assert(info.source == "db.lua")
    40  end
    41  
    42  local function f4()
    43    local a,b,c = 1,2,3
    44    local function f5()
    45      local name, value = debug.getlocal(2, 2)
    46      assert(debug.getlocal(2, 10) == nil)
    47      assert(name == "b")
    48      assert(value == 2)
    49      name = debug.setlocal(2, 2, 10)
    50      assert(debug.setlocal(2, 10, 10) == nil)
    51      assert(name == "b")
    52  
    53      local d = a
    54      local e = c
    55  
    56      local tb = debug.traceback("--msg--")
    57      assert(string.find(tb, "\\-\\-msg\\-\\-"))
    58      assert(string.find(tb, "in.*f5"))
    59      assert(string.find(tb, "in.*f4"))
    60    end
    61    f5()
    62    local name, value = debug.getupvalue(f5, 1)
    63    assert(debug.getupvalue(f5, 10) == nil)
    64    assert(name == "a")
    65    assert(value == 1)
    66    name = debug.setupvalue(f5, 1, 11)
    67    assert(debug.setupvalue(f5, 10, 11) == nil)
    68    assert(name == "a")
    69    assert(a == 11)
    70  
    71    assert(b == 10) -- changed by debug.setlocal in f4
    72  end
    73  f4()
    74  
    75  local ok, msg = pcall(function()
    76    debug.getlocal(10, 1)
    77  end)
    78  assert(not ok and string.find(msg, "level out of range"))
    79  
    80  local ok, msg = pcall(function()
    81    debug.setlocal(10, 1, 1)
    82  end)
    83  assert(not ok and string.find(msg, "level out of range"))
    84  
    85  assert(debug.getinfo(100) == nil)
    86  assert(debug.getinfo(1, "a") == nil)