diff options
| author | chai <chaifix@163.com> | 2020-07-20 09:42:30 +0800 |
|---|---|---|
| committer | chai <chaifix@163.com> | 2020-07-20 09:42:30 +0800 |
| commit | 77ac95b9985f5669d6659bfb54728786d28c2ef0 (patch) | |
| tree | ab42f7da14ffbd9ba72f503baff71b44298b113a /src/lua51/lobject.h | |
| parent | c5d9668a1b7092262b7132679e961a5297da2f75 (diff) | |
*misc
Diffstat (limited to 'src/lua51/lobject.h')
| -rw-r--r-- | src/lua51/lobject.h | 62 |
1 files changed, 48 insertions, 14 deletions
diff --git a/src/lua51/lobject.h b/src/lua51/lobject.h index f1e447e..af6b63a 100644 --- a/src/lua51/lobject.h +++ b/src/lua51/lobject.h @@ -40,6 +40,10 @@ typedef union GCObject GCObject; ** Common Header for all collectable objects (in macro form, to be ** included in other objects) */ +// 需要垃圾回收的类型包含这个头 +//c next 指向下一个gc链表的成员 +//c tt 数据类型 +//c GC标记,用来保存颜色,有白色(2种),灰色和黑色 #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked @@ -53,6 +57,8 @@ typedef struct GCheader { +//c lua_TValue下第二高级的数据封装类型,不含数据类型标识 +//c 包含需要GC的类型和不需要GC的类型 /* ** Union of all Lua values */ @@ -68,8 +74,10 @@ typedef union { ** Tagged Values */ +//c 用在两个地方,lua_TValue和TKey #define TValuefields Value value; int tt +//c 最上层的Value类型,包含值和类型 typedef struct lua_TValue { TValuefields; } TValue; @@ -112,7 +120,7 @@ typedef struct lua_TValue { lua_assert(!iscollectable(obj) || \ ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) - +//c 将TValue结构具体为对应的类型和值;即修改TValue的tt和value部分 /* Macros to set values */ #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) @@ -193,6 +201,9 @@ typedef struct lua_TValue { typedef TValue *StkId; /* index to stack elements */ +//c 字符串 +//c 每个字符串都需要计算hash,用来比较和查找字符串 +//c 如果这个字符串已经存在,不再生成新的字符串,而是使用旧的 /* ** String headers for string table */ @@ -200,8 +211,8 @@ typedef union TString { L_Umaxalign dummy; /* ensures maximum alignment for strings */ struct { CommonHeader; - lu_byte reserved; - unsigned int hash; + lu_byte reserved;//标记字符串是否是lua的关键字,如果是,不会被GC回收 + unsigned int hash;//字符串的哈希值,比较字符串的依据 size_t len; } tsv; } TString; @@ -210,8 +221,7 @@ typedef union TString { #define getstr(ts) cast(const char *, (ts) + 1) #define svalue(o) getstr(rawtsvalue(o)) - - +// userdata,和TString比较像 typedef union Udata { L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ struct { @@ -223,18 +233,22 @@ typedef union Udata { } Udata; - - +//c! 函数原型,是沟通前端和后端(分析阶段和执行阶段)的数据 +//c! 主要有 字节码、常量、局部变量、upvalue /* ** Function Prototypes */ typedef struct Proto { CommonHeader; + //c 函数的常量 TValue *k; /* constants used by the function */ + //c 函数字节码 Instruction *code; struct Proto **p; /* functions defined inside the function */ int *lineinfo; /* map from opcodes to source lines */ + //c 局部变量 struct LocVar *locvars; /* information about local variables */ + //c upvalue TString **upvalues; /* upvalue names */ TString *source; int sizeupvalues; @@ -259,6 +273,7 @@ typedef struct Proto { #define VARARG_NEEDSARG 4 +//c 局部变量 typedef struct LocVar { TString *varname; int startpc; /* first point where variable is active */ @@ -312,6 +327,7 @@ typedef union Closure { } Closure; +//c 判断函数类型,5.2通过高位判断,在基础类型基础上产生变体 variant #define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) #define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) @@ -319,36 +335,54 @@ typedef union Closure { /* ** Tables */ - +//c 表的某个元素的key typedef union TKey { + //nk 用来拿到下一个node的指针 struct { - TValuefields; + TValuefields; // 就是TValue + //c 指向下一个node struct Node *next; /* for chaining */ - } nk; - TValue tvk; + } nk; // next key + //tvk 用来拿本node的key值 + TValue tvk; // key value } TKey; - +//c 表的单个元素 typedef struct Node { - TValue i_val; - TKey i_key; + TValue i_val;//value + TKey i_key; //key } Node; +//c 表 typedef struct Table { CommonHeader; + //c 标记这个表有哪些元方法,第一次查找时为0,如果有某个元方法,将对应位置为1 + //c 下次查找时就不需要查找字符串了 + //c flag会在luaH_set清空 lu_byte flags; /* 1<<p means tagmethod(p) is not present */ + //c lsizenode=log2(length of hash table) 由此可知散列表大小一定是2的幂 + //c 所以如果散列表要扩展,在原大小基础上扩展一倍 + //c 要得到散列表大小,只需要移位 length of hash table = 1 << lsizenode lu_byte lsizenode; /* log2 of size of `node' array */ + //c 该表的元表 struct Table *metatable; + //c 数组部分 TValue *array; /* array part */ + //c 散列桶起始位置 + //c 多个桶,桶内通过TKey->nk->next连接 Node *node; + //c 空位置 Node *lastfree; /* any free position is before this position */ + //c 在global_State中有关gc的链表的下一个(如果这个对象被加入了的话) GCObject *gclist; + //c 数组部分的大小 int sizearray; /* size of `array' array */ } Table; +//c 计算哈希值,用在很多地方 /* ** `module' operation for hashing (size is always a power of 2) */ |
