2 ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
4 ** See Copyright Notice in lua.h
34 const char lua_ident[] =
35 "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n"
36 "$Authors: " LUA_AUTHORS " $\n"
37 "$URL: www.lua.org $\n";
41 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
43 #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
45 #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
49 static TValue *index2adr (lua_State *L, int idx) {
51 TValue *o = L->base + (idx - 1);
52 api_check(L, idx <= L->ci->top - L->base);
53 if (o >= L->top) return cast(TValue *, luaO_nilobject);
56 else if (idx > LUA_REGISTRYINDEX) {
57 api_check(L, idx != 0 && -idx <= L->top - L->base);
60 else switch (idx) { /* pseudo-indices */
61 case LUA_REGISTRYINDEX: return registry(L);
62 case LUA_ENVIRONINDEX: {
63 Closure *func = curr_func(L);
64 sethvalue(L, &L->env, func->c.env);
67 case LUA_GLOBALSINDEX: return gt(L);
69 Closure *func = curr_func(L);
70 idx = LUA_GLOBALSINDEX - idx;
71 return (idx <= func->c.nupvalues)
72 ? &func->c.upvalue[idx-1]
73 : cast(TValue *, luaO_nilobject);
79 static Table *getcurrenv (lua_State *L) {
80 if (L->ci == L->base_ci) /* no enclosing function? */
81 return hvalue(gt(L)); /* use global table as environment */
83 Closure *func = curr_func(L);
89 void luaA_pushobject (lua_State *L, const TValue *o) {
90 setobj2s(L, L->top, o);
95 LUA_API int lua_checkstack (lua_State *L, int size) {
98 if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)
99 res = 0; /* stack overflow */
101 luaD_checkstack(L, size);
102 if (L->ci->top < L->top + size)
103 L->ci->top = L->top + size;
110 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
112 if (from == to) return;
114 api_checknelems(from, n);
115 api_check(from, G(from) == G(to));
116 api_check(from, to->ci->top - to->top >= n);
118 for (i = 0; i < n; i++) {
119 setobj2s(to, to->top++, from->top + i);
125 LUA_API void lua_setlevel (lua_State *from, lua_State *to) {
126 to->nCcalls = from->nCcalls;
130 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
134 G(L)->panic = panicf;
140 LUA_API lua_State *lua_newthread (lua_State *L) {
144 L1 = luaE_newthread(L);
145 setthvalue(L, L->top, L1);
148 luai_userstatethread(L, L1);
155 ** basic stack manipulation
159 LUA_API int lua_gettop (lua_State *L) {
160 return cast_int(L->top - L->base);
164 LUA_API void lua_settop (lua_State *L, int idx) {
167 api_check(L, idx <= L->stack_last - L->base);
168 while (L->top < L->base + idx)
169 setnilvalue(L->top++);
170 L->top = L->base + idx;
173 api_check(L, -(idx+1) <= (L->top - L->base));
174 L->top += idx+1; /* `subtract' index (index is negative) */
180 LUA_API void lua_remove (lua_State *L, int idx) {
183 p = index2adr(L, idx);
184 api_checkvalidindex(L, p);
185 while (++p < L->top) setobjs2s(L, p-1, p);
191 LUA_API void lua_insert (lua_State *L, int idx) {
195 p = index2adr(L, idx);
196 api_checkvalidindex(L, p);
197 for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
198 setobjs2s(L, p, L->top);
203 LUA_API void lua_replace (lua_State *L, int idx) {
206 /* explicit test for incompatible code */
207 if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)
208 luaG_runerror(L, "no calling environment");
209 api_checknelems(L, 1);
210 o = index2adr(L, idx);
211 api_checkvalidindex(L, o);
212 if (idx == LUA_ENVIRONINDEX) {
213 Closure *func = curr_func(L);
214 api_check(L, ttistable(L->top - 1));
215 func->c.env = hvalue(L->top - 1);
216 luaC_barrier(L, func, L->top - 1);
219 setobj(L, o, L->top - 1);
220 if (idx < LUA_GLOBALSINDEX) /* function upvalue? */
221 luaC_barrier(L, curr_func(L), L->top - 1);
228 LUA_API void lua_pushvalue (lua_State *L, int idx) {
230 setobj2s(L, L->top, index2adr(L, idx));
238 ** access functions (stack -> C)
242 LUA_API int lua_type (lua_State *L, int idx) {
243 StkId o = index2adr(L, idx);
244 return (o == luaO_nilobject) ? LUA_TNONE : ttype_ext(o);
248 LUA_API const char *lua_typename (lua_State *L, int t) {
251 lua_assert( t!= LUA_TINT );
253 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
257 LUA_API int lua_iscfunction (lua_State *L, int idx) {
258 StkId o = index2adr(L, idx);
259 return iscfunction(o);
263 LUA_API int lua_isnumber (lua_State *L, int idx) {
265 const TValue *o = index2adr(L, idx);
266 return tonumber(o, &n);
270 LUA_API int lua_isinteger (lua_State *L, int idx) {
273 const TValue *o = index2adr(L, idx);
275 return tonumber(o,&tmp) && (ttisint(o) || tt_integer_valued(o,&dum));
277 return tonumber(o,&tmp) && tt_integer_valued(o,&dum);
282 LUA_API int lua_isstring (lua_State *L, int idx) {
283 int t = lua_type(L, idx);
284 return (t == LUA_TSTRING || t == LUA_TNUMBER);
288 LUA_API int lua_isuserdata (lua_State *L, int idx) {
289 const TValue *o = index2adr(L, idx);
290 return (ttisuserdata(o) || ttislightuserdata(o));
294 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
295 StkId o1 = index2adr(L, index1);
296 StkId o2 = index2adr(L, index2);
297 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
298 : luaO_rawequalObj(o1, o2);
302 LUA_API int lua_equal (lua_State *L, int index1, int index2) {
305 lua_lock(L); /* may call tag method */
306 o1 = index2adr(L, index1);
307 o2 = index2adr(L, index2);
308 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
314 LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
317 lua_lock(L); /* may call tag method */
318 o1 = index2adr(L, index1);
319 o2 = index2adr(L, index2);
320 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
321 : luaV_lessthan(L, o1, o2);
327 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
329 const TValue *o = index2adr(L, idx);
330 if (tonumber(o, &n)) {
332 if (nvalue_img(o) != 0)
333 luaG_runerror(L, "expecting a real number");
341 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
343 /* Lua 5.1 documented behaviour is to return nonzero for non-integer:
345 * "If the number is not an integer, it is truncated in some non-specified way."
347 #ifdef LUA_COMPAT_TOINTEGER
348 /* Lua 5.1 compatible */
349 const TValue *o = index2adr(L, idx);
350 if (tonumber(o, &n)) {
354 if (ttisint(o)) return ivalue(o);
357 if (nvalue_img_fast(o) != 0)
358 luaG_runerror(L, "expecting a real number");
361 lua_number2integer(i, d);
366 const TValue *o = index2adr(L, idx);
367 if (tonumber(o, &n)) {
369 if (ttisint(o)) return ivalue(o);
370 if (tt_integer_valued(o,&i)) return i;
378 LUA_API lua_Complex lua_tocomplex (lua_State *L, int idx) {
380 const TValue *o = index2adr(L, idx);
381 if (tonumber(o, &tmp))
382 return nvalue_complex(o);
388 LUA_API int lua_toboolean (lua_State *L, int idx) {
389 const TValue *o = index2adr(L, idx);
390 return !l_isfalse(o);
394 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
395 StkId o = index2adr(L, idx);
396 if (!ttisstring(o)) {
397 lua_lock(L); /* `luaV_tostring' may create a new string */
398 if (!luaV_tostring(L, o)) { /* conversion failed? */
399 if (len != NULL) *len = 0;
404 o = index2adr(L, idx); /* previous call may reallocate the stack */
407 if (len != NULL) *len = tsvalue(o)->len;
412 LUA_API size_t lua_objlen (lua_State *L, int idx) {
413 StkId o = index2adr(L, idx);
415 case LUA_TSTRING: return tsvalue(o)->len;
416 case LUA_TUSERDATA: return uvalue(o)->len;
417 case LUA_TTABLE: return luaH_getn(hvalue(o));
423 lua_lock(L); /* `luaV_tostring' may create a new string */
424 l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
433 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
434 StkId o = index2adr(L, idx);
435 return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
439 LUA_API void *lua_touserdata (lua_State *L, int idx) {
440 StkId o = index2adr(L, idx);
442 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
443 case LUA_TLIGHTUSERDATA: return pvalue(o);
444 default: return NULL;
449 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
450 StkId o = index2adr(L, idx);
451 return (!ttisthread(o)) ? NULL : thvalue(o);
455 LUA_API const void *lua_topointer (lua_State *L, int idx) {
456 StkId o = index2adr(L, idx);
458 case LUA_TTABLE: return hvalue(o);
459 case LUA_TFUNCTION: return clvalue(o);
460 case LUA_TTHREAD: return thvalue(o);
462 case LUA_TLIGHTUSERDATA:
463 return lua_touserdata(L, idx);
464 default: return NULL;
471 ** push functions (C -> stack)
475 LUA_API void lua_pushnil (lua_State *L) {
483 /* 'lua_pushnumber()' may lose accuracy on integers, 'lua_pushinteger' will not.
485 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
487 setnvalue(L->top, n);
493 LUA_API void lua_pushinteger (lua_State *L, lua_Integer i) {
495 setivalue(L->top, i);
502 LUA_API void lua_pushcomplex (lua_State *L, lua_Complex v) {
504 setnvalue_complex( L->top, v );
511 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
514 setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
520 LUA_API void lua_pushstring (lua_State *L, const char *s) {
524 lua_pushlstring(L, s, strlen(s));
528 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
533 ret = luaO_pushvfstring(L, fmt, argp);
539 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
545 ret = luaO_pushvfstring(L, fmt, argp);
552 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
556 api_checknelems(L, n);
557 cl = luaF_newCclosure(L, n, getcurrenv(L));
561 setobj2n(L, &cl->c.upvalue[n], L->top+n);
562 setclvalue(L, L->top, cl);
563 lua_assert(iswhite(obj2gco(cl)));
569 LUA_API void lua_pushboolean (lua_State *L, int b) {
571 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
577 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
579 setpvalue(L->top, p);
585 LUA_API int lua_pushthread (lua_State *L) {
587 setthvalue(L, L->top, L);
590 return (G(L)->mainthread == L);
596 ** get functions (Lua -> stack)
600 LUA_API void lua_gettable (lua_State *L, int idx) {
603 t = index2adr(L, idx);
604 api_checkvalidindex(L, t);
605 luaV_gettable(L, t, L->top - 1, L->top - 1);
610 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
614 t = index2adr(L, idx);
615 api_checkvalidindex(L, t);
616 setsvalue(L, &key, luaS_new(L, k));
617 luaV_gettable(L, t, &key, L->top);
623 LUA_API void lua_rawget (lua_State *L, int idx) {
626 t = index2adr(L, idx);
627 api_check(L, ttistable(t));
628 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
633 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
636 o = index2adr(L, idx);
637 api_check(L, ttistable(o));
638 setobj2s(L, L->top, luaH_getint(hvalue(o), n));
644 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
647 sethvalue(L, L->top, luaH_new(L, narray, nrec));
653 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
658 obj = index2adr(L, objindex);
659 switch (ttype(obj)) {
661 mt = hvalue(obj)->metatable;
664 mt = uvalue(obj)->metatable;
668 mt = G(L)->mt[LUA_TNUMBER];
672 mt = G(L)->mt[ttype(obj)];
678 sethvalue(L, L->top, mt);
687 LUA_API void lua_getfenv (lua_State *L, int idx) {
690 o = index2adr(L, idx);
691 api_checkvalidindex(L, o);
694 sethvalue(L, L->top, clvalue(o)->c.env);
697 sethvalue(L, L->top, uvalue(o)->env);
700 setobj2s(L, L->top, gt(thvalue(o)));
712 ** set functions (stack -> Lua)
716 LUA_API void lua_settable (lua_State *L, int idx) {
719 api_checknelems(L, 2);
720 t = index2adr(L, idx);
721 api_checkvalidindex(L, t);
722 luaV_settable(L, t, L->top - 2, L->top - 1);
723 L->top -= 2; /* pop index and value */
728 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
732 api_checknelems(L, 1);
733 t = index2adr(L, idx);
734 api_checkvalidindex(L, t);
735 setsvalue(L, &key, luaS_new(L, k));
736 luaV_settable(L, t, &key, L->top - 1);
737 L->top--; /* pop value */
742 LUA_API void lua_rawset (lua_State *L, int idx) {
745 api_checknelems(L, 2);
746 t = index2adr(L, idx);
747 api_check(L, ttistable(t));
748 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
749 luaC_barriert(L, hvalue(t), L->top-1);
755 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
758 api_checknelems(L, 1);
759 o = index2adr(L, idx);
760 api_check(L, ttistable(o));
761 setobj2t(L, luaH_setint(L, hvalue(o), n), L->top-1);
762 luaC_barriert(L, hvalue(o), L->top-1);
768 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
772 api_checknelems(L, 1);
773 obj = index2adr(L, objindex);
774 api_checkvalidindex(L, obj);
775 if (ttisnil(L->top - 1))
778 api_check(L, ttistable(L->top - 1));
779 mt = hvalue(L->top - 1);
781 switch (ttype(obj)) {
783 hvalue(obj)->metatable = mt;
785 luaC_objbarriert(L, hvalue(obj), mt);
788 case LUA_TUSERDATA: {
789 uvalue(obj)->metatable = mt;
791 luaC_objbarrier(L, rawuvalue(obj), mt);
795 G(L)->mt[ttype_ext(obj)] = mt;
805 LUA_API int lua_setfenv (lua_State *L, int idx) {
809 api_checknelems(L, 1);
810 o = index2adr(L, idx);
811 api_checkvalidindex(L, o);
812 api_check(L, ttistable(L->top - 1));
815 clvalue(o)->c.env = hvalue(L->top - 1);
818 uvalue(o)->env = hvalue(L->top - 1);
821 sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
827 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
835 ** `load' and `call' functions (run Lua code)
839 #define adjustresults(L,nres) \
840 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
843 #define checkresults(L,na,nr) \
844 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
847 LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
850 api_checknelems(L, nargs+1);
851 checkresults(L, nargs, nresults);
852 func = L->top - (nargs+1);
853 luaD_call(L, func, nresults);
854 adjustresults(L, nresults);
861 ** Execute a protected call.
863 struct CallS { /* data to `f_call' */
869 static void f_call (lua_State *L, void *ud) {
870 struct CallS *c = cast(struct CallS *, ud);
871 luaD_call(L, c->func, c->nresults);
876 LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
881 api_checknelems(L, nargs+1);
882 checkresults(L, nargs, nresults);
886 StkId o = index2adr(L, errfunc);
887 api_checkvalidindex(L, o);
888 func = savestack(L, o);
890 c.func = L->top - (nargs+1); /* function to be called */
891 c.nresults = nresults;
892 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
893 adjustresults(L, nresults);
900 ** Execute a protected C call.
902 struct CCallS { /* data to `f_Ccall' */
908 static void f_Ccall (lua_State *L, void *ud) {
909 struct CCallS *c = cast(struct CCallS *, ud);
911 cl = luaF_newCclosure(L, 0, getcurrenv(L));
913 setclvalue(L, L->top, cl); /* push function */
915 setpvalue(L->top, c->ud); /* push only argument */
917 luaD_call(L, L->top - 2, 0);
921 LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
927 status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
933 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
934 const char *chunkname) {
938 if (!chunkname) chunkname = "?";
939 luaZ_init(L, &z, reader, data);
940 status = luaD_protectedparser(L, &z, chunkname);
946 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
950 api_checknelems(L, 1);
953 status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
961 LUA_API int lua_status (lua_State *L) {
967 ** Garbage-collection function
970 LUA_API int lua_gc (lua_State *L, int what, int data) {
977 g->GCthreshold = MAX_LUMEM;
980 case LUA_GCRESTART: {
981 g->GCthreshold = g->totalbytes;
984 case LUA_GCCOLLECT: {
989 /* GC values are expressed in Kbytes: #bytes/2^10 */
990 res = cast_int(g->totalbytes >> 10);
994 res = cast_int(g->totalbytes & 0x3ff);
998 lu_mem a = (cast(lu_mem, data) << 10);
999 if (a <= g->totalbytes)
1000 g->GCthreshold = g->totalbytes - a;
1003 while (g->GCthreshold <= g->totalbytes) {
1005 if (g->gcstate == GCSpause) { /* end of cycle? */
1006 res = 1; /* signal it */
1012 case LUA_GCSETPAUSE: {
1017 case LUA_GCSETSTEPMUL: {
1019 g->gcstepmul = data;
1022 default: res = -1; /* invalid option */
1031 ** miscellaneous functions
1035 LUA_API int lua_error (lua_State *L) {
1037 api_checknelems(L, 1);
1040 return 0; /* to avoid warnings */
1044 LUA_API int lua_next (lua_State *L, int idx) {
1048 t = index2adr(L, idx);
1049 api_check(L, ttistable(t));
1050 more = luaH_next(L, hvalue(t), L->top - 1);
1054 else /* no more elements */
1055 L->top -= 1; /* remove key */
1061 LUA_API void lua_concat (lua_State *L, int n) {
1063 api_checknelems(L, n);
1066 luaV_concat(L, n, cast_int(L->top - L->base) - 1);
1069 else if (n == 0) { /* push empty string */
1070 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1073 /* else n == 1; nothing to do */
1078 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1081 if (ud) *ud = G(L)->ud;
1088 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1096 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1100 u = luaS_newudata(L, size, getcurrenv(L));
1101 setuvalue(L, L->top, u);
1110 static const char *aux_upvalue (StkId fi, int n, TValue **val) {
1112 if (!ttisfunction(fi)) return NULL;
1115 if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1116 *val = &f->c.upvalue[n-1];
1121 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1122 *val = f->l.upvals[n-1]->v;
1123 return getstr(p->upvalues[n-1]);
1128 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1132 name = aux_upvalue(index2adr(L, funcindex), n, &val);
1134 setobj2s(L, L->top, val);
1142 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1147 fi = index2adr(L, funcindex);
1148 api_checknelems(L, 1);
1149 name = aux_upvalue(fi, n, &val);
1152 setobj(L, val, L->top);
1153 luaC_barrier(L, clvalue(fi), L->top);
1160 /* Help function for 'luaB_tonumber()', avoids multiple str->number
1161 * conversions for Lua "tonumber()".
1163 * Also pushes floating point numbers with integer value as integer, which
1164 * can be used by 'tonumber()' in scripts to bring values back to integer
1167 * Note: The 'back to integer realm' is _not_ to affect string conversions:
1168 * 'tonumber("4294967295.1")' should give a floating point value, although
1169 * the value would be 4294967296 (and storable in int64 realm).
1172 int lua_pushvalue_as_number (lua_State *L, int idx)
1174 const TValue *o = index2adr(L, idx);
1177 if (ttisnumber(o)) {
1178 if ( (!ttisint(o)) && tt_integer_valued(o,&i)) {
1179 lua_pushinteger( L, i );
1182 } else if (!tonumber(o, &tmp)) {
1185 if (ttisint(o)) lua_pushinteger( L, ivalue(o) );
1186 else lua_pushnumber( L, nvalue_fast(o) );