由于lua版本迭代之间,数据结构和函数都会部分变化
以下是我分析的lua.h中的信息,以便确定版本

  1. #define LUA_VERSION_MAJOR "5"
  2. #define LUA_VERSION_MINOR "3"
  3. #define LUA_VERSION_NUM 503
  4. #define LUA_VERSION_RELEASE "3"
  1. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  2. /*
  3. ** Union of all collectable objects (only for conversions)
  4. */
  5. union GCUnion {
  6. GCObject gc; /* common header */
  7. struct TString ts;
  8. struct Udata u;
  9. union Closure cl;
  10. struct Table h;
  11. struct Proto p;
  12. struct lua_State th; /* thread */
  13. };

主要位于lobject.h中,部分位于lstate.h

以下和机器有关,比如在32位(64位)机器

  1. #define LUA_INT32 int
  2. #define LUAI_UMEM size_t
  3. #define LUAI_MEM ptrdiff_t

以下通用定义

  1. #define LUA_NUMBER_DOUBLE
  2. #define LUA_NUMBER double
  3. typedef unsigned char lu_byte;
  4. typedef TValue *StkId; /* index to stack elements */
  5. typedef struct lua_TValue TValue;
  6. typedef int (*lua_CFunction) (lua_State *L);
  7. typedef unsigned LUA_INT32 lu_int32;
  8. typedef LUAI_UMEM lu_mem;
  9. typedef LUAI_MEM l_mem;
  10. typedef lu_int32 Instruction;

保存lua数据的结构lua_TValue

lua_TValue 位于lobject.h

  1. //保存值和类型
  2. #define TValuefields Value value_; int tt_
  3. struct lua_TValue {
  4. TValuefields;
  5. };
  1. /*
  2. ** Union of all Lua values
  3. */
  4. typedef union Value {
  5. GCObject *gc; /* collectable objects */
  6. void *p; /* light userdata */
  7. int b; /* booleans */
  8. lua_CFunction f; /* light C functions 函数指针*/
  9. lua_Integer i; /* integer numbers */
  10. lua_Number n; /* float numbers 默认为double*/
  11. } Value;
  1. #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
  2. struct GCObject {
  3. CommonHeader;
  4. };
  1. /*
  2. ** Header for userdata; memory area follows the end of this structure
  3. ** (aligned according to 'UUdata'; see next).
  4. */
  5. typedef struct Udata {
  6. CommonHeader;
  7. lu_byte ttuv_; /* user value's tag */
  8. struct Table *metatable;
  9. size_t len; /* number of bytes */
  10. union Value user_; /* user value */
  11. } Udata;
  12. /*
  13. ** Ensures that address after this type is always fully aligned.
  14. */
  15. typedef union UUdata {
  16. L_Umaxalign dummy; /* ensures maximum alignment for 'local' udata */
  17. Udata uv;
  18. } UUdata;

string

  1. /*
  2. ** Header for string value; string bytes follow the end of this structure
  3. ** (aligned according to 'UTString'; see next).
  4. */
  5. typedef struct TString {
  6. CommonHeader;
  7. lu_byte extra; /* reserved words for short strings; "has hash" for longs */
  8. lu_byte shrlen; /* length for short strings */
  9. unsigned int hash;
  10. union {
  11. size_t lnglen; /* length for long strings */
  12. struct TString *hnext; /* linked list for hash table */
  13. } u;
  14. } TString;
  15. /*
  16. ** Ensures that address after this type is always fully aligned.
  17. */
  18. typedef union UTString {
  19. L_Umaxalign dummy; /* ensures maximum alignment for strings */
  20. TString tsv;
  21. } UTString;
  1. //闭包相关
  2. #define ClosureHeader CommonHeader; lu_byte nupvalues; GCObject *gclist
  3. typedef struct CClosure {
  4. ClosureHeader;
  5. lua_CFunction f;
  6. TValue upvalue[1]; /* list of upvalues */
  7. } CClosure;
  8. typedef struct LClosure {
  9. ClosureHeader;
  10. struct Proto *p;
  11. UpVal *upvals[1]; /* list of upvalues */
  12. } LClosure;
  13. typedef union Closure {
  14. CClosure c;
  15. LClosure l;
  16. } Closure;
  1. //表相关
  2. typedef union TKey {
  3. struct {
  4. TValuefields;
  5. struct Node *next; /* for chaining */
  6. } nk;
  7. TValue tvk;
  8. } TKey;
  9. typedef struct Node {
  10. TValue i_val;
  11. TKey i_key;
  12. } Node;
  13. typedef struct Table {
  14. CommonHeader;
  15. lu_byte flags; /* 1<<p means tagmethod(p) is not present */
  16. lu_byte lsizenode; /* log2 of size of `node' array */
  17. int sizearray; /* size of `array' array */
  18. TValue *array; /* array part */
  19. Node *node;
  20. Node *lastfree; /* any free position is before this position */
  21. struct Table *metatable;
  22. GCObject *gclist;
  23. } Table;
/*
** Description of an upvalue for function prototypes
*/
typedef struct Upvaldesc {
  TString *name;  /* upvalue name (for debug information) */
  lu_byte instack;  /* whether it is in stack */
  lu_byte idx;  /* index of upvalue (in stack or in outer function's list) */
} Upvaldesc;


/*
** Description of a local variable for function prototypes
** (used for debug information)
*/
typedef struct LocVar {
  TString *varname;
  int startpc;  /* first point where variable is active */
  int endpc;    /* first point where variable is dead */
} LocVar;


/*
** Function Prototypes
*/
typedef struct Proto {
  CommonHeader;
  TValue *k;  /* constants used by the function */
  Instruction *code;
  struct Proto **p;  /* functions defined inside the function */
  int *lineinfo;  /* map from opcodes to source lines (debug information) */
  LocVar *locvars;  /* information about local variables (debug information) */
  Upvaldesc *upvalues;  /* upvalue information */
  union Closure *cache;  /* last created closure with this prototype */
  TString  *source;  /* used for debug information */
  int sizeupvalues;  /* size of 'upvalues' */
  int sizek;  /* size of `k' */
  int sizecode;
  int sizelineinfo;
  int sizep;  /* size of `p' */
  int sizelocvars;
  int linedefined;
  int lastlinedefined;
  GCObject *gclist;
  lu_byte numparams;  /* number of fixed parameters */
  lu_byte is_vararg;
  lu_byte maxstacksize;  /* maximum stack used by this function */
} Proto;
//上值
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;