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

bug: pcall affecting function upvalues

Open cloudwindy opened this issue 1 year ago • 4 comments

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

  • [x] GopherLua is a Lua5.1 implementation. You should be familiar with Lua programming language. Have you read Lua 5.1 reference manual carefully?
  • [X] 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 avatar Jul 17 '23 21:07 cloudwindy

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 avatar Jul 17 '23 21:07 cloudwindy

@yuin

cloudwindy avatar Jul 17 '23 21:07 cloudwindy

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 avatar Jul 17 '23 22:07 cloudwindy

I found https://github.com/yuin/gopher-lua/blob/2b3f02d9173010b12bb3ff2c0e758921accb4096/_state.go#L626-L632 Is this an intented behavior?

cloudwindy avatar Jul 17 '23 22:07 cloudwindy