Open
Description
When investigating a bug report of OpenComputers I found that some lua code can behave incorrectly between loads (restoring the world after closing it)
MightyPirates/OpenComputers#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
Metadata
Metadata
Assignees
Labels
No labels