lua-users home
lua-l archive

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




2015-03-27 23:07 GMT-07:00 Sean Conner <sean@conman.org>:
It was thus said that the Great Dong Feng once stated:
> 2015-03-27 21:16 GMT-07:00 Sean Conner <sean@conman.org>:
>
> >   Also, while "you can use the debug library to serialize and reload the
> > upvalues of a function in a way adequate to your needs" is true, to a
> > degree, as mentioned elsewhere [2] it's not always that easy.
> >
> >   -spc (Why yes, I'm trying to serialize Lua functions, silly me)
> >
> > [2]     http://lua-users.org/lists/lua-l/2015-03/msg00180.html
>
> I have three points regarding this:
>
> First, this isn't 5.3 specific. You'd get the same in 5.2.
>
> Second, the document load() says it sets the "first" upval. Well, that's
> correct from the perspective of implementation. However, its intention is
> really to set the ONLY upval.

  Not quite.  string.dump() states:  "Functions with upvalues have only their
number of upvalues saved."  And I'm loading the output from string.dump()
via load().  So Lua knows how many upvalues it has, but not which one was
_ENV.

I mean the intended usage of load() is only for two cases, either you load a main chunk, or you deserialize a function with exactly the same upvals you serialize it. By "main chunk" I mean a piece of code with no outside lexical environment. In your example, foo is not main chunk because it has the outside lexical environment.
 

> The implied recommendation is, don't play
> load() with anything but main chunk (which does not have upval except _ENV).

  I'm not sure what you mean by this.

What I mean is described by the above definition of "main chunk", i.e. a piece of code without outside lexical environment.
 

 > Third, well, the document says you can serialize Lua function by
> string.dump and deserialize it by load. That's fine. Only in this case, you
> deserialize what you serialize, and you should not make any assumption what
> the "first" would be.

  Yeah, and all upvalues are set to nil, except the first.  And if you try
to run that function before the upvalues are fixed, well ...

  -spc
 

Yes, you have to fix all upval before calling that function. What I suggest is, the default use case of load() (by what you mean, with no need to fix upvals) is really reserved for "main chunk".