rlm@1: /* rlm@1: ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ rlm@1: ** Lua API rlm@1: ** See Copyright Notice in lua.h rlm@1: */ rlm@1: rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: rlm@1: #define lapi_c rlm@1: #define LUA_CORE rlm@1: rlm@1: #include "lua.h" rlm@1: rlm@1: #include "lapi.h" rlm@1: #include "ldebug.h" rlm@1: #include "ldo.h" rlm@1: #include "lfunc.h" rlm@1: #include "lgc.h" rlm@1: #include "lmem.h" rlm@1: #include "lobject.h" rlm@1: #include "lstate.h" rlm@1: #include "lstring.h" rlm@1: #include "ltable.h" rlm@1: #include "ltm.h" rlm@1: #include "lundump.h" rlm@1: #include "lvm.h" rlm@1: rlm@1: rlm@1: rlm@1: const char lua_ident[] = rlm@1: "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" rlm@1: "$Authors: " LUA_AUTHORS " $\n" rlm@1: "$URL: www.lua.org $\n"; rlm@1: rlm@1: rlm@1: rlm@1: #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) rlm@1: rlm@1: #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) rlm@1: rlm@1: #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} rlm@1: rlm@1: rlm@1: rlm@1: static TValue *index2adr (lua_State *L, int idx) { rlm@1: if (idx > 0) { rlm@1: TValue *o = L->base + (idx - 1); rlm@1: api_check(L, idx <= L->ci->top - L->base); rlm@1: if (o >= L->top) return cast(TValue *, luaO_nilobject); rlm@1: else return o; rlm@1: } rlm@1: else if (idx > LUA_REGISTRYINDEX) { rlm@1: api_check(L, idx != 0 && -idx <= L->top - L->base); rlm@1: return L->top + idx; rlm@1: } rlm@1: else switch (idx) { /* pseudo-indices */ rlm@1: case LUA_REGISTRYINDEX: return registry(L); rlm@1: case LUA_ENVIRONINDEX: { rlm@1: Closure *func = curr_func(L); rlm@1: sethvalue(L, &L->env, func->c.env); rlm@1: return &L->env; rlm@1: } rlm@1: case LUA_GLOBALSINDEX: return gt(L); rlm@1: default: { rlm@1: Closure *func = curr_func(L); rlm@1: idx = LUA_GLOBALSINDEX - idx; rlm@1: return (idx <= func->c.nupvalues) rlm@1: ? &func->c.upvalue[idx-1] rlm@1: : cast(TValue *, luaO_nilobject); rlm@1: } rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: static Table *getcurrenv (lua_State *L) { rlm@1: if (L->ci == L->base_ci) /* no enclosing function? */ rlm@1: return hvalue(gt(L)); /* use global table as environment */ rlm@1: else { rlm@1: Closure *func = curr_func(L); rlm@1: return func->c.env; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: void luaA_pushobject (lua_State *L, const TValue *o) { rlm@1: setobj2s(L, L->top, o); rlm@1: api_incr_top(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_checkstack (lua_State *L, int size) { rlm@1: int res = 1; rlm@1: lua_lock(L); rlm@1: if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) rlm@1: res = 0; /* stack overflow */ rlm@1: else if (size > 0) { rlm@1: luaD_checkstack(L, size); rlm@1: if (L->ci->top < L->top + size) rlm@1: L->ci->top = L->top + size; rlm@1: } rlm@1: lua_unlock(L); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { rlm@1: int i; rlm@1: if (from == to) return; rlm@1: lua_lock(to); rlm@1: api_checknelems(from, n); rlm@1: api_check(from, G(from) == G(to)); rlm@1: api_check(from, to->ci->top - to->top >= n); rlm@1: from->top -= n; rlm@1: for (i = 0; i < n; i++) { rlm@1: setobj2s(to, to->top++, from->top + i); rlm@1: } rlm@1: lua_unlock(to); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_setlevel (lua_State *from, lua_State *to) { rlm@1: to->nCcalls = from->nCcalls; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { rlm@1: lua_CFunction old; rlm@1: lua_lock(L); rlm@1: old = G(L)->panic; rlm@1: G(L)->panic = panicf; rlm@1: lua_unlock(L); rlm@1: return old; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_State *lua_newthread (lua_State *L) { rlm@1: lua_State *L1; rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: L1 = luaE_newthread(L); rlm@1: setthvalue(L, L->top, L1); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: luai_userstatethread(L, L1); rlm@1: return L1; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** basic stack manipulation rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API int lua_gettop (lua_State *L) { rlm@1: return cast_int(L->top - L->base); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_settop (lua_State *L, int idx) { rlm@1: lua_lock(L); rlm@1: if (idx >= 0) { rlm@1: api_check(L, idx <= L->stack_last - L->base); rlm@1: while (L->top < L->base + idx) rlm@1: setnilvalue(L->top++); rlm@1: L->top = L->base + idx; rlm@1: } rlm@1: else { rlm@1: api_check(L, -(idx+1) <= (L->top - L->base)); rlm@1: L->top += idx+1; /* `subtract' index (index is negative) */ rlm@1: } rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_remove (lua_State *L, int idx) { rlm@1: StkId p; rlm@1: lua_lock(L); rlm@1: p = index2adr(L, idx); rlm@1: api_checkvalidindex(L, p); rlm@1: while (++p < L->top) setobjs2s(L, p-1, p); rlm@1: L->top--; rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_insert (lua_State *L, int idx) { rlm@1: StkId p; rlm@1: StkId q; rlm@1: lua_lock(L); rlm@1: p = index2adr(L, idx); rlm@1: api_checkvalidindex(L, p); rlm@1: for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); rlm@1: setobjs2s(L, p, L->top); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_replace (lua_State *L, int idx) { rlm@1: StkId o; rlm@1: lua_lock(L); rlm@1: /* explicit test for incompatible code */ rlm@1: if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) rlm@1: luaG_runerror(L, "no calling environment"); rlm@1: api_checknelems(L, 1); rlm@1: o = index2adr(L, idx); rlm@1: api_checkvalidindex(L, o); rlm@1: if (idx == LUA_ENVIRONINDEX) { rlm@1: Closure *func = curr_func(L); rlm@1: api_check(L, ttistable(L->top - 1)); rlm@1: func->c.env = hvalue(L->top - 1); rlm@1: luaC_barrier(L, func, L->top - 1); rlm@1: } rlm@1: else { rlm@1: setobj(L, o, L->top - 1); rlm@1: if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ rlm@1: luaC_barrier(L, curr_func(L), L->top - 1); rlm@1: } rlm@1: L->top--; rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushvalue (lua_State *L, int idx) { rlm@1: lua_lock(L); rlm@1: setobj2s(L, L->top, index2adr(L, idx)); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** access functions (stack -> C) rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API int lua_type (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_typename (lua_State *L, int t) { rlm@1: UNUSED(L); rlm@1: return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_iscfunction (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: return iscfunction(o); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_isnumber (lua_State *L, int idx) { rlm@1: TValue n; rlm@1: const TValue *o = index2adr(L, idx); rlm@1: return tonumber(o, &n); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_isstring (lua_State *L, int idx) { rlm@1: int t = lua_type(L, idx); rlm@1: return (t == LUA_TSTRING || t == LUA_TNUMBER); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_isuserdata (lua_State *L, int idx) { rlm@1: const TValue *o = index2adr(L, idx); rlm@1: return (ttisuserdata(o) || ttislightuserdata(o)); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { rlm@1: StkId o1 = index2adr(L, index1); rlm@1: StkId o2 = index2adr(L, index2); rlm@1: return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 rlm@1: : luaO_rawequalObj(o1, o2); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_equal (lua_State *L, int index1, int index2) { rlm@1: StkId o1, o2; rlm@1: int i; rlm@1: lua_lock(L); /* may call tag method */ rlm@1: o1 = index2adr(L, index1); rlm@1: o2 = index2adr(L, index2); rlm@1: i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); rlm@1: lua_unlock(L); rlm@1: return i; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { rlm@1: StkId o1, o2; rlm@1: int i; rlm@1: lua_lock(L); /* may call tag method */ rlm@1: o1 = index2adr(L, index1); rlm@1: o2 = index2adr(L, index2); rlm@1: i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 rlm@1: : luaV_lessthan(L, o1, o2); rlm@1: lua_unlock(L); rlm@1: return i; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { rlm@1: TValue n; rlm@1: const TValue *o = index2adr(L, idx); rlm@1: if (tonumber(o, &n)) rlm@1: return nvalue(o); rlm@1: else rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { rlm@1: TValue n; rlm@1: const TValue *o = index2adr(L, idx); rlm@1: if (tonumber(o, &n)) { rlm@1: lua_Integer res; rlm@1: lua_Number num = nvalue(o); rlm@1: lua_number2integer(res, num); rlm@1: return res; rlm@1: } rlm@1: else rlm@1: return 0; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_toboolean (lua_State *L, int idx) { rlm@1: const TValue *o = index2adr(L, idx); rlm@1: return !l_isfalse(o); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { rlm@1: StkId o = index2adr(L, idx); rlm@1: if (!ttisstring(o)) { rlm@1: lua_lock(L); /* `luaV_tostring' may create a new string */ rlm@1: if (!luaV_tostring(L, o)) { /* conversion failed? */ rlm@1: if (len != NULL) *len = 0; rlm@1: lua_unlock(L); rlm@1: return NULL; rlm@1: } rlm@1: luaC_checkGC(L); rlm@1: o = index2adr(L, idx); /* previous call may reallocate the stack */ rlm@1: lua_unlock(L); rlm@1: } rlm@1: if (len != NULL) *len = tsvalue(o)->len; rlm@1: return svalue(o); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API size_t lua_objlen (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: switch (ttype(o)) { rlm@1: case LUA_TSTRING: return tsvalue(o)->len; rlm@1: case LUA_TUSERDATA: return uvalue(o)->len; rlm@1: case LUA_TTABLE: return luaH_getn(hvalue(o)); rlm@1: case LUA_TNUMBER: { rlm@1: size_t l; rlm@1: lua_lock(L); /* `luaV_tostring' may create a new string */ rlm@1: l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); rlm@1: lua_unlock(L); rlm@1: return l; rlm@1: } rlm@1: default: return 0; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void *lua_touserdata (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: switch (ttype(o)) { rlm@1: case LUA_TUSERDATA: return (rawuvalue(o) + 1); rlm@1: case LUA_TLIGHTUSERDATA: return pvalue(o); rlm@1: default: return NULL; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_State *lua_tothread (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: return (!ttisthread(o)) ? NULL : thvalue(o); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const void *lua_topointer (lua_State *L, int idx) { rlm@1: StkId o = index2adr(L, idx); rlm@1: switch (ttype(o)) { rlm@1: case LUA_TTABLE: return hvalue(o); rlm@1: case LUA_TFUNCTION: return clvalue(o); rlm@1: case LUA_TTHREAD: return thvalue(o); rlm@1: case LUA_TUSERDATA: rlm@1: case LUA_TLIGHTUSERDATA: rlm@1: return lua_touserdata(L, idx); rlm@1: default: return NULL; rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** push functions (C -> stack) rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API void lua_pushnil (lua_State *L) { rlm@1: lua_lock(L); rlm@1: setnilvalue(L->top); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { rlm@1: lua_lock(L); rlm@1: setnvalue(L->top, n); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { rlm@1: lua_lock(L); rlm@1: setnvalue(L->top, cast_num(n)); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushstring (lua_State *L, const char *s) { rlm@1: if (s == NULL) rlm@1: lua_pushnil(L); rlm@1: else rlm@1: lua_pushlstring(L, s, strlen(s)); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, rlm@1: va_list argp) { rlm@1: const char *ret; rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: ret = luaO_pushvfstring(L, fmt, argp); rlm@1: lua_unlock(L); rlm@1: return ret; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { rlm@1: const char *ret; rlm@1: va_list argp; rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: va_start(argp, fmt); rlm@1: ret = luaO_pushvfstring(L, fmt, argp); rlm@1: va_end(argp); rlm@1: lua_unlock(L); rlm@1: return ret; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { rlm@1: Closure *cl; rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: api_checknelems(L, n); rlm@1: cl = luaF_newCclosure(L, n, getcurrenv(L)); rlm@1: cl->c.f = fn; rlm@1: L->top -= n; rlm@1: while (n--) rlm@1: setobj2n(L, &cl->c.upvalue[n], L->top+n); rlm@1: setclvalue(L, L->top, cl); rlm@1: lua_assert(iswhite(obj2gco(cl))); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushboolean (lua_State *L, int b) { rlm@1: lua_lock(L); rlm@1: setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { rlm@1: lua_lock(L); rlm@1: setpvalue(L->top, p); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_pushthread (lua_State *L) { rlm@1: lua_lock(L); rlm@1: setthvalue(L, L->top, L); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: return (G(L)->mainthread == L); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** get functions (Lua -> stack) rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API void lua_gettable (lua_State *L, int idx) { rlm@1: StkId t; rlm@1: lua_lock(L); rlm@1: t = index2adr(L, idx); rlm@1: api_checkvalidindex(L, t); rlm@1: luaV_gettable(L, t, L->top - 1, L->top - 1); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { rlm@1: StkId t; rlm@1: TValue key; rlm@1: lua_lock(L); rlm@1: t = index2adr(L, idx); rlm@1: api_checkvalidindex(L, t); rlm@1: setsvalue(L, &key, luaS_new(L, k)); rlm@1: luaV_gettable(L, t, &key, L->top); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_rawget (lua_State *L, int idx) { rlm@1: StkId t; rlm@1: lua_lock(L); rlm@1: t = index2adr(L, idx); rlm@1: api_check(L, ttistable(t)); rlm@1: setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { rlm@1: StkId o; rlm@1: lua_lock(L); rlm@1: o = index2adr(L, idx); rlm@1: api_check(L, ttistable(o)); rlm@1: setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: sethvalue(L, L->top, luaH_new(L, narray, nrec)); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_getmetatable (lua_State *L, int objindex) { rlm@1: const TValue *obj; rlm@1: Table *mt = NULL; rlm@1: int res; rlm@1: lua_lock(L); rlm@1: obj = index2adr(L, objindex); rlm@1: switch (ttype(obj)) { rlm@1: case LUA_TTABLE: rlm@1: mt = hvalue(obj)->metatable; rlm@1: break; rlm@1: case LUA_TUSERDATA: rlm@1: mt = uvalue(obj)->metatable; rlm@1: break; rlm@1: default: rlm@1: mt = G(L)->mt[ttype(obj)]; rlm@1: break; rlm@1: } rlm@1: if (mt == NULL) rlm@1: res = 0; rlm@1: else { rlm@1: sethvalue(L, L->top, mt); rlm@1: api_incr_top(L); rlm@1: res = 1; rlm@1: } rlm@1: lua_unlock(L); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_getfenv (lua_State *L, int idx) { rlm@1: StkId o; rlm@1: lua_lock(L); rlm@1: o = index2adr(L, idx); rlm@1: api_checkvalidindex(L, o); rlm@1: switch (ttype(o)) { rlm@1: case LUA_TFUNCTION: rlm@1: sethvalue(L, L->top, clvalue(o)->c.env); rlm@1: break; rlm@1: case LUA_TUSERDATA: rlm@1: sethvalue(L, L->top, uvalue(o)->env); rlm@1: break; rlm@1: case LUA_TTHREAD: rlm@1: setobj2s(L, L->top, gt(thvalue(o))); rlm@1: break; rlm@1: default: rlm@1: setnilvalue(L->top); rlm@1: break; rlm@1: } rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** set functions (stack -> Lua) rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API void lua_settable (lua_State *L, int idx) { rlm@1: StkId t; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 2); rlm@1: t = index2adr(L, idx); rlm@1: api_checkvalidindex(L, t); rlm@1: luaV_settable(L, t, L->top - 2, L->top - 1); rlm@1: L->top -= 2; /* pop index and value */ rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { rlm@1: StkId t; rlm@1: TValue key; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: t = index2adr(L, idx); rlm@1: api_checkvalidindex(L, t); rlm@1: setsvalue(L, &key, luaS_new(L, k)); rlm@1: luaV_settable(L, t, &key, L->top - 1); rlm@1: L->top--; /* pop value */ rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_rawset (lua_State *L, int idx) { rlm@1: StkId t; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 2); rlm@1: t = index2adr(L, idx); rlm@1: api_check(L, ttistable(t)); rlm@1: setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); rlm@1: luaC_barriert(L, hvalue(t), L->top-1); rlm@1: L->top -= 2; rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_rawseti (lua_State *L, int idx, int n) { rlm@1: StkId o; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: o = index2adr(L, idx); rlm@1: api_check(L, ttistable(o)); rlm@1: setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); rlm@1: luaC_barriert(L, hvalue(o), L->top-1); rlm@1: L->top--; rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_setmetatable (lua_State *L, int objindex) { rlm@1: TValue *obj; rlm@1: Table *mt; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: obj = index2adr(L, objindex); rlm@1: api_checkvalidindex(L, obj); rlm@1: if (ttisnil(L->top - 1)) rlm@1: mt = NULL; rlm@1: else { rlm@1: api_check(L, ttistable(L->top - 1)); rlm@1: mt = hvalue(L->top - 1); rlm@1: } rlm@1: switch (ttype(obj)) { rlm@1: case LUA_TTABLE: { rlm@1: hvalue(obj)->metatable = mt; rlm@1: if (mt) rlm@1: luaC_objbarriert(L, hvalue(obj), mt); rlm@1: break; rlm@1: } rlm@1: case LUA_TUSERDATA: { rlm@1: uvalue(obj)->metatable = mt; rlm@1: if (mt) rlm@1: luaC_objbarrier(L, rawuvalue(obj), mt); rlm@1: break; rlm@1: } rlm@1: default: { rlm@1: G(L)->mt[ttype(obj)] = mt; rlm@1: break; rlm@1: } rlm@1: } rlm@1: L->top--; rlm@1: lua_unlock(L); rlm@1: return 1; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_setfenv (lua_State *L, int idx) { rlm@1: StkId o; rlm@1: int res = 1; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: o = index2adr(L, idx); rlm@1: api_checkvalidindex(L, o); rlm@1: api_check(L, ttistable(L->top - 1)); rlm@1: switch (ttype(o)) { rlm@1: case LUA_TFUNCTION: rlm@1: clvalue(o)->c.env = hvalue(L->top - 1); rlm@1: break; rlm@1: case LUA_TUSERDATA: rlm@1: uvalue(o)->env = hvalue(L->top - 1); rlm@1: break; rlm@1: case LUA_TTHREAD: rlm@1: sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); rlm@1: break; rlm@1: default: rlm@1: res = 0; rlm@1: break; rlm@1: } rlm@1: if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); rlm@1: L->top--; rlm@1: lua_unlock(L); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** `load' and `call' functions (run Lua code) rlm@1: */ rlm@1: rlm@1: rlm@1: #define adjustresults(L,nres) \ rlm@1: { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } rlm@1: rlm@1: rlm@1: #define checkresults(L,na,nr) \ rlm@1: api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) rlm@1: rlm@1: rlm@1: LUA_API void lua_call (lua_State *L, int nargs, int nresults) { rlm@1: StkId func; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, nargs+1); rlm@1: checkresults(L, nargs, nresults); rlm@1: func = L->top - (nargs+1); rlm@1: luaD_call(L, func, nresults); rlm@1: adjustresults(L, nresults); rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** Execute a protected call. rlm@1: */ rlm@1: struct CallS { /* data to `f_call' */ rlm@1: StkId func; rlm@1: int nresults; rlm@1: }; rlm@1: rlm@1: rlm@1: static void f_call (lua_State *L, void *ud) { rlm@1: struct CallS *c = cast(struct CallS *, ud); rlm@1: luaD_call(L, c->func, c->nresults); rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { rlm@1: struct CallS c; rlm@1: int status; rlm@1: ptrdiff_t func; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, nargs+1); rlm@1: checkresults(L, nargs, nresults); rlm@1: if (errfunc == 0) rlm@1: func = 0; rlm@1: else { rlm@1: StkId o = index2adr(L, errfunc); rlm@1: api_checkvalidindex(L, o); rlm@1: func = savestack(L, o); rlm@1: } rlm@1: c.func = L->top - (nargs+1); /* function to be called */ rlm@1: c.nresults = nresults; rlm@1: status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); rlm@1: adjustresults(L, nresults); rlm@1: lua_unlock(L); rlm@1: return status; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** Execute a protected C call. rlm@1: */ rlm@1: struct CCallS { /* data to `f_Ccall' */ rlm@1: lua_CFunction func; rlm@1: void *ud; rlm@1: }; rlm@1: rlm@1: rlm@1: static void f_Ccall (lua_State *L, void *ud) { rlm@1: struct CCallS *c = cast(struct CCallS *, ud); rlm@1: Closure *cl; rlm@1: cl = luaF_newCclosure(L, 0, getcurrenv(L)); rlm@1: cl->c.f = c->func; rlm@1: setclvalue(L, L->top, cl); /* push function */ rlm@1: api_incr_top(L); rlm@1: setpvalue(L->top, c->ud); /* push only argument */ rlm@1: api_incr_top(L); rlm@1: luaD_call(L, L->top - 2, 0); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { rlm@1: struct CCallS c; rlm@1: int status; rlm@1: lua_lock(L); rlm@1: c.func = func; rlm@1: c.ud = ud; rlm@1: status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); rlm@1: lua_unlock(L); rlm@1: return status; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, rlm@1: const char *chunkname) { rlm@1: ZIO z; rlm@1: int status; rlm@1: lua_lock(L); rlm@1: if (!chunkname) chunkname = "?"; rlm@1: luaZ_init(L, &z, reader, data); rlm@1: status = luaD_protectedparser(L, &z, chunkname); rlm@1: lua_unlock(L); rlm@1: return status; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { rlm@1: int status; rlm@1: TValue *o; rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: o = L->top - 1; rlm@1: if (isLfunction(o)) rlm@1: status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); rlm@1: else rlm@1: status = 1; rlm@1: lua_unlock(L); rlm@1: return status; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_status (lua_State *L) { rlm@1: return L->status; rlm@1: } rlm@1: rlm@1: rlm@1: /* rlm@1: ** Garbage-collection function rlm@1: */ rlm@1: rlm@1: LUA_API int lua_gc (lua_State *L, int what, int data) { rlm@1: int res = 0; rlm@1: global_State *g; rlm@1: lua_lock(L); rlm@1: g = G(L); rlm@1: switch (what) { rlm@1: case LUA_GCSTOP: { rlm@1: g->GCthreshold = MAX_LUMEM; rlm@1: break; rlm@1: } rlm@1: case LUA_GCRESTART: { rlm@1: g->GCthreshold = g->totalbytes; rlm@1: break; rlm@1: } rlm@1: case LUA_GCCOLLECT: { rlm@1: luaC_fullgc(L); rlm@1: break; rlm@1: } rlm@1: case LUA_GCCOUNT: { rlm@1: /* GC values are expressed in Kbytes: #bytes/2^10 */ rlm@1: res = cast_int(g->totalbytes >> 10); rlm@1: break; rlm@1: } rlm@1: case LUA_GCCOUNTB: { rlm@1: res = cast_int(g->totalbytes & 0x3ff); rlm@1: break; rlm@1: } rlm@1: case LUA_GCSTEP: { rlm@1: lu_mem a = (cast(lu_mem, data) << 10); rlm@1: if (a <= g->totalbytes) rlm@1: g->GCthreshold = g->totalbytes - a; rlm@1: else rlm@1: g->GCthreshold = 0; rlm@1: while (g->GCthreshold <= g->totalbytes) { rlm@1: luaC_step(L); rlm@1: if (g->gcstate == GCSpause) { /* end of cycle? */ rlm@1: res = 1; /* signal it */ rlm@1: break; rlm@1: } rlm@1: } rlm@1: break; rlm@1: } rlm@1: case LUA_GCSETPAUSE: { rlm@1: res = g->gcpause; rlm@1: g->gcpause = data; rlm@1: break; rlm@1: } rlm@1: case LUA_GCSETSTEPMUL: { rlm@1: res = g->gcstepmul; rlm@1: g->gcstepmul = data; rlm@1: break; rlm@1: } rlm@1: default: res = -1; /* invalid option */ rlm@1: } rlm@1: lua_unlock(L); rlm@1: return res; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: /* rlm@1: ** miscellaneous functions rlm@1: */ rlm@1: rlm@1: rlm@1: LUA_API int lua_error (lua_State *L) { rlm@1: lua_lock(L); rlm@1: api_checknelems(L, 1); rlm@1: luaG_errormsg(L); rlm@1: lua_unlock(L); rlm@1: return 0; /* to avoid warnings */ rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API int lua_next (lua_State *L, int idx) { rlm@1: StkId t; rlm@1: int more; rlm@1: lua_lock(L); rlm@1: t = index2adr(L, idx); rlm@1: api_check(L, ttistable(t)); rlm@1: more = luaH_next(L, hvalue(t), L->top - 1); rlm@1: if (more) { rlm@1: api_incr_top(L); rlm@1: } rlm@1: else /* no more elements */ rlm@1: L->top -= 1; /* remove key */ rlm@1: lua_unlock(L); rlm@1: return more; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_concat (lua_State *L, int n) { rlm@1: lua_lock(L); rlm@1: api_checknelems(L, n); rlm@1: if (n >= 2) { rlm@1: luaC_checkGC(L); rlm@1: luaV_concat(L, n, cast_int(L->top - L->base) - 1); rlm@1: L->top -= (n-1); rlm@1: } rlm@1: else if (n == 0) { /* push empty string */ rlm@1: setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); rlm@1: api_incr_top(L); rlm@1: } rlm@1: /* else n == 1; nothing to do */ rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { rlm@1: lua_Alloc f; rlm@1: lua_lock(L); rlm@1: if (ud) *ud = G(L)->ud; rlm@1: f = G(L)->frealloc; rlm@1: lua_unlock(L); rlm@1: return f; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { rlm@1: lua_lock(L); rlm@1: G(L)->ud = ud; rlm@1: G(L)->frealloc = f; rlm@1: lua_unlock(L); rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API void *lua_newuserdata (lua_State *L, size_t size) { rlm@1: Udata *u; rlm@1: lua_lock(L); rlm@1: luaC_checkGC(L); rlm@1: u = luaS_newudata(L, size, getcurrenv(L)); rlm@1: setuvalue(L, L->top, u); rlm@1: api_incr_top(L); rlm@1: lua_unlock(L); rlm@1: return u + 1; rlm@1: } rlm@1: rlm@1: rlm@1: rlm@1: rlm@1: static const char *aux_upvalue (StkId fi, int n, TValue **val) { rlm@1: Closure *f; rlm@1: if (!ttisfunction(fi)) return NULL; rlm@1: f = clvalue(fi); rlm@1: if (f->c.isC) { rlm@1: if (!(1 <= n && n <= f->c.nupvalues)) return NULL; rlm@1: *val = &f->c.upvalue[n-1]; rlm@1: return ""; rlm@1: } rlm@1: else { rlm@1: Proto *p = f->l.p; rlm@1: if (!(1 <= n && n <= p->sizeupvalues)) return NULL; rlm@1: *val = f->l.upvals[n-1]->v; rlm@1: return getstr(p->upvalues[n-1]); rlm@1: } rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { rlm@1: const char *name; rlm@1: TValue *val; rlm@1: lua_lock(L); rlm@1: name = aux_upvalue(index2adr(L, funcindex), n, &val); rlm@1: if (name) { rlm@1: setobj2s(L, L->top, val); rlm@1: api_incr_top(L); rlm@1: } rlm@1: lua_unlock(L); rlm@1: return name; rlm@1: } rlm@1: rlm@1: rlm@1: LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { rlm@1: const char *name; rlm@1: TValue *val; rlm@1: StkId fi; rlm@1: lua_lock(L); rlm@1: fi = index2adr(L, funcindex); rlm@1: api_checknelems(L, 1); rlm@1: name = aux_upvalue(fi, n, &val); rlm@1: if (name) { rlm@1: L->top--; rlm@1: setobj(L, val, L->top); rlm@1: luaC_barrier(L, clvalue(fi), L->top); rlm@1: } rlm@1: lua_unlock(L); rlm@1: return name; rlm@1: } rlm@1: