Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: pcall affecting function upvalues #448

Open
2 tasks done
cloudwindy opened this issue Jul 17, 2023 · 4 comments
Open
2 tasks done

bug: pcall affecting function upvalues #448

cloudwindy opened this issue Jul 17, 2023 · 4 comments

Comments

@cloudwindy
Copy link

cloudwindy commented Jul 17, 2023

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

  • GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • GopherLua is a Lua5.1 implementation. In Lua, to keep it simple, it is more important to remove functionalities rather than to add functionalities unlike other languages . If you are going to introduce some new cool functionalities into the GopherLua code base and the functionalities can be implemented by existing APIs, It should be implemented as a library.

Please answer the following before submitting your issue:

  1. What version of GopherLua are you using? : I'm using cmd/glua
  2. What version of Go are you using? : 1.20.5
  3. What operating system and processor architecture are you using? : linux/amd64
  4. What did you do? :
    I tried to run ilua.lua, but it failed and after experimenting I made a simple version of the code to reproduce the bug
local a

function Use_a()
  print(a)
  a('test')
end

pcall(function()
  error('an error')
end)
a = function(prompt)
  print(prompt)
end

Use_a()
  1. What did you expect to see? : pcall should not affect upvalues as in Lua 5.1.5. Its output is as follows:
$ lua test.lua
function: 0x556330c5dfa0
test
  1. What did you see instead? :
$ glua test.lua
nil
test.lua:5: attempt to call a non-function object
stack traceback:
        test.lua:5: in function 'Use_a'
        test.lua:15: in main chunk
        [G]: ?
@cloudwindy
Copy link
Author

In my experiment, removing the pcall method:

local a

function Use_a()
  print(a)
  a('test')
end

a = function(prompt)
  print(prompt)
end

Use_a()

Produces same result for lua and glua:

$ lua test.lua
function: 0x564dc15af480
test

$ glua test.lua
function: 0xc000156f00
test

@cloudwindy
Copy link
Author

@yuin

@cloudwindy
Copy link
Author

cloudwindy commented Jul 17, 2023

In my experiment I also found that in certain situations this bug won't reproduce if:

  1. no error is raised in pcall
  2. a is declared before the first failed pcall
  3. a is a global variable instead of local one

Looks like the environment is isolated in error() and everything before become read-only.

local a = 123
function Print_a()
  print(a)
end
pcall(function() error() end)
a = 456
print(a)
Print_a()
$ lua test.lua
456
456
$ glua test.lua
456
123

@cloudwindy
Copy link
Author

I found

gopher-lua/_state.go

Lines 626 to 632 in 2b3f02d

func (ls *LState) closeAllUpvalues() { // +inline-start
for cf := ls.currentFrame; cf != nil; cf = cf.Parent {
if !cf.Fn.IsG {
ls.closeUpvalues(cf.LocalBase)
}
}
} // +inline-end

Is this an intented behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant