lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi!

Am 19.05.2015 um 07:50 schröbte "书呆彭, Peng Yi":

I checked the attached script and examined the source code of Lua.
interestingly, if you return something (even `nil') instead of nothing
from that function,
you'll get the expected result (I have Lua 5.2 installed, but I believe
it applies to 5.3):

Thanks very much! I can confirm that a single explicit return value fixes the behavior of the test script on Lua 5.1, 5.2, and 5.3 (an empty `return` is insufficient). Unfortunately, in my original code where I encountered the problem I already had an explicit return value (and it still doesn't work except on LuaJIT). I'll try to come up with an updated test case.

In the meantime I have come across another case of inconsistent behavior while implementing my line hook workaround (this one I can definitely fix with an explicit return -- or any extra statement for that matter):

If I have a chunk that consists entirely of local function definitions (using the syntax sugar) I can't get the last local variable from within the line hook (this can actually be explained via the manual since the line hook is called *before* a line is evaluated). However, if I don't use the syntax sugar but `local x; function x() end` instead, then I *can* get that last local. Also if I use normal functions instead of chunks, I can get the last local no matter what syntax I use. This applies to Lua 5.2, 5.3, and LuaJIT (Lua 5.1 is more consistent and I never get the last local).

Test script is attached, here is the output for Lua 5.2 (notice how the `y` variable from `func1` is missing):

    func1	x	function: 0x18a2ef0

    func2	x	function: 0x189e560
    func2	x	function: 0x189e560
    func2	y	nil
    func2	x	function: 0x189e560
    func2	y	nil
    func2	x	function: 0x189e560
    func2	y	function: 0x18a2060

    func3	x	function: 0x18a18d0
    func3	x	function: 0x18a18d0
    func3	y	function: 0x18a1a20

    func4	x	function: 0x18a0ff0
    func4	x	function: 0x18a0ff0
    func4	y	nil
    func4	x	function: 0x18a0ff0
    func4	y	nil
    func4	x	function: 0x18a0ff0
    func4	y	function: 0x18a11f0


Thanks again,
Philipp


local func1 = (loadstring or load)[[
local function x()
end
local function y()
end
]]

local func2 = (loadstring or load)[[
local function x()
end
local y; function y()
end
]]

local func3 = function()
  local function x()
  end
  local function y()
  end
end

local func4 = function()
  local function x()
  end
  local y; function y()
  end
end

local N

local function hook( event, no )
  local func = debug.getinfo( 2, "f" ).func
  if func == func1 or func == func2 or
     func == func3 or func == func4 then
    local i, name, value = 2, debug.getlocal( 2, 1 )
    while name do
      if #name == 1 then
        print( N, name, value )
      end
      i, name, value = i+1, debug.getlocal( 2, i )
    end
  end
end

debug.sethook( hook, "l" )
N = "func1"
func1()
print()
N = "func2"
func2()
print()
N = "func3"
func3()
print()
N = "func4"
func4()
debug.sethook()