您的位置:首页 > 编程语言 > Lua

lua源码阅读(5)-lua_State

2017-09-25 21:04 393 查看
在C语言中编写的脚本解释器运行lua是,首先是要创建lua_Stat结构体。lua_State中管理了一个数组,可以视为lua虚拟机的"寄存器"。在Lstat.h中

struct lua_State {
CommonHeader;
lu_byte status;

//线程栈的栈顶指针
StkId top;  /* first free slot in the stack */

StkId base;  /* base of current function */

global_State *l_G;

//当前运行的函数调用信息
CallInfo *ci;  /* call info for current function */

//函数调用前,记录上一个函数的pc位置
const Instruction *savedpc;  /* `savedpc' of current function */

//栈的实际最后一个位置(栈的长度是动态增长的)
StkId stack_last;  /* last free slot in the stack */

//栈的数组的指针
StkId stack;  /* stack base */

CallInfo *end_ci;  /* points after end of ci array*/
CallInfo *base_ci;  /* array of CallInfo's */

int stacksize;

int size_ci;  /* size of array `base_ci' */

unsigned short nCcalls;  /* number of nested C calls */
unsigned short baseCcalls;  /* nested C calls when resuming coroutine */
lu_byte hookmask;
lu_byte allowhook;
int basehookcount;
int hookcount;
lua_Hook hook;
TValue l_gt;  /* table of globals */
TValue env;  /* temporary place for environments */
GCObject *openupval;  /* list of open upvalues in this stack */
GCObject *gclist;
struct lua_longjmp *errorJmp;  /* current error recover point */
ptrdiff_t errfunc;  /* current error handling function (stack index) */
};

typedef struct CallInfo {
StkId base;  /* base for this function */
StkId func;  /* function index in the stack */
StkId	top;  /* top for this function */
const Instruction *savedpc;
int nresults;  /* expected number of results from this function */
int tailcalls;  /* number of tail calls lost under this entry */
} CallInfo;
CallInfo描述了一个函数调用时使用寄存器的信息。因为许多函数调用都是共用同一个lua_State的,需要对位置进行记录。lua_State中,指针stack指向一个数组,就是所谓的寄存器。整个lua_State结构大概如下:



调用一个函数的时候,函数也是一个变量,存放在func位置,之后是参数,这些信息由一个调callinfo记录。在Lstate.h中,还有一个global_state的结构体

typedef struct global_State {

//字符串hash表
stringtable strt;  /* hash table for strings */

//内存分配函数
lua_Alloc frealloc;  /* function to reallocate memory */

void *ud;         /* auxiliary data to `frealloc' */

lu_byte currentwhite;
lu_byte gcstate;  /* state of garbage collector */
int sweepstrgc;  /* position of sweep in `strt' */
GCObject *rootgc;  /* list of all collectable objects */
GCObject **sweepgc;  /* position of sweep in `rootgc' */
GCObject *gray;  /* list of gray objects */
GCObject *grayagain;  /* list of objects to be traversed atomically */
GCObject *weak;  /* list of weak tables (to be cleared) */
GCObject *tmudata;  /* last element of list of userdata to be GC */
Mbuffer buff;  /* temporary buffer for string concatentation */
lu_mem GCthreshold;
lu_mem totalbytes;  /* number of bytes currently allocated */
lu_mem estimate;  /* an estimate of number of bytes actually in use */
lu_mem gcdept;  /* how much GC is `behind schedule' */
int gcpause;  /* size of pause between successive GCs */
int gcstepmul;  /* GC `granularity' */
lua_CFunction panic;  /* to be called in unprotected errors */
TValue l_registry;
struct lua_State *mainthread;
UpVal uvhead;  /* head of double-linked list of all open upvalues */
struct Table *mt[NUM_TAGS];  /* metatables for basic types */
TString *tmname[TM_N];  /* array with tag-method names */
} global_State;
该结构体中有字符串hash表,内存分配函数,还有许多与gc有关的部分,暂不研究。每一个lua_State中都有一个指向blobal_State的指针。在创建lua_State的时候,也会创建global_State,在创建协程的时候,只会创建lua_State,所以,如果加入协程的话,lua_State的结构如下

lua_State提供了执行lua代码的环境,global_State管理内存。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: