** 'per thread' state */ struct { CommonHeader; /* */ lu_byte status; /* 解析容器的,用于记录中间状态 */ global_State *l_G; /* 全局状态机 */ /*调用栈:调用栈的信息管理*/ CallInfo *ci; /* call info for current function 当前函数的运行信息 */ CallInfo base_ci; /* CallInfo for first level (C calling Lua) 调用栈的头部指针 */ /* 数据栈:栈指针地址管理 */ StkId top; /* first free slot in the stack 指向线程栈的栈顶 */ StkId stack_last; /* last free slot in the stack 线程栈的最后一个位置 */ StkId stack; /* stack base 栈的指针,当前运行的位置 */
GCObject *gclist; /* GC列表 */ const Instruction *oldpc; /* last pc traced 在当前thread 的解释执行指令的过程中,指向最后一次执行的指令的指针 */ struct *twups;/* list of threads with open upvalues */ structlua_longjmp *errorJmp;/* current error recover point */ UpVal *openupval; /* list of open upvalues in this stack */ /* Hook 相关管理 - 服务于debug模块 */ lua_Hook hook; ptrdiff_t errfunc; /* current error handling function (stack index) */ int stacksize; int basehookcount; int hookcount; lu_byte hookmask; lu_byte allowhook; /* 跟C语言通信 管理 */ unsignedshort nny; /* number of non-yieldable calls in stack */ unsignedshort nCcalls; /* number of nested C calls */ };
GCUnion
1 2 3 4 5 6 7 8 9 10 11 12
** Union of all collectable objects (only for conversions) */ union GCUnion { GCObject gc; /* common header */ structTStringts; structUdatau; union Closure cl; structTableh; structProtop; structth;/* thread */ };
** 'global state', shared by all threads of this state ** lua全局状态机 ** 作用:管理全局数据、全局字符串表、内存管理函数 */ typedefstructglobal_State { const lua_Number *version; /* pointer to version number 版本号 */ /* 内存管理 */ lua_Alloc frealloc; /* function to reallocate memory Lua的全局内存分配器 */ void *ud; /* auxiliary data to 'frealloc' 分配器的userdata */ lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */ lu_mem GCmemtrav; /* memory traversed by the GC */ lu_mem GCestimate; /* an estimate of the non-garbage memory in use */ TValue l_registry; unsignedint seed; /* randomized seed for hashes */ lu_byte currentwhite; lu_byte gcstate; /* state of garbage collector */ lu_byte gckind; /* kind of GC running */ lu_byte gcrunning; /* true if GC is running */ GCObject *allgc; /* list of all collectable objects */ GCObject **sweepgc; /* current position of sweep in list */ GCObject *finobj; /* list of collectable objects with finalizers */ GCObject *gray; /* list of gray objects */ GCObject *grayagain; /* list of objects to be traversed atomically */ GCObject *weak; /* list of tables with weak values */ GCObject *ephemeron; /* list of ephemeron tables (weak keys) */ GCObject *allweak; /* list of all-weak tables */ GCObject *tobefnz; /* list of userdata to be GC */ GCObject *fixedgc; /* list of objects not to be collected */
Mbuffer buff; /* temporary buffer for string concatenation */ /* 字符串管理 */ stringtable strt; /* hash table for strings */ /* GC管理 */ unsignedint gcfinnum; /* number of finalizers to call in each GC step */ int gcpause; /* size of pause between successive GCs */ int gcstepmul; /* GC 'granularity' */ /* 线程管理 */ struct *mainthread;/* 主线程 */ struct *twups;/* list of threads with open upvalues 闭包了当前线程变量的其他线程列表 */ /* 错误处理 */ lua_CFunction panic; /* to be called in unprotected errors */ TString *memerrmsg; /* memory-error message */ /* 虚函数表 */ TString *tmname[TM_N]; /* array with tag-method names 预定义方法名字数组 */ structTable *mt[LUA_NUMTAGS];/* metatables for basic types 每个基本类型一个metatable */ } global_State;
** Information about a call. ** When a thread yields, 'func' is adjusted to pretend that the ** top function has only the yielded values in its stack; in that ** case, the actual 'func' value is saved in field 'extra'. ** When a function calls another with a continuation, 'extra' keeps ** the function index so that, in case of errors, the continuation ** function can be called with the correct top. */ typedefstructCallInfo { StkId func; /* function index in the stack func 指向正在执行的函数在数据栈上的位置 */ StkId top; /* top for this function */ structCallInfo *previous, *next;/* dynamic call link */ union { struct {/* only for Lua functions */ StkId base; /* base for this function */ const Instruction *savedpc; } l; struct {/* only for C functions */ lua_KFunction k; /* continuation in case of yields */ ptrdiff_t old_errfunc; lua_KContext ctx; /* context info. in case of yields */ } c; } u; ptrdiff_t extra; short nresults; /* expected number of results from this function */ lu_byte callstatus; } CallInfo;