lua-users home
lua-l archive

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



On 16/02/15 11:21 PM, Paul K wrote:
Hi All,

I recently noticed that I don't get expected results from activelines table when I use it on the function returned by load/loadstring. The following fragment shows the difference:

local function a()
  print("a")
  print("b")
end

local b = (loadstring or load)("function a()\n  print(1)\n  print(2)\nend")

for i in pairs(debug.getinfo(a, "L").activelines) do
  print("case a", i)
end
for i in pairs(debug.getinfo(b, "L").activelines) do
  print("case b", i)
end

For case a, I get 2, 3, 4 as expected, but for case b I get 1 and 4. I get exactly the same result with Lua 5.1-5.3 and LuaJIT, which tells me that there is a reason for this behavior. How do I get line numbers the execution will be stopped on without executing the fragment?

Paul.
Try this maybe?

local function a() -- this defines a
  print("a")
  print("b")
end

local b = (loadstring or load)("function a()\n  print(1)\n  print(2)\nend\n return a") -- added "return a"

for i in pairs(debug.getinfo(a, "L").activelines) do
  print("case a", i)
end
for i in pairs(debug.getinfo(b, "L").activelines) do
  print("case b", i)
end

-- now do for the main chunk
for i in pairs(debug.getinfo(debug.getinfo(1, "f").func, "L").activelines) do
  print("case c", i)
end

-- and run b
local d = b()

for i in pairs(debug.getinfo(d, "L").activelines) do
  print("case d", i)
end
-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.