(lobject.h) 338 typedef struct Table { 339 CommonHeader; 340 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ 341 lu_byte lsizenode; /* log2 of size of `node' array */ 342 struct Table *metatable; 343 TValue *array; /* array part */ 344 Node *node; 345 Node *lastfree; /* any free position is before this position */ 346 GCObject *gclist; 347 int sizearray; /* size of `array' array */ 348 } Table;
其中CommonHeader的定义如下:
1 2 3 4 5 6
(lobject.h) 39 /* 40 ** Common Header for all collectable objects (in macro form, to be 41 ** included in other objects) 42 */ 43 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
同时,还有一个名为GCheader的结构体,其中的成员只有CommonHeader:
1 2 3 4 5 6 7
(lobject.h) 46 /* 47 ** Common header in struct form 48 */ 49 typedef struct GCheader { 50 CommonHeader; 51 } GCheader;
于是,在Lua中就使用了一个GCObject的union将所有可gc类型囊括了进来:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
(lstate.h) 133 /* 134 ** Union of all collectable objects 135 */ 136 union GCObject { 137 GCheader gch; 138 union TString ts; 139 union Udata u; 140 union Closure cl; 141 struct Table h; 142 struct Proto p; 143 struct UpVal uv; 144 struct lua_State th; /* thread */ 145 };