Reputation: 85975
In the description of Lua' Pluto library, it says it lib persist functions and threads.
Can persist any Lua function
Can persist threads
Works with any Lua chunkreader/chunkwriter
Support for "invariant" permanent objects, of all datatypes
Hmm, I can't imagine how the functions and threads to be persisted. Can I have some explanation about this feature?
Upvotes: 1
Views: 420
Reputation: 52651
The source code is relatively easy to follow and very commented.
What the lib does is determine what parts compose the functions and/or threads, and then store every part separately.
If you skip the code and just read the comments, here's how the two relevant functions look:
static void persistfunction(PersistInfo *pi)
{
...
if(cl->c.isC) {
/* It's a C function. For now, we aren't going to allow
* persistence of C closures, even if the "C proto" is
* already in the permanents table. */
lua_pushstring(pi->L, "Attempt to persist a C function");
lua_error(pi->L);
} else { /* It's a Lua closure. */
/* Persist prototype */
...
/* Persist upvalue values (not the upvalue objects themselves) */
...
/* Persist function environment */
...
}
}
static void persistthread(PersistInfo *pi)
{
...
/* Persist the stack */
...
/* Now, persist the CallInfo stack. */
...
/* Serialize the state's other parameters, with the exception of upval stuff */
...
/* Finally, record upvalues which need to be reopened */
...
}
So, as you can see, a function can be considered as a composition of a prototype, a group of upvalues and an environment (a table). A thread is two "stacks" (the call stack and the memory stack, I think), the state information (excluding upvalues), which is basically what variables had which values when the thread was defined, and the upvalues.
You may read more about upvalues in PiL 27.3.3
Upvotes: 2
Reputation: 16753
Can persist any Lua function
This means that Pluto can persist any Lua function by persisting it's bytecode and all required upvalues. See here and here for source. When you unpersist it, you can call the function as usual. Note that it cannot persist C functions registered in Lua.
Can persist threads
It persists the thread's stack and activation records, so when you unpersist it, you can resume where the stack was executing. Code is here.
Upvotes: 0