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

     1  function f(a)
     2  	local b = 1
     3  
     4  	assert(debug.getlocal(1, 1) == 'a')
     5  	b, v = debug.getlocal(1, 2)
     6  	assert(b == 'b' and v == 1)
     7  end
     8  
     9  assert(debug.getlocal(f, 1) == 'a')
    10  
    11  f()
    12  
    13  function f2(...)
    14  	k, v = debug.getlocal(1, -1)
    15  	assert(k == "(*vararg)" and v == 1)
    16  
    17  	k, v = debug.getlocal(1, -2)
    18  	assert(k == "(*vararg)" and v == 2)
    19  
    20  	k, v = debug.getlocal(1, -3)
    21  	assert(k == "(*vararg)" and v == 3)
    22  
    23  	assert(debug.getlocal(1, -4) == nil)
    24  
    25  	local _ = ...
    26  end
    27  
    28  f2(1, 2, 3)
    29  
    30  function x()
    31  	local a = 4
    32  	y()
    33  end
    34  
    35  function y()
    36  	k, v = debug.getlocal(2, 1)
    37  	assert(k == "a" and v == 4)
    38  end
    39  
    40  x()
    41  
    42  
    43  function f(a) end
    44  
    45  debug.sethook(function ()
    46    k, v = debug.getlocal(2, 1)
    47    assert(k == "a" and v == 18)
    48  
    49    -- spec: don't guarantee temporary variable
    50    -- k, v = debug.getlocal(2, 2)
    51    -- assert(k == "(*temporary)" and v == 19)
    52  
    53    debug.sethook()
    54  end, "c")
    55  
    56  f(18, 19)