lua-users home
lua-l archive

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


> I'm still having some trouble getting persistence to work exactly the
> way I want it.  The latest problem is that I want to be able to save
> _everything_ in a table, including functions.  There doesn't seem to be
> an easy way to do this.  I'm also in need of a way to get the name of a
> variable into a Lua string.

In Lua 5beta, there is a library function dump which can be used to export
a compiled function into a string. However, it has a major limitation: the
function cannot have any upvalues. It is actually important to remember
that in Lua, functions are not objects; what you have is "function
closures" which is the function itself bound together with references to
any upvalues it has. Since the compiled format (currently) has no mechanism
for storing any other than the function itself, closures can only be
exported if there are no closed values. Hence the limitation.

It is also important to remember that variables are not objects either. Lua
is "pointer-free"; there is no such thing as a reference object (well,
upvalues are sort of references but I'll skip over that for a bit.) So
there is no sense in which you can, at run-time, talk about a variable.
What you have is values, but the same object could be the value of a number
of "variables" (or no variable, for that matter), so the "name of an
object" is not a meaningful phrase. If you wish to give values names, of
course, there is nothing stopping you from doing that. I posted something
on the list a month ago or so with sample code for given "names" to values
assigned to globals as a debugging technique.

> Then I realized, for this specific application, it would be easier to
> just flatten and save the entire lua_state, so that the next time you
> launched the app, the entire script environment would be preserved from
> before.

> I searched the list archives and saw a lot of people asking for this,
> but no real resolution...  Did anyone figure it out?  Is it a planned
> feature?

It is an interesting idea and I know different people have looked at it.
But it is not as simple as it looks at first glance.

Pure Lua objects could be serialised without too much problem. But few Lua
environments consist solely of pure Lua objects. Most of them at least have
references to the standard library and many of them have references to
other C code. In addition, it is quite common for applications embedding
Lua to implement their own datatypes as userdata -- remember that Lua is
designed as an embedded language. It may be an overstatement, but I would
say that it is impossible to serialise an environment which contains
arbitrary foreign (i.e. C) code and objects -- at least without the
cooperation of that code and those objects. To take a simple example,
suppose that my application has an open pipe to another process (using the
popen facility). How do I serialise that? Do I have to serialise the state
of the other process? (And how on earth do I do that? It might mean
serialising the whole world.) Or do I just leave the pipe out of the
serialisation with the result that when I restore the saved environment,
the next read function crashes?

Suppose that I have loaded some C functions into the address space using
the dynamic load facility. How do I serialise that? Do I try to remember
the filesystem path to the dynamically library? What happens if I restore
the environment on a different machine?

I am not saying that it is a bad idea -- I think it would be very
interesting. However, answers need to be provided to questions like the
above (and there are more).

I for one would find discussion of this topic interesting...

Rici