summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lua51/lapi.c16
-rw-r--r--src/lua51/lauxlib.c13
-rw-r--r--src/lua51/lauxlib.h1
-rw-r--r--src/lua51/lbaselib.c9
-rw-r--r--src/lua51/ldebug.c1
-rw-r--r--src/lua51/ldo.c11
-rw-r--r--src/lua51/lgc.c2
-rw-r--r--src/lua51/loadlib.c14
-rw-r--r--src/lua51/lobject.h30
-rw-r--r--src/lua51/lstate.h2
-rw-r--r--src/lua51/ltable.c2
-rw-r--r--src/lua51/ltm.c2
-rw-r--r--src/lua51/lua.h15
-rw-r--r--src/lua51/lvm.c3
14 files changed, 84 insertions, 37 deletions
diff --git a/src/lua51/lapi.c b/src/lua51/lapi.c
index b640e66..cfcffcd 100644
--- a/src/lua51/lapi.c
+++ b/src/lua51/lapi.c
@@ -45,18 +45,29 @@ const char lua_ident[] =
#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
-
+//c 根据idx到不同的地方取值
static TValue *index2adr (lua_State *L, int idx) {
- if (idx > 0) {
+ // 对于(0, ∞)和(LUA_REGISTRYINDEX, 0)的索引到当前栈上取
+ // 对于特殊idx分别返回注册表、环境表、全局表
+ // 对于(-∞, LUA_GLOBALSINDEX)的idx,返回upvalue值
+
+ //1. 正数,到当前函数的栈上取,相对栈底向上的偏移值
+ if (idx > 0) {
TValue *o = L->base + (idx - 1);
api_check(L, idx <= L->ci->top - L->base);
if (o >= L->top) return cast(TValue *, luaO_nilobject);
else return o;
}
+ //2. 负数,相对栈顶向下的偏移值
else if (idx > LUA_REGISTRYINDEX) {
api_check(L, idx != 0 && -idx <= L->top - L->base);
return L->top + idx;
}
+ //3. 对特殊索引进行处理
+ // 1) LUA_REGISTRYINDEX 返回注册表
+ // 2) LUA_ENVIRONINDEX 返回环境表
+ // 3) LUA_GLOBALSINDEX 返回全局表
+ // 4) 返回upvalue中的值
else switch (idx) { /* pseudo-indices */
case LUA_REGISTRYINDEX: return registry(L);
case LUA_ENVIRONINDEX: {
@@ -107,6 +118,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) {
}
+//c 协程通信,从from协程中移动n个数据到to协程
LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
int i;
if (from == to) return;
diff --git a/src/lua51/lauxlib.c b/src/lua51/lauxlib.c
index c6ec9b0..83fc2ac 100644
--- a/src/lua51/lauxlib.c
+++ b/src/lua51/lauxlib.c
@@ -24,7 +24,7 @@
#include "lauxlib.h"
-
+//c t[FREELIST_REF] 保存可以用luaL_ref引用的索引号
#define FREELIST_REF 0 /* free list of references */
@@ -240,7 +240,7 @@ static int libsize (const luaL_Reg *l) {
LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
- const luaL_Reg *l, int nup) {
+ const luaL_Reg *l, int nup) { // nup number of upvalues
if (libname) {
int size = libsize(l);
/* check whether lib already exists */
@@ -478,14 +478,18 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
/* }====================================================== */
+//c t是目标表的Idx,数据在-1
LUALIB_API int luaL_ref (lua_State *L, int t) {
+// 数据在栈顶
+
+ // 1. 拿到ref号
int ref;
t = abs_index(L, t);
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* remove from stack */
return LUA_REFNIL; /* `nil' has a unique fixed reference */
}
- lua_rawgeti(L, t, FREELIST_REF); /* get first free element */
+ lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ // table的[FREELIST_REF]位置会保存可用的引用索引号,FREELIST_REF = 0
ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */
lua_pop(L, 1); /* remove it from stack */
if (ref != 0) { /* any free element? */
@@ -496,7 +500,10 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
ref = (int)lua_objlen(L, t);
ref++; /* create new reference */
}
+
+ // 2. t[ref] = o 引用
lua_rawseti(L, t, ref);
+
return ref;
}
diff --git a/src/lua51/lauxlib.h b/src/lua51/lauxlib.h
index 51c9528..f2e089e 100644
--- a/src/lua51/lauxlib.h
+++ b/src/lua51/lauxlib.h
@@ -172,4 +172,3 @@ LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
#endif
-
diff --git a/src/lua51/lbaselib.c b/src/lua51/lbaselib.c
index 2ab550b..b4f0b8f 100644
--- a/src/lua51/lbaselib.c
+++ b/src/lua51/lbaselib.c
@@ -140,6 +140,7 @@ static int luaB_getfenv (lua_State *L) {
}
+//c 设置闭包的env
static int luaB_setfenv (lua_State *L) {
luaL_checktype(L, 2, LUA_TTABLE);
getfunc(L, 0);
@@ -515,6 +516,7 @@ static int luaB_costatus (lua_State *L) {
}
+//c coroutine.resume
static int auxresume (lua_State *L, lua_State *co, int narg) {
int status = costatus(L, co);
if (!lua_checkstack(co, narg))
@@ -539,12 +541,13 @@ static int auxresume (lua_State *L, lua_State *co, int narg) {
}
}
-
+//c coroutine.resume()
static int luaB_coresume (lua_State *L) {
lua_State *co = lua_tothread(L, 1);
int r;
luaL_argcheck(L, co, 1, "coroutine expected");
r = auxresume(L, co, lua_gettop(L) - 1);
+// 根据auxresume返回false+错误信息或者true+返回值
if (r < 0) {
lua_pushboolean(L, 0);
lua_insert(L, -2);
@@ -558,6 +561,7 @@ static int luaB_coresume (lua_State *L) {
}
+//c wrap coroutine
static int luaB_auxwrap (lua_State *L) {
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
int r = auxresume(L, co, lua_gettop(L));
@@ -573,8 +577,9 @@ static int luaB_auxwrap (lua_State *L) {
}
+//c 创建协程
static int luaB_cocreate (lua_State *L) {
- lua_State *NL = lua_newthread(L);
+ lua_State *NL = lua_newthread(L); //创建协程
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected");
lua_pushvalue(L, 1); /* move function to top */
diff --git a/src/lua51/ldebug.c b/src/lua51/ldebug.c
index 50ad3d3..50fa8b7 100644
--- a/src/lua51/ldebug.c
+++ b/src/lua51/ldebug.c
@@ -50,6 +50,7 @@ static int currentline (lua_State *L, CallInfo *ci) {
}
+//c 设置钩子函数
/*
** this function can be called asynchronous (e.g. during a signal)
*/
diff --git a/src/lua51/ldo.c b/src/lua51/ldo.c
index 736697d..dc4451a 100644
--- a/src/lua51/ldo.c
+++ b/src/lua51/ldo.c
@@ -183,6 +183,7 @@ static CallInfo *growCI (lua_State *L) {
}
+//c 调用钩子函数
void luaD_callhook (lua_State *L, int event, int line) {
lua_Hook hook = L->hook;
if (hook && L->allowhook) {
@@ -310,7 +311,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
setnilvalue(st);
L->top = ci->top; //c top一致
- if (L->hookmask & LUA_MASKCALL) {
+ if (L->hookmask & LUA_MASKCALL) { // 触发“函数调用”hook
L->savedpc++; /* hooks assume 'pc' is already incremented */
luaD_callhook(L, LUA_HOOKCALL, -1);
L->savedpc--; /* correct 'pc' */
@@ -334,7 +335,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
ci->top = L->top + LUA_MINSTACK;//c 设置当前调用的最大数据栈容量
lua_assert(ci->top <= L->stack_last); //c 调用的栈顶不能超过lua数据栈的上限
ci->nresults = nresults;
- if (L->hookmask & LUA_MASKCALL)
+ if (L->hookmask & LUA_MASKCALL) // 触发“函数调用”hook
luaD_callhook(L, LUA_HOOKCALL, -1);
lua_unlock(L);
n = (*curr_func(L)->c.f)(L); /* do the actual call */
@@ -353,7 +354,7 @@ static StkId callrethooks (lua_State *L, StkId firstResult) {
ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */
luaD_callhook(L, LUA_HOOKRET, -1);
if (f_isLua(L->ci)) { /* Lua function? */
- while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */
+ while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ // 触发函数返回hook
luaD_callhook(L, LUA_HOOKTAILRET, -1);
}
return restorestack(L, fr);
@@ -367,7 +368,7 @@ int luaD_poscall (lua_State *L, StkId firstResult) {
int wanted, i;
//c 本次调用的ci
CallInfo *ci;
- if (L->hookmask & LUA_MASKRET)
+ if (L->hookmask & LUA_MASKRET) // 触发函数返回hook
firstResult = callrethooks(L, firstResult);
ci = L->ci--;
res = ci->func; /* res == final position of 1st result */
@@ -409,6 +410,7 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
}
+//c
static void resume (lua_State *L, void *ud) {
StkId firstArg = cast(StkId, ud);
CallInfo *ci = L->ci;
@@ -469,6 +471,7 @@ LUA_API int lua_resume (lua_State *L, int nargs) {
}
+//c coroutine.yield
LUA_API int lua_yield (lua_State *L, int nresults) {
luai_userstateyield(L, nresults);
lua_lock(L);
diff --git a/src/lua51/lgc.c b/src/lua51/lgc.c
index afebe04..3da447b 100644
--- a/src/lua51/lgc.c
+++ b/src/lua51/lgc.c
@@ -879,7 +879,7 @@ void luaC_linkupval (lua_State *L, UpVal *uv) {
g->rootgc = o;
// 这里和普通对象不一样
- if (isgray(o)) { // 在扫描阶段realymarkobject标记为了灰色,
+ if (isgray(o)) {// 在扫描阶段realymarkobject标记为了灰色,
if (g->gcstate == GCSpropagate) {//如果在扫描阶段,直接标记为黑色
log("GCSpropagate");
gray2black(o); /* closed upvalues need barrier */
diff --git a/src/lua51/loadlib.c b/src/lua51/loadlib.c
index a7c68c6..b55f9b5 100644
--- a/src/lua51/loadlib.c
+++ b/src/lua51/loadlib.c
@@ -526,6 +526,7 @@ static void dooptions (lua_State *L, int n) {
}
+//c 设置模块的特殊域 _M _NAME _PACKAGE
static void modinit (lua_State *L, const char *modname) {
const char *dot;
lua_pushvalue(L, -1);
@@ -541,6 +542,7 @@ static void modinit (lua_State *L, const char *modname) {
}
+//c 创建模块即module("")
static int ll_module (lua_State *L) {
const char *modname = luaL_checkstring(L, 1);
int loaded = lua_gettop(L) + 1; /* index of _LOADED table */
@@ -563,12 +565,13 @@ static int ll_module (lua_State *L) {
modinit(L, modname);
}
lua_pushvalue(L, -1);
- setfenv(L);
+ setfenv(L); // 注意这里会将模块的环境设为当前模块的表,所以外面的全局变量将会在后续无法访问到,除非开启packge.seeall
dooptions(L, loaded - 1);
return 0;
}
+//c 让这个模块的代码能够访问_G表,通过给模块设置一个元表,__index指向_G
static int ll_seeall (lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
if (!lua_getmetatable(L, 1)) {
@@ -624,12 +627,19 @@ static const lua_CFunction loaders[] =
{loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
+
+//c package模块 , 包含
+// loaded 表,即registry["_LOADED"]
+// preload 表
+//
LUALIB_API int luaopen_package (lua_State *L) {
int i;
+
/* create new type _LOADLIB */
luaL_newmetatable(L, "_LOADLIB");
lua_pushcfunction(L, gctm);
lua_setfield(L, -2, "__gc");
+
/* create `package' table */
luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
#if defined(LUA_COMPAT_LOADLIB)
@@ -638,6 +648,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
#endif
lua_pushvalue(L, -1);
lua_replace(L, LUA_ENVIRONINDEX);
+
/* create `loaders' table */
lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
/* fill it with pre-defined loaders */
@@ -648,6 +659,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */
setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */
setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
+
/* store config information */
lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
LUA_EXECDIR "\n" LUA_IGMARK);
diff --git a/src/lua51/lobject.h b/src/lua51/lobject.h
index 157fb37..ff63e2d 100644
--- a/src/lua51/lobject.h
+++ b/src/lua51/lobject.h
@@ -345,7 +345,7 @@ typedef struct CClosure {
struct Table *env //c 这个闭包的环境
*/
lua_CFunction f;
- TValue upvalue[1];
+ TValue upvalue[1]; // upvalue
} CClosure;
@@ -359,7 +359,7 @@ typedef struct LClosure {
struct Table *env //c 这个闭包的环境表,在luaF_newLclosure设置
*/
struct Proto *p; // lua闭包的函数原型
- UpVal *upvals[1]; // lua闭包的upvalue
+ UpVal *upvals[1]; // lua闭包的upvalue变长数组
} LClosure;
@@ -399,26 +399,26 @@ typedef struct Node {
//c 表
typedef struct Table {
CommonHeader;
- //c 标记这个表有哪些元方法,第一次查找时为0,如果有某个元方法,将对应位置为1
- //c 下次查找时就不需要查找字符串了
- //c flag会在luaH_set清空
+ // 标记这个表有哪些元方法,第一次查找时为0,如果有某个元方法,将对应位置为1
+ // 下次查找时就不需要查找字符串了
+ // 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
+ // lsizenode=log2(length of hash table) 由此可知散列表大小一定是2的幂
+ // 所以如果散列表要扩展,在原大小基础上扩展一倍
+ // 要得到散列表大小,只需要移位 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连接
+ // 散列桶起始位置
+ // 多个桶,桶内通过TKey->nk->next连接
Node *node;
- //c 空位置
+ // 空闲位置
Node *lastfree; /* any free position is before this position */
- //c 在global_State中有关gc的链表的下一个(如果这个对象被加入了的话)
+ // 在global_State中有关gc的链表的下一个(如果这个对象被加入了的话)
GCObject *gclist;
- //c 数组部分的大小
+ // 数组部分的大小
int sizearray; /* size of `array' array */
} Table;
diff --git a/src/lua51/lstate.h b/src/lua51/lstate.h
index cb4244b..7a776bc 100644
--- a/src/lua51/lstate.h
+++ b/src/lua51/lstate.h
@@ -172,7 +172,7 @@ struct lua_State {
int hookcount;
lua_Hook hook;
TValue l_gt; /* table of globals */ //全局表 _G global table
- TValue env; /* temporary place for environments */
+ TValue env; /* temporary place for environments */ // 协程私有的环境
GCObject *openupval; /* list of open upvalues in this stack */
GCObject *gclist;
struct lua_longjmp *errorJmp; /* current error recover point */
diff --git a/src/lua51/ltable.c b/src/lua51/ltable.c
index 20fde8e..2147642 100644
--- a/src/lua51/ltable.c
+++ b/src/lua51/ltable.c
@@ -586,7 +586,7 @@ static int unbound_search (Table *t, unsigned int j) {
}
-//c 获得table长度
+//c 获得table长度(仅数组部分)
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
diff --git a/src/lua51/ltm.c b/src/lua51/ltm.c
index c27f0f6..60e9f41 100644
--- a/src/lua51/ltm.c
+++ b/src/lua51/ltm.c
@@ -1,3 +1,5 @@
+// 元表处理
+
/*
** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
** Tag methods
diff --git a/src/lua51/lua.h b/src/lua51/lua.h
index 9f45735..75a8f19 100644
--- a/src/lua51/lua.h
+++ b/src/lua51/lua.h
@@ -30,6 +30,7 @@
#define LUA_MULTRET (-1)
+//c 特殊的表索引
/*
** pseudo-indices
*/
@@ -319,10 +320,10 @@ LUA_API void lua_setlevel (lua_State *from, lua_State *to);
/*
** Event masks
*/
-#define LUA_MASKCALL (1 << LUA_HOOKCALL)
-#define LUA_MASKRET (1 << LUA_HOOKRET)
-#define LUA_MASKLINE (1 << LUA_HOOKLINE)
-#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
+#define LUA_MASKCALL (1 << LUA_HOOKCALL) // 函数调用时触发
+#define LUA_MASKRET (1 << LUA_HOOKRET) // 函数返回时触发
+#define LUA_MASKLINE (1 << LUA_HOOKLINE) // 每执行一个指令触发
+#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) // 每执行n个指令触发
typedef struct lua_Debug lua_Debug; /* activation record */
@@ -344,9 +345,11 @@ LUA_API int lua_gethookmask (lua_State *L);
LUA_API int lua_gethookcount (lua_State *L);
+//c 当前执行信息,用来调试程序
+// 通过lua_getinfo能够得到信息
struct lua_Debug {
- int event;
- const char *name; /* (n) */
+ int event; // hook事件
+ const char *name; /* (n) */ // 当前函数名
const char *namewhat; /* (n) `global', `local', `field', `method' */
const char *what; /* (S) `Lua', `C', `main', `tail' */
const char *source; /* (S) */
diff --git a/src/lua51/lvm.c b/src/lua51/lvm.c
index 374dcd3..500ca4a 100644
--- a/src/lua51/lvm.c
+++ b/src/lua51/lvm.c
@@ -401,6 +401,8 @@ reentry: /* entry point */
for (;;) {
const Instruction i = *pc++;
StkId ra;
+
+ // 触发hook
if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
(--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
traceexec(L, pc);
@@ -410,6 +412,7 @@ reentry: /* entry point */
}
base = L->base;
}
+
/* warning!! several calls may realloc the stack and invalidate `ra' */
ra = RA(i); //c 从指令中拿到ra
lua_assert(base == L->base && L->base == L->ci->base);