一、从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;
}