From 63cb4fbbb961da133c68865845eaf22d9b876700 Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 27 Sep 2020 20:31:53 +0800 Subject: *misc --- src/lua51/lobject.h | 78 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 31 deletions(-) (limited to 'src/lua51/lobject.h') diff --git a/src/lua51/lobject.h b/src/lua51/lobject.h index ead9391..5d6df6c 100644 --- a/src/lua51/lobject.h +++ b/src/lua51/lobject.h @@ -43,7 +43,7 @@ typedef union GCObject GCObject; // 需要垃圾回收的类型包含这个头,包含TString, Udata, Proto, UpVal, Closure, Table以及lua_State七个 //c next 指向下一个gc链表的成员 //c tt 数据类型 -//c marked GC标记,用来保存颜色,有白色(2种),灰色和黑色 +//c marked GC标记,用一个字节进行标记,在lgc.h #define CommonHeader GCObject *next; lu_byte tt; lu_byte marked @@ -242,16 +242,16 @@ typedef struct Proto { CommonHeader; //c 常量表,能看出来lua保存常量的单元是函数原型,所有代码片段都会被编译为proto TValue *k; /* constants used by the function */ - //c 函数字节码起始点 + //c 这个函数的字节码 Instruction *code; - //c 内部函数 + //c 这个函数内嵌套的函数 struct Proto **p; /* functions defined inside the function */ int *lineinfo; /* map from opcodes to source lines */ - //c 局部变量 + //c 局部变量,在lparser.c > registerlocalvar注册 struct LocVar *locvars; /* information about local variables */ - //c upvalues + //c upvalue表 TString **upvalues; /* upvalue names */ - TString *source; //文件名 + TString *source; //c 文件名,只有顶层函数有,内嵌函数这个字段是空的 int sizeupvalues; int sizek; /* size of `k' */ int sizecode; @@ -276,7 +276,9 @@ typedef struct Proto { //c 局部变量 typedef struct LocVar { - TString *varname; + //c 变量名,只会在编译器用到,存在FuncState结构,用来查找局部变量ID + TString *varname; + //c int startpc; /* first point where variable is active */ int endpc; /* first point where variable is dead */ } LocVar; @@ -287,19 +289,30 @@ typedef struct LocVar { ** Upvalues */ //c 判断upvalue是关闭的方式是 uv->v == &uv->u.value -//c upvalue有开闭的概念 -//c 开是指upvalue完全由此方法所有,之前的调用已经结束 -//c 关闭是指upvalue还在之前的栈上 +// upvalue有开闭的概念 +// 开是指upvalue完全由此方法所有,之前的调用已经结束 +// 关闭是指upvalue还在之前的栈上 +typedef struct UpVal { + CommonHeader; + TValue *v; /* points to stack or to its own value */ //c upvalue的栈地址 + union { + TValue value; /* the value (when closed) */ + struct { /* double linked list (when open) */ + struct UpVal *prev; + struct UpVal *next; + } l; + } u; +} UpVal; /* -* function func() -* local a = 10; -* local b = function() -* a = a + 1 -* print(a) -* end +* function func() +* local a = 10; +* local b = function() +* a = a + 1 +* print(a) +* end * b() -- 这个是开 -* end -* +* end +* * function func() * local a = 10; * local b = function() @@ -307,22 +320,11 @@ typedef struct LocVar { * print(a) * end * return b -* end -* +* end +* * fn = func() * fn() -- 这个是关 */ -typedef struct UpVal { - CommonHeader; - TValue *v; /* points to stack or to its own value */ //c upvalue的栈地址 - union { - TValue value; /* the value (when closed) */ - struct { /* double linked list (when open) */ - struct UpVal *prev; - struct UpVal *next; - } l; - } u; -} UpVal; /* @@ -335,6 +337,13 @@ typedef struct UpVal { typedef struct CClosure { ClosureHeader; + /* + CommonHeader; //c gc header + lu_byte isC; //c is c closure + lu_byte nupvalues; //c number of upvalues + GCObject *gclist; //c gclist? + struct Table *env //c 这个闭包的环境 + */ lua_CFunction f; TValue upvalue[1]; } CClosure; @@ -342,6 +351,13 @@ typedef struct CClosure { typedef struct LClosure { ClosureHeader; + /* + CommonHeader; //c gc header + lu_byte isC; //c is c closure + lu_byte nupvalues; //c number of upvalues + GCObject *gclist; //c gclist? + struct Table *env //c 这个闭包的环境,在luaF_newLclosure设置 + */ struct Proto *p; // lua闭包的函数原型 UpVal *upvals[1]; // lua闭包的upvalue } LClosure; -- cgit v1.1-26-g67d0