gopher-lua
gopher-lua copied to clipboard
bug: pcall affecting function upvalues
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:
- What version of GopherLua are you using? : I'm using cmd/glua
- What version of Go are you using? : 1.20.5
- What operating system and processor architecture are you using? : linux/amd64
- 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()
- 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
- 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]: ?
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
@yuin
In my experiment I also found that in certain situations this bug won't reproduce if:
- no error is raised in
pcall
- a is declared before the first failed
pcall
- 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
I found https://github.com/yuin/gopher-lua/blob/2b3f02d9173010b12bb3ff2c0e758921accb4096/_state.go#L626-L632 Is this an intented behavior?