關(guān)于Lua數(shù)據(jù)類型源代碼分析
關(guān)于Lua數(shù)據(jù)類型源代碼分析是本文要介紹的內(nèi)容,主要是來(lái)了解lua源代碼的分析理解,具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。
Lua語(yǔ)言是不用聲明變量的類型的,而且是類型可變的,如下面的語(yǔ)句:
- local a = 1;
- a = “hello”;
開(kāi)始是a的類型是number,當(dāng)復(fù)制為字符串時(shí),類型改為string,可以通過(guò)type(a)查看。那么它是怎么做到的呢?參見(jiàn)如下的TValue定義:
- typedef struct lua_TValue { // lobject.h, line 73
- TValuefields;
- } TValue;
- #define TValuefields Value value; int tt // lobject.h, line 71
- typedef union { // lobject.h, line 59
- GCObject *gc;
- void *p;
- lua_Number n;
- int b;
- } Value;
- typedef LUA_NUMBER lua_Number; // lua.h, line 100
- #define LUA_NUMBER double // luaconf.h, line 505
Lua中所有的類型都定義為T(mén)Value類型。tt表示類型,定義參見(jiàn):
- #define LUA_TNONE (-1) // lua.h, line 73
- #define LUA_TNIL 0
- #define LUA_TBOOLEAN 1
- #define LUA_TLIGHTUSERDATA 2 // light userdata
- #define LUA_TNUMBER 3
- #define LUA_TSTRING 4
- #define LUA_TTABLE 5
- #define LUA_TFUNCTION 6
- #define LUA_TUSERDATA 7
- #define LUA_TTHREAD 8
上面的定義中,除了8種基本的數(shù)據(jù)類型之外,還包括未知類型和light userdata,light userdata表示僅僅在lua中保存了userdata的指針,占用的內(nèi)存不歸lua管。Value代表變量的具體值,b表示整形,n表示浮點(diǎn)型;gc表示可以用于垃圾回收的對(duì)象的指針;當(dāng)gc取gch值時(shí),p應(yīng)該是lua對(duì)象的指針,否則有可能只想TValue本身。其中相關(guān)的定義如下:
- union GCObject { // lstate.h, line 136
- GCheader gch;
- union TString ts;
- union Udata u;
- union Closure cl;
- struct Table h;
- struct Proto p;
- struct UpVal uv;
- struct lua_State th; /* thread */
- };
- typedef struct GCheader { // lobject.h, line 49
- CommonHeader;
- } GCheader;
- #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked // lobject.h, line 43
GCObject定義中,gch用于垃圾回收;ts表示用于字符串表的類型;u表示userdata;cl表示閉合函數(shù);h表示表;p表示函數(shù);uv表示upvalue;th表示線程,每一個(gè)lua_State相當(dāng)于一個(gè)線程;具體的定義及注釋如下:
- TString
- typedef union TString { // lobject.h, line 200
- L_Umaxalign dummy; /* ensures maximum alignment for strings */// 對(duì)齊用
- struct {
- CommonHeader;
- lu_byte reserved;
- unsigned int hash;
- size_t len;
- } tsv;
- } TString;
Udata表示userdata
- typedef union Udata { // lobject.h, line 216
- L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
- struct {
- CommonHeader;
- struct Table *metatable;
- struct Table *env;
- size_t len;
- } uv;
- } Udata;
Closure又分為兩種,一種用于lua中,另一種用于C代碼中。
- #define ClosureHeader / // lobject.h, line 292
- CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; /
- struct Table *env
- typedef struct CClosure {
- ClosureHeader;
- lua_CFunction f;
- TValue upvalue[1];
- } CClosure;
- typedef struct LClosure {
- ClosureHeader;
- struct Proto *p;
- UpVal *upvals[1];
- } LClosure;
- typedef union Closure {
- CClosure c;
- LClosure l;
- } Closure;
C代碼中使用的函數(shù)類型是lua_CFunction,而lua中使用的函數(shù)是Proto;
- table
- typedef struct Table {
- CommonHeader; // for GC
- lu_byte flags; /* 1<<p means tagmethod(p) is not present */
- lu_byte lsizenode; /* log2 of size of `node' array */ // size of node array
- struct Table *metatable; // 元表
- TValue *array; /* array part */// 數(shù)組,沒(méi)有索引值時(shí)使用
- Node *node; // node array
- Node *lastfree; /* any free position is before this position */
- GCObject *gclist;
- int sizearray; /* size of `array' array */// 數(shù)組的大小
- } Table;
- Proto
- typedef struct Proto {
- CommonHeader; // for GC
- TValue *k; /* constants used by the function */ // 常量
- Instruction *code; // function code is here, code array?
- struct Proto **p; /* functions defined inside the function */
- int *lineinfo; /* map from opcodes to source lines */
- struct LocVar *locvars; /* information about local variables */
- TString **upvalues; /* upvalue names */
- TString *source; // 源代碼?
- int sizeupvalues; // size of upvalue names
- int sizek; /* size of `k' */
- int sizecode; // size of code
- int sizelineinfo; // size of line
- int sizep; /* size of `p' */ // size of Protos
- int sizelocvars; // size of local values
- int linedefined;
- int lastlinedefined;
- GCObject *gclist;
- lu_byte nups; /* number of upvalues */
- lu_byte numparams; // 參數(shù)個(gè)數(shù)
- lu_byte is_vararg; // 是否是變參
- lu_byte maxstacksize; // 函數(shù)用到的棧?
- } Proto;
- UpVal
- typedef struct UpVal {
- CommonHeader;
- TValue *v; /* points to stack or to its own value */
- union {
- TValue value; /* the value (when closed) */
- struct { /* double linked list (when open) */
- struct UpVal *prev;
- struct UpVal *next;
- } l;
- } u;
- } UpVal;
后續(xù)會(huì)不斷補(bǔ)充,分析數(shù)據(jù)類型每個(gè)的確切用途。
小結(jié):關(guān)于Lua數(shù)據(jù)類型源代碼分析的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!