- 1 数据结构和宏
- 1.1 Closure 闭包
- 1.2 Proto 函数原型
- 1.3 UpVal 外部局部变量(upvalue)
- 1.4 LocVar 局部变量信息
- 1.5 SParser 语法分析所需要的结构
- 1.6 Zio 读写流对象
- 1.7 Mbuffer 缓冲对象
- 1.8 lua_Debug 调试信息
- 1.9 CallInfo 函数调用信息
- 1.10 lua_longjmp 跳转信息
- 1.11 虚拟机状态码
- 1.12 luaT_typenames 类型名称字符串
- 1.13 CallS 调用函数结构(f_call的参数)
- 1.14 CCallS 调用C函数结构 (f_Ccall的参数)
- 2 闭包相关API
- 2.1 luaF_newproto 新建函数原型
- 2.2 luaF_newCclosure 新建C闭包
- 2.3 luaF_newLclosure 新建lua闭包
- 2.4 luaF_newupval 新建upvalue(起初为close状态)
- 2.5 luaF_findupval 寻找open地址为level的upvalue,若找不到则新建一个
- 2.6 luaF_close 关闭协程L上所有open地址>=level的upvalue
- 2.7 luaF_freeproto 释放一个函数原型
- 2.8 luaF_freeclosure 释放一个闭包
- 2.9 luaF_freeupval 释放一个upvalue(若是open状态,则移出g->uvhead链表)
- 2.10 luaF_getlocalname 获取函数原型f中的第local_number个存活的局部变量的名字
- 3 函数调用相关API
- 3.1 luaD_protectedparser 保护模式下进行语法分析
- 3.2 luaD_callhook 调用钩子方法
- 3.3 luaD_precall 调用函数的准备工作
- 3.4 luaD_call 调用函数func,期望的返回值时nResults个,第一个返回值在func位置,后面的返回值依次往上
- 3.5 luaD_pcall 以保护模式调用C函数func,若有异常则做收尾工作
- 3.6 luaD_reallocCI 重新分配CallInfo数组并重新设置ci和end_ci指针的指向
- 3.7 luaD_rawrunprotected 以保护模式执行C函数f,返回状态码
- 3.8 luaD_throw 以错误码errcode抛出异常,若无L->errorJmp,则执行g->panic函数并退出程序
- 3.9 luaD_seterrorobj 根据错误码errcode将错误信息放在oldtop,栈顶设为oldtop+1
- 3.10 luaD_poscall 函数调用的收尾工作,firstResult为第一个返回值的地址(调整之前的地址),返回值若为0表示函数调用返回值数量由函数原型决定
- 3.11 luaD_reallocstack 重新分配栈内存
- 3.12 luaD_growstack 给栈扩容(至少扩大2倍)
- 4 错误处理API
- 4.1 luaG_errormsg 尝试调用错误处理函数,参数是原栈顶的错误信息字符串
- 4.2 luaG_runerror 处理运行时错误,为fmt为错误信息的格式串
- 4.3 luaG_typeerror 处理类型错误
- 4.4 luaG_concaterror 处理字符串连接错误
- 4.5 luaG_aritherror 处理算术错误
- 4.6 luaG_ordererror 处理比较错误
- 5 对外API
- 5.1 lua_call 调用L->top-nargs-1处的函数,以其上nargs个值为参数,实际返回值为nresults个
- 5.2 lua_pcall 以保护模式调用L->top-nargs-1处的函数,以其上nargs个值为参数,实际返回值为nresults个,且错误函数距离栈起始地址偏移为errfunc
- 5.3 lua_cpcall 以保护模式执行C函数func
- 5.4 lua_load 加载函数
- 5.5 lua_dump 序列化字节码
- 5.6 lua_atpanic 设置兜底异常处理函数(用于处理未捕获异常的函数),返回旧的兜底异常处理函数
1 数据结构和宏
在lua中,函数是第一类的数据类型,也叫闭包。闭包分为Lua闭包和C闭包,Lua闭包中重要的数据结构是函数原型和upvalue地址数组,而C闭包中重要的数据结构是C函数指针和upvalue数组。而upvalue数量为0的闭包,称为“函数”。
1.1 Closure 闭包
(lobject.h) Closure 闭包的结构
#define ClosureHeader \CommonHeader; \ //#define CommonHeader GCObject *next; lu_byte tt; lu_byte markedlu_byte isC; \ //是否为C闭包lu_byte nuvalues; \ //upvalue的数量GCObject* gclist; \ //自己所能关联到的GC对象链表,见GC章节struct Table* env; //环境表//C闭包的结构
typedef struct CClosure
{ClosureHeader;lua_CFunction f;//C函数指针 //typedef int (*lua_CFunction) (lua_State *L);TValue upvalue[1];//第一个upval的首地址
} CClosure;//Lua闭包的结构
typedef struct LClosure
{ClosureHeader;struct Proto* p;//函数原型UpVal* upvals[1];//第一个upval地址值所在的首地址
} LClosure;//闭包的结构
typedef union Closure
{CClosure c;//C闭包LClosure l;//lua闭包
} Closure;
1.2 Proto 函数原型
函数原型中,重要的结构有:常量数组,指令数组,内嵌的函数原型地址的数组,行号信息数组,局部变量信息数组,upvalue名字数组等等。
(lobject.h) Proto
typedef struct Proto
{CommonHeader;//#define CommonHeader GCObject *next; lu_byte tt; lu_byte markedTValue* k;//常量数组//#define LUAI_UINT32 unsigned int//typedef LUAI_UINT32 lu_int32;//typedef lu_int32 Instruction;Instruction* code;//指令数组 struct Proto** p;//内嵌函数原型地址的数组int* lineinfo;//指令的行号信息数组struct LocVar* locvars;//局部变量信息的数组TString** upvalues;//upvalue名字字符串的数组TString* source;//源文件路径字符串int sizeupvalues;//upvalue名字字符串的数组的元素个数int sizek;//常量数组的元素个数int sizecode;//指令数组的元素个数int sizelineinfo;//指令的行号信息数组的元素个数int sizep;//内嵌函数原型地址的数组的元素个数int sizelocvars;//局部变量信息的数组的元素个数int linedefined;//源码第一行的行号int linelastdefined;//源码最后一行的行号GCObject* gclist;//自己所能关联到的GC对象链表,见GC章节lu_byte nups;//upvalue的数量lu_byte numparams;//参数数量lu_byte is_vararg;//是否是可变参数函数lu_byte maxstacksize;//函数栈的最大容量
} Proto;
1.3 UpVal 外部局部变量(upvalue)
(lobject.h) UpVal
typedef struct UpVal
{CommonHeader;//#define CommonHeader GCObject *next; lu_byte tt; lu_byte markedTValue* v;//若为open状态,则指向L->stack中的某个位置,下文我们称呼为upvalue的open地址;若为close状态,则指向u.value地址union{TValue value;//upvalue为close状态时的值struct//upvalue为open状态时的结构{struct UpVal *prev;//在g->uvhead链表中的前驱UpValstruct UpVal *next;//在g->uvhead链表中的后继UpVal} l;} u;
} UpVal;
1.4 LocVar 局部变量信息
(lobject.h) LocVar
typedef struct LocVar
{TString* varname;//局部变量名字int startpc;//该局部变量存活的第一个指令序号int endpc;//该局部变量不存活的第一个指令序号
} LocVar;
1.5 SParser 语法分析所需要的结构
(ldo.c) SParser
struct SParser
{ZIO* z;//读写流对象 //typedef struct Zio ZIO;Mbuffer buff;//缓冲对象const char* name;//源代码名字
}
1.6 Zio 读写流对象
(lzio.h) Zio
struct Zio
{size_t n;//还未读的字节数const char* p;//缓冲区当前读到的位置//typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);lua_Reader reader;//读写lua代码块的函数void* data;//附加的数据lua_State* L;//lua虚拟机
};
1.7 Mbuffer 缓冲对象
(lzio.h) Mbuffer
typedef struct Mbuffer
{char* buff;//字节数组size_t n;//字节数size_t buffsize;//字节数组的容量
} Mbuffer;
1.8 lua_Debug 调试信息
(lua.h) lua_Debug
struct lua_Debug
{int event;const char* name;const char* namewhat;/* `global', `local', `field', `method' */const char* what;/* `Lua', `C', `main', `tail' */const char* source;//源代码路径名(短)int currentline;//当前行数int nups;//upvalues数量int linedefined;//第一行代码行数int lastlinedefined;//最后一行代码行数char short_src[LUA_IDSIZE];//源代码路径名(短)//#define LUA_IDSIZE 60int i_ci;//当前激活的函数索引
};
1.9 CallInfo 函数调用信息
(lstate.h) CallInfo
typedef struct CallInfo
{StkId base;//函数调用栈基址StkId func;//函数在栈上的位置StkId top;//函数调用栈顶位置const Instruction* savedpc;//用于保存指令的执行现场,等回到本函数时再赋值给L->savedpc以恢复现场int nresults;//该函数期望的返回值数量int tailcalls;//该函数尾调用的层数(尾调用不会用新的CallInfo,而是用旧的CallInfo)
}
1.10 lua_longjmp 跳转信息
(ldo.c) lua_longjmp
struct lua_longjmp
{struct lua_longjmp* previous;//跳转信息的前驱节点luai_jmpbuf b;//#define luai_jmpbuf jmp_bufvolatile int status;//状态码
}
1.11 虚拟机状态码
(lua.h)
#define LUA_YIELD 1 //挂起
#define LUA_ERRRUN 2 //运行时错误
#define LUA_ERRSYNTAX 3 //语法错误
#define LUA_ERRMEM 4 //内存错误
#define LUA_ERRERR 5 //在错误处理函数中出错
1.12 luaT_typenames 类型名称字符串
(ltm.c) 与 lua.h中基础类型枚举 和 lobject.h中的扩展类型枚举一一对应
const char *const luaT_typenames[] = {"nil", "boolean", "userdata", "number","string", "table", "function", "userdata", "thread","proto", "upval"
};
(lua.h) 基础类型枚举
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8
(lobject.h) 扩展类型枚举
#define LAST_TAG LUA_TTHREAD
#define LUA_TPROTO (LAST_TAG+1)
#define LUA_TUPVAL (LAST_TAG+2)
1.13 CallS 调用函数结构(f_call的参数)
(lapi.c) CallS
struct CallS
{StkId func;//函数地址int nresults;//实际返回值个数
};
1.14 CCallS 调用C函数结构 (f_Ccall的参数)
(lapi.c) CCallS
struct CCallS
{lua_CFunction func;//C函数 //typedef int (*lua_CFunction) (lua_State *L);void* ud;
};
2 闭包相关API
2.1 luaF_newproto 新建函数原型
(lfunc.c) luaF_newproto 新建函数原型
Proto* luaF_newproto(lua_State* L)
{Proto* f = luaM_new(L, Proto);//#define LAST_TAG LUA_TTHREAD//#define LUA_TPROTO (LAST_TAG+1)luaC_link(L, obj2gco(f), LUA_TPROTO);f->k = NULL;f->sizek = 0;f->p = NULL;f->sizep = 0;f->code = NULL;f->sizecode = 0;f->sizelineinfo = 0;f->sizeupvalues = 0;f->nups = 0;f->upvalues = NULL;f->numparams = 0;f->is_vararg = 0;f->maxstacksize = 0;f->lineinfo = NULL;f->sizelocvars = 0;f->locvars = NULL;f->linedefined = 0;f->lastlinedefined = 0;f->source = NULL;return f;
}
2.2 luaF_newCclosure 新建C闭包
(lfunc.c) luaF_newCclosure
Closure* luaF_newCclosure(lua_State* L, int nelems, Table* e)
{//#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + cast(int, sizeof(TValue)*((n)-1)))Closure* c = cast(Closure*, luaM_malloc(L, sizeCclosure(nelems)));luaC_link(L, obj2gco(c), LUA_TFUNCTION);c->c.isC = 1;c->c.env = e;c->c.nupvalues = cast_byte(nelems);return c;
}
2.3 luaF_newLclosure 新建lua闭包
(lfunc.c) luaF_newLclosure
Closure* luaF_newLclosure(lua_State* L, int nelems, Table* e)
{Closure* c = cast(Closure*, luaM_malloc(L, sizeLclosure(nelems)));luaC_link(L, obj2gco(c), LUA_TFUNCTION);c->l.isC = 0;c->l.env = e;c->l.nupvalues = cast_byte(nelems);while (nelems--){c->l.upvals[nelems] = NULL;}
}
2.4 luaF_newupval 新建upvalue(起初为close状态)
(lfunc.c) luaF_newupval
UpVal* luaF_newupval(lua_State* L)
{UpVal* uv = luaM_new(L, UpVal);luaC_link(L, obj2gco(uv), LUA_TUPVAL);//#define LUA_TUPVAL (LAST_TAG+2)uv->v = &uv->u.value;//新创建的upvalue的value地址指向u.value地址,表示是close状态setnilvalue(uv.v);return uv;
}
2.5 luaF_findupval 寻找open地址为level的upvalue,若找不到则新建一个
(lfunc.c) luaF_findupval
UpVal* luaF_findupval(lua_State* L, StkId level)
{global_State* g = G(L);GCObject** pp = &L->openupval;//open状态的upvalue链表UpVal* p;//当前遍历的upvaluewhile (*pp != NULL && (p = ngcotouv(*pp))->v >= level){//找到了v==level的upvalueif (p->v == level){if (isdead(g, obj2gco(p))){changewhite(obj2gco(p));}return p;}pp = &p->next;}//若找不到,则新建一个UpVal* uv = luaM_new(L, UpVal);uv->tt = LUA_TUPVAL;uv->marked = luaC_white(g);uv.v = level;//新的upvalue插入L->openupval链表头部uv->next = *pp;*pp = obj2gco(uv);//新的upvalue插入g->uvhead链表头部uv->u.l.prev = &g->uvhead;uv->u.l.next = g->uvhead.u.l.next;uv->u.l.next->u.l.prev = uv;g->uvhead.u.l.next = uv;return uv;
}
2.6 luaF_close 关闭协程L上所有open地址>=level的upvalue
(lfunc.c) luaF_close
void luaF_close(lua_State* L, StkId level)
{UpVal* uv;global_State* g = G(L);//遍历openupval 链表中的upvalue,对于每个upvalue,若其在栈上的地址>=level,将其移出upvalue链表//#define ngcotouv(o) check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level){//将该upvalue移出openupval 链表GCObject* o = obj2gco(uv);//#define obj2gco(v) (cast(GCObject *, (v)))L->openupval = uv->next;//若该upval已经死了,则移出链表并释放掉if (isdead(g, o))//#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS){luaF_freeupval(L, uv);}//若upval没死,则移出链表,设为close状态,加入GC管辖范围else{//upvalue移出链表unlinkupval(uv);//将upvalue在栈上指向的值赋值给内部的value字段setobj(L, &uv->u.value, uv->v);//将upvalue的v指向自己内部的value,即设为close状态uv->v = &uv->u.value;//将upvalue加入gc链luaC_linkupval(L, uv);//luaC_linkupval见GC章节}}
}
2.7 luaF_freeproto 释放一个函数原型
(lfunc.c) luaF_freeproto
void luaF_freeproto(lua_State* L, Proto* f)
{//释放指令数组luaM_freearray(L, f->code, f->sizecode, Instruction);//释放子函数原型数组luaM_freearray(L, f->p, f->sizep, Proto*);//释放常量数组luaM_freearray(L, f->k, f->sizek, TValue);//释放行号信息数组luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);//释放局部变量信息luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);//释放upvalue名字数组luaM_freearray(L, f->upvalues, f->sizeupvalues, TString*);//释放函数原型本身luaM_free(L, f);
}
2.8 luaF_freeclosure 释放一个闭包
(lfunc.c) luaF_freeclosure
void luaF_freeclosure(lua_State* L, Closure* c)
{int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : sizeLclosure(c->l.nupvalues);//#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0)luaM_freemem(L, c, size);
}
2.9 luaF_freeupval 释放一个upvalue(若是open状态,则移出g->uvhead链表)
(lfunc.c) luaF_freeupval
void luaF_freeupval(lua_State* L, UpVal* uv)
{//若upvalue的状态是open,则将它移出双链表int is_open = uv->v != &uv->u.value;if (is_open){//将uv所在的移出open upval双链表unlinkupval(uv);}//释放内存luaM_free(L, uv);
}
(lfunc.c) unlinkupval 将uv移出g->uvhead链表
static void unlinkupval(UpVal* uv)
{uv.u.l.next->u.l.prev = uv->u.l.prev;uv.u.l.prev->u.l.next = uv->u.l.next;
}
2.10 luaF_getlocalname 获取函数原型f中的第local_number个存活的局部变量的名字
(lfunc.c) luaF_getlocalname
const char* luaF_getlocalname(const Proto* f, int local_number, int pc)
{for (int i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++){if (pc < f->locvars[i].endpc)//pc属于[startpc, endpc)范围内,则表示局部变量是存活的{local_number--;if (local_number == 0){return getstr(f->locvars[i].varname);}}}return NULL;
}
3 函数调用相关API
3.1 luaD_protectedparser 保护模式下进行语法分析
(ldo.c) luaD_protectedparser
int luaD_protectedparser(lua_State* L, ZIO* z, const char* name)
{struct SParser p;p.z = z;p.name = name;//#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)luaZ_initbuffer(L, &p.buff);int status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);//#define luaZ_resizebuffer(L, buff, size) (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), (buff)->buffsize = size)//#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)luaZ_freebuffer(L, &p.buff);return status;
}
(ldo.c) f_parser 通过luaU_undump或luaY_parser生成的函数原型,最终生成一个闭包,并压栈
static void f_parser(lua_State* L, void* ud)
{struct SParser* p = cast(struct SParser*, ud);//获取第一个字符int c = luaZ_lookahead(p->z);luaC_checkGC(L);//luaC_checkGC见GC章节//#define LUA_SIGNATURE "\033Lua"//若第一个字符是 \033 则说明是lua二进制文件,则执行luaU_undump;若不是,则说明是lua源文件,则执行luaY_parser//luaU_undump见指令章节 luaY_parser见解释器章节Proto* tf = ((c == LUA_SIGNATURE[o]) ? luaU_undump : luaY_parser)(L, p->z, &p->buff, p->name);//新建Lua闭包,指定其upvalue数量为函数原型的nups,指定其环境表为Global表,指定其函数原型Closure* cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));cl->l.p = tf;for (int i = 0; i < tf->nups; i++){cl->l.upvals[i] = luaF_newupval(L);}//闭包压栈setclvalue(L, L->top, cl);incr_top(L);//#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
}
(lzio.c) luaZ_lookahead 向后读一个字符,若缓冲对象的内容全部读完了,则继续定量读取文件到缓冲对象中
int luaZ_lookahead(ZIO* z)
{//若一个缓冲对象中 未读的字节数为0,也就是全部读完了,则从文件中继续读取内容到缓冲区,若文件也读完了,则返回EOZif (z->n == 0){//定量读取文件内容,填充到缓冲区,读到第一个字符if (luaZ_fill(z) == EOZ) //#define EOZ (-1) /* end of stream */{return EOZ;}//由于luaZ_fill多读了一个字符,且文件指针向后移了一位,所以这里要移回来,未读的字符数量也要+1回来z->n++;z->p--;}return char2int(*z->p);
}
(lzio.c) luaZ_fill 定量读取文件内容到缓冲对象中,返回读到的第一个字符
int luaZ_fill(ZIO* z)
{//定量读取文件内容到缓冲对象中,若文件都读完了,则返回EOZlua_State* L = z->L;size_t size;const char* buff = z->reader(L, z->data, &size);if (buff == NULL || size == 0){return EOZ;}//返回读到的第一个字符,也就是说 剩余未读的有size-1个,缓冲区指针指向索引为1的字符z->n = size - 1;z->p = buff;return char2int(*(z->p++));
}
3.2 luaD_callhook 调用钩子方法
(ldo.c) luaD_callhook
//typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
void luaD_callhook(lua_State* L, int event, int line)
{lua_Hook hook = L->hook;if (hook && L->allowhook){ptrdiff_t top = savestack(L, L->top);//#define savestack(L,p) ((char *)(p) - (char *)L->stack)ptrdiff_t ci_top = savestack(L, L->ci->top);lua_Debug ar;ar.event = event;ar.currentline = line;if (event == LUA_HOOKTAILRET){ar.i_ci = 0;//尾调用,没有调试信息}else{ar.i_ci = cast_int(L->ci, L->base_ci);}luaD_checkstack(L, LUA_MINSTACK);L->ci->top = L->top + LUA_MINSTACK;L->allowhook = 0;//在钩子函数调用过程中不允许再调用钩子//调用钩子函数(*hook)(L, &ar);L->allowhook = 1;L->ci->top = restorestack(L, ci_top);L->top = restorestack(L, top);}
}
3.3 luaD_precall 调用函数的准备工作
(ldo.c) luaD_precall 调用函数的准备工作
int luaD_precall(lua_State* L, StkId func, int nresults)
{//要检测的函数不是函数类型,则获取该函数的 __call 元方法,插入func位置if (!ttisfunction(func)){func = tryfuncTM(L, func);}ptrdiff_t funcr = savestack(L, func);LClosure* cl = &clvalue(func)->l;L->ci->savedpc = L->savedpc;//保留指令执行现场//若为lua函数if (!cl->isC){StkId base;Proto* p = cl->p;luaD_checkstack(L, p->maxstacksize);//luaD_checkstack可能会导致L->stack重新分配内存,所以需要配套使用 savestack宏 和 restorestack宏func = restorestack(L, funcr);//若无可变参数if (!p->is_vararg){base = func + 1;//限制栈顶为 最后一个形参 的 下一个位置if (L->top > base + p->numparams){L->top = base + p->numparams;}}//若有可变参数else{//实参数量int nargs = cast_int(L->top - func) - 1;//根据函数原型 和 实参数量 调整 栈结构base = adjust_varargs(L, p, nargs);//adjust_varargs可能会导致L->stack重新分配内存,所以需要配套使用 savestack宏 和 restorestack宏func = restorestack(L, funcr);}//新增一个CallInfo,压入L->ci栈//#define inc_ci(L) ((L->ci == L->end_ci) ? growCI(L) : ++L->ci)CallInfo* ci = inc_ci(L);ci->func = func;L->base = ci->base = base;//记录当前函数调用的栈基址ci->top = L->base + p->maxstacksize;L->savedpc = p->code;//指令地址指向函数原型的第一条指令ci->tailcalls = 0;ci->nresults = nresults;for (StkId st = L->top; st < ci->top; st++){setnilvalue(st);}L->top = ci->top;if (L->hookmask & LUA_MASKCALL){L->savedpc++;luaD_callhook(L, LUA_HOOKCALL, -1);L->savedpc--;}return PCRLUA;}//C函数else{luaD_checkstack(L, LUA_MINSTACK);//#define LUA_MINSTACK 20CallInfo* ci = inc_ci(L)ci->func = restorestack(L, funcr);L->base = ci->base = ci->func + 1;ci->top = L->top + LUA_MINSTACK;ci->nresults = nresults;if (L->hookmask & LUA_MASKCALL){luaD_callhook(L, LUA_HOOKCALL, -1);}//直接调用C函数int n = (*curr_func(L)->c.f)(L);//#define curr_func(L) (clvalue(L->ci->func))if (n < 0)//yield{return PCRYIELD;}else{//函数调用收尾工作luaD_poscall(L, L->top - n);return PCRC;}}
}
(ldo.c) tryfuncTM 将func的__call元方法插入func位置
static StkId tryfuncTM(lua_State* L, StkId func)
{const TValue* tm = luaT_gettmbyobj(L, func, TM_CALL);ptrdiff_t funcr = savestack(L, func);//若 func 的 __call 元方法依然不是函数类型,则报错if (!ttisfunction(tm)){luaG_typeerror(L, func, "call");}//从 L->top - 1 到 func 位置,所有的元素都上移一位for (StkId p = L->top; p > func; p--){setobjs2s(L, p, p - 1);}incr_top(L);//#define incr_top(L) {luaD_checkstack(L,1); L->top++;}func = restorestack(L, funcr);//incr_top可能会导致L->stack重新分配内存,所以需要配套使用 savestack宏 和 restorestack宏//func 位置放 _call 元方法 tmsetobj2s(L, func, tm);return func;
}
(ldo.c) adjust_varargs 对于可变参数函数,根据函数原型p和实际参数actual来调整栈
调整之后的栈结构, […,func,nfixargs个nil,nfixargs个参数,arg表,栈顶位置]
static StkId adjust_varargs(lua_State* L, Proto* p, int actual)
{int nfixargs = p->numparams;Table* htab = NULL;StkId base;//若实参比形参少,则将栈顶调整到最后一个形参的下一个位置for (; actual < nfixargs; ++actual){setnilvalue(L->top++);}//若函数形参有 ... 且 函数体有 名为 arg 的变量 的情况if (p->is_vararg & VARARG_NEEDSARG){int nvar = actual - nfixargs;//额外的参数luaC_checkGC(L);//创建arg表,这个表的array部分是所有可变参数,hash部分有个为"n"的key存放可变参数的数量htab = luaH_new(L, nvar, 1);for (int i = 0; i < nvar; i++){setobj2n(L, luaH_setnum(L, htab, i + 1), L->top - nvar + i);}setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));}//将固定参数移到最后StkId fixed = L->top - actual;//第一个固定参数的原来位置base = L->top;for (int i = 0; i < nfixargs; i++){setobjs2s(L, L->top++, fixed + i);setnilvalue(fixed + i);}//arg表压栈if (htab){sethvalue(L, L->top++, htab);}return base;
}
3.4 luaD_call 调用函数func,期望的返回值时nResults个,第一个返回值在func位置,后面的返回值依次往上
(ldo.c) luaD_call
void luaD_call(lua_State* L, StkId func, int nResults)
{if (++L->nCalls >= LUAI_MAXCCALLS){if (L->nCalls == LUAI_MAXCCALLS){luaG_runeeror(L, "C stack overflow");}else if (L->nCcalls >= (LUAI_MAXCCALLS + LUAI_MAXCCALLS >> 3)){luaD_throw(L, LUA_ERRERR);}}//若为lua函数,则执行下一条指令(下一条指令已经指向函数原型第一条指令);而C函数在luaD_precall的里就已经执行了if (luaD_precall(L, func, nResults) == PCRLUA){luaV_execute(L, 1);//luaV_execute见指令章节}L->nCcalls--;luaC_checkGC(L);
}
3.5 luaD_pcall 以保护模式调用C函数func,若有异常则做收尾工作
(ldo.c) luaD_pcall
//typedef void (*Pfunc) (lua_State *L, void *ud);
int luaD_pcall(lua_State* L, Pfunc func, void* u, ptrdiff_t old_top, ptrdiff_t ef)
{unsigned short oldnCcalls = L->nCcalls;ptrdiff_t old_ci = saveci(L, L->ci);//#define saveci(L,p) ((char *)(p) - (char *)L->base_ci)lu_byte old_allowhooks = L->allowhook;ptrdiff_t old_errfunc = L->errfunc;L->errfunc = ef;//以保护模式执行funcint status = luaD_rawrunprotected(L, func, u);//状态码不为0表示有异常if (status != 0){StkId oldtop = restorestack(L, old_top);//关闭所有 open地址>=oldtop 的upvalaueluaF_close(L, oldtop);//根据状态码status将错误信息放在oldtop位置,并以oldtop的下一个位置为栈顶luaD_seterrorobj(L, status, oldtop);L->nCcalls = oldnCcalls;L->ci = restoreci(L, old_ci);L->base = L->ci->base;L->savedpc = L->ci->savedpc;L->allowhook = old_allowhook;//调整CallInfo数组restore_stack_limit(L);}L->errfunc = old_errfunc;return status;
}
(ldo.c) restore_stack_limit 调整CallInfo数组(若CallInfo数组太大了,且正在使用的CallInfo数量小于限定值,则重新分配CallInfo数组)
static void restore_stack_limit(lua_State* L)
{if (L->size_ci > LUAI_MAXCALLS)//#define LUAI_MAXCALLS 20000{int inuse = cast_int(L->ci - L->base_ci);if (inuse + 1 < LUAI_MAXCALLS){luaD_reallocCI(L, LUAI_MAXCALLS);}}
}
3.6 luaD_reallocCI 重新分配CallInfo数组并重新设置ci和end_ci指针的指向
(ldo.c) luaD_reallocCI
void luaD_reallocCI(lua_State* L, int newsize)
{CallInfo* oldci = L->base_ci;luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);L->size_ci = newsize;L->ci += L->base_ci - oldci;L->end_ci = L->base_ci + L->size_ci - 1;
}
3.7 luaD_rawrunprotected 以保护模式执行C函数f,返回状态码
(ldo.c) luaD_rawrunprotected
int luaD_rawrunprotected(lua_State* L, Pfunc f, void* ud)
{struct lua_longjmp lj;lj.status = 0;lj.previous = L->errorJmp;L->errorJmp = &lj;//#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }LUAI_TRY(L, &lj, (*f)(L, ud););//宏展开就是/*//setjmp一开始为0,会进入if//若f执行过程中有longjmp执行,则代码会跳转到此处,那时setjmp的值由longjmp决定,若不为0,则不进入if了if (setjmp((&lj)->b) == 0){(*f)(L, ud);}*/L->errorJmp = lj.previous;return lj.status;
}
3.8 luaD_throw 以错误码errcode抛出异常,若无L->errorJmp,则执行g->panic函数并退出程序
(ldo.c) luaD_throw
void luaD_throw(lua_State* L, int errcode)
{if (L->errorJmp){L->errorJmp->status = errcode;//#define LUAI_THROW(L,c) longjmp((c)->b, 1)LUAI_THROW(L, L->errorJmp);}else{L->status = cast_byte(errcode);if (G(L)->panic){resetstack(L, errcode);G(L)->panic(L);}exit(EXIT_FAILURE);}
}
(ldo.c) resetstack
static void resetstack(lua_State* L, int status)
{L->ci = L->base_ci;L->base = L->ci->base;luaF_close(L, L->base);//关闭所有open地址>=level的upvalueluaD_seterrorobj(L, status, L->base);//根据错误码status将错误信息放在L->base,栈顶设为L->base+1L->nCcalls = L->baseCcalls;L->allowhook = 1;restore_stack_limit(L);//调整CallInfo数组L->errfunc = 0;L->errorJmp = NULL;
}
3.9 luaD_seterrorobj 根据错误码errcode将错误信息放在oldtop,栈顶设为oldtop+1
(ldo.c) luaD_seterrorobj
void luaD_seterrorobj(lua_State* L, int errcode, StkId oldtop)
{switch (errcode){case LUA_ERRMEM:{setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));//#define MEMERRMSG "not enough memory"break;}case LUA_ERRERR:{setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));break;}case LUA_ERRSYNTAX:case LUA_ERRRUN:{setobjs2s(L, oldtop, L->top - 1);//将L->top-1处错误信息移到oldtop处break;}}L->top = oldtop + 1;
}
3.10 luaD_poscall 函数调用的收尾工作,firstResult为第一个返回值的地址(调整之前的地址),返回值若为0表示函数调用返回值数量由函数原型决定
(ldo.c) luaD_poscall
int luaD_poscall(lua_State* L, StkId firstResult)
{if (L->hookmask & LUA_MASKRET){//触发函数返回钩子回调firstResult = callrethooks(L, firstResult); }CallInfo* ci = L->ci--;//恢复L->ci,指向上一个函数调用StkId res = ci->func;//第一个返回值的位置int wanted = ci->nresults;//函数原型的返回值数量L->base = (ci - 1)->base;//恢复函数环境栈基址L->savedpc = (ci - 1)->savedpc;//恢复指令指针//纠正返回值的位置for (int i = wanted; i != 0 && firstResult < L->top; i--){setobjs2s(L, res++, firstResult++);}//多出的实际返回值以nil填充while (i-- > 0){setnilvalue(res++);}//栈顶指向原来的函数地址处(栈又恢复到了该函数调用之前)L->top = res;return (wanted - LUA_MULTRET);//若 wanted == LUA_MULTRET 则返回0
}
(ldo.c) callrethooks 触发函数返回的钩子回调
static StkId callrethooks(lua_State* L, StkId firstResult)
{ptrdiff_t fr = savestack(L, firstResult);//触发LUA_HOOKRET钩子回调luaD_callhook(L, LUA_HOOKRET, -1);//#define ci_func(ci) (clvalue((ci)->func))//#define f_isLua(ci) (!ci_func(ci)->c.isC)if (f_isLua(L->ci)){//若是尾调用,逐层触发LUA_HOOKTAILRET钩子回调while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--){luaD_callhook(L, LUA_HOOKTAILRET, -1);}}return restorestack(L, fr);
}
3.11 luaD_reallocstack 重新分配栈内存
(ldo.c) luaD_reallocstack
void luaD_reallocstack(lua_State* L, int newsize)
{//记录旧栈起始地址,方便后续调整栈做参考用TValue* oldstack = L->stack;//栈新的容量int realsize = newsize + 1 + EXTRA_SIZE;//#define EXTRA_STACK 5 预留的空间,比如为元方法调用预留等//重新分配栈空间luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);L->stacksize = realsize;L->stack_last = L->stack + newsize;//根据栈的旧起始地址调整栈的 栈顶地址,upvalue的值的地址,函数调用信息的栈顶地址、基址、函数地址,栈基址correctstack(L, oldstack);
}
(ldo.c) correctstack 对栈进行纠正(栈顶地址,upvalue的值的地址,函数调用信息的栈顶地址、基址、函数地址,栈基址)
static void correctstack(lua_State* L, TValue* oldstack)
{int offset = L->stack - oldstack;//更新栈顶位置L->top += offset;//更新各个upvalue中值的地址for (GCObject* up = L->openupval; up != NULL; up = up->gch.next){//#define gco2uv(o) &((o)->uv)gco2uv(up)->v += offset;}//更新各个函数调用信息中的 栈顶地址,基址,函数地址for (CallInfo* ci = L->base_ci; ci <= L->ci; ci++){ci->top += offset;ci->base += offset;ci->func += offset;}//更新栈基址L->base += offset;
}
3.12 luaD_growstack 给栈扩容(至少扩大2倍)
(ldo.c) luaD_growstack
void luaD_growstack(lua_State* L, int n)
{if (n <= L->stacksize){luaD_reallocstack(L, 2 * L->stacksize);}else{luaD_reallocstack(L, L->stacksize + n);}
}
4 错误处理API
4.1 luaG_errormsg 尝试调用错误处理函数,参数是原栈顶的错误信息字符串
(ldebug.c) luaG_errormsg
void luaG_errormsg(lua_State* L)
{if (L->errfunc != 0){//注意 L->errfunc 记录的是 错误处理函数距离栈起始地址的 差值;通过 restorestack 宏还原得到错误处理函数在栈上的地址StkId errfunc = restorestack(L, L->errfunc);if (!ttisfunction(errfunc)){luaD_throw(L, LUA_ERRERR);}setobjs2s(L, L->top, L->top - 1);//栈顶的错误信息字符串向上移一位setobjs2s(L, L->top - 1, errfunc);//错误函数插入到此位置incr_top(L);luaD_call(L, L->top - 2, 1);//调用错误处理函数}luaD_throw(L, LUA_ERRRUN);
}
4.2 luaG_runerror 处理运行时错误,为fmt为错误信息的格式串
(ldebug.c) luaG_runerror
void luaG_runerror(lua_State* L, const char* fmt, ...)
{va_list argp;va_start(argp, fmt);//将错误信息准备到栈顶 //luaO_pushvfstring功能是将格式化字符串压栈,返回字符串首地址addinfo(L, luaO_pushvfstring(L, fmt, argp));va_end(argp);luaG_errormsg(L);
}
(ldebug.c) addinfo 以msg为信息添加更完善的信息
static void addinfo(lua_State* L, const char* msg)
{CallInfo* ci = L->ci;//若当前函数调用时lua函数,则添加 文件名和行号信息if (isLua(ci)){char buff[LUA_IDSIZE];//#define LUA_IDSIZE 60int line = currentline(L, ci);//获取当前行号/*static Proto *getluaproto (CallInfo *ci) {return (isLua(ci) ? ci_func(ci)->l.p : NULL);}*/luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);}
}
(ldebug.c) currentline 根据函数调用信息ci获取当前执行到的行号
static int currentline(lua_State* L, CallInfo* ci)
{int pc = currentpc(L, ci);//获取当前指令索引if (pc < 0){return -1;//只有当前激活的lua函数才能有行号信息}//#define getline(proto,pc) (((proto)->lineinfo) ? (proto)->lineinfo[pc] : 0)return getline(ci_func(ci)->l.p, pc);
}
(ldebug.c) currentpc 根据函数调用信息ci获取当前执行的指令索引
static int currentpc(lua_State* L, CallInfo* ci)
{if (!isLua(ci)){return -1;//不是lua函数当然没有指令索引}if (ci == L->ci){ci->savedpc = L->savedpc;}//#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1)//因为savedpc指向的是下一条要执行的指令,所以减1表示当前正在处理的指令return pcRel(ci->savedpc, ci_func(ci)->l.p);
}
(lobject.c) luaO_chunkid 根据源文件路径source获取chunkid字符串,输出到字符串地址out
void luaO_chunkid(char* out, const char* source, size_t bufflen)
{//若源文件路径以 = 开头,则去掉 =if (*source == '='){strncpy(out, source + 1, bufflen);out[bufflen - 1] = '\0';}else{if (*source == '@'){source++;//跳过'@'bufflen -= sizeof(" '...' ");size_t l = strlen(source);strcpy(out, "");//若源文件路径太长了,则带上省略号,取最后的部分if (l > bufflen){source += l - bufflen;strcat(out, "...");}strcat(out, source);}else{//检索字符串 source 开头连续有几个字符都不含 \n 或 \r //也就是获取第一行字符个数size_t len = strcspn(source, "\n\r");bufflen -= sizeof(" [string \"...\"] ");//若第一行字符太多,则限制为bufflen个if (len > bufflen){len = bufflen;}strcpy(out, "[string \"");if (source[len] != '\0'){strncat(out, source, len);strcat(out, "...");}else{strcat(out, source);}strcat(out, "\"]");}}
}
4.3 luaG_typeerror 处理类型错误
(ldebug.c) luaG_typeerror
void luaG_typeerror(lua_State* L, const TValue* o, const char* op)
{const char* name = NULL;const char* t = luaT_typenames[ttype(o)];const char* kind = (isinstack(L->ci, o)) ? getobjname(L, L->ci, cast_int(o - L->base), &name) : NULL;if (kind){//#define LUA_QL(x) "'" x "'"//#define LUA_QS LUA_QL("%s")luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", op, kind, name, t);}else{luaG_runerror(L, "attempt to %s a %s value", op, t);}
}
(ldebug.c) isinstack 判断值o是否在函数调用ci的栈帧内
static int isinstack(CallInfo* ci, const TValue* o)
{for (StkId p = ci->base; p < ci->top; p++){if (o == p){return 1;}}return 0;
}
(ldebug.c) getobjname 根据函数调ci,栈起始地址偏移stackpos位置,获取对象的名字,输出到name地址处,返回值有 NULL|“local”|“global”|“field”|“upvalue”|“method”
static const char* getobjname(lua_State* L, CallInfo* ci, int stackpos, const char** name)
{if (isLua(ci)){Proto* p = ci_func(ci)->l.p;int pc = currentpc(L, ci);*name = luaF_getlocalname(p, stackpos + 1, pc);if (*name){return "local";}Instruction i = symbexec(p, pc, stackpos);switch (GET_OPCODE(i)){//...//见指令章节}}return NULL;
}
4.4 luaG_concaterror 处理字符串连接错误
(ldebug.c) luaG_concaterror
void luaG_concaterror(lua_State* L, StkId p1, StkId p2)
{if (ttisstring(p1) || ttisnumber(p1)){p1 = p2;}luaG_typeerror(L, p1, "concatenate");
}
4.5 luaG_aritherror 处理算术错误
(ldebug.c) luaG_aritherror
void luaG_aritherror(lua_State* L, const TValue* p1, const TValue* p2)
{TValue temp;if (luaV_tonumber(p1, &temp) == NULL){p2 = p1;}luaG_typeerror(L, p2, "perform arithmetic on");
}
4.6 luaG_ordererror 处理比较错误
(ldebug.c) luaG_ordererror
int luaG_ordererror(lua_State* L, const TValue* p1, const TValue* p2)
{const char* t1 = luaT_typenames[ttype(p1)];const char* t2 = luaT_typenames[ttype(p2)];/*nil boolean userdata number string table function thread 的2号字符分别是l o e m r b n r糟了! string 和 thread 的2号字符是相等的吗,所以2号字符不能作为判断类型相同的依据和群友讨论了,是一个bug,lua 5.4 已经改成了 strcmp(t1, t2) == 0 判断了*/if (t1[2] == t2[2]){luaG_runerror(L, "attempt to compare two %s value", t1);}else{luaG_runerror(L, "attempt to compare %s with %s", t1, t2);}return 0;
}
5 对外API
5.1 lua_call 调用L->top-nargs-1处的函数,以其上nargs个值为参数,实际返回值为nresults个
(lua.c) lua_call
LUA_API void lua_call(lua_State* L, int nargs, int nresults)
{StkId func = L->top - (nargs + 1);luaD_call(L, func, nresults);//若实际返回值数量==函数原型返回值数量,L->ci->top = max(L->ci->top, L->top)//#define adjustresults(L,nres) { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }adjustresults(L, nresults);
}
5.2 lua_pcall 以保护模式调用L->top-nargs-1处的函数,以其上nargs个值为参数,实际返回值为nresults个,且错误函数距离栈起始地址偏移为errfunc
(lua.c) lua_pcall
LUA_API int lua_pcall(lua_State* L, int nargs, int nresults, int errfunc)
{ptrdiff_t func;if (errfunc == 0){func = 0;}else{StkId o = index2adr(L, errfunc);func = savestack(L, o);}struct CallS c;c.func = L->top - (nargs + 1);c.nresults = nresults;//以保护模式执行 f_call 这个C函数int status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);#define adjustresults(L,nres) { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }adjustresults(L, nresults);return status;
}
(lapi.c) f_call 调用ud数据内的func所指的函数
static void f_call(lua_State* L, void* ud)
{struct CallS* c = cast(struct CallS*, ud);luaD_call(L, c->func, c->nresults);
}
5.3 lua_cpcall 以保护模式执行C函数func
(lapi.c) lua_cpcall
LUA_API int lua_cpcall(lua_State* L, lua_CFunction func, void* ud)
{struct CCallS c;c.func = func;c.ud = ud;int status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);return status;
}
5.4 lua_load 加载函数
(lapi.c) lua_load
LUA_API int lua_load(lua_State* L, lua_Reader reader, void* data, const char* chunkname)
{if (!chunkname){chunkname = "?";}ZIO z;luaZ_init(L, &z, reader, data);int status = luaD_protectedparser(L, &z, chunkname);return status;
}
(lzio.c) luaZ_init 初始化读写流对象
void luaZ_init(lua_State* L, ZIO* z, lua_Reader reader, void* data)
{z->L = L;z->reader = reader;z->data = data;z->n = 0;z->p = NULL;
}
5.5 lua_dump 序列化字节码
(lapi.c) lua_dump
//typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
LUA_API int lua_dump(lua_State* L, lua_Writer writer, void* data)
{int status;TValue* o = L->top - 1;if (isLfunction(o)) //#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC){status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);//luaU_dump见指令章节}else{status = 1;}return status;
}
5.6 lua_atpanic 设置兜底异常处理函数(用于处理未捕获异常的函数),返回旧的兜底异常处理函数
(lapi.c) lua_atpanic
//typedef int (*lua_CFunction) (lua_State *L);
LUA_API lua_CFunction lua_atpanic(lua_State* L, lua_CFunction panicf)
{lua_CFunction old = G(L)->panic;G(L)->panic = panicf;return old;
}