自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于Lua數(shù)據(jù)類型源代碼分析

移動(dòng)開(kāi)發(fā) iOS
關(guān)于Lua數(shù)據(jù)類型源代碼分析是本文要介紹的內(nèi)容,主要是來(lái)了解lua源代碼的分析理解,具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。

關(guān)于Lua數(shù)據(jù)類型源代碼分析是本文要介紹的內(nèi)容,主要是來(lái)了解lua源代碼的分析理解,具體內(nèi)容的實(shí)現(xiàn)來(lái)看本文詳解。

Lua語(yǔ)言是不用聲明變量的類型的,而且是類型可變的,如下面的語(yǔ)句:

  1. local a = 1;  
  2. a = “hello”; 

開(kāi)始是a的類型是number,當(dāng)復(fù)制為字符串時(shí),類型改為string,可以通過(guò)type(a)查看。那么它是怎么做到的呢?參見(jiàn)如下的TValue定義:

  1. typedef struct lua_TValue { // lobject.h, line 73  
  2.   TValuefields;  
  3. } TValue;  
  4. #define TValuefields       Value value; int tt // lobject.h, line 71  
  5. typedef union { // lobject.h, line 59  
  6.   GCObject *gc;  
  7.   void *p;  
  8.   lua_Number n;  
  9.   int b;  
  10. } Value;  
  11. typedef LUA_NUMBER lua_Number; // lua.h, line 100  
  12. #define LUA_NUMBER  double // luaconf.h, line 505 

Lua中所有的類型都定義為T(mén)Value類型。tt表示類型,定義參見(jiàn):

  1. #define LUA_TNONE           (-1) // lua.h, line 73  
  2. #define LUA_TNIL        0  
  3. #define LUA_TBOOLEAN            1  
  4. #define LUA_TLIGHTUSERDATA       2 // light userdata  
  5. #define LUA_TNUMBER             3  
  6. #define LUA_TSTRING        4  
  7. #define LUA_TTABLE          5  
  8. #define LUA_TFUNCTION          6  
  9. #define LUA_TUSERDATA          7  
  10. #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)的定義如下:

  1. union GCObject { // lstate.h, line 136  
  2.   GCheader gch;  
  3.   union TString ts;  
  4.   union Udata u;  
  5.   union Closure cl;  
  6.   struct Table h;  
  7.   struct Proto p;  
  8.   struct UpVal uv;  
  9.   struct lua_State th;  /* thread */  
  10. };  
  11. typedef struct GCheader { // lobject.h, line 49  
  12.   CommonHeader;  
  13. } GCheader;  
  14.  
  15. #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è)線程;具體的定義及注釋如下:

  1. TString  
  2. typedef union TString { // lobject.h, line 200  
  3.   L_Umaxalign dummy;  /* ensures maximum alignment for strings */// 對(duì)齊用  
  4.   struct {  
  5.     CommonHeader;  
  6.     lu_byte reserved;  
  7.     unsigned int hash;  
  8.     size_t len;  
  9.   } tsv;  
  10. } TString; 

Udata表示userdata

  1. typedef union Udata { // lobject.h, line 216  
  2.   L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */  
  3.   struct {  
  4.     CommonHeader;  
  5.     struct Table *metatable;  
  6.     struct Table *env;  
  7.     size_t len;  
  8.   } uv;  
  9. } Udata; 

Closure又分為兩種,一種用于lua中,另一種用于C代碼中。

  1. #define ClosureHeader / // lobject.h, line 292  
  2.        CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; /  
  3.       struct Table *env  
  4.  
  5. typedef struct CClosure {  
  6.   ClosureHeader;  
  7.   lua_CFunction f;  
  8.   TValue upvalue[1];  
  9. } CClosure;  
  10.  
  11. typedef struct LClosure {  
  12.   ClosureHeader;  
  13.   struct Proto *p;  
  14.   UpVal *upvals[1];  
  15. } LClosure;  
  16.  
  17. typedef union Closure {  
  18.   CClosure c;  
  19.   LClosure l;  
  20. } Closure; 

C代碼中使用的函數(shù)類型是lua_CFunction,而lua中使用的函數(shù)是Proto;

  1. table  
  2. typedef struct Table {  
  3.   CommonHeader; // for GC  
  4.   lu_byte flags;  /* 1<<p means tagmethod(p) is not present */  
  5.   lu_byte lsizenode;  /* log2 of size of `node' array */ // size of node array  
  6.   struct Table *metatable; // 元表  
  7.   TValue *array;  /* array part */// 數(shù)組,沒(méi)有索引值時(shí)使用  
  8.   Node *node; // node array  
  9.   Node *lastfree;  /* any free position is before this position */  
  10.   GCObject *gclist;  
  11.   int sizearray;  /* size of `array' array */// 數(shù)組的大小  
  12. } Table;  
  13.  
  14. Proto  
  15. typedef struct Proto {  
  16.   CommonHeader; // for GC  
  17.   TValue *k;  /* constants used by the function */ // 常量  
  18.   Instruction *code; // function code is here, code array?  
  19.   struct Proto **p;  /* functions defined inside the function */  
  20.   int *lineinfo;  /* map from opcodes to source lines */  
  21.   struct LocVar *locvars;  /* information about local variables */  
  22.   TString **upvalues;  /* upvalue names */  
  23.   TString  *source; // 源代碼?  
  24.   int sizeupvalues; // size of upvalue names  
  25.   int sizek;  /* size of `k' */  
  26.   int sizecode; // size of code  
  27.   int sizelineinfo; // size of line  
  28.   int sizep;  /* size of `p' */ // size of Protos  
  29.   int sizelocvars; // size of local values  
  30.   int linedefined;  
  31.   int lastlinedefined;  
  32.   GCObject *gclist;  
  33.   lu_byte nups;  /* number of upvalues */  
  34.   lu_byte numparams; // 參數(shù)個(gè)數(shù)  
  35.   lu_byte is_vararg; // 是否是變參  
  36.   lu_byte maxstacksize; // 函數(shù)用到的棧?  
  37. } Proto;  
  38.  
  39. UpVal  
  40. typedef struct UpVal {  
  41.   CommonHeader;  
  42.   TValue *v;  /* points to stack or to its own value */  
  43.   union {  
  44.     TValue value;  /* the value (when closed) */  
  45.     struct {  /* double linked list (when open) */  
  46.       struct UpVal *prev;  
  47.       struct UpVal *next;  
  48.     } l;  
  49.   } u;  
  50. } UpVal; 

后續(xù)會(huì)不斷補(bǔ)充,分析數(shù)據(jù)類型每個(gè)的確切用途。

小結(jié):關(guān)于Lua數(shù)據(jù)類型源代碼分析的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: CSDN博客
相關(guān)推薦

2011-08-24 15:42:38

LUA源代碼

2021-03-24 09:37:41

數(shù)據(jù)類型數(shù)據(jù)分析數(shù)據(jù)的分類

2011-08-24 16:59:59

LuaModule

2011-08-04 15:14:39

Objective-C 數(shù)據(jù)類型

2018-11-15 09:45:47

JavaScript數(shù)據(jù)類型變量

2011-08-24 17:15:33

Lua源代碼文件

2011-02-23 14:54:58

FileZilla

2011-02-23 14:46:21

FileZilla

2011-02-23 14:39:27

FileZilla

2011-02-23 14:16:43

FileZilla

2011-02-23 14:26:28

FileZilla

2011-02-23 15:26:01

FileZilla

2011-02-23 15:33:42

FileZilla

2011-02-23 13:47:33

FileZilla

2011-02-23 15:11:27

FileZilla

2011-02-23 15:21:06

FileZilla

2019-08-12 11:40:48

數(shù)據(jù)庫(kù)SQLite3數(shù)據(jù)類型

2016-08-18 14:13:55

JavaScript基本數(shù)據(jù)引用數(shù)據(jù)

2009-08-14 13:52:18

C#判斷數(shù)據(jù)類型

2014-01-05 17:08:09

PostgreSQL數(shù)據(jù)類型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)