eris icon indicating copy to clipboard operation
eris copied to clipboard

upvalues in global functions to local variables not restored properly

Open payonel opened this issue 7 years ago • 4 comments

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

payonel avatar Feb 21 '18 23:02 payonel

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

payonel avatar Feb 21 '18 23:02 payonel

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

payonel avatar Feb 23 '18 14:02 payonel

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 avatar Feb 27 '18 17:02 payonel

@payonel Do you have a workaround for it?

ptwohig avatar Jun 13 '18 23:06 ptwohig