Lua2.4 函數相關 func.c

看一下和函數相關的數據結構和方法。

數據結構
數據結構

typedef struct LocVar
{
  TaggedString *varname;           /* NULL signals end of scope */
  int       line;
} LocVar;

局部變量描述信息,這個主要是調試時有用,也就是編譯時打開了調試選項。
varname 局部變量的名字,若是爲空,表示函數的局部變量結束。
line 局部變量開始定義的地方。

函數

/*
** Function Headers*/
typedef struct TFunc
{
  struct TFunc    *next;
  int        marked;
  int        size;
  Byte        *code;
  int        lineDefined;
  char        *fileName;
  LocVar        *locvars;
} TFunc;

函數描述信息。
next   : 下一個函數,全部有函數連成鏈表,以便垃圾回收。
marked : 垃圾回收標籤。
size   : 函數的大小。
code   : 函數的字節碼。
lineDefined : 定義函數行號。
fileName    : 定義函數的文件名。
locvars     : 函數的全部局部變量。

func.h 頭文件中定義的幾個對外接口,前四個函數簡單的看一下。後面的四個都是和調試相關的,先放一放,留待之後再分析。
lua

/*
** Initialize TFunc struct
*/
void luaI_initTFunc (TFunc *f)
{
  f->next = NULL;
  f->marked = 0;
  f->size = 0;
  f->code = NULL;
  f->lineDefined = 0;
  f->fileName = NULL;
  f->locvars = NULL;
}

初始化函數的數據結構。

debug

/*
** Insert function in list for GC
*/
void luaI_insertfunction (TFunc *f)
{
  lua_pack();
  f->next = function_root;
  function_root = f;
  f->marked = 0;
}

函數插入函數鏈表,以便進行垃圾回收。

指針

/*
** Free function
*/
void luaI_freefunc (TFunc *f)
{
  luaI_free (f->code);
  luaI_free (f->locvars);
  luaI_free (f);
}

釋放函數內存空間。

調試

/*
** Garbage collection function.
** This function traverse the function list freeing unindexed functions
*/
Long luaI_funccollector (void)
{
  TFunc *curr = function_root;
  TFunc *prev = NULL;
  Long counter = 0;
  while (curr)
  {
    TFunc *next = curr->next;
    if (!curr->marked)
    {
      if (prev == NULL)
        function_root = next;
      else
        prev->next = next;
      luaI_freefunc (curr);
      ++counter;
    }
    else
    {
      curr->marked = 0;
      prev = curr;
    }
    curr = next;
  }
  return counter;
}

垃圾回收。
遍歷函數鏈表,釋放沒有 marked 的函數,同時調整鏈表指針。

這幾個看到的代碼都比較直接,後面的幾個調試相關的代碼相對來講複雜一點。

調試相關代碼有一個比較好辨識的地方是,他們基本上都接在一個以 lua_debug 爲條件的 if 判斷裏。
好比:
code

if (lua_debug)
   luaI_registerlocalvar(name, lua_linenumber

還有咱們以前看到的 luaI_codedebugline,記錄下腳本代碼的行號的。
能夠看到,他在 lex.c 中的調用就也是這樣:
token

if (lua_debug)
  luaI_codedebugline(linelasttoken);

調試相關的信息還有的在執行時能夠加回調的地方。
具體的描述能夠參考下手冊裏的描述。
調試相關信息和程序的具體功能關係不大,暫時忽略它對程序具體功能分析並沒有影響。

----------------------------------------
到目前爲止的問題:
> lua_parser 是什麼? do_dump 方法裏調的那幾個方法又分別是幹什麼的?
----------------------------------------
接口

相關文章
相關標籤/搜索