在Lua的Pluto库的description中,它写着lib持久化函数和线程。
Can persist any Lua function
Can persist threads
Works with any Lua chunkreader/chunkwriter
Support for "invariant" permanent objects, of all datatypes嗯,我无法想象如何持久化函数和线程。我可以得到一些关于这个功能的解释吗?
发布于 2012-01-11 00:57:38
发布于 2012-01-11 01:09:27
source code相对容易理解,并且很容易被注释。
lib所做的是确定哪些部分组成函数和/或线程,然后分别存储每个部分。
如果您跳过代码,只阅读注释,下面是两个相关函数的外观:
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 */
...
}因此,正如您所看到的,一个函数可以被认为是一个原型、一组upvalue和一个环境(表)的组合。一个线程由两个“堆栈”(我认为是调用堆栈和内存堆栈)、状态信息(不包括upvalue)和upvalue组成。
你可以在PiL 27.3.3中阅读更多关于upvalues的内容
https://stackoverflow.com/questions/8797598
复制相似问题