關于Lua數(shù)據類型源代碼分析
關于Lua數(shù)據類型源代碼分析是本文要介紹的內容,主要是來了解lua源代碼的分析理解,具體內容的實現(xiàn)來看本文詳解。
Lua語言是不用聲明變量的類型的,而且是類型可變的,如下面的語句:
- local a = 1;
- a = “hello”;
開始是a的類型是number,當復制為字符串時,類型改為string,可以通過type(a)查看。那么它是怎么做到的呢?參見如下的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中所有的類型都定義為TValue類型。tt表示類型,定義參見:
- #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ù)據類型之外,還包括未知類型和light userdata,light userdata表示僅僅在lua中保存了userdata的指針,占用的內存不歸lua管。Value代表變量的具體值,b表示整形,n表示浮點型;gc表示可以用于垃圾回收的對象的指針;當gc取gch值時,p應該是lua對象的指針,否則有可能只想TValue本身。其中相關的定義如下:
- 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表示線程,每一個lua_State相當于一個線程;具體的定義及注釋如下:
- TString
- typedef union TString { // lobject.h, line 200
- L_Umaxalign dummy; /* ensures maximum alignment for strings */// 對齊用
- 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ù)組,沒有索引值時使用
- 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ù)個數(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ù)會不斷補充,分析數(shù)據類型每個的確切用途。
小結:關于Lua數(shù)據類型源代碼分析的內容介紹完了,希望通過本文的學習能對你有所幫助!