diff options
Diffstat (limited to 'src/lapi.c')
-rw-r--r-- | src/lapi.c | 214 |
1 files changed, 84 insertions, 130 deletions
@@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.219 2014/06/19 18:27:20 roberto Exp $ +** $Id: lapi.c,v 2.232 2014/07/30 14:00:14 roberto Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -44,32 +44,35 @@ const char lua_ident[] = /* test for pseudo index */ #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX) +/* test for upvalue */ +#define isupvalue(i) ((i) < LUA_REGISTRYINDEX) + /* test for valid but not pseudo index */ #define isstackindex(i, o) (isvalid(o) && !ispseudo(i)) -#define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index") +#define api_checkvalidindex(o) api_check(isvalid(o), "invalid index") -#define api_checkstackindex(L, i, o) \ - api_check(L, isstackindex(i, o), "index not in the stack") +#define api_checkstackindex(i, o) \ + api_check(isstackindex(i, o), "index not in the stack") static TValue *index2addr (lua_State *L, int idx) { CallInfo *ci = L->ci; if (idx > 0) { TValue *o = ci->func + idx; - api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index"); + api_check(idx <= ci->top - (ci->func + 1), "unacceptable index"); if (o >= L->top) return NONVALIDVALUE; else return o; } else if (!ispseudo(idx)) { /* negative index */ - api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); + api_check(idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index"); return L->top + idx; } else if (idx == LUA_REGISTRYINDEX) return &G(L)->l_registry; else { /* upvalues */ idx = LUA_REGISTRYINDEX - idx; - api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large"); + api_check(idx <= MAXUPVAL + 1, "upvalue index too large"); if (ttislcf(ci->func)) /* light C function? */ return NONVALIDVALUE; /* it has no upvalues */ else { @@ -94,7 +97,7 @@ LUA_API int lua_checkstack (lua_State *L, int size) { int res; CallInfo *ci = L->ci; lua_lock(L); - api_check(L, size >= 0, "negative 'size'"); + api_check(size >= 0, "negative 'size'"); if (L->stack_last - L->top > size) /* stack large enough? */ res = 1; /* yes; check is OK */ else { /* no; need to grow stack */ @@ -116,8 +119,8 @@ LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { if (from == to) return; lua_lock(to); api_checknelems(from, n); - api_check(from, G(from) == G(to), "moving among independent states"); - api_check(from, to->ci->top - to->top >= n, "not enough elements to move"); + api_check(G(from) == G(to), "moving among independent states"); + api_check(to->ci->top - to->top >= n, "not enough elements to move"); from->top -= n; for (i = 0; i < n; i++) { setobj2s(to, to->top++, from->top + i); @@ -168,13 +171,13 @@ LUA_API void lua_settop (lua_State *L, int idx) { StkId func = L->ci->func; lua_lock(L); if (idx >= 0) { - api_check(L, idx <= L->stack_last - (func + 1), "new top too large"); + api_check(idx <= L->stack_last - (func + 1), "new top too large"); while (L->top < (func + 1) + idx) setnilvalue(L->top++); L->top = (func + 1) + idx; } else { - api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top"); + api_check(-(idx+1) <= (L->top - (func + 1)), "invalid new top"); L->top += idx+1; /* `subtract' index (index is negative) */ } lua_unlock(L); @@ -183,7 +186,7 @@ LUA_API void lua_settop (lua_State *L, int idx) { /* ** Reverse the stack segment from 'from' to 'to' -** (auxiliar to 'lua_rotate') +** (auxiliary to 'lua_rotate') */ static void reverse (lua_State *L, StkId from, StkId to) { for (; from < to; from++, to--) { @@ -204,8 +207,8 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { lua_lock(L); t = L->top - 1; /* end of stack segment being rotated */ p = index2addr(L, idx); /* start of segment */ - api_checkstackindex(L, idx, p); - api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); + api_checkstackindex(idx, p); + api_check((n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ reverse(L, p, m); /* reverse the prefix with length 'n' */ reverse(L, m + 1, t); /* reverse the suffix */ @@ -214,31 +217,17 @@ LUA_API void lua_rotate (lua_State *L, int idx, int n) { } -static void moveto (lua_State *L, TValue *fr, int idx) { - TValue *to = index2addr(L, idx); - api_checkvalidindex(L, to); +LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { + TValue *fr, *to; + lua_lock(L); + fr = index2addr(L, fromidx); + to = index2addr(L, toidx); + api_checkvalidindex(to); setobj(L, to, fr); - if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ + if (isupvalue(toidx)) /* function upvalue? */ luaC_barrier(L, clCvalue(L->ci->func), fr); /* LUA_REGISTRYINDEX does not need gc barrier (collector revisits it before finishing collection) */ -} - - -LUA_API void lua_replace (lua_State *L, int idx) { - lua_lock(L); - api_checknelems(L, 1); - moveto(L, L->top - 1, idx); - L->top--; - lua_unlock(L); -} - - -LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { - TValue *fr; - lua_lock(L); - fr = index2addr(L, fromidx); - moveto(L, fr, toidx); lua_unlock(L); } @@ -265,6 +254,7 @@ LUA_API int lua_type (lua_State *L, int idx) { LUA_API const char *lua_typename (lua_State *L, int t) { UNUSED(L); + api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); return ttypename(t); } @@ -289,8 +279,8 @@ LUA_API int lua_isnumber (lua_State *L, int idx) { LUA_API int lua_isstring (lua_State *L, int idx) { - int t = lua_type(L, idx); - return (t == LUA_TSTRING || t == LUA_TNUMBER); + const TValue *o = index2addr(L, idx); + return (ttisstring(o) || cvt2str(o)); } @@ -334,7 +324,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break; case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break; case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break; - default: api_check(L, 0, "invalid option"); + default: api_check(0, "invalid option"); } } lua_unlock(L); @@ -372,40 +362,6 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { } -LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { - lua_Unsigned res = 0; - const TValue *o = index2addr(L, idx); - int isnum = 0; - switch (ttype(o)) { - case LUA_TNUMINT: { - res = l_castS2U(ivalue(o)); - isnum = 1; - break; - } - case LUA_TNUMFLT: { /* compute floor(n) % 2^(numbits in an integer) */ - const lua_Number two2n = cast_num(LUA_MAXUNSIGNED) + cast_num(1); - lua_Number n = fltvalue(o); /* get value */ - int neg = 0; - n = l_floor(n); /* get its floor */ - if (n < 0) { - neg = 1; - n = -n; /* make 'n' positive, so that 'fmod' is the same as '%' */ - } - n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */ - if (luai_numisnan(n)) /* not a number? */ - break; /* not an integer, too */ - res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */ - if (neg) res = 0u - res; /* back to negative, if needed */ - isnum = 1; - break; - } - default: break; - } - if (pisnum) *pisnum = isnum; - return res; -} - - LUA_API int lua_toboolean (lua_State *L, int idx) { const TValue *o = index2addr(L, idx); return !l_isfalse(o); @@ -415,14 +371,14 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { StkId o = index2addr(L, idx); if (!ttisstring(o)) { - lua_lock(L); /* `luaV_tostring' may create a new string */ - if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (!cvt2str(o)) { /* not convertible? */ if (len != NULL) *len = 0; - lua_unlock(L); return NULL; } + lua_lock(L); /* `luaO_tostring' may create a new string */ luaC_checkGC(L); o = index2addr(L, idx); /* previous call may reallocate the stack */ + luaO_tostring(L, o); lua_unlock(L); } if (len != NULL) *len = tsvalue(o)->len; @@ -453,7 +409,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { LUA_API void *lua_touserdata (lua_State *L, int idx) { StkId o = index2addr(L, idx); switch (ttnov(o)) { - case LUA_TUSERDATA: return (rawuvalue(o) + 1); + case LUA_TUSERDATA: return getudatamem(uvalue(o)); case LUA_TLIGHTUSERDATA: return pvalue(o); default: return NULL; } @@ -512,14 +468,6 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { } -LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { - lua_lock(L); - setivalue(L->top, l_castU2S(u)); - api_incr_top(L); - lua_unlock(L); -} - - LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); @@ -582,7 +530,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { else { CClosure *cl; api_checknelems(L, n); - api_check(L, n <= MAXUPVAL, "upvalue index too large"); + api_check(n <= MAXUPVAL, "upvalue index too large"); luaC_checkGC(L); cl = luaF_newCclosure(L, n); cl->f = fn; @@ -667,7 +615,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); lua_unlock(L); return ttnov(L->top - 1); @@ -678,7 +626,7 @@ LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) { StkId t; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); setobj2s(L, L->top, luaH_getint(hvalue(t), n)); api_incr_top(L); lua_unlock(L); @@ -691,7 +639,7 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { TValue k; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); setpvalue(&k, cast(void *, p)); setobj2s(L, L->top, luaH_get(hvalue(t), &k)); api_incr_top(L); @@ -746,8 +694,8 @@ LUA_API int lua_getuservalue (lua_State *L, int idx) { StkId o; lua_lock(L); o = index2addr(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - getuservalue(L, rawuvalue(o), L->top); + api_check(ttisfulluserdata(o), "full userdata expected"); + getuservalue(L, uvalue(o), L->top); api_incr_top(L); lua_unlock(L); return ttnov(L->top - 1); @@ -796,42 +744,48 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { LUA_API void lua_rawset (lua_State *L, int idx) { - StkId t; + StkId o; + Table *t; lua_lock(L); api_checknelems(L, 2); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); - invalidateTMcache(hvalue(t)); - luaC_barrierback(L, gcvalue(t), L->top-1); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); + setobj2t(L, luaH_set(L, t, L->top-2), L->top-1); + invalidateTMcache(t); + luaC_barrierback(L, t, L->top-1); L->top -= 2; lua_unlock(L); } LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { - StkId t; + StkId o; + Table *t; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); - luaH_setint(L, hvalue(t), n, L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top-1); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); + luaH_setint(L, t, n, L->top - 1); + luaC_barrierback(L, t, L->top-1); L->top--; lua_unlock(L); } LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { - StkId t; + StkId o; + Table *t; TValue k; lua_lock(L); api_checknelems(L, 1); - t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + o = index2addr(L, idx); + api_check(ttistable(o), "table expected"); + t = hvalue(o); setpvalue(&k, cast(void *, p)); - setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); - luaC_barrierback(L, gcvalue(t), L->top - 1); + setobj2t(L, luaH_set(L, t, &k), L->top - 1); + luaC_barrierback(L, t, L->top - 1); L->top--; lua_unlock(L); } @@ -846,7 +800,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { if (ttisnil(L->top - 1)) mt = NULL; else { - api_check(L, ttistable(L->top - 1), "table expected"); + api_check(ttistable(L->top - 1), "table expected"); mt = hvalue(L->top - 1); } switch (ttnov(obj)) { @@ -861,7 +815,7 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { case LUA_TUSERDATA: { uvalue(obj)->metatable = mt; if (mt) { - luaC_objbarrier(L, rawuvalue(obj), mt); + luaC_objbarrier(L, uvalue(obj), mt); luaC_checkfinalizer(L, gcvalue(obj), mt); } break; @@ -882,8 +836,8 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { lua_lock(L); api_checknelems(L, 1); o = index2addr(L, idx); - api_check(L, ttisfulluserdata(o), "full userdata expected"); - setuservalue(L, rawuvalue(o), L->top - 1); + api_check(ttisfulluserdata(o), "full userdata expected"); + setuservalue(L, uvalue(o), L->top - 1); luaC_barrier(L, gcvalue(o), L->top - 1); L->top--; lua_unlock(L); @@ -896,18 +850,18 @@ LUA_API void lua_setuservalue (lua_State *L, int idx) { #define checkresults(L,na,nr) \ - api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ + api_check((nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \ "results from function overflow current stack size") -LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx, +LUA_API void lua_callk (lua_State *L, int nargs, int nresults, lua_Ctx ctx, lua_KFunction k) { StkId func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); func = L->top - (nargs+1); if (k != NULL && L->nny == 0) { /* need to prepare continuation? */ @@ -940,21 +894,21 @@ static void f_call (lua_State *L, void *ud) { LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, - int ctx, lua_KFunction k) { + lua_Ctx ctx, lua_KFunction k) { struct CallS c; int status; ptrdiff_t func; lua_lock(L); - api_check(L, k == NULL || !isLua(L->ci), + api_check(k == NULL || !isLua(L->ci), "cannot use continuations inside hooks"); api_checknelems(L, nargs+1); - api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread"); + api_check(L->status == LUA_OK, "cannot do calls on non-normal thread"); checkresults(L, nargs, nresults); if (errfunc == 0) func = 0; else { StkId o = index2addr(L, errfunc); - api_checkstackindex(L, errfunc, o); + api_checkstackindex(errfunc, o); func = savestack(L, o); } c.func = L->top - (nargs+1); /* function to be called */ @@ -999,7 +953,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ setobj(L, f->upvals[0]->v, gt); - luaC_barrier(L, f->upvals[0], gt); + luaC_upvalbarrier(L, f->upvals[0]); } } lua_unlock(L); @@ -1062,7 +1016,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { case LUA_GCSTEP: { l_mem debt = 1; /* =1 to signal that it did an actual step */ int oldrunning = g->gcrunning; - g->gcrunning = 1; /* force GC to run */ + g->gcrunning = 1; /* allow GC to run */ if (data == 0) { luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */ luaC_step(L); @@ -1119,7 +1073,7 @@ LUA_API int lua_next (lua_State *L, int idx) { int more; lua_lock(L); t = index2addr(L, idx); - api_check(L, ttistable(t), "table expected"); + api_check(ttistable(t), "table expected"); more = luaH_next(L, hvalue(t), L->top - 1); if (more) { api_incr_top(L); @@ -1183,19 +1137,19 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { setuvalue(L, L->top, u); api_incr_top(L); lua_unlock(L); - return u + 1; + return getudatamem(u); } static const char *aux_upvalue (StkId fi, int n, TValue **val, - GCObject **owner, UpVal **uv) { + CClosure **owner, UpVal **uv) { switch (ttype(fi)) { case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); if (!(1 <= n && n <= f->nupvalues)) return NULL; *val = &f->upvalue[n-1]; - if (owner) *owner = obj2gco(f); + if (owner) *owner = f; return ""; } case LUA_TLCL: { /* Lua closure */ @@ -1230,7 +1184,7 @@ LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { const char *name; TValue *val = NULL; /* to avoid warnings */ - GCObject *owner = NULL; + CClosure *owner = NULL; UpVal *uv = NULL; StkId fi; lua_lock(L); @@ -1251,9 +1205,9 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { LClosure *f; StkId fi = index2addr(L, fidx); - api_check(L, ttisLclosure(fi), "Lua function expected"); + api_check(ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); - api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); + api_check((1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } @@ -1267,11 +1221,11 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { } case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); - api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index"); + api_check(1 <= n && n <= f->nupvalues, "invalid upvalue index"); return &f->upvalue[n - 1]; } default: { - api_check(L, 0, "closure expected"); + api_check(0, "closure expected"); return NULL; } } |