一、從hello world說起
當執行print("Hello World!")時,明顯這是需要有執行一個特定的向標准輸出打印字符串的功能,所以這里首先涉及到的是一個函數查找的問題,也就是如何把字符串和對應的功能聯系起來。在最為常見的C語言中,這個查找是由鏈接器完成的:它從所有的obj文件中查找這個函數的定義,然后把這個地方轉換成對應位置的CPU call指令。對應地,對於lua,同樣需要有方法將這個字符串和真正的函數調用聯系起來。那么最后的問題就是:當lua遇到一個變量時,它會如何查找這個變量?
二、查找的實現
1、查找的實現代碼
這個代碼實現也比較直觀:首先是在函數級別的Local中查找,如果查找則認為是VLOCAL類型,否則查找當前函數已經存在的upvalue,最后默認認為是在全局變量表中。這個“全局”就是很高級別了,相當於整個lua虛擬機共享,也就是全局變量了。
這些變量確定類型的原因在於它們會影響指令的生成,而不同指令會指明變量從哪里查找。
lua-5.3.4\src\lparser.c
/*
Find variable with given name 'n'. If it is an upvalue, add this
upvalue into all intermediate functions.
*/
static void singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {
if (fs == NULL) /* no more levels? */
init_exp(var, VVOID, 0); /* default is global */
else {
int v = searchvar(fs, n); /* look up locals at current level */
if (v >= 0) { /* found? */
init_exp(var, VLOCAL, v); /* variable is local */
if (!base)
markupval(fs, v); /* local will be used as an upval */
}
else { /* not found as local at current level; try upvalues */
int idx = searchupvalue(fs, n); /* try existing upvalues */
if (idx < 0) { /* not found? */
singlevaraux(fs->prev, n, var, 0); /* try upper levels */
if (var->k == VVOID) /* not found? */
return; /* it is a global */
/* else was LOCAL or UPVAL */
idx = newupvalue(fs, n, var); /* will be a new upvalue */
}
init_exp(var, VUPVAL, idx); /* new or old upvalue */
}
}
}
2、Local變量的處理流程
以下面代碼為例:
local a,b,c=1,2,3
首先執行的是函數中的do while {}(testnext(ls, ','))循環,這樣相當於在函數的locvars中注冊了變量,而它在其中的位置也將是一個確定的位置。
static void localstat (LexState *ls) {
/* stat -> LOCAL NAME {',' NAME} ['=' explist] */
int nvars = 0;
int nexps;
expdesc e;
do {
new_localvar(ls, str_checkname(ls));
nvars++;
} while (testnext(ls, ','));
if (testnext(ls, '='))
nexps = explist(ls, &e);
else {
e.k = VVOID;
nexps = 0;
}
adjust_assign(ls, nvars, nexps, &e);
adjustlocalvars(ls, nvars);
}
3、對於全局變量的處理
對於一個變量g,如果從local和upvalue中沒有查找到,則認為是從全局env中使用變量名為字符串進行查找,並且在luaK_indexed函數中為key分配寄存器,也就是全局變量的查找同樣需要使用寄存器。
static void singlevar (LexState *ls, expdesc *var) {
TString *varname = str_checkname(ls);
FuncState *fs = ls->fs;
singlevaraux(fs, varname, var, 1);
if (var->k == VVOID) { /* global name? */
expdesc key;
singlevaraux(fs, ls->envn, var, 1); /* get environment variable */
lua_assert(var->k != VVOID); /* this one must exist */
codestring(ls, &key, varname); /* key is variable name */
luaK_indexed(fs, var, &key); /* env[varname] */
}
}
三、print函數的查找
1、函數調用的解析
由於在本地沒有查找到print這個符號,所以生成代碼會要求從env中查找,所以只要在運行時將該字符串變量放在全局變量表中即可。
2、系統函數的注冊
print由於lua內部調用函數參數都是一個狀態機(這個的好處就是當調用函數指針時,這個函數指針的類型是明確的),所以是定制的luaB_print函數。
LUAMOD_API int luaopen_base (lua_State *L) {
/* open lib into global table */
lua_pushglobaltable(L);
luaL_setfuncs(L, base_funcs, 0);
/* set global _G */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "_G");
/* set global _VERSION */
lua_pushliteral(L, LUA_VERSION);
lua_setfield(L, -2, "_VERSION");
return 1;
}
3、系統注冊表的設置
LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
const char *chunkname, const char *mode) {
ZIO z;
int status;
lua_lock(L);
if (!chunkname) chunkname = "?";
luaZ_init(L, &z, reader, data);
status = luaD_protectedparser(L, &z, chunkname, mode);
if (status == LUA_OK) { /* no errors? */
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
if (f->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, f->upvals[0]->v, gt);
luaC_upvalbarrier(L, f->upvals[0]);
}
}
lua_unlock(L);
return status;
}