gopher-lua icon indicating copy to clipboard operation
gopher-lua copied to clipboard

Bug with `debug.getlocal` (mismatch compared to official Lua 5.1)

Open zyedidia opened this issue 3 years ago • 1 comments

You must post issues only here. Questions, ideas must be posted in discussions.

Please answer the following before submitting your issue:

  1. What version of GopherLua are you using? : latest version (commit 6581935)
  2. What version of Go are you using? : Go 1.19
  3. What operating system and processor architecture are you using? : linux/amd64
  4. What did you do? :

I ran this program:

if true then
    local not_visible = "true"
else
    local not_visible = "false"
end

function get_locals(func)
    local n = 1
    local locals = {}
    while true do
        local lname, lvalue = debug.getlocal(2, n)
        if lname == nil then break end
        locals[lname] = lvalue
        n = n + 1
    end
    return locals
end

local should_exist = "foo"

local vars = get_locals(1)

print("should not exist:", vars["not_visible"])
print("should exist:", vars["should_exist"])
  1. What did you expect to see? :

The get_locals function should return a table that does not have not_visible as a key, and does have should_exist as a key. I would expect it to print:

should not exist:	nil
should exist:	foo

That is what the official Lua5.1 interpreter prints when it runs this file.

  1. What did you see instead? :

Instead I see

should not exist:	function: 0xc0000fb080
should exist:	nil

For some reason not_visible exists and is a function, and should_exist is nil.

For this case, glua does not produce the same output as lua5.1, which is a bug.

zyedidia avatar Oct 31 '22 22:10 zyedidia

I have made an initial fix on my fork at: https://github.com/zyedidia/gopher-lua. I may clean it up and open a PR at some point.

zyedidia avatar Nov 01 '22 05:11 zyedidia