lua-users home
lua-l archive

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

Jerome Vuarand wrote:
> A close form could already be implemented, without upvalues though:
> local f = y[[ x + y with x, y ]]

several times, i've wanted to generate functions like this.  usually not for the short 'lambda' syntax; but for simple DSLs

unfortunately, the no-upvalues limitation makes it not-so-useful.  it would be really great if there was a way to make the 'load()' or 'loadstring()' functions create full closures.  I guess the security implications are terrible, maybe can be mitigated by specifying a list of valid upvalue names?

but in theory it shouldn't be so hard... after all, the Lua core manages to create closures, surely its possible to add some extra function to do it from Lua's control.

reading the source, it seems that the important function is luaF_newLclosure(), which returns (in some structures too novel to me) the list of upvalues, to be defined by the calling function.  it's called at two points: on ldo.c and lvm.c.  the first one seems to be only for defining 'main chunks', so the upvalues are all defined as 'new'; while the second one is used to create function objects from the compiled code, and the upvalues are 'linked' to local variables or upvalues to the enclosing function.

maybe somebody more familiar with the deep guts of Lua could answer if it's doable to create a new 'loadclosure()' function that works more like the call from lvm.c, but reads from some given text, like on ldo.c ?