Unisay / purescript-lua

Purescript compiler back-end for Lua
GNU General Public License v3.0
55 stars 2 forks source link

Function at line XXX has more than NNN upvalues #19

Closed Unisay closed 6 months ago

Unisay commented 6 months ago

Upvalues are closures over an external local variable, for example:

function newCounter ()
  local i = 0
  return function () -- closure
    -- `i` is an "upvalue" because it is external relative to the closure 
    -- and is local to "newCounter"
    i = i + 1 
      return i
    end
end

It looks like Lua v.5.1 has the limit on a maximum number of upvalues set to 60; Newer versions have it set to 255

This is a problem because pslua produces a chain of nested functions which has more than 60 upvalues (and quite possibly more than 255).

One example when it happens is when Effect expressions are composed

return Init_I_discard(function(..) ... 
  -- could be a big closure containing the rest of computation containing many 
  -- references to top level local variable definitions.
  return Init_I_discard(function(..) ...
    return Init_I_discard(function(..) ...

Related: