很早就想讀lua的源碼,也曾很多次瀏覽過大概。不過我一直沒有深入去讀,一是想自己在讀lua源碼之前,僅憑自己對(duì)lua使用的理解自己先實(shí)現(xiàn)一個(gè)簡(jiǎn)單的lua子集,二是我覺得自己實(shí)現(xiàn)過lua的子集之后也能幫助自己更容易的理解lua源碼。前段時(shí)間,花了幾個(gè)月的業(yè)余時(shí)間,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單粗糙的lua子集(https://github.com/airtrack/luna)之后,我覺得現(xiàn)在可以開始讀lua的源碼了。
從lua.c的main函數(shù)開始,lua.c是一個(gè)stand-alone的解釋器,編譯完就是一個(gè)交互式命令行解釋器,輸入一段lua代碼,然后執(zhí)行并返回結(jié)果,也可以執(zhí)行一個(gè)lua文件。
main:
/* call 'pmain' in protected mode */
lua_pushcfunction(L, &pmain);
lua_pushinteger(L, argc); /* 1st argument */
lua_pushlightuserdata(L, argv); /* 2nd argument */
status = lua_pcall(L, 2, 1, 0);
result = lua_toboolean(L, -1); /* get result */
main函數(shù)創(chuàng)建了lua_State之后就按照調(diào)用C導(dǎo)出給lua函數(shù)的方式調(diào)用了pmain函數(shù)。pmain函數(shù)中通過lua棧獲取到命令行的argc和argv參數(shù)之后,對(duì)參數(shù)進(jìn)行分析后,主要可以分為兩個(gè)分支,一個(gè)處理交互命令行,一個(gè)處理文件。dotty出來交互命令行,handle_script處理lua文件。
handle_script:
status = luaL_loadfile(L, fname);
lua_insert(L, -(narg+1));
if (status == LUA_OK)
status = docall(L, narg, LUA_MULTRET);
else
lua_pop(L, narg);
在handle_script中先loadfile,然后docall。
loadfile會(huì)產(chǎn)生一個(gè)什么東西在棧上呢?寫過lua的程序的人估計(jì)都會(huì)了解到下面這段lua代碼:
local f = load(filename)
f()
load會(huì)將文件chunk編譯成一個(gè)function,然后我們就可以對(duì)它調(diào)用。如果我們?cè)敿?xì)看lua文檔的話,這個(gè)函數(shù)可以帶有upvalues,也就是這個(gè)函數(shù)其實(shí)是一個(gè)閉包(closure)。按照我自己實(shí)現(xiàn)的那個(gè)粗糙的lua子集的方式的話,每個(gè)運(yùn)行時(shí)期的可調(diào)用的lua函數(shù)都是閉包。
#define luaL_loadfile(L,f) luaL_loadfilex(L,f,NULL)
luaL_loadfilex:
if (filename == NULL) {
lua_pushliteral(L, "=stdin");
lf.f = stdin;
}
else {
lua_pushfstring(L, "@%s", filename);
lf.f = fopen(filename, "r");
if (lf.f == NULL) return errfile(L, "open", fnameindex);
}
if (skipcomment(&lf, &c)) /* read initial portion */
lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
skipcomment(&lf, &c); /* re-read initial portion */
}
if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
luaL_loadfile是一個(gè)宏,實(shí)際是luaL_loadfilex函數(shù),在luaL_loadfilex函數(shù)中,我們發(fā)現(xiàn)是通過調(diào)用lua_load函數(shù)實(shí)現(xiàn),lua_load的函數(shù)原型是:
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode);
定義在lapi.c中,它接受一個(gè)lua_Reader的函數(shù)并把data作為這個(gè)reader的參數(shù)。在luaL_loadfilex函數(shù)中傳給lua_load作為reader是一個(gè)static函數(shù)getF,getF通過fread讀取文件。
lua_load:
ZIO z;
int status;
lua_lock(L);
if (!chunkname) chunkname = "?";
luaZ_init(L, &z, reader, data);
status = luaD_protectedparser(L, &z, chunkname, mode);
在函數(shù)lua_load中,又將lua_Reader和data通過luaZ_init函數(shù)把數(shù)據(jù)綁定到ZIO的結(jié)構(gòu)中,ZIO是buffered streams。之后調(diào)用luaD_protectedparser,此函數(shù)定義在ldo.c中,在這個(gè)函數(shù)中,我們發(fā)現(xiàn)它使用了構(gòu)造lua_Reader和data的方式構(gòu)造了調(diào)用函數(shù)f_parser和它的數(shù)據(jù)SParser,并將它們傳給luaD_pcall,luaD_pcall的功能是在protected模式下用SParser數(shù)據(jù)調(diào)用f_parser函數(shù),因此我們只需追蹤f_parser函數(shù)即可。
luaD_protectedparser:
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
f_parser:
if (c == LUA_SIGNATURE[0]) {
checkmode(L, p->mode, "binary");
cl = luaU_undump(L, p->z, &p->buff, p->name);
}
else {
checkmode(L, p->mode, "text");
cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
}
f_parser通過數(shù)據(jù)頭的signature來判斷讀取的數(shù)據(jù)是binary還是text的,如果是binary的數(shù)據(jù),則調(diào)用luaU_undump來讀取預(yù)編譯好的lua chunks,如果是text數(shù)據(jù),則調(diào)用luaY_parser來parse lua代碼。我們發(fā)現(xiàn)luaU_undump和luaY_parser函數(shù)的返回值都是Closure *類型,這個(gè)剛好就和我們前面預(yù)計(jì)的一樣,一個(gè)chunk load之后返回一個(gè)閉包。
進(jìn)入luaY_parser函數(shù)后,就調(diào)用了一個(gè)static的mainfunc開始parse lua代碼。
仔細(xì)回顧上面看過的函數(shù),我們會(huì)發(fā)現(xiàn)每個(gè)C文件的導(dǎo)出函數(shù)都會(huì)使用lua開頭,如果沒有l(wèi)ua開頭的函數(shù)都是static函數(shù)。并且我們會(huì)發(fā)現(xiàn)lua后的大寫前綴可以標(biāo)識(shí)這個(gè)函數(shù)所屬的文件:
luaL_loadfile luaL_loadfilex L應(yīng)該是library的意思,屬于lauxlib
luaD_protectedparser luaD_pcall D是do的意思,屬于ldo
luaU_undump U 是undump的意思,屬于lundump
luaY_parser Y 是代表yacc的意思,lua的parser最早是用過yacc生成的,后來改成手寫,名字也保留下來,屬于lparser
其它的lua函數(shù)也都有這個(gè)規(guī)律。