AboutLua data source codeAnalysis is the content to be introduced in this article, mainly to understandLua source codeFor more information, see the implementation of the specific content.
LuaThe language does not need to declare the type of the variable, and the type is variable, the following statement:
- local a = 1;
- a = “hello”;
The data type starting with "a" is "number". When the data type is copied as a string, the data type is changed to string. You can view the data type through "type (. So how does it do it? See the following TValue definition:
- 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
All types in Lua are defined as TValue. Tt indicates the type. For definitions, see:
- #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
In the preceding definition, apart from the eight basic data types, the unknown type and light userdata are also included.LuaThe userdata pointer is saved, and the Occupied memory is notLuaManagement. Value indicates the specific Value of the variable, B indicates the integer type, and n indicates the floating point type. gc indicates the pointer of the object that can be used for garbage collection. When gc gets the gch Value, p should beLuaObject Pointer. Otherwise, only TValue itself may exist. The related definitions are as follows:
- 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
In the GCObject definition, gch is used for garbage collection; ts indicates the type used for string tables; utable indicates userdata; cl indicates closed functions; h indicates tables; p indicates functions; uv indicates upvalue; th indicates the thread, eachLua_ State is equivalent to a thread. The specific definition and comments are as follows:
- TString
- Typedef union TString {// lobject. h, line 200
- Rochelle umaxalign dummy;/* ensures maximum alignment for strings * // alignment
- Struct {
- CommonHeader;
- Lu_byte reserved;
- Unsigned int hash;
- Size_t len;
- } Tsv;
- } TString;
Udata indicates 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;
There are two types of Closure: one for lua and the other for C code.
- #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;
The function type used in C code is lua_CFunction, and the function used in lua is 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 * retriable; // metadata Table
- TValue * array;/* array part * // array, used when no index value exists
- Node * node; // node array
- Node * lastfree;/* any free position is before this position */
- GCObject * gclist;
- Int sizearray;/* size of 'array' array * // array size
- } Table;
-
- Proto
- Typedef struct Proto {
- CommonHeader; // for GC
- TValue * k;/* constants used by the function * // constant
- 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; // source code?
- 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; // number of parameters
- Lu_byte is_vararg; // whether it is a variable parameter
- Lu_byte maxstacksize; // stack used by the function?
- } 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;
Will be supplemented and analyzed in the futureData TypeThe exact purpose of each.
Summary: AboutLua data source codeThe analysis is complete. I hope this article will help you!