这个模块:主要函数就是index2addr,理解我们知道怎么获取栈中的数据
static TValue *index2addr (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) {//返回L->ci->func 为栈基址的表元素中第idx 个对象 TValue *o = ci->func + idx; api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return o; } else if (!ispseudo(idx)) { /* negative index */ //返回L->top 为栈基址的表元素中的第idx 个表对象 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); return L->top + idx; } else if (idx == LUA_REGISTRYINDEX)//下面的都是伪索引,返回&L->l_G-> l_registry 表对象,用于表示全局表 return &G(L)->l_registry; else { /* upvalues */ //将idx 转为c 闭包idxc,并将L->ci->func 转为c 闭包函数func取出闭包函数func 在idx 处的upvalue idx = LUA_REGISTRYINDEX - idx;//转化idx为闭包的索引 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttislcf(ci->func)) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { CClosure *func = clCvalue(ci->func); return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE; } } }
整合成表格就如下:
重要的宏:
#define isstackindex(i, o) (isvalid(o) && !ispseudo(i))//判断赌赢的i是不是有效的栈index
栈的基本操作函数:int lua_absindex (lua_State *L, int idx)
int lua_gettop (lua_State *L)
void lua_settop (lua_State *L, int idx)
void lua_settop (lua_State *L, int idx)
void lua_insert (lua_State *L, int idx)
void moveto (lua_State *L, TValue *fr, int idx)
void lua_replace (lua_State *L, int idx)
void lua_copy (lua_State *L, int fromidx, int toidx)
void lua_pushvalue (lua_State *L, int idx)
查看栈数据的函数:
int lua_type (lua_State *L, int idx)
const char *lua_typename (lua_State *L, int t)
int lua_iscfunction (lua_State *L, int idx)
int lua_isnumber (lua_State *L, int idx)
int lua_isstring (lua_State *L, int idx)
int lua_isuserdata (lua_State *L, int idx)
int lua_rawequal (lua_State *L, int index1, int index2)
void lua_arith (lua_State *L, int op)//栈内元素的算法操作
int lua_compare (lua_State *L, int index1, int index2, int op)//元素比较
转换函数将栈对象转化为c的类型
lua_tonumberx (lua_State *L, int idx, int *isnum)
lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum)
lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum)
int lua_toboolean (lua_State *L, int idx)
const char *lua_tolstring (lua_State *L, int idx, size_t *len)
size_t lua_rawlen (lua_State *L, int idx)
lua_CFunction lua_tocfunction (lua_State *L, int idx)
void *lua_touserdata (lua_State *L, int idx)
lua_State *lua_tothread (lua_State *L, int idx)
const void *lua_topointer (lua_State *L, int idx)
写入数据到栈
void lua_pushnil (lua_State *L)
void lua_pushnumber (lua_State *L, lua_Number n)
void lua_pushinteger (lua_State *L, lua_Integer n)
void lua_pushunsigned (lua_State *L, lua_Unsigned u)
const char *lua_pushlstring (lua_State *L, const char *s, size_t len)
const char *lua_pushstring (lua_State *L, const char *s)
const char *lua_pushvfstring (lua_State *L, const char *fmt, va_list argp)
const char *lua_pushfstring (lua_State *L, const char *fmt, ...)
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n)
void lua_pushboolean (lua_State *L, int b)
void lua_pushlightuserdata (lua_State *L, void *p)
int lua_pushthread (lua_State *L)
lua调用函数,参数来自栈,并且输出值也在栈
void lua_getglobal (lua_State *L, const char *var)
void lua_gettable (lua_State *L, int idx)
void lua_getfield (lua_State *L, int idx, const char *k)
void lua_rawget (lua_State *L, int idx)
void lua_rawgeti (lua_State *L, int idx, int n)
void lua_rawgetp (lua_State *L, int idx, const void *p)
void lua_createtable (lua_State *L, int narray, int nrec)
void lua_createtable (lua_State *L, int narray, int nrec)
int lua_getmetatable (lua_State *L, int objindex)
void lua_getuservalue (lua_State *L, int idx)
栈的set函数(修改栈的状态)lua调用者
void lua_setglobal (lua_State *L, const char *var)
void lua_settable (lua_State *L, int idx)
void lua_setfield (lua_State *L, int idx, const char *k)
void lua_rawset (lua_State *L, int idx)
void lua_rawseti (lua_State *L, int idx, int n)
void lua_rawsetp (lua_State *L, int idx, const void *p)
int lua_setmetatable (lua_State *L, int objindex)
void lua_setuservalue (lua_State *L, int idx)
运行lua函数
void lua_callk (lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k)
int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k)
int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode)
upval的相关函数
const char *lua_getupvalue (lua_State *L, int funcindex, int n)
const char *lua_setupvalue (lua_State *L, int funcindex, int n)
UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf)
void *lua_upvalueid (lua_State *L, int fidx, int n)
void lua_upvaluejoin (lua_State *L, int fidx1, int n1, int fidx2, int n2)
gc的相关函数
int lua_gc (lua_State *L, int what, int data)
其他函数
int lua_next (lua_State *L, int idx)