Lua2.4 函數相關 func.c

來源:互聯網
上載者:User

標籤:io   ar   for   檔案   資料   sp   問題   on   cti   

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

資料結構

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 標頭檔中定義的幾個對外介面,前四個函數簡單的看一下。後面的四個都是和調試相關的,先放一放,留待以後再分析。

/*** 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;}

初始化函數的資料結構。

/*** 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 判斷裡。
比如:

if (lua_debug)   luaI_registerlocalvar(name, lua_linenumber

還有我們之前看到的 luaI_codedebugline,記錄下指令碼代碼的行號的。
可以看到,他在 lex.c 中的調用就也是這樣:

if (lua_debug)  luaI_codedebugline(linelasttoken);

調試相關的資訊還有的在執行時可以加回調的地方。
具體的描述可以參考下手冊裡的描述。
調試相關資訊和程式的具體功能關係不大,暫時忽略它對程式具體功能分析並無影響。

----------------------------------------
到目前為止的問題:
> lua_parser 是什嗎? do_dump 方法裡調的那幾個方法又分別是幹什麼的?
----------------------------------------

Lua2.4 函數相關 func.c

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.