Lua 5.3 源码分析(四)线程 lua_State
Lua 的THREAD 类型定义如下:
/*
** 'per thread' state
*/
struct lua_State {
//1 Lua 类型公共头部
CommonHeader;
//2
global_State *l_G;
//3
lu_byte status;
//4 数据栈:一个动态TValue 数组,所有的Lua object 都是TValue
int stacksize;
StkId top; /* first free slot in the stack */
StkId stack_last; /* last free slot in the stack */
StkId stack; /* stack base */
//5 函数调用栈
CallInfo *ci; /* call info for current function */
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */
//6 Upvalue info
UpVal *openupval; /* list of open upvalues in this stack */
struct lua_State *twups; /* list of threads with open upvalues */
//7 Hook Debug
lua_Hook hook;
int basehookcount;
int hookcount;
lu_byte hookmask;
lu_byte allowhook;
//8 类似PC 寄存器,指向最后一次执行的指令的指令
const Instruction *oldpc; /* last pc traced */
//9 execption
struct lua_longjmp *errorJmp; /* current error recover point */
ptrdiff_t errfunc; /* current error handling function (stack index) */
//10 GC
GCObject *gclist;
};
1. CommonHeader : Lua 类型公共头部。
2. l_G : Lua的全局对象,只存在一份,所有的 L 都共享这个 G。
3. status:每一个THREAD 实际上就是一个死循环解释执行指令的容器,本质上