diff options
Diffstat (limited to 'src/lua51/lapi.c')
-rw-r--r-- | src/lua51/lapi.c | 16 |
1 files changed, 14 insertions, 2 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; |