很早就想讀lua的源碼,也曾很多次瀏覽過大概。不過我一直沒有深入去讀,一是想自己在讀lua源碼之前,僅憑自己對lua使用的理解自己先實現一個簡單的lua子集,二是我覺得自己實現過lua的子集之后也能幫助自己更容易的理解lua源碼。前段時間,花了幾個月的業余時間,實現了一個簡單粗糙的lua子集(https://github.com/airtrack/luna)之后,我覺得現在可以開始讀lua的源碼了。
從lua.c的main函數開始,lua.c是一個stand-alone的解釋器,編譯完就是一個交互式命令行解釋器,輸入一段lua代碼,然后執行并返回結果,也可以執行一個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函數創建了lua_State之后就按照調用C導出給lua函數的方式調用了pmain函數。pmain函數中通過lua棧獲取到命令行的argc和argv參數之后,對參數進行分析后,主要可以分為兩個分支,一個處理交互命令行,一個處理文件。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會產生一個什么東西在棧上呢?寫過lua的程序的人估計都會了解到下面這段lua代碼:
local f = load(filename)
f()
load會將文件chunk編譯成一個function,然后我們就可以對它調用。如果我們詳細看lua文檔的話,這個函數可以帶有upvalues,也就是這個函數其實是一個閉包(closure)。按照我自己實現的那個粗糙的lua子集的方式的話,每個運行時期的可調用的lua函數都是閉包。
#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是一個宏,實際是luaL_loadfilex函數,在luaL_loadfilex函數中,我們發現是通過調用lua_load函數實現,lua_load的函數原型是:
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode);
定義在lapi.c中,它接受一個lua_Reader的函數并把data作為這個reader的參數。在luaL_loadfilex函數中傳給lua_load作為reader是一個static函數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);
在函數lua_load中,又將lua_Reader和data通過luaZ_init函數把數據綁定到ZIO的結構中,ZIO是buffered streams。之后調用luaD_protectedparser,此函數定義在ldo.c中,在這個函數中,我們發現它使用了構造lua_Reader和data的方式構造了調用函數f_parser和它的數據SParser,并將它們傳給luaD_pcall,luaD_pcall的功能是在protected模式下用SParser數據調用f_parser函數,因此我們只需追蹤f_parser函數即可。
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通過數據頭的signature來判斷讀取的數據是binary還是text的,如果是binary的數據,則調用luaU_undump來讀取預編譯好的lua chunks,如果是text數據,則調用luaY_parser來parse lua代碼。我們發現luaU_undump和luaY_parser函數的返回值都是Closure *類型,這個剛好就和我們前面預計的一樣,一個chunk load之后返回一個閉包。
進入luaY_parser函數后,就調用了一個static的mainfunc開始parse lua代碼。
仔細回顧上面看過的函數,我們會發現每個C文件的導出函數都會使用lua開頭,如果沒有lua開頭的函數都是static函數。并且我們會發現lua后的大寫前綴可以標識這個函數所屬的文件:
luaL_loadfile luaL_loadfilex L應該是library的意思,屬于lauxlib
luaD_protectedparser luaD_pcall D是do的意思,屬于ldo
luaU_undump U 是undump的意思,屬于lundump
luaY_parser Y 是代表yacc的意思,lua的parser最早是用過yacc生成的,后來改成手寫,名字也保留下來,屬于lparser
其它的lua函數也都有這個規律。
posted on 2012-07-19 18:24
airtrack 閱讀(12508)
評論(3) 編輯 收藏 引用