github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/debug/testdata/getinfo.lua (about)

     1  
     2  
     3  d = debug.getinfo(1)
     4  assert(d.currentline == 3)
     5  assert(d.source == "@testdata/getinfo.lua" or d.source == "@testdata\\getinfo.lua")
     6  assert(d.what == "main")
     7  
     8  function f()
     9  	d = debug.getinfo(1)
    10  	assert(d.currentline == 9)
    11  	assert(d.name == "f")
    12  	assert(d.what == "Lua")
    13  	assert(d.namewhat == "global")
    14  	assert(d.nparams == 0)
    15  	assert(d.isvararg == false)
    16  	assert(d.istailcall == false)
    17  	assert(d.linedefined == 8)
    18  	assert(d.lastlinedefined == 19)
    19  end
    20  
    21  d = debug.getinfo(f)
    22  
    23  assert(d.currentline == -1)
    24  assert(d.what == "Lua")
    25  assert(d.nparams == 0)
    26  assert(d.isvararg == false)
    27  assert(d.istailcall == false)
    28  assert(d.linedefined == 8)
    29  assert(d.lastlinedefined == 19)
    30  
    31  f()
    32  
    33  d = debug.getinfo(0)
    34  
    35  assert(d.namewhat == "field")
    36  assert(d.linedefined == -1)
    37  assert(d.what == "Go" or d.what == "C")
    38  assert(d.isvararg == true)
    39  assert(d.currentline == -1)
    40  assert(d.source == "=[Go]" or d.source == "=[C]")
    41  assert(d.name == "getinfo")
    42  assert(d.istailcall == false)
    43  assert(d.short_src == "[Go]" or d.short_src == "[C]")
    44  assert(d.nups == 0)
    45  assert(d.nparams == 0)
    46  assert(d.lastlinedefined == -1)
    47  
    48  
    49  local function x(a)
    50  	y()
    51  end
    52  
    53  function y()
    54  	d = debug.getinfo(2)
    55  	assert(d.namewhat == "local")
    56  	assert(d.linedefined == 49)
    57  	assert(d.isvararg == false)
    58  	assert(d.currentline == 50)
    59  	assert(d.name == "x")
    60  	assert(d.istailcall == false)
    61  	assert(d.nups == 1)
    62  	assert(d.nparams == 1)
    63  	assert(d.lastlinedefined == 51)
    64  end
    65  
    66  x()
    67  
    68  function x()
    69  	y()
    70  end
    71  
    72  function y()
    73  	return z()
    74  end
    75  
    76  function z()
    77  	return w()
    78  end
    79  
    80  function w()
    81  	d = debug.getinfo(2)
    82  
    83  	assert(d.what == "Lua")
    84  	assert(d.namewhat == "local")
    85  	assert(d.name == "x")
    86  	assert(d.nparams == 0)
    87  	assert(d.isvararg == false)
    88  	assert(d.nups == 1)
    89  	assert(d.istailcall == false)
    90  	assert(d.currentline == 69)
    91  	assert(d.linedefined == 68)
    92  	assert(d.lastlinedefined == 70)
    93  end
    94  
    95  x()
    96  
    97  pcall(function()
    98  	d = debug.getinfo(2)
    99  end)
   100  
   101  assert(d.name == "pcall")
   102  
   103  xpcall(function()
   104  	d1 = debug.getinfo(2)
   105  	d2 = debug.getinfo(3)
   106  
   107  	error("err")
   108  end, function()
   109  	d3 = debug.getinfo(2)
   110  end)
   111  
   112  assert(d1.name == "xpcall")
   113  assert(d2.what == "main")
   114  assert(d3.name == "error" or d3.name == "xpcall") -- plua returns "xpcall" here, but OK.
   115  
   116  debug.sethook(function()
   117  	assert(debug.getinfo(0).name == "getinfo")
   118  	assert(debug.getinfo(1).namewhat == "hook" or debug.getinfo(1).namewhat == "") -- lua 5.3.3 returns "", but it should return "hook"
   119  	assert(debug.getinfo(2).name == "sethook")
   120  	assert(debug.getinfo(3).what == "main")
   121  end, "c")
   122  
   123  debug.sethook()
   124  
   125  -- test tagmethod information
   126  local a = {}
   127  local function index(t)
   128    local info = debug.getinfo(1);
   129    assert(info.namewhat == "metamethod")
   130    a.op = info.name
   131    return info.name
   132  end
   133  setmetatable(a, {
   134    __index = index
   135  })
   136  
   137  assert(a[3] == "__index")