Source code analysis of Lua Data Types

Source: Internet
Author: User

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:

 
 
  1. local a = 1;  
  2. 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:

 
 
  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 

All types in Lua are defined as TValue. Tt indicates the type. For definitions, see:

 
 
  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 

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:

 
 
  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 

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:

 
 
  1. TString
  2. Typedef union TString {// lobject. h, line 200
  3. Rochelle umaxalign dummy;/* ensures maximum alignment for strings * // alignment
  4. Struct {
  5. CommonHeader;
  6. Lu_byte reserved;
  7. Unsigned int hash;
  8. Size_t len;
  9. } Tsv;
  10. } TString;

Udata indicates 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; 

There are two types of Closure: one for lua and the other for C code.

 
 
  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; 

The function type used in C code is lua_CFunction, and the function used in lua is 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 * retriable; // metadata Table
  7. TValue * array;/* array part * // array, used when no index value exists
  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 * // array size
  12. } Table;
  13.  
  14. Proto
  15. Typedef struct Proto {
  16. CommonHeader; // for GC
  17. TValue * k;/* constants used by the function * // constant
  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; // source code?
  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; // number of parameters
  35. Lu_byte is_vararg; // whether it is a variable parameter
  36. Lu_byte maxstacksize; // stack used by the function?
  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;

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.