|
|
@ -1,5 +1,5 @@ |
|
|
|
/*
|
|
|
|
** $Id: lapi.c,v 2.223 2014/06/30 19:48:08 roberto Exp roberto $ |
|
|
|
** $Id: lapi.c,v 2.224 2014/07/15 21:14:49 roberto Exp roberto $ |
|
|
|
** Lua API |
|
|
|
** See Copyright Notice in lua.h |
|
|
|
*/ |
|
|
@ -47,29 +47,29 @@ const char lua_ident[] = |
|
|
|
/* 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 +94,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 +116,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 +168,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); |
|
|
@ -204,8 +204,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 */ |
|
|
@ -216,7 +216,7 @@ 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); |
|
|
|
api_checkvalidindex(to); |
|
|
|
setobj(L, to, fr); |
|
|
|
if (idx < LUA_REGISTRYINDEX) /* function upvalue? */ |
|
|
|
luaC_barrier(L, clCvalue(L->ci->func), fr); |
|
|
@ -264,8 +264,8 @@ LUA_API int lua_type (lua_State *L, int idx) { |
|
|
|
|
|
|
|
|
|
|
|
LUA_API const char *lua_typename (lua_State *L, int t) { |
|
|
|
(void)L; |
|
|
|
api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); |
|
|
|
UNUSED(L); |
|
|
|
api_check(LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag"); |
|
|
|
return ttypename(t); |
|
|
|
} |
|
|
|
|
|
|
@ -335,7 +335,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); |
|
|
@ -541,7 +541,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; |
|
|
@ -626,7 +626,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); |
|
|
@ -637,7 +637,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); |
|
|
@ -650,7 +650,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); |
|
|
@ -705,7 +705,7 @@ 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"); |
|
|
|
api_check(ttisfulluserdata(o), "full userdata expected"); |
|
|
|
getuservalue(L, rawuvalue(o), L->top); |
|
|
|
api_incr_top(L); |
|
|
|
lua_unlock(L); |
|
|
@ -759,7 +759,7 @@ LUA_API void lua_rawset (lua_State *L, int idx) { |
|
|
|
lua_lock(L); |
|
|
|
api_checknelems(L, 2); |
|
|
|
t = index2addr(L, idx); |
|
|
|
api_check(L, ttistable(t), "table expected"); |
|
|
|
api_check(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); |
|
|
@ -773,7 +773,7 @@ LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) { |
|
|
|
lua_lock(L); |
|
|
|
api_checknelems(L, 1); |
|
|
|
t = index2addr(L, idx); |
|
|
|
api_check(L, ttistable(t), "table expected"); |
|
|
|
api_check(ttistable(t), "table expected"); |
|
|
|
luaH_setint(L, hvalue(t), n, L->top - 1); |
|
|
|
luaC_barrierback(L, gcvalue(t), L->top-1); |
|
|
|
L->top--; |
|
|
@ -787,7 +787,7 @@ LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) { |
|
|
|
lua_lock(L); |
|
|
|
api_checknelems(L, 1); |
|
|
|
t = index2addr(L, idx); |
|
|
|
api_check(L, ttistable(t), "table expected"); |
|
|
|
api_check(ttistable(t), "table expected"); |
|
|
|
setpvalue(&k, cast(void *, p)); |
|
|
|
setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1); |
|
|
|
luaC_barrierback(L, gcvalue(t), L->top - 1); |
|
|
@ -805,7 +805,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)) { |
|
|
@ -841,7 +841,7 @@ 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"); |
|
|
|
api_check(ttisfulluserdata(o), "full userdata expected"); |
|
|
|
setuservalue(L, rawuvalue(o), L->top - 1); |
|
|
|
luaC_barrier(L, gcvalue(o), L->top - 1); |
|
|
|
L->top--; |
|
|
@ -855,7 +855,7 @@ 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") |
|
|
|
|
|
|
|
|
|
|
@ -863,10 +863,10 @@ LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int 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? */ |
|
|
@ -904,16 +904,16 @@ LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc, |
|
|
|
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 */ |
|
|
@ -1078,7 +1078,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); |
|
|
@ -1210,9 +1210,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 */ |
|
|
|
} |
|
|
@ -1226,11 +1226,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; |
|
|
|
} |
|
|
|
} |
|
|
|