/* weet: * 1. 将idx所在位置设置为当前栈顶元素 * 2. 栈顶指针下移一个 * */ LUA_API void lua_replace (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); setobj(luaA_index(L, idx), L->top - 1); /* write barrier */ L->top--; lua_unlock(L); }
LUA_API int lua_getn (lua_State *L, int index) { StkId t; const TObject *value; int n; lua_lock(L); t = luaA_index(L, index); api_check(L, ttype(t) == LUA_TTABLE); value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */ if (ttype(value) == LUA_TNUMBER) lua_number2int(n, nvalue(value)); else { Node *nd; Table *a = hvalue(t); lua_Number max = 0; int i; i = a->sizearray; while (i--) { if (ttype(&a->array[i]) != LUA_TNIL) break; } max = i+1; i = sizenode(a); nd = a->node; while (i--) { if (ttype(gkey(nd)) == LUA_TNUMBER && ttype(gval(nd)) != LUA_TNIL && nvalue(gkey(nd)) > max) max = nvalue(gkey(nd)); nd++; } lua_number2int(n, max); } lua_unlock(L); return n; }
LUA_API int lua_next (lua_State *L, int index) { StkId t = luaA_index(L, index); Node *n; LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); n = luaH_next(L, hvalue(t), luaA_index(L, -1)); if (n) { *(L->top-1) = *key(n); *L->top = *val(n); api_incr_top(L); return 1; } else { /* no more elements */ L->top -= 1; /* remove key */ return 0; } }
LUA_API void lua_gettable (lua_State *L, int idx) { StkId t; lua_lock(L); t = luaA_index(L, idx); setobj2s(L->top - 1, luaV_gettable(L, t, L->top - 1, 0)); lua_unlock(L); }
LUA_API int lua_setmetatable (lua_State *L, int objindex) { TObject *obj, *mt; int res = 1; lua_lock(L); api_checknelems(L, 1); obj = luaA_index(L, objindex); mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); api_check(L, ttistable(mt)); switch (ttype(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = hvalue(mt); /* write barrier */ break; } case LUA_TUSERDATA: { uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */ break; } default: { res = 0; /* cannot set */ break; } } L->top--; lua_unlock(L); return res; }
LUA_API void lua_insert (lua_State *L, int index) { StkId p = luaA_index(L, index); StkId q; for (q = L->top; q>p; q--) *q = *(q-1); *p = *L->top; }
LUA_API void lua_rawget (lua_State *L, int idx) { StkId t; lua_lock(L); t = luaA_index(L, idx); api_check(L, ttistable(t)); setobj2s(L->top - 1, luaH_get(hvalue(t), L->top - 1)); lua_unlock(L); }
LUA_API void lua_getfenv (lua_State *L, int idx) { StkId o; lua_lock(L); o = luaA_index(L, idx); setobj2s(L->top, isLfunction(o) ? &clvalue(o)->l.g : gt(L)); api_incr_top(L); lua_unlock(L); }
LUA_API void lua_remove (lua_State *L, int idx) { StkId p; lua_lock(L); p = luaA_index(L, idx); while (++p < L->top) setobjs2s(p-1, p); L->top--; lua_unlock(L); }
LUA_API void lua_settable (lua_State *L, int idx) { StkId t; lua_lock(L); api_checknelems(L, 2); t = luaA_index(L, idx); luaV_settable(L, t, L->top - 2, L->top - 1); L->top -= 2; /* pop index and value */ lua_unlock(L); }
/* weet: * 从idx 到(L->top - 1)的元素向上移动一个位置 * */ LUA_API void lua_insert (lua_State *L, int idx) { StkId p; StkId q; lua_lock(L); p = luaA_index(L, idx); for (q = L->top; q>p; q--) setobjs2s(q, q-1); setobjs2s(p, L->top); lua_unlock(L); }
/* weet: * 移除位于idx的栈元素 * */ LUA_API void lua_remove (lua_State *L, int idx) { StkId p; lua_lock(L); p = luaA_index(L, idx); // weet: 那个被删除元素的内存是在什么时候被释放的呢? 需要通过什么条件来判断? while (++p < L->top) setobjs2s(p-1, p); L->top--; lua_unlock(L); }
LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { StkId o; lua_lock(L); o = luaA_index(L, idx); api_check(L, ttistable(o)); setobj2s(L->top, luaH_getnum(hvalue(o), n)); api_incr_top(L); lua_unlock(L); }
LUA_API void lua_rawset (lua_State *L, int idx) { StkId t; lua_lock(L); api_checknelems(L, 2); t = luaA_index(L, idx); api_check(L, ttistable(t)); setobj2t(luaH_set(L, hvalue(t), L->top-2), L->top-1); /* write barrier */ L->top -= 2; lua_unlock(L); }
LUA_API void lua_rawseti (lua_State *L, int idx, int n) { StkId o; lua_lock(L); api_checknelems(L, 1); o = luaA_index(L, idx); api_check(L, ttistable(o)); setobj2t(luaH_setnum(L, hvalue(o), n), L->top-1); /* write barrier */ L->top--; lua_unlock(L); }
LUA_API void lua_settable (lua_State *L, int idx) { StkId t; lua_lock(L); api_checknelems(L, 2); t = luaA_index(L, idx); luaV_settable(L, t, L->top - 2, L->top - 1); #if LUA_REFCOUNT setnilvalue(--L->top); setnilvalue(--L->top); #else !LUA_REFCOUNT L->top -= 2; /* pop index and value */ #endif LUA_REFCOUNT lua_unlock(L); }
LUA_API void lua_rawseti (lua_State *L, int idx, int n) { StkId o; lua_lock(L); api_checknelems(L, 1); o = luaA_index(L, idx); api_check(L, ttistable(o)); setobj2t(luaH_setnum(L, hvalue(o), n), L->top-1); /* write barrier */ #if LUA_REFCOUNT setnilvalue(--L->top); #else !LUA_REFCOUNT L->top--; #endif LUA_REFCOUNT lua_unlock(L); }
LUA_API int lua_setfenv (lua_State *L, int idx) { StkId o; int res = 0; lua_lock(L); api_checknelems(L, 1); o = luaA_index(L, idx); L->top--; api_check(L, ttistable(L->top)); if (isLfunction(o)) { res = 1; clvalue(o)->l.g = *(L->top); } lua_unlock(L); return res; }
LUA_API void lua_rawset (lua_State *L, int idx) { StkId t; lua_lock(L); api_checknelems(L, 2); t = luaA_index(L, idx); api_check(L, ttistable(t)); setobj2t(luaH_set(L, hvalue(t), L->top-2), L->top-1); /* write barrier */ #if LUA_REFCOUNT setnilvalue(--L->top); setnilvalue(--L->top); #else !LUA_REFCOUNT L->top -= 2; #endif LUA_REFCOUNT lua_unlock(L); }
LUA_API int lua_getn (lua_State *L, int index) { Hash *h = hvalue(luaA_index(L, index)); const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ if (ttype(value) == LUA_TNUMBER) return (int)nvalue(value); else { Number max = 0; int i = h->size; Node *n = h->node; while (i--) { if (ttype(key(n)) == LUA_TNUMBER && ttype(val(n)) != LUA_TNIL && nvalue(key(n)) > max) max = nvalue(key(n)); n++; } return (int)max; } }
LUA_API int lua_setmetatable (lua_State *L, int objindex) { TObject *obj, *mt; int res = 1; lua_lock(L); api_checknelems(L, 1); obj = luaA_index(L, objindex); mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); api_check(L, ttistable(mt)); switch (ttype(obj)) { case LUA_TTABLE: { #if LUA_REFCOUNT __AddRefDirect(hvalue(mt)); __ReleaseDirect(L, hvalue(obj)->metatable); #endif LUA_REFCOUNT hvalue(obj)->metatable = hvalue(mt); /* write barrier */ break; } case LUA_TUSERDATA: { #if LUA_REFCOUNT __AddRefDirect(hvalue(mt)); __ReleaseDirect(L, &uvalue(obj)->uv.metatable); #endif LUA_REFCOUNT uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */ break; } default: { res = 0; /* cannot set */ break; } } #if LUA_REFCOUNT setnilvalue(--L->top); #else !LUA_REFCOUNT L->top--; #endif LUA_REFCOUNT lua_unlock(L); return res; }
/* weet: * 1. 将idx所在位置的元素复制到栈顶 * 2. 增加栈顶指针 * */ LUA_API void lua_pushvalue (lua_State *L, int idx) { lua_lock(L); setobj2s(L->top, luaA_index(L, idx)); api_incr_top(L); lua_unlock(L); }
LUA_API void lua_pushvalue (lua_State *L, int index) { *L->top = *luaA_index(L, index); api_incr_top(L); }
LUA_API void lua_remove (lua_State *L, int index) { StkId p = luaA_index(L, index); while (++p < L->top) *(p-1) = *p; L->top--; }