- 1 背景
- 2 栈(寄存器数组),虚拟机,全局状态机
- 2.1 栈定义在lua_State结构体中
- 2.2 global_State 全局状态机
- 2.3 lua_newstate 主虚拟机和全局状态机的创建
- 2.4 lua_close 关闭虚拟机
- 3 栈上的地址
- 3.1 假索引
- 3.2 根据数字索引获取栈上的地址
- 4 基本的栈操作API
- 4.1 lua_gettop 获取 栈顶 相对于 当前函数栈基址 的偏移
- 4.2 lua_settop 设置栈顶的位置
- 4.3 lua_pushvalue 将指定索引的值复制到栈顶
- 4.4 lua_remove 将指定索引的值入栈,且上面的值依次下移一位
- 4.5 lua_insert 将栈顶的值插入到指定索引,且上面的值依次上移一位
- 4.6 lua_replace 将栈顶的值替代掉指定索引的值
- 4.7 lua_checkstack 检查栈可用空间是否够size个,不够的话且没超过范围,则扩容。返回值若返回非0则表示扩容成功
- 4.8 lua_xmove 将协程from的n个值从栈拿出,以原顺序压入协程to的栈中
- 5 C访问栈
- 5.1 lua_isnumber 判断指定索引的值是否是数字
- 5.2 lua_isstring判断指定索引的值是否是字符串(字符串和数字都算字符串)
- 5.3 lua_iscfunction 判断指定索引的值是否C函数
- 5.4 lua_isuserdata 判断指定索引的值是否是userdata类型
- 5.5 lua_type 获取指定索引值的类型
- 5.6 lua_typename 获取指定类型的类型名
- 5.7 lua_equal 判断2个索引对应的值是否相等
- 5.8 lua_rawequal判断2个索引对应的值是否相等(不通过元表)
- 5.9 lua_lessthan判断索引1的值是否比索引2的值小
- 5.10 lua_tonumber 以数字的方式解析指定索引的值(原值不变)
- 5.11 lua_tointeger 以整数的方式解析指定索引的值(原值不变)
- 5.12 lua_toboolean 以bool的方式解析指定索引的值(原值不变)
- 5.13 lua_tolstring 以string的形式解析指定索引的值(会导致原值改变)
- 5.14 lua_objlen 获取指定索引的值的长度(如果是数字,还会将原值转为字符串类型)
- 5.15 lua_tocfunction 获取指定索引的值的C函数的地址
- 5.16 lua_touserdata 获取指定索引的userdata内容的首地址
- 5.17 lua_tothread 获取指定索引的thread的地址
- 5.18 lua_topointer 获取指定索引的值的有效内容首地址
- 6 C将值压栈
- 6.1 lua_pushnil 将nil压栈
- 6.2 lua_pushnumber 将数字压栈
- 6.3 lua_pushinteger 将整数压栈
- 6.4 lua_pushlstring 将字符串入栈
- 6.5 lua_pushstring 将字符串入栈,若为NULL,则将nil压栈
- 6.6 lua_pushvfstring 将格式化字符串压栈
- 6.7 lua_pushfstring 将格式化字符串压栈
- 6.8 lua_pushcclosure 将C闭包压栈(从L->top-n到L->top-1的值作为这个C闭包的upvalue,n个upvalue出栈)
- 6.9 lua_pushboolean 将bool压栈
- 6.10 lua_pushlightuserdata 将lightuserdata压栈
- 6.11 lua_pushthread 将当前协程压栈(若当前协程是主协程,则返回true)
- 7 通过栈访问Lua
- 7.1 lua_gettable 以L->top-1处为key,访问idx处的表,将value放在L->top-1处
- 7.2 lua_getfield 以k为字符串key,访问idx处的表,将value放在L->top处,L->top++
- 7.3 lua_rawget 以L->top-1处为key,访问idx处的表,将value放在L->top-1处(不经过元表)
- 7.4 lua_rawgeti 以n为数字key,访问idx处的表,将value放在L->top处,L->top++(不经过元表)
- 7.5 lua_createtable 创建一个数组长度为narray,哈希长度为nrec的表,压栈
- 7.6 lua_newuserdata 创建一个大小为size的userdata,压栈,返回其内容的首地址
- 7.7 lua_getmetatable 获取指定索引的值的元表,若成功取得,压栈 (若成功取得,则返回true)
- 7.8 lua_getfenv 获取指定索引值的环境表,若成功取得,压栈;否则将nil压栈
- 8 通过栈操作Lua
- 8.1 lua_settable 以L->top-2处为key,以L->top-1处为value,操作idx处的表,L->top-=2
- 8.2 lua_setfield 以k为字符串key,以L->top-1处为value,操作idx处的表,L->top--
- 8.3 lua_rawset 以L->top-2处为key,以L->top-1处为value,操作idx处的表(不涉及元表),L->top-=2
- 8.4 lua_rawseti 以n为数字键,L->top -1处为value,操作idx处的表(不涉及元表),L->top--
- 8.5 lua_setmetatable 将L->top-1处的值,设置为objindex处的值的元表,L->top--
- 8.6 lua_setfenv 将L->top-1处的值,设置为objindex处的值的环境表,L->top--
1 背景
- lua使用的是基于“寄存器”的虚拟机实现方式,操作数放在指定索引的“寄存器”中,通过指令来访问这些“寄存器”实现操作。
所谓“寄存器”是通过是通过栈结构实现的,而栈可以通过数字索引来操作指定的位置,因此非常符合寄存器的操作方式。 - lua使用这种虚拟机的实现方式,最大的原因就是尽可能高效。
- lua虚拟机运作,就伴随着数据的出栈和入栈。
2 栈(寄存器数组),虚拟机,全局状态机
2.1 栈定义在lua_State结构体中
每个lua虚拟机实例对应一个lua_State结构体,栈就定义在其中。
(lstate.h) lua_State
struct lua_State
{CommonHeader;//#define CommonHeader GCObject *next; lu_byte tt; lu_byte markedlu_byte status;//虚拟机的错误状态码StkId top;//栈顶元素所在位置的下一个位置,也就是栈上第一个空闲的位置StkId base;//栈上,当前函数的基址(注意不是函数所在位置)global_State* l_G;//全局表,环境章节再说CallInfo* ci;//当前函数调用信息const Instruction* savedpc;//当前函数的指令位置,指向待取指指令的地址StkId stack_last;//栈最后一个位置的下一个位置StkId stack;//寄存器数组的起始位置CallInfo* end_ci;//函数调用信息数组的最后一个位置的下一个位置CallInfo* base_ci;//函数调用信息数组首地址int stacksize;//栈的大小int size_ci;//函数调用信息数组大小unsigned short nCcalls;//内嵌C调用层数unsigned short baseCcalls;//唤醒协程时的内嵌C调用层数lu_byte hookmask;lu_byte allowhook;int basehookcount;int hookcount;lua_Hook hook;TValue l_gt;//Global表TValue env;//环境表的临时位置GCObject* openupval;//open状态的upvaluesGCObject* gclist;struct lua_longjmp* errorJmp;//跳转信息单链表,实现try catch的功能,见函数章节ptrdiff_t errfunc;//当前错误处理函数在栈上的索引
};
lua_State使用TValue数组来模拟栈,包括几个重要的数据成员
- stack 寄存器数组的起始位置
- base 当前函数栈的基地址(一般是第一个参数的地址)(每个函数各自的栈并不是新起的一个栈,而是对应于lua_State栈上的某段空间)
- top 当前函数栈的下一个可用位置
- stack_last 数组的最后一个可用位置
- stacksize 栈数组的大小
2.2 global_State 全局状态机
(lstate.h) global_State
typedef struct global_State
{stringtable strt;//全局字符串表lua_Alloc freealloc;//内存重分配函数void* ud;//freealloc的辅助数据lu_byte currentwhite;//当前白色,见GC章节lu_byte gcstate;//GC状态,见GC章节int sweepstrgc;//strt中GC扫描到的位置GCObject* rootgc;//所有可回收对象的链表GCObject** sweepgc;//rootgc中扫描到的位置GCObject* gray;//灰色对象链表GCObject* grayagain;//需要被原子性遍历地对象链表GCObject* week;//弱表的链表GCObject* tmudata;//需要被GC的userdata的链表的最后一个元素Mbuffer* buff;//字符串连接操作用的临时缓冲对象lu_mem GCthreshold;//触发GC的边界值,见GC章节lu_mem totalbytes;//当前分配的总字节数lu_mem estimate;//实际上使用的总字节数的估计值lu_mem gcdept;//在预定的回收字节数中,还欠多少字节没有回收int gcpause;//连续的GC中的停顿步长相关值,见GC章节int gcstepmul;//GC的粒度lua_CFunction panic;//对于未捕获异常,会调用这个函数TValue l_registry;//注册表struct lua_State* mainthread;//主虚拟机(主协程)UpVal uvhead;//open状态的upvalue双链表的头部struct Table* mt[NUM_TAGS];//存放 基本类型的元表 的数组TString* tmname[TM_N];//存放 元方法名字符串对象地址 的数组
}
2.3 lua_newstate 主虚拟机和全局状态机的创建
(lstate.c) lua_newstate 虚拟机创建
//typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); 内存分配函数类型
LUA_API lua_State* lua_newstate(lua_Alloc f, void* ud)
{//#define LUAI_EXTRASPACE 0//#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)void* l = (*f)(ud, NULL, 0, state_size(LG));//分配主虚拟机和全局状态机的内存if (l == NULL){return NULL;}//#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))lua_State* L = tostate(l);global_State* g = &((LG*)L)->g;L->next = NULL;L->tt = LUA_TTHREAD;//#define bitmask(b) (1<<(b))//#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2))g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);//#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT)//#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)L->marked = luaC_white(g);//#define setbits(x,m) ((x) |= (m))//#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2)))set2bits(L->marked, FIXDEBIT, SFIXEDBIT);preinit_state(L, g);g->frealloc = f;g->ud = ud;g->mainthread = L;g->uvhead.u.l.prev = &g->uvhead;g->uvhead.u.l.next = &g->uvhead;g->GCthreshold = 0;g->strt.size = 0;g->strt.nuse = 0;g->strt.hash = NULL;setnilvalue(registry(L));luaZ_initbuffer(L, &g->buff);//#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)g->panic = NULL;g->gcstate = GCSpasuse;g->rootgc = obj2gco(L);g->sweepstrgc = 0;g->sweepgc = &g->rootgc;g->gray = NULL;g->grayagain = NULL;g->weak = NULL;g->tmudata = NULL;g->totalbytes = sizeof(LG);g->gcpause = LUAI_GCPAUSE;//#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */g->gcstepmul = LUAI_GCMUL;//#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */g->gcdebt = 0;for (int i = 0; i < NUM_TAGS; i++){g->mt[i] = NULL;}if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0)//luaD_rawrunprotected见函数章节{close_state(L);L = NULL;}return L;
}
(lstate.c) LG 主虚拟机和全局状态机的组合结构
typedef struct LG {lua_State l;global_State g;
} LG;
(lstate.c) preinit_state 初始化虚拟机/协程
static void preinit_state(lua_State* L, global_State* g)
{G(L) = g;L->stack = NULL;L->stacksize = 0;L->errorJmp = NULL;L->hook = NULL;L->hookmask = 0;L->basehookcount = 0;L->allowhook = 1;resethookcount(L);//#define resethookcount(L) (L->hookcount = L->basehookcount)L->openupval = NULL;L->size_ci = 0;L->cCcalls = L->baseCcalls = 0;L->status = 0;L->base_ci = L->ci = NULL;L->savedpc = NULL;L->errfunc = 0;setnilvalue(gt(L));
}
(lstate.c) f_luaopen 开启虚拟机
static void f_luaopen(lua_State* L, void* ud)
{global_State* g = G(L);//栈初始化stack_init(L, L);//新建Global表sethvalue(L, gt(L), luaH_new(L, 0, 2));//新建注册表sethvalue(L, registry(L), luaH_new(L, 0, 2));//初始化全局字符串表luaS_resize(L, MINSTRTABSIZE);//#define MINSTRTABSIZE 32//元方法名初始化luaT_init(L);//初始化保留字luaX_init(L);//#define l_setbit(x,b) setbits(x, bitmask(b))//#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT)//#define MEMERRMSG "not enough memory"luaS_fix(luaS_newliteral(L, MEMERRMSG));g->GCthreashold = 4 * g->totalbytes;
}
(lstate.c) stack_init 栈初始化
static void stack_init(lua_State* L1, lua_State* L2)
{//初始化 CallInfo 数组L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);//#define BASIC_CI_SIZE 8L1->ci = L1->base_ci;L1->size_ci = BASIC_CI_SIZE;L1->end_ci = L1->base_ci + L1->size_ci - 1;//初始化 “寄存器” 数组L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;L1->top = L1->stack;L1->stack_last = L1->stack + (L1->stacksize - EXTRA_STACK) - 1;//初始化第一个CallInfoL1->ci->func = L1->top;setnilvalue(L1->top++);L1->base = L1->ci->base = L1->top;L1->ci->top = L1->top + LUA_MINSTACK;//#define LUA_MINSTACK 20
}
(ltm.c) luaT_init 元方法名初始化
void luaT_init(lua_State* L)
{static const char* const luaT_eventname[] = {"__index", "__newindex", "__gc", "__mode", "__eq", "__add", "__sub", "__mul", "__div", "__mod","__pow", "__unm", "__len", "__lt", "__le", "__concat", "__call"};for (int i = 0; i < TM_N; i++){G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);luaS_fix(G(L)->tmname[i]);}
}
(ltm.h) TM_N 元方法名的数量
typedef enum {TM_INDEX,TM_NEWINDEX,TM_GC,TM_MODE,TM_EQ, /* last tag method with `fast' access */TM_ADD,TM_SUB,TM_MUL,TM_DIV,TM_MOD,TM_POW,TM_UNM,TM_LEN,TM_LT,TM_LE,TM_CONCAT,TM_CALL,TM_N /* number of elements in the enum */
} TMS;
(llex.c) luaX_init 初始化保留字
void luaX_init(lua_State* L)
{for (int i = 0; i < NUM_RESERVED; i++)//#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)){//尝试新建每个保留字字符串TString* ts = luaS_new(L, luaX_tokens[i]);//标记不会被GC,修改ts->tsv.marked为FIXEDBITluaS_fix(ts);//记录在保留字数组的索引+1值ts->tsv.reserved = cast_byte(i + 1);}
}
(llex.h) 保留字枚举
enum RESERVED {/* terminal symbols denoted by reserved words */TK_AND = FIRST_RESERVED, TK_BREAK,TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,/* other terminal symbols */TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,TK_NAME, TK_STRING, TK_EOS
};
(llex.c) luaX_tokens 保留字的数组
const char *const luaX_tokens [] = {"and", "break", "do", "else", "elseif","end", "false", "for", "function", "if","in", "local", "nil", "not", "or", "repeat","return", "then", "true", "until", "while","..", "...", "==", ">=", "<=", "~=","<number>", "<name>", "<string>", "<eof>",NULL
};
2.4 lua_close 关闭虚拟机
(lstate.c) lua_close
LUA_API void lua_close(lua_State* L)
{L = G(L)->mainthread;//只有主虚拟机才可以被关闭//关闭所有的upvalueluaF_close(L, L->stack);//luaF_close见函数章节//分离那些有GC元方法的userdata,见GC章节luaC_separateudata(L, 1);L->errfunc = 0;do {L->ci = L->base_ci;L->base = L->top = L->ci->base;L->nCcalls = L->baseCcalls = 0;} while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);//见callallgcTM章节close_state(L);
}
(lstate.c) close_state 关闭虚拟机的收尾工作
static void close_state(lua_State* L)
{global_State* g = G(L);luaF_close(L, L->stack);//回收所有的对象luaC_freeall(L);//luaC_freeall见GC章节luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString*);luaZ_freebuffer(L, &g->buff);freestack(L, L);//#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)(*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
}
(lstate.c) freestack 释放协程L1上“寄存器”数组和CallInfo数组
static void freestack(lua_State* L, lua_State* L1)
{luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);luaM_freearray(L, L1->stack, L1->stacksize, TValue);
}
3 栈上的地址
虽然可根据数字索引定位栈中地地址,但是官方也定义了几个不在栈中的假索引
3.1 假索引
(lua.h)
#define LUA_REGISTRYINDEX (-10000) //注册表
#define LUA_ENVIRONINDEX (-10001) //环境表
#define LUA_GLOBALSINDEX (-10002) //全局变量表
#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) //C函数upvalue表
3.2 根据数字索引获取栈上的地址
(lapi.c) index2adr
static TValue* index2adr(lua_State* L, int idx)
{//若 idx > 0,则是以L->base为基准,要找的地址时 L->base + idx - 1if (idx > 0){TValue* o = L->base + idx - 1;//因为base就是相对的1,所以要减1啦//越上界了,当然返回nil啦if (o >= L->top){return cast(TValue*, luaO_nilobject);}//没越界,就返回找到的对象地址return o;}//若 LUA_REGISTRYINDEX < idx <= 0,则是以 L->top 为基准,要找的地址时 L->top + idxif (idx > LUA_REGISTRYINDEX){//以栈顶为相对位置返回对象地址return L->top + idx;}//不满足以上取值范围的idx都是假索引,因为不是从栈里取地址了,而是特殊处理,如下文...//若 假索引 为 注册表索引,就返回注册表地址if (idx == LUAREGISTRYINDEX){return registry(L);//#define registry(L) (&G(L)->l_registry)}//若 假索引 为 环境表索引,则返回当前函数的env表if (idx == LUA_ENVIRONINDEX){//获取当前函数环境的闭包地址Closure* func = curr_func(L);//#define curr_func(L) (clvalue(L->ci->func))//设置当前函数环境为当前lua虚拟机的环境sethvalue(L, &L->env, func->c.env);return &L->env;}//若 假索引 为 全局表索引,则返回当前lua_State的全局表if (idx == LUA_GLOBALSINDEX){return gt(L);//#define gt(L) (&L->l_gt)}//除以上的所有情况(也就是从-10003到负无穷),就是访问C函数的upvalue了 Closure* func = curr_func(L);//计算upvalue的索引idx = lua_upvalueindex(idx);//返回upvalue地址或者nilreturn (idx <= func->c.nupvalues) ? &func->c.upvalue[idx - 1] : cast(TValue*, luaO_nilobject);
}
4 基本的栈操作API
4.1 lua_gettop 获取 栈顶 相对于 当前函数栈基址 的偏移
(lapi.c) lua_gettop
LUA_API int lua_gettop(lua_State* L)
{return cast_int(L->top - L->base);
}
4.2 lua_settop 设置栈顶的位置
(lapi.c) lua_settop
LUA_API int lua_settop(lua_State* L, int idx)
{if (idx >= 0){while (L->top < L->base + idx){setnilvalue(L->top++);}L->top = L->base + idx;}else{L->top += idx + 1;}
}
4.3 lua_pushvalue 将指定索引的值复制到栈顶
(lapi.c) lua_pushvalue
LUA_API void lua_pushvalue(lua_State* L, int idx)
{setobj2s(L, L->top, index2adr(L, idx));api_incr_top(L);//#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
}
4.4 lua_remove 将指定索引的值入栈,且上面的值依次下移一位
(lapi.c) lua_remove
LUA_API void lua_remove(lua_State* L, int idx)
{StkId p = index2adr(L, idx);while (++p < L->top){setobjs2s(L, p - 1, p);}L->top--;
}
4.5 lua_insert 将栈顶的值插入到指定索引,且上面的值依次上移一位
(lapi.c) lua_insert
LUA_API void lua_insert(lua_State* L, int idx)
{StkId p = index2adr(L, idx);for (StkId q = L->top; q > p; q--){setobjs2s(L, q, q - 1);}setobjs2s(L, p, L->top);
}
4.6 lua_replace 将栈顶的值替代掉指定索引的值
(lapi.c) lua_replace
LUA_API void lua_replace(lua_State* L, int idx)
{//若想用栈顶的值替换 base_ci的环境表,则报错if (idx = LUA_ENVIRONINDEX && L->ci == L->base_ci){luaG_runerror(L, "no calling environment");}StkId o = index2adr(L, idx);if (idx == LUA_ENVIRONINDEX){Closure* func = curr_func(L);//curr_func见环境章节func->c.env = hvalue(L->top - 1);luaC_barrier(L, func, L->top - 1);//luaC_barrier见GC章节}else{setobj(L, o, L->top - 1);if (idx < LUA_GLOBALSINDEX){luaC_barrier(L, curr_func(L), L->top - 1);}}L->top--;
}
4.7 lua_checkstack 检查栈可用空间是否够size个,不够的话且没超过范围,则扩容。返回值若返回非0则表示扩容成功
(lapi.c) lua_checkstack
LUA_API int lua_checkstack(lua_State* L, int size)
{int res = 1;//#define LUAI_MAXCSTACK 8000if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK){res = 0;}else if (size > 0){//luaD_checkstack 若栈空间不足size个,则扩容/*#define luaD_checkstack(L,n) \if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \luaD_growstack(L, n); \else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));*/luaD_checkstack(L, size);if (L->ci->top < L->top + size){L->ci->top = L->top + size;}}return res;
}
4.8 lua_xmove 将协程from的n个值从栈拿出,以原顺序压入协程to的栈中
(lapi.c) lua_xmove
LUA_API void lua_xmove(lua_State* from, lua_State* to, int n)
{if (from == to) {return;}from->top -= n;for (int i = 0; i < n; i++){setobj2s(to, to->top++, from->top + i);}
}
5 C访问栈
5.1 lua_isnumber 判断指定索引的值是否是数字
(lapi.c) lua_isnumber
LUA_API int lua_isnumber(lua_State* L, int idx)
{TValue n;const TValue* o = index2adr(L, idx);return tonumber(o, &n);
}
(lvm.h) tonumber
#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || (((o) = luaV_tonumber(o,n)) != NULL))
(lvm.c) luaV_tonumber 尝试将obj转为数字,在地址n输出。(若obj就是数字类型=>返回obj地址;若obj是字符串类型且执行转化为数字成功,则返回n地址;否则返回NULL)
const TValue* luaV_tonumber(const TValue* obj, TValue* n)
{lua_Number num;if (ttisnumber(obj))//#define ttisnumber(o) (ttype(o) == LUA_TNUMBER){return obj;}//#define ttisstring(o) (ttype(o) == LUA_TSTRING)//#define svalue(o) getstr(rawtsvalue(o))//#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)//#define getstr(ts) cast(const char *, (ts) + 1)if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)){setnvalue(n, num);return n;}return NULL;
}
(lobject.c) luaO_str2d 尝试将字符串转为数字,以result为输出地址(返回值1表示成功,0表示失败)
int luaO_str2d(const char* s, lua_Number* result)
{char* endptr;*result = lua_str2number(s, &endptr);//#define lua_str2number(s,p) strtod((s), (p))if (endptr == s)//转换失败{return 0;}if (*endptr == 'x' || *endptr == 'X')//十六进制数{*result = cast_num(strtoul(s, &endptr, 16));}if (*endptr == '\0'){return 1;}while (isspace(cast(unsigned char, *endptr)))//跳过空白字符{endptr++;}if (*endptr != '\0'){return 0;}return 1;
}
5.2 lua_isstring判断指定索引的值是否是字符串(字符串和数字都算字符串)
(lapi.c) lua_isstring
LUA_API int lua_isstring(lua_State* L, int idx)
{int t = lua_type(L, idx);return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
5.3 lua_iscfunction 判断指定索引的值是否C函数
(lapi.c) lua_iscfunction
LUA_API int luac_iscfunction(lua_State* L, int idx)
{StkId o = index2adr(L, idx);return iscfunction(o);//#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
}
5.4 lua_isuserdata 判断指定索引的值是否是userdata类型
(lapi.c) lua_isuserdata
LUA_API int lua_isuserdata(lua_State* L, int idx)
{const TValue* o = index2adr(L, idx);//#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)//#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)return (ttisuserdata(o) || ttislightuserdata(o));
}
5.5 lua_type 获取指定索引值的类型
(lapi.c) lua_type
LUA_API int lua_type(lua_State* L, int idx)
{StkId o = index2adr(L, idx);return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);//#define ttype(o) ((o)->tt)
}
5.6 lua_typename 获取指定类型的类型名
(lapi.c) lua_typename
LUA_API const char* lua_typename(lua_State* L, int t)
{return (t == LUA_TNONE) ? "no value" : luaT_tynames[t];
}
(ltm.c) luaT_typenames
const char *const luaT_typenames[] = {"nil", "boolean", "userdata", "number","string", "table", "function", "userdata", "thread","proto", "upval"
};
5.7 lua_equal 判断2个索引对应的值是否相等
(lapi.c) lua_equal
LUA_API int lua_equal(lua_State* L, int index1, int index2)
{StkId o1 = index2adr(L, index1);StkId o2 = index2adr(L, index2);//只要有一个为luaO_nilobject 则必定不相等//#define equalobj(L,o1,o2) (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))int i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);return i;
}
(lvm.c) luaV_equalval 判断俩类型相同的值是否相等(注意:大前提是类型相同)
int luaV_equalval(lua_State* L, const TValue* t1, const TValue* t2)
{const TValue* tm;switch(ttype(t1)){case LUA_TNIL:{return 1;}case LUA_TNUMBER:{//#define luai_numeq(a,b) ((a)==(b))//#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)return luai_numeq(nvalue(t1), nvalue(t2));}case LUA_TBOOLEAN:{//#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)return bvalue(t1) == bvalue(t2);}case LUA_TLIGHTUSERDATA:{//#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)return pvalue(t1) == pvalue(t2);}case LUA_TUSERDATA:{//#define uvalue(o) (&rawuvalue(o)->uv)if (uvalue(t1) == uvalue(t2)){return 1;}tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatble, TM_EQ);break;}case LUA_TATBLE:{//#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)if (hvalue(t1) == hvalue(t2)) {return 1;}tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatble, TM_EQ);break;}default:{return gcvalue(t1) == gcvalue(t2);}}if (tm == NULL){return 0;}callTMres(L, L->top, tm, t1, t2);//#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))return !l_isfalse(L->top);
}
(lvm.c) get_compTM
static const TValue* get_compTM(lua_State* L, Table* mt1, Table* mt2, TMS event)
{const TValue* tm1 = fasttm(L, mt1, event);if (tm1 == NULL){return NULL;}if (mt1 == mt2){return tm1;//相同的元表,当然是相同的元方法啦}const TValue* tm2 = fasttm(L, mt2, event);if (tm2 == NULL){return NULL;}if (luaO_rawequalObj(tm1, tm2)){return tm1;}return NULL;
}
(ltm.h) fasttm (含义:快速获取 元表et 关于 元方法标识e 的元方法)
快速在于:
- 先判断 元表et 的flags 关于e的标志位 是否为真,若真则表示无该元方法,无需后面的查表过程;
- 否则,搜索global_State里的tmname数组获取元方法名,再调用luaT_gettm获取元方法
#define gfasttm(g,et,e) ((et) == NULL ? NULL : \((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
#define fasttm(l,et,e) gfasttm(G(l), et, e)
(ltm.c) luaT_gettm (含义:根据 元方法名ename 获取 元表events 的元方法,若获取不到则根据元方法标识event设置 元方法events的flags)
const TValue* luaT_gettm(Table* events, TMS event, TString* ename)
{const TValue* tm = luaH_getstr(events, ename);//若没找到元方法,则标记这个元表的flags,以便下次访问的时候节省查找耗时if (ttisnil(tm)){events->flags |= cast_byte(1u << event);return NULL;}return tm;
}
(lobject.c) luaO_rawequalObj 直接判断2对象是否相等
int luaO_rawequalObj(const TValue* t1, const TValue* t2)
{//若类型都不相同,则就是不相同if (ttype(t1) != ttype(t2)){return 0;}switch (ttype(t1)){//nil类型只有一个值,必定相同case LUA_TNIL:{return 1;}//数字地话,判断t->value.n是否相同case LUA_TNUMBER:{return luai_numeq(nvalue(t1), nvalue(t2));}//若为bool类型,判断t->value.b是否相同case LUA_TBOOLEAN:{return bvalue(t1)==bvalue(t2);}//若为lightuserdata类型,判断t->value.p是否相等case LUA_TLIGHTUSERDATA:{return pvalue(t1)==pvalue(t2);}//否则判断t->value.gc是否指向同一个GCObjectdefault:{return gcvalue(t1)==gcvalue(t2);}}
}
(lvm.c) callTMres (含义: res=f(p1, p2))
//@param res 返回值位置
//@param f 元方法
//@param p1 参数1
//@param p2 参数2
static void callTMres(lua_State* L, StkId res, const TValue* f, const TValue* p1, const TValue* p2)
{//记录返回值地址 和 栈基址 的差值ptrdiff_t result = savestack(L, res);//#define savestack(L,p) ((char *)(p) - (char *)L->stack)//元方法入栈setobj2s(L, L->top, f);//参数1入栈setobj2s(L, L->top+1, p1);//参数2入栈setobj2s(L, L->top+2, p2);//检查栈够不够三个元素,不够的话就扩容luaD_checkstack(L, 3);//入栈了3个元素,当然栈顶地址+3L->top += 3;//执行函数,函数到栈顶中的值都是参数,返回值是1个luaD_call(L, L->top - 3, 1);//luaD_call,见函数章节//根据偏移量获取返回值在栈上的地址(因为前面的luaD_checkstack可能会导致lua栈被重新分配地址)res = restorestack(L, result);//#define restorestack(L,n) ((TValue *)((char *)L->stack + (n)))//函数返回值出栈,将栈顶的值复制到返回值位置L->top--;setobj2s(L, res, L->top);
}
5.8 lua_rawequal判断2个索引对应的值是否相等(不通过元表)
(lapi.c) lua-rawequal
LUA_API int lua_rawequal(lua_State* L, int index1, int index2)
{StkId o1 = index2adr(L, index1);StkId o2 = index2adr(L, index2);return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : luaO_rawequalObj(o1, o2);
}
5.9 lua_lessthan判断索引1的值是否比索引2的值小
(lapi.c) lua_lessthan
LUA_API int lua_lessthan(lua_State* L, int index1, int index2)
{StkId o1 = index2adr(L, index1);StkId o2 = index2adr(L, index2);return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : luaV_lessthan(o1, o2);
}
(lvm.c) luaV_lessthan
int luaV_lessthan(lua_State* L, const TValue* l, const TValue* r)
{int res;if (ttype(l) != ttype(r))//类型不相等,则报错{return luaG_ordererror(L, l, r);//luaG_ordererror,见异常章节}else if (ttisnumber(l))//左边是数字,则双方都取数字比较{//#define luai_numlt(a,b) ((a)<(b))return luai_numlt(nvalue(l), nvalue(r));}else if (ttistring(l)){//#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;}else if ((res = call_orderTM(L, l, r, TM_LT)) != -1){return res;}return luaG_ordererror(L, l, r);
}
(lvm.c) l_strcmp 比较字符串
static int l_strcmp(const TString* ls, const TString* rs)
{const char* l = getstr(ls);//#define getstr(ts) cast(const char *, (ts) + 1)size_t ll = ls->tsv.len;//l串的待比较长度const char* r = getstr(rs);size_t lr = rs->tsv.len;//r串的待比较长度for (;;){int temp = strcoll(l, r);//l>r则>0,l==r则=0,l<r则<0if (temp != 0){return temp;}else{size_t len = strlen(l);if (len == lr)//r串结束了{return (len == ll) ? 0 : 1;}else if (len == ll)//r串未结束,l串结束了{return -1;}len++;l += len;ll -= len;r += len;lr -= len;}}
}
(lvm.c) call_orderTM 执行元方法(前提:从p1获取的元方法要和从p2获取的元方法要相等)
static int call_orderTM(lua_State* L, const TValue* p1, const TValue* p2, TMS event)
{const TValue* tm1 = luaT_gettmbyobj(L, p1, event);if (ttisnil(tm1)){return -1;}const TValue* tm2 = luaT_gettmbyobj(L, p2, event);if (!luaO_rawequalObj(tm1, tm2)){return -1;}callTMres(L, L->top, tm1, p1, p2);return !is_false(L->top);
}
(ltm.c) luaT_gettmbyobj (根据 元方法标识event 获取 对象o 的元方法)
const TValue* luaT_gettmbyobj(lua_State* L, const TValue* o, TMS event)
{Table* mt;switch(ttype(o)){case LUA_TTABLE:{mt = hvalue(o)->metatable;break;}case LUA_TUSERDATA:{mt = uvalue(o)->metatble;break;}default:{mt = G(L)->mt[ttype(o)];//非table非userdata类型的元表统统在global_State的mt数组内}}//为何不用 fasttm(L, mt, event) 呢?return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
}
5.10 lua_tonumber 以数字的方式解析指定索引的值(原值不变)
(lapi.c) lua_tonumber
LUA_API lua_Number lua_tonumber(lua_State* L, int idx)
{TValue n;const TValue* o = index2adr(L, idx);if (tonumber(o, &n)){return nvalue(o);}return 0;
}
5.11 lua_tointeger 以整数的方式解析指定索引的值(原值不变)
(lapi.c) lua_tointeger
LUA_API lua_Integer lua_tointeger(lua_State* L, int idx)
{TValue n;const TValue* o = index2adr(L, idx);if (tonumber(o, &n)){lua_Integer res;lua_Number num = nvalue(o);lua_number2integer(res, num);return res;}return 0;
}
5.12 lua_toboolean 以bool的方式解析指定索引的值(原值不变)
(lapi.c) lua_toboolean
LUA_API int lua_toboolean(lua_State* L, int idx)
{const TValue* o = index2adr(L, idx);return !l_isfalse(o);
}
5.13 lua_tolstring 以string的形式解析指定索引的值(会导致原值改变)
(lapi.c) lua_tolstring
LUA_API const char* lua_tolstring(lua_State* L, int idx, size_t* len)
{StkId o = index2adr(L, idx);if (!ttisstring(o))//#define ttisstring(o) (ttype(o) == LUA_TSTRING){if (!luaV_tostring(L, o)){if (len != NULL){*len =0;}return NULL;}luaC_checkGC(L);o = index2adr(L, idx);//前面的操作可能回导致栈的重新分配,所以需要重新获取一下}if (len != NULL){*len = tsvalue(o)->len;//#define tsvalue(o) (&rawtsvalue(o)->tsv)}//#define getstr(ts) cast(const char *, (ts) + 1)//#define svalue(o) getstr(rawtsvalue(o))return svalue(o);
}
(lvm.c) luaV_tostring 将非字符串类型能否转为字符串类型,转成功则返回true
int luaV_tostring(lua_State* L, StkId obj)
{//不是数字,则不能if (!ttisnumber(obj)){return 0;}char s[LUAI_MAXNUMBER2STR];//#define LUAI_MAXNUMBER2STR 32lua_Number n = nvalue(obj);//#define LUA_NUMBER_FMT "%.14g" //系统自动选择占宽度较小的某种格式输出,g格式符不输出小数点后无意义的零//#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))lua_number2str(s, n);//把字符串赋值给obj位置setsvalue2s(L, obj, luaS_new(L, s));return 1;
}
5.14 lua_objlen 获取指定索引的值的长度(如果是数字,还会将原值转为字符串类型)
(lapi.c) lua_objlen
LUA_API size_t lua_objlen(lua_State* L, int idx)
{StkId o = index2adr(L, idx);switch (ttype(o)){case LUA_TSTRING:{return tsvalue(o)->len;}case LUA_TUSERDATA:{return uvalue(o)->len;}case LUA_TTABLE:{return luaH_getn(hvalue(o));}case LUA_TNUMBER:{return luaV_tostring(L, o) ? tsvalue(o)->len : 0;}default:{return 0;}}
}
5.15 lua_tocfunction 获取指定索引的值的C函数的地址
(lapi.c) lua_tocfunction
//typedef int (*lua_CFunction) (lua_State *L);
LUA_API lua_CFunction lua_tocfunction(lua_State* L, int idx)
{StkId o = index2adr(L, idx);//#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)//#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)return (!iscfunction(o)) ? NULL : cvalue(o)->c.f;
}
5.16 lua_touserdata 获取指定索引的userdata内容的首地址
(lapi.c) lua_touserdata
LUA_API void* lua_touserdata(lua_State* L, int idx)
{StkId o = index2adr(L, idx);switch(ttype(o)){case LUA_TUSERDATA:{//#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)return (rawvalue(o) + 1);//Udata只是一个userdata的头部信息,真正的内容紧随头部之后}case LUA_TLIGHTUSERDATA:{//#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)return pvalue(o);}default:{return NULL;}}
}
5.17 lua_tothread 获取指定索引的thread的地址
(lapi.c) lua_tothread
LUA_API lua_State* lua_tothread(lua_State* L, int idx)
{StkId o = index2adr(L, idx);//#define ttisthread(o) (ttype(o) == LUA_TTHREAD)//#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)return (!ttisthread(o) ? NULL : thvalue(o));
}
5.18 lua_topointer 获取指定索引的值的有效内容首地址
(lapi.c) lua_topointer
LUA_API const void* lua_topointer(lua_State* L, int idx)
{StkId o = index2adr(L, idx);switch (ttype(o)){case LUA_TTABLE:{return hvalue(o);}case LUA_TFUNCTION:{return clvalue(o);}case LUA_TTHREAD:{return thvalue(o);}case LUA_TUSERDATA:case LUA_TLIGHTUSERDATA:{return lua_touserdata(L, idx);}default:{return NULL;}}
}
6 C将值压栈
6.1 lua_pushnil 将nil压栈
(lapi.c) lua_pushnil
LUA_API void lua_pushnil(lua_State* L)
{setnilvalue(L->top);//#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)api_incr_top(L);
}
6.2 lua_pushnumber 将数字压栈
(lapi.c) lua_pushnumber
LUA_API void lua_pushnumber(lua_State* L, lua_Number n)
{//#define setnvalue(obj,x) { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }setnvalue(L->top, n);api_incr_top(L);
}
6.3 lua_pushinteger 将整数压栈
(lapi.c) lua_pushinteger
LUA_API void lua_pushinteger(lua_State* L, lua_Integer n)
{setnvalue(L->top, cast_num(n));api_incr_top(L);
}
6.4 lua_pushlstring 将字符串入栈
(lapi.c) lua_pushlstring
LUA_API void lua_pushlstring(lua_State* L, const char* s, size_t len)
{luaC_checkGC(L);setsvalue2s(L, L->top, luaS_newlstr(L, s, len));api_incr_top(L);
}
6.5 lua_pushstring 将字符串入栈,若为NULL,则将nil压栈
(lapi.c) lua_pushstring
LUA_API void lua_pushstring(lua_State* L, const char* s)
{if (s == NULL){lua_pushnil(L);}else{lua_pushlstring(L, s, strlen(s));}
}
6.6 lua_pushvfstring 将格式化字符串压栈
(lapi.c) lua_pushvfstring
LUA_API const char* lua_pushvfstring(lua_State* L, const char* fmt, va_list argp)
{luaC_checkGC(L);return luaO_pushfstring(L, fmt, argp);
}
(lobject.c) luaO_pushvfstring 根据格式串fmt和va_list向lua栈压入格式化的字符串
const char* luaO_pushvfstring(lua_State* L, const char* fmt, va_list argp)
{pushstr(L, "");//记录被压栈的字符串数量int num_pushed_str = 1;for (;;){//查找fmt中第一个%的地址,若无,则退出循环const char* e = strchr(fmt, '%');if (e == NULL){break;}//截取e - fmt长度的字符串压栈setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e - fmt));incr_top(L);//分析百分号的下一个字符switch(*(e + 1)){//若匹配到字符串,字符串压栈case 's':{const char* s = va_arg(argp, char*);if (s == NULL){s = "(null)";}pushstr(L, s);break;}//若匹配到字符,字符后接一个\0字符形成一个新的字符串压栈case 'c':{char buff[2];buff[0] = cast(char, va_arg(argp, int));buff[1] = '\0';pushstr(L, buff);break;}//若匹配到整数,则整数压栈case 'd':{setnvalue(L->top, cast_num(va_arg(argp, int)));incr_top(L);break;}//若匹配到浮点数,则浮点数压栈case 'f':{setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));//l_uacNumber就是doubleincr_top(L);break;}//若匹配到地址值,则将地址值的字符串表示压栈case 'p':{char buff[4*sizoef(void*) + 8];sprintf(buff, "%p", va_arg(argp, void*));pushstr(L, buff);break;}//匹配到%字符case '%':{pushstr(L, "%");break;}//若%匹配失败,则直接将%字符连同下一个字符,再后接一个\0字符连成字符串,压栈default:{char buff[3];buff[0] = '%';buff[1] = *(e + 1);buff[2] = '\0';pushstr(L, buff);break;}}num_pushed_str += 2;fmt = e + 2;}pushstr(L, fmt);num_pushed_str++;//将压入的字符串连接起来luaV_concat(L, num_pushed_str, cast_int(L->top - L->base) - 1);//虽然压入num_pushed_str个字符串,但是最后要比函数执行前要多1字符串,所以top只是减少num_pushed_str - 1L->top -= (num_pushed_str - 1);return svalue(L->top - 1);
}
(lobject.c) pushstr 向栈顶压入1个字符串
static void pushstr(lua_State* L, const char* str)
{setsvalue2s(L, L->top, luaS_new(L, str));//将栈顶位置设为新创建或者从全字符串表查到的字符串incr_top(L);//#define incr_top(L) {luaD_checkstack(L,1); L->top++;}
}
(lvm.c) luaV_concat 连接栈上的字符串
//@param total 需要处理的字符串总数
//@param last 相对于base的偏移
void luaV_concat(lua_State* L, int total, int last)
{do{//计算栈顶位置StkId top = L->base + last + 1;int n = 2;//若top-2不是字符串或数字,或者 top-1不是字符串,则尝试调用TM_CONCAT元方法//#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))if (!(ttisstring(top - 2) || ttisnumber(top - 2)) || !tostring(L, top - 1)){//如果没有 TM_CONCAT 元方法,则报错if (!call_binTM(L, top - 2, top - 1, top - 2, TM_CONCAT)){luaG_concaterror(L, top - 2, top - 1);return;}}//若top-1串长度为0,则不需要连接了,top-2直接转为字符串else if (tsvalue(top - 1)->len == 0){(void)tostring(L, top - 2);}else{//每n个串合并成1个大串(n个串拷贝到缓冲中,n个串只对应1个TString对象)//tl为n个串的总长度size_t tl = tsvalue(top - 1)->len;for (n = 1; n < total && tostring(L, top - n - 1); n++){size_t l = tsvalue(top - n - 1)->len;if (l >= MAX_SIZET - tl)//#define MAX_SIZET ((size_t)(~(size_t)0)-2){luaG_runerror(L, "string length overflow");}tl += l;}char* buffer = luaZ_openspace(L, &G(L)->buff, tl);tl = 0;for (int i = n; i > 0; i--){size_t l = tsvalue(top - i)->len;memcpy(buffer + tl, svalue(top - i), l);tl += l;}setsvalue2s(L, top - n, luaS_newlstr(L, buffer, tl));}total -= n - 1;//需要处理的串数量 减少了 n-1个,很好理解,n个字符串合并成了1个(-n+1)last -= n - 1;} while (total > 1);//合并嘛,肯定是>1才有合并的意义
}
(lvm.c) call_binTM 调用元方法(不要求双方的元方法对等)
static int call_binTM(lua_State* L, const TValue* p1, const TValue* p2, StkId res, TMS event)
{//p1和p2找到一个元方法就行,否则失败,返回0const TValue* tm = luaT_gettmbyobj(L, p1, event);if (ttisnil(tm)){tm = luaT_gettmbyobj(L, p2, event);}if (ttisnil(tm)){return 0;}//执行元方法callTMres(L, res, tm, p1, p2);return 1;
}
(lzio.c) luaZ_openspace 尝试在buff->buffer地址处开辟长度为n字节的缓冲区
char* luaZ_openspace(lua_State* L, Mbuffer* buff, size_t n)
{//若需要的字节数 > 缓冲区容量,则扩容if (n > buff->buffsize){if (n < LUA_MINBUFFER)//#define LUA_MINBUFFER 32{n = LUA_MINBUFFER;}//#define luaZ_resizebuffer(L, buff, size) (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), (buff)->buffsize = size)luaZ_resizebuffer(L, buff, n);//为buff->buffer重新分配内存,大小为n}return buff->buffer;
}
6.7 lua_pushfstring 将格式化字符串压栈
(lapi.c) lua_pushfstring
LUA_API const char* lua_pushfstring(lua_State* L, const char* fmt, ...)
{luaC_checkGC(L)va_list argp;va_start(argp, fmt);const char* ret = luaO_pushvfstring(L, fmt, argp);va_end(argp);return ret;
}
6.8 lua_pushcclosure 将C闭包压栈(从L->top-n到L->top-1的值作为这个C闭包的upvalue,n个upvalue出栈)
(lapi.c) lua_pushcclosure
LUA_API void lua_pushcclosure(lua_State* L, lua_CFunction fn, int n)
{luaC_checkGC(L);Closure* cl = luaF_newCclosure(L, n, getcurrentv(L));//luaF_newCclosure,getcurrentv见环境章节cl->c.f = fn;L->top -= n;while(n--){setobj2n(L, &cl->c.upvalue[n], L->top + n);}setclvalue(L, L->top, cl);api_incr_top(L);
}
6.9 lua_pushboolean 将bool压栈
(lapi.c) lua_pushboolean
LUA_API void lua_pushboolean(lua_State* L, int b)
{//#define setbvalue(obj,x) { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }setbvalue(L->top, b != 0);api_incr_top(L);
}
6.10 lua_pushlightuserdata 将lightuserdata压栈
(lapi.c) lua_pushlightuserdata
LUA_API void lua_pushlightuserdata(lua_State* L, void* p)
{setpvalue(L->top, p);api_incr_top(L);
}
6.11 lua_pushthread 将当前协程压栈(若当前协程是主协程,则返回true)
(lapi.c) lua_pushthread
LUA_API int lua_pushthread(lua_State* L)
{//#define setthvalue(L,obj,x) { TValue *i_o=(obj); i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD;}setthvalue(L, L->top, L);api_incr_top(L);return (G(L)->mainthread == L);
}
7 通过栈访问Lua
7.1 lua_gettable 以L->top-1处为key,访问idx处的表,将value放在L->top-1处
(lapi.c) lua_gettable
LUA_API void lua_gettable(lua_State* L, int idx)
{StkId t = index2adr(L, idx);luaV_gettable(L, t, L->top - 1, L->top - 1);
}
(lvm.c) luaV_gettable (含义:val = t[key])
void luaV_gettable(lua_State* L, const TValue* t, TValue* key, StkId val)
{//根据key沿着元表一层一层获取valuefor (int loop = 0; loop < MAXTAGLOOP ; loop++)//#define MAXTAGLOOP 100{const TValue* tm;//用来指向t的__index元方法if (ttistable(t)){Table* h = hvalue(t);const TValue* res = luaH_get(h, key);//这里就是所谓的rawget(原始的查表逻辑)////找到的值非空或者有无__index元方法,则直接将找到的值赋给val位置if (!ttisnil(res) || (tm = fasttm(L, h->metatble, TM_INDEX) == NULL)){setobj2s(L, val, res);return;}}else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))){//若t不是表,但也无__index元方法,则报错luaG_typeerror(L, t, "index");//luaG_typeerror见异常章节}//若__index元方法是函数,则执行这个元方法if (ttisfunction(tm)){callTMres(L, val, tm, t, key);return;}t = tm;}//若MAXTAGLOOP层都还没找到,则报错luaG_runerror(L, "loop in gettable");
}
7.2 lua_getfield 以k为字符串key,访问idx处的表,将value放在L->top处,L->top++
(lapi.c) lua_getfield
LUA_API void lua_getfield(lua_State* L, int idx, const char* k)
{StkId t = index2adr(L, idx);TValue key;setsvalue(L, &key, luaS_new(L, k));luaV_gettable(L, t, &key, L->top);api_incr_top(L);
}
7.3 lua_rawget 以L->top-1处为key,访问idx处的表,将value放在L->top-1处(不经过元表)
(lapi.c) lua_rawget
LUA_API void lua_rawget(lua_State* L, int idx)
{StkId t = index2adr(L, idx);setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
}
7.4 lua_rawgeti 以n为数字key,访问idx处的表,将value放在L->top处,L->top++(不经过元表)
(lapi.c) lua_rawgeti
LUA_API void lua_rawgeti(lua_State* L, int idx, int n)
{StkId o = index2adr(L, idx);setobj2s(L, L->top, luaH_getnum(hvalue(o), n));api_incr_top(L);
}
7.5 lua_createtable 创建一个数组长度为narray,哈希长度为nrec的表,压栈
(lapi.c) lua_createtable
LUA_API void lua_createtable(lua_State* L, int narray, int nrec)
{luaC_checkGC(L);sethvalue(L, L->top, luaH_new(L, narray, nrec));api_incr_top(L);
}
7.6 lua_newuserdata 创建一个大小为size的userdata,压栈,返回其内容的首地址
(lapi.c) lua_newuserdata
LUA_API void* lua_newuserdata(lua_State* L, size_t size)
{luaC_checkGC(L);Udata* u = luaS_newudata(L, size, getcurrenv(L));setuvalue(L, L->top, u);api_incr_top(L);return u + 1;
}
(lstring.c) luaS_newudata 新建一个userdata,并加入主协程的Udata链表
Udata* luaS_newudata(lua_State* L, size_t s, Table* e)
{//若太大,则报错if (s > MAX_SIZET - sizeof(Udata))//#define MAX_SIZET ((size_t)(~(size_t)0)-2){luaM_toobig(L);}Udata* u = cast(Udata*, luaM_malloc(L, s + sizeof(Udata)));u->uv.marked = luaC_white(G(L));//luaC_white见GC章节u->uv.tt = LUA_TUSERDATA;u->uv.len = s;u->uv.metatable = NULL;u->uv.env = e;//插入到主协程的next链表里(这个链表存所有的udata)u->uv.next = G(L)->mainthread->next;G(L)->mainthread->next = obj2gco(u);//#define obj2gco(v) (cast(GCObject *, (v)))return u;
}
(lmem.c) luaM_toobig
void* luaM_toobig(lua_State* L)
{luaG_runerror(L, "memory allocation error: block too big");return NULL;
}
7.7 lua_getmetatable 获取指定索引的值的元表,若成功取得,压栈 (若成功取得,则返回true)
(lapi.c) lua_getmetatable
LUA_API int lua_getmetatable(lua_State* L, int objindex)
{Table* mt = NULL;int res;const TValue* obj = index2adr(L, objindex);switch (ttype(obj)){case LUA_TTABLE:{mt = hvalue(obj)->metatable;break;}case LUA_TUSERDATA:{mt = uvalue(obj)->metatable;break;}default:{mt = G(L)->mt[ttype(obj)];break;}}if (mt == NULL){res = 0;}else{sethvalue(L, L->top, mt);api_incr_top(L);res = 1;}return res;
}
7.8 lua_getfenv 获取指定索引值的环境表,若成功取得,压栈;否则将nil压栈
(lapi.c) lua_getfenv
LUA_API void lua_getfenv(lua_State* L, int idx)
{StkId o = index2adr(L, idx);switch (ttype(o)){case LUA_TFUNCTION:{sethvalue(L, L->top, clvalue(o)->c.env);break;}case LUA_TUSERDATA:{sethvalue(L, L->top, uvalue(o)->env);break;}case LUA_TTHREAD:{setobj2s(L, L->top, gt(thvalue(o)));break;}default:{setnilvalue(L->top);break;}}api_incr_top(L);
}
8 通过栈操作Lua
8.1 lua_settable 以L->top-2处为key,以L->top-1处为value,操作idx处的表,L->top-=2
(lapi.c) lua_settable
LUA_API void lua_settable(lua_State* L, int idx)
{StkId t = index2adr(L, idx);luaV_settable(L, t, t->top - 2, L->top - 1);L->top -= 2;
}
(lvm.c) luaV_settable (含义:t[key]=val)
void luaV_settable(lua_State* L, const TValue* t, TValue* key, StkId val)
{for (int loop = 0; loop < MAXTAGLOOP; loop++)//#define MAXTAGLOOP 100{const TValue* tm;if (ttistable(t)){Table* h = hvalue(t);TValue* oldval = luaH_set(L, h, key);//rawsetif (!ttisnil(oldval) || (tm = fasttm(L, h->metatble, TM_NEWINDEX)) == NULL){setobj2t(L, oldval, val);luaC_barriert(L, h, val);//luaC_barriert见GC章节return;}}else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))){luaG_typeerror(L, t, "index");}if (ttisfunction(tm)){callTM(L, tm, t, key, val);return;}t = tm;}luaG_runerror(L, "loop in settable");
}
(lvm.c) callTM(含义:f(p1,p2,p3))
static void callTM(lua_State* L, const TValue* f, const TValue* p1, const TValue* p2, const TValue* p3)
{setobj2s(L, L->top, f);setobj2s(L, L->top + 1, p1);setobj2s(L, L->top + 2, p2);setobj2s(L, L->top + 3, p3);luaD_checkstack(L, 4);L->top += 4;luaD_call(L, L->top - 4, 0);//luaD_call见函数章节
}
8.2 lua_setfield 以k为字符串key,以L->top-1处为value,操作idx处的表,L->top–
(lapi.c) lua_setfield
LUA_API void lua_setfield(lua_State* L, int idx, const char* k)
{StkId t = index2adr(L, idx);setsvalue(L, &key, luaS_new(L, k));luaV_settable(L, t, &key, L->top - 1);L->top--;
}
8.3 lua_rawset 以L->top-2处为key,以L->top-1处为value,操作idx处的表(不涉及元表),L->top-=2
(lapi.c) lua_rawset
LUA_API void lua_rawset(lua_State* L, int idx)
{StkId t = index2adr(L, idx);setobj2t(L, luaH_set(L, hvalue(t), L->top - 2), L->top - 1);luaC_barriert(L, hvalue(t), L->top - 1);//luaC_barriert见GC章节L->top -= 2;
}
8.4 lua_rawseti 以n为数字键,L->top -1处为value,操作idx处的表(不涉及元表),L->top–
(lapi.c) lua_rawseti
LUA_API void lua_rawseti(lua_State* L, int idx, int n)
{StkId o = index2adr(L, idx);setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top - 1);luaC_barriert(L, hvalue(o), L->top - 1);L->top--;
}
8.5 lua_setmetatable 将L->top-1处的值,设置为objindex处的值的元表,L->top–
(lapi.c) lua_setmetatable
LUA_API int lua_setmetatable(lua_State* L, int objindex)
{Table* mt;TValue* obj = index2adr(L, idx);if (ttisnil(L->top - 1)){mt = NULL;}else{mt = hvalue(L->top - 1);}switch (ttype(obj)){case LUA_TTABLE:{hvalue(obj)->metatable = mt;if (mt){luaC_objbarriert(L, hvalue(obj), mt);}break;}case LUA_TUSERDATA:{uvalue(obj)->metatable = mt;if (mt){luaC_objbarriert(L, rawuvalue(obj), mt);}break;}default:{G(L)->mt[ttype(o)] = mt;break;}}L->top--;return 1;
}
8.6 lua_setfenv 将L->top-1处的值,设置为objindex处的值的环境表,L->top–
(lapi.c) lua_setfenv
LUA_API int lua_setfenv(lua_State* L, int idx)
{int res = 1;StkId o = index2adr(L, idx);switch (ttype(o)){case LUA_TFUNCTION:{clvalue(o)->c.env = hvalue(L->top - 1);break;}case LUA_TUSERDATA:{uvalue(o)->env = hvalue(L->top - 1);break;}case LUA_TTHREAD:{sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));break;}default:{res = 0;break;}}if (res){luaC_objbarriert(L, gcvalue(o), hvalue(L->top - 1));}L->top--;return res;
}