lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2016-05-04 5:47 GMT+02:00 Duane Leslie <parakleta@darkreality.org>:
>
> I needed a way to specify the upvalues of a function explicitly so
> that I could serialise and de-serialise user defined functions in a
> reliable way.  I have added the syntax of an angle bracketed parameter
> list for upvalues which comes after the argument parameter list in the
> function definition.
>
> For example, the main chunk which was previously defined as:
>
> function (...)
>   stmt;*
>   return
> end
>
> is now defined as
>
> function (...) <_ENV>
>   stmt;*
>   return
> end

You can do that without changing the parser.
All you need to do is to use the upvalues in the
desired order at the start of the function before
referring to anything else. You can even make it look
like a declaration. For example:

---
local function upvalues(...) end

local c='c'
local a='a'
local b='b'

function f(x,y) upvalues(a,b,c)
  print(table.concat{a,b,c})
end
---

will generate a function with a,b,c as upvalues numbered
1,2,3 ('upvalues' being upvalue 0). _ENV only comes in
as upvalue 4.