eris
eris copied to clipboard
upvalues in global functions to local variables not restored properly
When investigating a bug report of OpenComputers I found that some lua code can behave incorrectly between loads (restoring the world after closing it)
https://github.com/MightyPirates/OpenComputers/issues/2598
The summary of the problem is:
The issue appears to be that local values captured (upvalue) in global functions do not restore the same reference, the local scope gets a new object, and the global function has its own. It's like its own global upvalue. In fact, all global functions share the same upvalue, and all local functions share the same upvalue, those being 2 separate upvalues.
Here is a simple repro
local value = 0
local sync = true
function g_fp()
value = value + 1
if not sync then
print("out of sync")
end
end
function g2(given_value)
if given_value ~= value then
sync = false
end
end
while true do
-- read here just to delay/slow the loop for observation
io.read()
local before_call = value
g_fp()
if before_call < value then
print("value increased", value)
else
g2(value)
end
end
as mentioned in my last comment on the OC bug report, I also want to add that:
This isn't an issue with all local values captured in global functions In my same examples, if I also make a local table and capture it in the functions (globals and locals), suddenly the bug is not hit -- it seems more is packed and correctly restored when there is a table to share
I'm testing this with eris and the bug is not repro'ing There must be something more happening in OpenComputers. I'll continue to investigate and report
This still appears to be an eris issue to me, but i did test this in "eeprom" (sangar will understand) and it DOES NOT repro. There is a lot more loading (i.e. lua's load
) between eeprom and a user script, so, it'll take more time to pin down a sample script that repro's more easily
@payonel Do you have a workaround for it?