lua/lapi.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: lapi.c,v 2.159 2011/11/30 12:32:05 roberto Exp $
00003 ** Lua API
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 
00008 #include <stdarg.h>
00009 #include <string.h>
00010 
00011 #define lapi_c
00012 #define LUA_CORE
00013 
00014 #include "lua.h"
00015 
00016 #include "lapi.h"
00017 #include "ldebug.h"
00018 #include "ldo.h"
00019 #include "lfunc.h"
00020 #include "lgc.h"
00021 #include "lmem.h"
00022 #include "lobject.h"
00023 #include "lstate.h"
00024 #include "lstring.h"
00025 #include "ltable.h"
00026 #include "ltm.h"
00027 #include "lundump.h"
00028 #include "lvm.h"
00029 
00030 
00031 
00032 const char lua_ident[] =
00033   "$LuaVersion: " LUA_COPYRIGHT " $"
00034   "$LuaAuthors: " LUA_AUTHORS " $";
00035 
00036 
00037 /* value at a non-valid index */
00038 #define NONVALIDVALUE       cast(TValue *, luaO_nilobject)
00039 
00040 /* corresponding test */
00041 #define isvalid(o)  ((o) != luaO_nilobject)
00042 
00043 #define api_checkvalidindex(L, i)  api_check(L, isvalid(i), "invalid index")
00044 
00045 
00046 static TValue *index2addr (lua_State *L, int idx) {
00047   CallInfo *ci = L->ci;
00048   if (idx > 0) {
00049     TValue *o = ci->func + idx;
00050     api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
00051     if (o >= L->top) return NONVALIDVALUE;
00052     else return o;
00053   }
00054   else if (idx > LUA_REGISTRYINDEX) {
00055     api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
00056     return L->top + idx;
00057   }
00058   else if (idx == LUA_REGISTRYINDEX)
00059     return &G(L)->l_registry;
00060   else {  /* upvalues */
00061     idx = LUA_REGISTRYINDEX - idx;
00062     api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
00063     if (ttislcf(ci->func))  /* light C function? */
00064       return NONVALIDVALUE;  /* it has no upvalues */
00065     else {
00066       CClosure *func = clCvalue(ci->func);
00067       return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
00068     }
00069   }
00070 }
00071 
00072 
00073 /*
00074 ** to be called by 'lua_checkstack' in protected mode, to grow stack
00075 ** capturing memory errors
00076 */
00077 static void growstack (lua_State *L, void *ud) {
00078   int size = *(int *)ud;
00079   luaD_growstack(L, size);
00080 }
00081 
00082 
00083 LUA_API int lua_checkstack (lua_State *L, int size) {
00084   int res;
00085   CallInfo *ci = L->ci;
00086   lua_lock(L);
00087   if (L->stack_last - L->top > size)  /* stack large enough? */
00088     res = 1;  /* yes; check is OK */
00089   else {  /* no; need to grow stack */
00090     int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
00091     if (inuse > LUAI_MAXSTACK - size)  /* can grow without overflow? */
00092       res = 0;  /* no */
00093     else  /* try to grow stack */
00094       res = (luaD_rawrunprotected(L, &growstack, &size) == LUA_OK);
00095   }
00096   if (res && ci->top < L->top + size)
00097     ci->top = L->top + size;  /* adjust frame top */
00098   lua_unlock(L);
00099   return res;
00100 }
00101 
00102 
00103 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
00104   int i;
00105   if (from == to) return;
00106   lua_lock(to);
00107   api_checknelems(from, n);
00108   api_check(from, G(from) == G(to), "moving among independent states");
00109   api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
00110   from->top -= n;
00111   for (i = 0; i < n; i++) {
00112     setobj2s(to, to->top++, from->top + i);
00113   }
00114   lua_unlock(to);
00115 }
00116 
00117 
00118 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
00119   lua_CFunction old;
00120   lua_lock(L);
00121   old = G(L)->panic;
00122   G(L)->panic = panicf;
00123   lua_unlock(L);
00124   return old;
00125 }
00126 
00127 
00128 LUA_API const lua_Number *lua_version (lua_State *L) {
00129   static const lua_Number version = LUA_VERSION_NUM;
00130   if (L == NULL) return &version;
00131   else return G(L)->version;
00132 }
00133 
00134 
00135 
00136 /*
00137 ** basic stack manipulation
00138 */
00139 
00140 
00141 /*
00142 ** convert an acceptable stack index into an absolute index
00143 */
00144 LUA_API int lua_absindex (lua_State *L, int idx) {
00145   return (idx > 0 || idx <= LUA_REGISTRYINDEX)
00146          ? idx
00147          : cast_int(L->top - L->ci->func + idx);
00148 }
00149 
00150 
00151 LUA_API int lua_gettop (lua_State *L) {
00152   return cast_int(L->top - (L->ci->func + 1));
00153 }
00154 
00155 
00156 LUA_API void lua_settop (lua_State *L, int idx) {
00157   StkId func = L->ci->func;
00158   lua_lock(L);
00159   if (idx >= 0) {
00160     api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
00161     while (L->top < (func + 1) + idx)
00162       setnilvalue(L->top++);
00163     L->top = (func + 1) + idx;
00164   }
00165   else {
00166     api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
00167     L->top += idx+1;  /* `subtract' index (index is negative) */
00168   }
00169   lua_unlock(L);
00170 }
00171 
00172 
00173 LUA_API void lua_remove (lua_State *L, int idx) {
00174   StkId p;
00175   lua_lock(L);
00176   p = index2addr(L, idx);
00177   api_checkvalidindex(L, p);
00178   while (++p < L->top) setobjs2s(L, p-1, p);
00179   L->top--;
00180   lua_unlock(L);
00181 }
00182 
00183 
00184 LUA_API void lua_insert (lua_State *L, int idx) {
00185   StkId p;
00186   StkId q;
00187   lua_lock(L);
00188   p = index2addr(L, idx);
00189   api_checkvalidindex(L, p);
00190   for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
00191   setobjs2s(L, p, L->top);
00192   lua_unlock(L);
00193 }
00194 
00195 
00196 static void moveto (lua_State *L, TValue *fr, int idx) {
00197   TValue *to = index2addr(L, idx);
00198   api_checkvalidindex(L, to);
00199   setobj(L, to, fr);
00200   if (idx < LUA_REGISTRYINDEX)  /* function upvalue? */
00201     luaC_barrier(L, clCvalue(L->ci->func), fr);
00202   /* LUA_REGISTRYINDEX does not need gc barrier
00203      (collector revisits it before finishing collection) */
00204 }
00205 
00206 
00207 LUA_API void lua_replace (lua_State *L, int idx) {
00208   lua_lock(L);
00209   api_checknelems(L, 1);
00210   moveto(L, L->top - 1, idx);
00211   L->top--;
00212   lua_unlock(L);
00213 }
00214 
00215 
00216 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
00217   TValue *fr;
00218   lua_lock(L);
00219   fr = index2addr(L, fromidx);
00220   api_checkvalidindex(L, fr);
00221   moveto(L, fr, toidx);
00222   lua_unlock(L);
00223 }
00224 
00225 
00226 LUA_API void lua_pushvalue (lua_State *L, int idx) {
00227   lua_lock(L);
00228   setobj2s(L, L->top, index2addr(L, idx));
00229   api_incr_top(L);
00230   lua_unlock(L);
00231 }
00232 
00233 
00234 
00235 /*
00236 ** access functions (stack -> C)
00237 */
00238 
00239 
00240 LUA_API int lua_type (lua_State *L, int idx) {
00241   StkId o = index2addr(L, idx);
00242   return (isvalid(o) ? ttypenv(o) : LUA_TNONE);
00243 }
00244 
00245 
00246 LUA_API const char *lua_typename (lua_State *L, int t) {
00247   UNUSED(L);
00248   return ttypename(t);
00249 }
00250 
00251 
00252 LUA_API int lua_iscfunction (lua_State *L, int idx) {
00253   StkId o = index2addr(L, idx);
00254   return (ttislcf(o) || (ttisCclosure(o)));
00255 }
00256 
00257 
00258 LUA_API int lua_isnumber (lua_State *L, int idx) {
00259   TValue n;
00260   const TValue *o = index2addr(L, idx);
00261   return tonumber(o, &n);
00262 }
00263 
00264 
00265 LUA_API int lua_isstring (lua_State *L, int idx) {
00266   int t = lua_type(L, idx);
00267   return (t == LUA_TSTRING || t == LUA_TNUMBER);
00268 }
00269 
00270 
00271 LUA_API int lua_isuserdata (lua_State *L, int idx) {
00272   const TValue *o = index2addr(L, idx);
00273   return (ttisuserdata(o) || ttislightuserdata(o));
00274 }
00275 
00276 
00277 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
00278   StkId o1 = index2addr(L, index1);
00279   StkId o2 = index2addr(L, index2);
00280   return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
00281 }
00282 
00283 
00284 LUA_API void  lua_arith (lua_State *L, int op) {
00285   StkId o1;  /* 1st operand */
00286   StkId o2;  /* 2nd operand */
00287   lua_lock(L);
00288   if (op != LUA_OPUNM) /* all other operations expect two operands */
00289     api_checknelems(L, 2);
00290   else {  /* for unary minus, add fake 2nd operand */
00291     api_checknelems(L, 1);
00292     setobjs2s(L, L->top, L->top - 1);
00293     L->top++;
00294   }
00295   o1 = L->top - 2;
00296   o2 = L->top - 1;
00297   if (ttisnumber(o1) && ttisnumber(o2)) {
00298     changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
00299   }
00300   else
00301     luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
00302   L->top--;
00303   lua_unlock(L);
00304 }
00305 
00306 
00307 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
00308   StkId o1, o2;
00309   int i = 0;
00310   lua_lock(L);  /* may call tag method */
00311   o1 = index2addr(L, index1);
00312   o2 = index2addr(L, index2);
00313   if (isvalid(o1) && isvalid(o2)) {
00314     switch (op) {
00315       case LUA_OPEQ: i = equalobj(L, o1, o2); break;
00316       case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
00317       case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
00318       default: api_check(L, 0, "invalid option");
00319     }
00320   }
00321   lua_unlock(L);
00322   return i;
00323 }
00324 
00325 
00326 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
00327   TValue n;
00328   const TValue *o = index2addr(L, idx);
00329   if (tonumber(o, &n)) {
00330     if (isnum) *isnum = 1;
00331     return nvalue(o);
00332   }
00333   else {
00334     if (isnum) *isnum = 0;
00335     return 0;
00336   }
00337 }
00338 
00339 
00340 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
00341   TValue n;
00342   const TValue *o = index2addr(L, idx);
00343   if (tonumber(o, &n)) {
00344     lua_Integer res;
00345     lua_Number num = nvalue(o);
00346     lua_number2integer(res, num);
00347     if (isnum) *isnum = 1;
00348     return res;
00349   }
00350   else {
00351     if (isnum) *isnum = 0;
00352     return 0;
00353   }
00354 }
00355 
00356 
00357 LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
00358   TValue n;
00359   const TValue *o = index2addr(L, idx);
00360   if (tonumber(o, &n)) {
00361     lua_Unsigned res;
00362     lua_Number num = nvalue(o);
00363     lua_number2unsigned(res, num);
00364     if (isnum) *isnum = 1;
00365     return res;
00366   }
00367   else {
00368     if (isnum) *isnum = 0;
00369     return 0;
00370   }
00371 }
00372 
00373 
00374 LUA_API int lua_toboolean (lua_State *L, int idx) {
00375   const TValue *o = index2addr(L, idx);
00376   return !l_isfalse(o);
00377 }
00378 
00379 
00380 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
00381   StkId o = index2addr(L, idx);
00382   if (!ttisstring(o)) {
00383     lua_lock(L);  /* `luaV_tostring' may create a new string */
00384     if (!luaV_tostring(L, o)) {  /* conversion failed? */
00385       if (len != NULL) *len = 0;
00386       lua_unlock(L);
00387       return NULL;
00388     }
00389     luaC_checkGC(L);
00390     o = index2addr(L, idx);  /* previous call may reallocate the stack */
00391     lua_unlock(L);
00392   }
00393   if (len != NULL) *len = tsvalue(o)->len;
00394   return svalue(o);
00395 }
00396 
00397 
00398 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
00399   StkId o = index2addr(L, idx);
00400   switch (ttypenv(o)) {
00401     case LUA_TSTRING: return tsvalue(o)->len;
00402     case LUA_TUSERDATA: return uvalue(o)->len;
00403     case LUA_TTABLE: return luaH_getn(hvalue(o));
00404     default: return 0;
00405   }
00406 }
00407 
00408 
00409 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
00410   StkId o = index2addr(L, idx);
00411   if (ttislcf(o)) return fvalue(o);
00412   else if (ttisCclosure(o))
00413     return clCvalue(o)->f;
00414   else return NULL;  /* not a C function */
00415 }
00416 
00417 
00418 LUA_API void *lua_touserdata (lua_State *L, int idx) {
00419   StkId o = index2addr(L, idx);
00420   switch (ttypenv(o)) {
00421     case LUA_TUSERDATA: return (rawuvalue(o) + 1);
00422     case LUA_TLIGHTUSERDATA: return pvalue(o);
00423     default: return NULL;
00424   }
00425 }
00426 
00427 
00428 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
00429   StkId o = index2addr(L, idx);
00430   return (!ttisthread(o)) ? NULL : thvalue(o);
00431 }
00432 
00433 
00434 LUA_API const void *lua_topointer (lua_State *L, int idx) {
00435   StkId o = index2addr(L, idx);
00436   switch (ttype(o)) {
00437     case LUA_TTABLE: return hvalue(o);
00438     case LUA_TLCL: return clLvalue(o);
00439     case LUA_TCCL: return clCvalue(o);
00440     case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
00441     case LUA_TTHREAD: return thvalue(o);
00442     case LUA_TUSERDATA:
00443     case LUA_TLIGHTUSERDATA:
00444       return lua_touserdata(L, idx);
00445     default: return NULL;
00446   }
00447 }
00448 
00449 
00450 
00451 /*
00452 ** push functions (C -> stack)
00453 */
00454 
00455 
00456 LUA_API void lua_pushnil (lua_State *L) {
00457   lua_lock(L);
00458   setnilvalue(L->top);
00459   api_incr_top(L);
00460   lua_unlock(L);
00461 }
00462 
00463 
00464 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
00465   lua_lock(L);
00466   setnvalue(L->top, n);
00467   luai_checknum(L, L->top,
00468     luaG_runerror(L, "C API - attempt to push a signaling NaN"));
00469   api_incr_top(L);
00470   lua_unlock(L);
00471 }
00472 
00473 
00474 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
00475   lua_lock(L);
00476   setnvalue(L->top, cast_num(n));
00477   api_incr_top(L);
00478   lua_unlock(L);
00479 }
00480 
00481 
00482 LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
00483   lua_Number n;
00484   lua_lock(L);
00485   n = lua_unsigned2number(u);
00486   setnvalue(L->top, n);
00487   api_incr_top(L);
00488   lua_unlock(L);
00489 }
00490 
00491 
00492 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
00493   TString *ts;
00494   lua_lock(L);
00495   luaC_checkGC(L);
00496   ts = luaS_newlstr(L, s, len);
00497   setsvalue2s(L, L->top, ts);
00498   api_incr_top(L);
00499   lua_unlock(L);
00500   return getstr(ts);
00501 }
00502 
00503 
00504 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
00505   if (s == NULL) {
00506     lua_pushnil(L);
00507     return NULL;
00508   }
00509   else {
00510     TString *ts;
00511     lua_lock(L);
00512     luaC_checkGC(L);
00513     ts = luaS_new(L, s);
00514     setsvalue2s(L, L->top, ts);
00515     api_incr_top(L);
00516     lua_unlock(L);
00517     return getstr(ts);
00518   }
00519 }
00520 
00521 
00522 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
00523                                       va_list argp) {
00524   const char *ret;
00525   lua_lock(L);
00526   luaC_checkGC(L);
00527   ret = luaO_pushvfstring(L, fmt, argp);
00528   lua_unlock(L);
00529   return ret;
00530 }
00531 
00532 
00533 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
00534   const char *ret;
00535   va_list argp;
00536   lua_lock(L);
00537   luaC_checkGC(L);
00538   va_start(argp, fmt);
00539   ret = luaO_pushvfstring(L, fmt, argp);
00540   va_end(argp);
00541   lua_unlock(L);
00542   return ret;
00543 }
00544 
00545 
00546 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
00547   lua_lock(L);
00548   if (n == 0) {
00549     setfvalue(L->top, fn);
00550   }
00551   else {
00552     Closure *cl;
00553     api_checknelems(L, n);
00554     api_check(L, n <= MAXUPVAL, "upvalue index too large");
00555     luaC_checkGC(L);
00556     cl = luaF_newCclosure(L, n);
00557     cl->c.f = fn;
00558     L->top -= n;
00559     while (n--)
00560       setobj2n(L, &cl->c.upvalue[n], L->top + n);
00561     setclCvalue(L, L->top, cl);
00562   }
00563   api_incr_top(L);
00564   lua_unlock(L);
00565 }
00566 
00567 
00568 LUA_API void lua_pushboolean (lua_State *L, int b) {
00569   lua_lock(L);
00570   setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
00571   api_incr_top(L);
00572   lua_unlock(L);
00573 }
00574 
00575 
00576 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
00577   lua_lock(L);
00578   setpvalue(L->top, p);
00579   api_incr_top(L);
00580   lua_unlock(L);
00581 }
00582 
00583 
00584 LUA_API int lua_pushthread (lua_State *L) {
00585   lua_lock(L);
00586   setthvalue(L, L->top, L);
00587   api_incr_top(L);
00588   lua_unlock(L);
00589   return (G(L)->mainthread == L);
00590 }
00591 
00592 
00593 
00594 /*
00595 ** get functions (Lua -> stack)
00596 */
00597 
00598 
00599 LUA_API void lua_getglobal (lua_State *L, const char *var) {
00600   Table *reg = hvalue(&G(L)->l_registry);
00601   const TValue *gt;  /* global table */
00602   lua_lock(L);
00603   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
00604   setsvalue2s(L, L->top++, luaS_new(L, var));
00605   luaV_gettable(L, gt, L->top - 1, L->top - 1);
00606   lua_unlock(L);
00607 }
00608 
00609 
00610 LUA_API void lua_gettable (lua_State *L, int idx) {
00611   StkId t;
00612   lua_lock(L);
00613   t = index2addr(L, idx);
00614   api_checkvalidindex(L, t);
00615   luaV_gettable(L, t, L->top - 1, L->top - 1);
00616   lua_unlock(L);
00617 }
00618 
00619 
00620 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
00621   StkId t;
00622   lua_lock(L);
00623   t = index2addr(L, idx);
00624   api_checkvalidindex(L, t);
00625   setsvalue2s(L, L->top, luaS_new(L, k));
00626   api_incr_top(L);
00627   luaV_gettable(L, t, L->top - 1, L->top - 1);
00628   lua_unlock(L);
00629 }
00630 
00631 
00632 LUA_API void lua_rawget (lua_State *L, int idx) {
00633   StkId t;
00634   lua_lock(L);
00635   t = index2addr(L, idx);
00636   api_check(L, ttistable(t), "table expected");
00637   setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
00638   lua_unlock(L);
00639 }
00640 
00641 
00642 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
00643   StkId t;
00644   lua_lock(L);
00645   t = index2addr(L, idx);
00646   api_check(L, ttistable(t), "table expected");
00647   setobj2s(L, L->top, luaH_getint(hvalue(t), n));
00648   api_incr_top(L);
00649   lua_unlock(L);
00650 }
00651 
00652 
00653 LUA_API void lua_rawgetp (lua_State *L, int idx, const void *p) {
00654   StkId t;
00655   TValue k;
00656   lua_lock(L);
00657   t = index2addr(L, idx);
00658   api_check(L, ttistable(t), "table expected");
00659   setpvalue(&k, cast(void *, p));
00660   setobj2s(L, L->top, luaH_get(hvalue(t), &k));
00661   api_incr_top(L);
00662   lua_unlock(L);
00663 }
00664 
00665 
00666 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
00667   Table *t;
00668   lua_lock(L);
00669   luaC_checkGC(L);
00670   t = luaH_new(L);
00671   sethvalue(L, L->top, t);
00672   api_incr_top(L);
00673   if (narray > 0 || nrec > 0)
00674     luaH_resize(L, t, narray, nrec);
00675   lua_unlock(L);
00676 }
00677 
00678 
00679 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
00680   const TValue *obj;
00681   Table *mt = NULL;
00682   int res;
00683   lua_lock(L);
00684   obj = index2addr(L, objindex);
00685   switch (ttypenv(obj)) {
00686     case LUA_TTABLE:
00687       mt = hvalue(obj)->metatable;
00688       break;
00689     case LUA_TUSERDATA:
00690       mt = uvalue(obj)->metatable;
00691       break;
00692     default:
00693       mt = G(L)->mt[ttypenv(obj)];
00694       break;
00695   }
00696   if (mt == NULL)
00697     res = 0;
00698   else {
00699     sethvalue(L, L->top, mt);
00700     api_incr_top(L);
00701     res = 1;
00702   }
00703   lua_unlock(L);
00704   return res;
00705 }
00706 
00707 
00708 LUA_API void lua_getuservalue (lua_State *L, int idx) {
00709   StkId o;
00710   lua_lock(L);
00711   o = index2addr(L, idx);
00712   api_checkvalidindex(L, o);
00713   api_check(L, ttisuserdata(o), "userdata expected");
00714   if (uvalue(o)->env) {
00715     sethvalue(L, L->top, uvalue(o)->env);
00716   } else
00717     setnilvalue(L->top);
00718   api_incr_top(L);
00719   lua_unlock(L);
00720 }
00721 
00722 
00723 /*
00724 ** set functions (stack -> Lua)
00725 */
00726 
00727 
00728 LUA_API void lua_setglobal (lua_State *L, const char *var) {
00729   Table *reg = hvalue(&G(L)->l_registry);
00730   const TValue *gt;  /* global table */
00731   lua_lock(L);
00732   api_checknelems(L, 1);
00733   gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
00734   setsvalue2s(L, L->top++, luaS_new(L, var));
00735   luaV_settable(L, gt, L->top - 1, L->top - 2);
00736   L->top -= 2;  /* pop value and key */
00737   lua_unlock(L);
00738 }
00739 
00740 
00741 LUA_API void lua_settable (lua_State *L, int idx) {
00742   StkId t;
00743   lua_lock(L);
00744   api_checknelems(L, 2);
00745   t = index2addr(L, idx);
00746   api_checkvalidindex(L, t);
00747   luaV_settable(L, t, L->top - 2, L->top - 1);
00748   L->top -= 2;  /* pop index and value */
00749   lua_unlock(L);
00750 }
00751 
00752 
00753 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
00754   StkId t;
00755   lua_lock(L);
00756   api_checknelems(L, 1);
00757   t = index2addr(L, idx);
00758   api_checkvalidindex(L, t);
00759   setsvalue2s(L, L->top++, luaS_new(L, k));
00760   luaV_settable(L, t, L->top - 1, L->top - 2);
00761   L->top -= 2;  /* pop value and key */
00762   lua_unlock(L);
00763 }
00764 
00765 
00766 LUA_API void lua_rawset (lua_State *L, int idx) {
00767   StkId t;
00768   lua_lock(L);
00769   api_checknelems(L, 2);
00770   t = index2addr(L, idx);
00771   api_check(L, ttistable(t), "table expected");
00772   setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
00773   invalidateTMcache(hvalue(t));
00774   luaC_barrierback(L, gcvalue(t), L->top-1);
00775   L->top -= 2;
00776   lua_unlock(L);
00777 }
00778 
00779 
00780 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
00781   StkId t;
00782   lua_lock(L);
00783   api_checknelems(L, 1);
00784   t = index2addr(L, idx);
00785   api_check(L, ttistable(t), "table expected");
00786   luaH_setint(L, hvalue(t), n, L->top - 1);
00787   luaC_barrierback(L, gcvalue(t), L->top-1);
00788   L->top--;
00789   lua_unlock(L);
00790 }
00791 
00792 
00793 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
00794   StkId t;
00795   TValue k;
00796   lua_lock(L);
00797   api_checknelems(L, 1);
00798   t = index2addr(L, idx);
00799   api_check(L, ttistable(t), "table expected");
00800   setpvalue(&k, cast(void *, p));
00801   setobj2t(L, luaH_set(L, hvalue(t), &k), L->top - 1);
00802   luaC_barrierback(L, gcvalue(t), L->top - 1);
00803   L->top--;
00804   lua_unlock(L);
00805 }
00806 
00807 
00808 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
00809   TValue *obj;
00810   Table *mt;
00811   lua_lock(L);
00812   api_checknelems(L, 1);
00813   obj = index2addr(L, objindex);
00814   api_checkvalidindex(L, obj);
00815   if (ttisnil(L->top - 1))
00816     mt = NULL;
00817   else {
00818     api_check(L, ttistable(L->top - 1), "table expected");
00819     mt = hvalue(L->top - 1);
00820   }
00821   switch (ttypenv(obj)) {
00822     case LUA_TTABLE: {
00823       hvalue(obj)->metatable = mt;
00824       if (mt)
00825         luaC_objbarrierback(L, gcvalue(obj), mt);
00826         luaC_checkfinalizer(L, gcvalue(obj), mt);
00827       break;
00828     }
00829     case LUA_TUSERDATA: {
00830       uvalue(obj)->metatable = mt;
00831       if (mt) {
00832         luaC_objbarrier(L, rawuvalue(obj), mt);
00833         luaC_checkfinalizer(L, gcvalue(obj), mt);
00834       }
00835       break;
00836     }
00837     default: {
00838       G(L)->mt[ttypenv(obj)] = mt;
00839       break;
00840     }
00841   }
00842   L->top--;
00843   lua_unlock(L);
00844   return 1;
00845 }
00846 
00847 
00848 LUA_API void lua_setuservalue (lua_State *L, int idx) {
00849   StkId o;
00850   lua_lock(L);
00851   api_checknelems(L, 1);
00852   o = index2addr(L, idx);
00853   api_checkvalidindex(L, o);
00854   api_check(L, ttisuserdata(o), "userdata expected");
00855   if (ttisnil(L->top - 1))
00856     uvalue(o)->env = NULL;
00857   else {
00858     api_check(L, ttistable(L->top - 1), "table expected");
00859     uvalue(o)->env = hvalue(L->top - 1);
00860     luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
00861   }
00862   L->top--;
00863   lua_unlock(L);
00864 }
00865 
00866 
00867 /*
00868 ** `load' and `call' functions (run Lua code)
00869 */
00870 
00871 
00872 #define checkresults(L,na,nr) \
00873      api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
00874     "results from function overflow current stack size")
00875 
00876 
00877 LUA_API int lua_getctx (lua_State *L, int *ctx) {
00878   if (L->ci->callstatus & CIST_YIELDED) {
00879     if (ctx) *ctx = L->ci->u.c.ctx;
00880     return L->ci->u.c.status;
00881   }
00882   else return LUA_OK;
00883 }
00884 
00885 
00886 LUA_API void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
00887                         lua_CFunction k) {
00888   StkId func;
00889   lua_lock(L);
00890   api_check(L, k == NULL || !isLua(L->ci),
00891     "cannot use continuations inside hooks");
00892   api_checknelems(L, nargs+1);
00893   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
00894   checkresults(L, nargs, nresults);
00895   func = L->top - (nargs+1);
00896   if (k != NULL && L->nny == 0) {  /* need to prepare continuation? */
00897     L->ci->u.c.k = k;  /* save continuation */
00898     L->ci->u.c.ctx = ctx;  /* save context */
00899     luaD_call(L, func, nresults, 1);  /* do the call */
00900   }
00901   else  /* no continuation or no yieldable */
00902     luaD_call(L, func, nresults, 0);  /* just do the call */
00903   adjustresults(L, nresults);
00904   lua_unlock(L);
00905 }
00906 
00907 
00908 
00909 /*
00910 ** Execute a protected call.
00911 */
00912 struct CallS {  /* data to `f_call' */
00913   StkId func;
00914   int nresults;
00915 };
00916 
00917 
00918 static void f_call (lua_State *L, void *ud) {
00919   struct CallS *c = cast(struct CallS *, ud);
00920   luaD_call(L, c->func, c->nresults, 0);
00921 }
00922 
00923 
00924 
00925 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
00926                         int ctx, lua_CFunction k) {
00927   struct CallS c;
00928   int status;
00929   ptrdiff_t func;
00930   lua_lock(L);
00931   api_check(L, k == NULL || !isLua(L->ci),
00932     "cannot use continuations inside hooks");
00933   api_checknelems(L, nargs+1);
00934   api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
00935   checkresults(L, nargs, nresults);
00936   if (errfunc == 0)
00937     func = 0;
00938   else {
00939     StkId o = index2addr(L, errfunc);
00940     api_checkvalidindex(L, o);
00941     func = savestack(L, o);
00942   }
00943   c.func = L->top - (nargs+1);  /* function to be called */
00944   if (k == NULL || L->nny > 0) {  /* no continuation or no yieldable? */
00945     c.nresults = nresults;  /* do a 'conventional' protected call */
00946     status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
00947   }
00948   else {  /* prepare continuation (call is already protected by 'resume') */
00949     CallInfo *ci = L->ci;
00950     ci->u.c.k = k;  /* save continuation */
00951     ci->u.c.ctx = ctx;  /* save context */
00952     /* save information for error recovery */
00953     ci->u.c.extra = savestack(L, c.func);
00954     ci->u.c.old_allowhook = L->allowhook;
00955     ci->u.c.old_errfunc = L->errfunc;
00956     L->errfunc = func;
00957     /* mark that function may do error recovery */
00958     ci->callstatus |= CIST_YPCALL;
00959     luaD_call(L, c.func, nresults, 1);  /* do the call */
00960     ci->callstatus &= ~CIST_YPCALL;
00961     L->errfunc = ci->u.c.old_errfunc;
00962     status = LUA_OK;  /* if it is here, there were no errors */
00963   }
00964   adjustresults(L, nresults);
00965   lua_unlock(L);
00966   return status;
00967 }
00968 
00969 
00970 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
00971                       const char *chunkname, const char *mode) {
00972   ZIO z;
00973   int status;
00974   lua_lock(L);
00975   if (!chunkname) chunkname = "?";
00976   luaZ_init(L, &z, reader, data);
00977   status = luaD_protectedparser(L, &z, chunkname, mode);
00978   if (status == LUA_OK) {  /* no errors? */
00979     LClosure *f = clLvalue(L->top - 1);  /* get newly created function */
00980     if (f->nupvalues == 1) {  /* does it have one upvalue? */
00981       /* get global table from registry */
00982       Table *reg = hvalue(&G(L)->l_registry);
00983       const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
00984       /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
00985       setobj(L, f->upvals[0]->v, gt);
00986       luaC_barrier(L, f->upvals[0], gt);
00987     }
00988   }
00989   lua_unlock(L);
00990   return status;
00991 }
00992 
00993 
00994 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
00995   int status;
00996   TValue *o;
00997   lua_lock(L);
00998   api_checknelems(L, 1);
00999   o = L->top - 1;
01000   if (isLfunction(o))
01001     status = luaU_dump(L, getproto(o), writer, data, 0);
01002   else
01003     status = 1;
01004   lua_unlock(L);
01005   return status;
01006 }
01007 
01008 
01009 LUA_API int  lua_status (lua_State *L) {
01010   return L->status;
01011 }
01012 
01013 
01014 /*
01015 ** Garbage-collection function
01016 */
01017 
01018 LUA_API int lua_gc (lua_State *L, int what, int data) {
01019   int res = 0;
01020   global_State *g;
01021   lua_lock(L);
01022   g = G(L);
01023   switch (what) {
01024     case LUA_GCSTOP: {
01025       g->gcrunning = 0;
01026       break;
01027     }
01028     case LUA_GCRESTART: {
01029       luaE_setdebt(g, 0);
01030       g->gcrunning = 1;
01031       break;
01032     }
01033     case LUA_GCCOLLECT: {
01034       luaC_fullgc(L, 0);
01035       break;
01036     }
01037     case LUA_GCCOUNT: {
01038       /* GC values are expressed in Kbytes: #bytes/2^10 */
01039       res = cast_int(gettotalbytes(g) >> 10);
01040       break;
01041     }
01042     case LUA_GCCOUNTB: {
01043       res = cast_int(gettotalbytes(g) & 0x3ff);
01044       break;
01045     }
01046     case LUA_GCSTEP: {
01047       if (g->gckind == KGC_GEN) {  /* generational mode? */
01048         res = (g->lastmajormem == 0);  /* 1 if will do major collection */
01049         luaC_forcestep(L);  /* do a single step */
01050       }
01051       else {
01052         while (data-- >= 0) {
01053           luaC_forcestep(L);
01054           if (g->gcstate == GCSpause) {  /* end of cycle? */
01055             res = 1;  /* signal it */
01056             break;
01057           }
01058         }
01059       }
01060       break;
01061     }
01062     case LUA_GCSETPAUSE: {
01063       res = g->gcpause;
01064       g->gcpause = data;
01065       break;
01066     }
01067     case LUA_GCSETMAJORINC: {
01068       res = g->gcmajorinc;
01069       g->gcmajorinc = data;
01070       break;
01071     }
01072     case LUA_GCSETSTEPMUL: {
01073       res = g->gcstepmul;
01074       g->gcstepmul = data;
01075       break;
01076     }
01077     case LUA_GCISRUNNING: {
01078       res = g->gcrunning;
01079       break;
01080     }
01081     case LUA_GCGEN: {  /* change collector to generational mode */
01082       luaC_changemode(L, KGC_GEN);
01083       break;
01084     }
01085     case LUA_GCINC: {  /* change collector to incremental mode */
01086       luaC_changemode(L, KGC_NORMAL);
01087       break;
01088     }
01089     default: res = -1;  /* invalid option */
01090   }
01091   lua_unlock(L);
01092   return res;
01093 }
01094 
01095 
01096 
01097 /*
01098 ** miscellaneous functions
01099 */
01100 
01101 
01102 LUA_API int lua_error (lua_State *L) {
01103   lua_lock(L);
01104   api_checknelems(L, 1);
01105   luaG_errormsg(L);
01106   lua_unlock(L);
01107   return 0;  /* to avoid warnings */
01108 }
01109 
01110 
01111 LUA_API int lua_next (lua_State *L, int idx) {
01112   StkId t;
01113   int more;
01114   lua_lock(L);
01115   t = index2addr(L, idx);
01116   api_check(L, ttistable(t), "table expected");
01117   more = luaH_next(L, hvalue(t), L->top - 1);
01118   if (more) {
01119     api_incr_top(L);
01120   }
01121   else  /* no more elements */
01122     L->top -= 1;  /* remove key */
01123   lua_unlock(L);
01124   return more;
01125 }
01126 
01127 
01128 LUA_API void lua_concat (lua_State *L, int n) {
01129   lua_lock(L);
01130   api_checknelems(L, n);
01131   if (n >= 2) {
01132     luaC_checkGC(L);
01133     luaV_concat(L, n);
01134   }
01135   else if (n == 0) {  /* push empty string */
01136     setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
01137     api_incr_top(L);
01138   }
01139   /* else n == 1; nothing to do */
01140   lua_unlock(L);
01141 }
01142 
01143 
01144 LUA_API void lua_len (lua_State *L, int idx) {
01145   StkId t;
01146   lua_lock(L);
01147   t = index2addr(L, idx);
01148   luaV_objlen(L, L->top, t);
01149   api_incr_top(L);
01150   lua_unlock(L);
01151 }
01152 
01153 
01154 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
01155   lua_Alloc f;
01156   lua_lock(L);
01157   if (ud) *ud = G(L)->ud;
01158   f = G(L)->frealloc;
01159   lua_unlock(L);
01160   return f;
01161 }
01162 
01163 
01164 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
01165   lua_lock(L);
01166   G(L)->ud = ud;
01167   G(L)->frealloc = f;
01168   lua_unlock(L);
01169 }
01170 
01171 
01172 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
01173   Udata *u;
01174   lua_lock(L);
01175   luaC_checkGC(L);
01176   u = luaS_newudata(L, size, NULL);
01177   setuvalue(L, L->top, u);
01178   api_incr_top(L);
01179   lua_unlock(L);
01180   return u + 1;
01181 }
01182 
01183 
01184 
01185 static const char *aux_upvalue (StkId fi, int n, TValue **val,
01186                                 GCObject **owner) {
01187   switch (ttype(fi)) {
01188     case LUA_TCCL: {  /* C closure */
01189       CClosure *f = clCvalue(fi);
01190       if (!(1 <= n && n <= f->nupvalues)) return NULL;
01191       *val = &f->upvalue[n-1];
01192       if (owner) *owner = obj2gco(f);
01193       return "";
01194     }
01195     case LUA_TLCL: {  /* Lua closure */
01196       LClosure *f = clLvalue(fi);
01197       TString *name;
01198       Proto *p = f->p;
01199       if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
01200       *val = f->upvals[n-1]->v;
01201       if (owner) *owner = obj2gco(f->upvals[n - 1]);
01202       name = p->upvalues[n-1].name;
01203       return (name == NULL) ? "" : getstr(name);
01204     }
01205     default: return NULL;  /* not a closure */
01206   }
01207 }
01208 
01209 
01210 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
01211   const char *name;
01212   TValue *val = NULL;  /* to avoid warnings */
01213   lua_lock(L);
01214   name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL);
01215   if (name) {
01216     setobj2s(L, L->top, val);
01217     api_incr_top(L);
01218   }
01219   lua_unlock(L);
01220   return name;
01221 }
01222 
01223 
01224 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
01225   const char *name;
01226   TValue *val = NULL;  /* to avoid warnings */
01227   GCObject *owner = NULL;  /* to avoid warnings */
01228   StkId fi;
01229   lua_lock(L);
01230   fi = index2addr(L, funcindex);
01231   api_checknelems(L, 1);
01232   name = aux_upvalue(fi, n, &val, &owner);
01233   if (name) {
01234     L->top--;
01235     setobj(L, val, L->top);
01236     luaC_barrier(L, owner, L->top);
01237   }
01238   lua_unlock(L);
01239   return name;
01240 }
01241 
01242 
01243 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
01244   LClosure *f;
01245   StkId fi = index2addr(L, fidx);
01246   api_check(L, ttisLclosure(fi), "Lua function expected");
01247   f = clLvalue(fi);
01248   api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
01249   if (pf) *pf = f;
01250   return &f->upvals[n - 1];  /* get its upvalue pointer */
01251 }
01252 
01253 
01254 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
01255   StkId fi = index2addr(L, fidx);
01256   switch (ttype(fi)) {
01257     case LUA_TLCL: {  /* lua closure */
01258       return *getupvalref(L, fidx, n, NULL);
01259     }
01260     case LUA_TCCL: {  /* C closure */
01261       CClosure *f = clCvalue(fi);
01262       api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
01263       return &f->upvalue[n - 1];
01264     }
01265     default: {
01266       api_check(L, 0, "closure expected");
01267       return NULL;
01268     }
01269   }
01270 }
01271 
01272 
01273 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
01274                                             int fidx2, int n2) {
01275   LClosure *f1;
01276   UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
01277   UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
01278   *up1 = *up2;
01279   luaC_objbarrier(L, f1, *up2);
01280 }
01281 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:03 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs