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(o);
248 LUA_API const char *lua_typename (lua_State *L, int t) {
250 return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
254 LUA_API int lua_iscfunction (lua_State *L, int idx) {
255 StkId o = index2adr(L, idx);
256 return iscfunction(o);
260 LUA_API int lua_isnumber (lua_State *L, int idx) {
262 const TValue *o = index2adr(L, idx);
263 return tonumber(o, &n);
267 LUA_API int lua_isstring (lua_State *L, int idx) {
268 int t = lua_type(L, idx);
269 return (t == LUA_TSTRING || t == LUA_TNUMBER);
273 LUA_API int lua_isuserdata (lua_State *L, int idx) {
274 const TValue *o = index2adr(L, idx);
275 return (ttisuserdata(o) || ttislightuserdata(o));
279 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
280 StkId o1 = index2adr(L, index1);
281 StkId o2 = index2adr(L, index2);
282 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
283 : luaO_rawequalObj(o1, o2);
287 LUA_API int lua_equal (lua_State *L, int index1, int index2) {
290 lua_lock(L); /* may call tag method */
291 o1 = index2adr(L, index1);
292 o2 = index2adr(L, index2);
293 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);
299 LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
302 lua_lock(L); /* may call tag method */
303 o1 = index2adr(L, index1);
304 o2 = index2adr(L, index2);
305 i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
306 : luaV_lessthan(L, o1, o2);
313 LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
315 const TValue *o = index2adr(L, idx);
323 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {
325 const TValue *o = index2adr(L, idx);
326 if (tonumber(o, &n)) {
328 lua_Number num = nvalue(o);
329 lua_number2integer(res, num);
337 LUA_API int lua_toboolean (lua_State *L, int idx) {
338 const TValue *o = index2adr(L, idx);
339 return !l_isfalse(o);
343 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
344 StkId o = index2adr(L, idx);
345 if (!ttisstring(o)) {
346 lua_lock(L); /* `luaV_tostring' may create a new string */
347 if (!luaV_tostring(L, o)) { /* conversion failed? */
348 if (len != NULL) *len = 0;
353 o = index2adr(L, idx); /* previous call may reallocate the stack */
356 if (len != NULL) *len = tsvalue(o)->len;
361 LUA_API size_t lua_objlen (lua_State *L, int idx) {
362 StkId o = index2adr(L, idx);
364 case LUA_TSTRING: return tsvalue(o)->len;
365 case LUA_TUSERDATA: return uvalue(o)->len;
366 case LUA_TTABLE: return luaH_getn(hvalue(o));
369 lua_lock(L); /* `luaV_tostring' may create a new string */
370 l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);
379 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
380 StkId o = index2adr(L, idx);
381 return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
385 LUA_API void *lua_touserdata (lua_State *L, int idx) {
386 StkId o = index2adr(L, idx);
388 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
389 case LUA_TLIGHTUSERDATA: return pvalue(o);
390 default: return NULL;
395 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
396 StkId o = index2adr(L, idx);
397 return (!ttisthread(o)) ? NULL : thvalue(o);
401 LUA_API const void *lua_topointer (lua_State *L, int idx) {
402 StkId o = index2adr(L, idx);
404 case LUA_TTABLE: return hvalue(o);
405 case LUA_TFUNCTION: return clvalue(o);
406 case LUA_TTHREAD: return thvalue(o);
408 case LUA_TLIGHTUSERDATA:
409 return lua_touserdata(L, idx);
410 default: return NULL;
417 ** push functions (C -> stack)
421 LUA_API void lua_pushnil (lua_State *L) {
429 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
431 setnvalue(L->top, n);
437 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
439 setnvalue(L->top, cast_num(n));
445 LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
448 setsvalue2s(L, L->top, luaS_newlstr(L, s, len));
454 LUA_API void lua_pushstring (lua_State *L, const char *s) {
458 lua_pushlstring(L, s, strlen(s));
462 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
467 ret = luaO_pushvfstring(L, fmt, argp);
473 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
479 ret = luaO_pushvfstring(L, fmt, argp);
486 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
490 api_checknelems(L, n);
491 cl = luaF_newCclosure(L, n, getcurrenv(L));
495 setobj2n(L, &cl->c.upvalue[n], L->top+n);
496 setclvalue(L, L->top, cl);
497 lua_assert(iswhite(obj2gco(cl)));
503 LUA_API void lua_pushboolean (lua_State *L, int b) {
505 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
511 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
513 setpvalue(L->top, p);
519 LUA_API int lua_pushthread (lua_State *L) {
521 setthvalue(L, L->top, L);
524 return (G(L)->mainthread == L);
530 ** get functions (Lua -> stack)
534 LUA_API void lua_gettable (lua_State *L, int idx) {
537 t = index2adr(L, idx);
538 api_checkvalidindex(L, t);
539 luaV_gettable(L, t, L->top - 1, L->top - 1);
544 LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
548 t = index2adr(L, idx);
549 api_checkvalidindex(L, t);
550 setsvalue(L, &key, luaS_new(L, k));
551 luaV_gettable(L, t, &key, L->top);
557 LUA_API void lua_rawget (lua_State *L, int idx) {
560 t = index2adr(L, idx);
561 api_check(L, ttistable(t));
562 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
567 LUA_API void lua_rawgeti (lua_State *L, int idx, int n) {
570 o = index2adr(L, idx);
571 api_check(L, ttistable(o));
572 setobj2s(L, L->top, luaH_getnum(hvalue(o), n));
578 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
581 sethvalue(L, L->top, luaH_new(L, narray, nrec));
587 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
592 obj = index2adr(L, objindex);
593 switch (ttype(obj)) {
595 mt = hvalue(obj)->metatable;
598 mt = uvalue(obj)->metatable;
601 mt = G(L)->mt[ttype(obj)];
607 sethvalue(L, L->top, mt);
616 LUA_API void lua_getfenv (lua_State *L, int idx) {
619 o = index2adr(L, idx);
620 api_checkvalidindex(L, o);
623 sethvalue(L, L->top, clvalue(o)->c.env);
626 sethvalue(L, L->top, uvalue(o)->env);
629 setobj2s(L, L->top, gt(thvalue(o)));
641 ** set functions (stack -> Lua)
645 LUA_API void lua_settable (lua_State *L, int idx) {
648 api_checknelems(L, 2);
649 t = index2adr(L, idx);
650 api_checkvalidindex(L, t);
651 luaV_settable(L, t, L->top - 2, L->top - 1);
652 L->top -= 2; /* pop index and value */
657 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
661 api_checknelems(L, 1);
662 t = index2adr(L, idx);
663 api_checkvalidindex(L, t);
664 setsvalue(L, &key, luaS_new(L, k));
665 luaV_settable(L, t, &key, L->top - 1);
666 L->top--; /* pop value */
671 LUA_API void lua_rawset (lua_State *L, int idx) {
674 api_checknelems(L, 2);
675 t = index2adr(L, idx);
676 api_check(L, ttistable(t));
677 setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);
678 luaC_barriert(L, hvalue(t), L->top-1);
684 LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
687 api_checknelems(L, 1);
688 o = index2adr(L, idx);
689 api_check(L, ttistable(o));
690 setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);
691 luaC_barriert(L, hvalue(o), L->top-1);
697 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
701 api_checknelems(L, 1);
702 obj = index2adr(L, objindex);
703 api_checkvalidindex(L, obj);
704 if (ttisnil(L->top - 1))
707 api_check(L, ttistable(L->top - 1));
708 mt = hvalue(L->top - 1);
710 switch (ttype(obj)) {
712 hvalue(obj)->metatable = mt;
714 luaC_objbarriert(L, hvalue(obj), mt);
717 case LUA_TUSERDATA: {
718 uvalue(obj)->metatable = mt;
720 luaC_objbarrier(L, rawuvalue(obj), mt);
724 G(L)->mt[ttype(obj)] = mt;
734 LUA_API int lua_setfenv (lua_State *L, int idx) {
738 api_checknelems(L, 1);
739 o = index2adr(L, idx);
740 api_checkvalidindex(L, o);
741 api_check(L, ttistable(L->top - 1));
744 clvalue(o)->c.env = hvalue(L->top - 1);
747 uvalue(o)->env = hvalue(L->top - 1);
750 sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
756 if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
764 ** `load' and `call' functions (run Lua code)
768 #define adjustresults(L,nres) \
769 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
772 #define checkresults(L,na,nr) \
773 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
776 LUA_API void lua_call (lua_State *L, int nargs, int nresults) {
779 api_checknelems(L, nargs+1);
780 checkresults(L, nargs, nresults);
781 func = L->top - (nargs+1);
782 luaD_call(L, func, nresults);
783 adjustresults(L, nresults);
790 ** Execute a protected call.
792 struct CallS { /* data to `f_call' */
798 static void f_call (lua_State *L, void *ud) {
799 struct CallS *c = cast(struct CallS *, ud);
800 luaD_call(L, c->func, c->nresults);
805 LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
810 api_checknelems(L, nargs+1);
811 checkresults(L, nargs, nresults);
815 StkId o = index2adr(L, errfunc);
816 api_checkvalidindex(L, o);
817 func = savestack(L, o);
819 c.func = L->top - (nargs+1); /* function to be called */
820 c.nresults = nresults;
821 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
822 adjustresults(L, nresults);
829 ** Execute a protected C call.
831 struct CCallS { /* data to `f_Ccall' */
837 static void f_Ccall (lua_State *L, void *ud) {
838 struct CCallS *c = cast(struct CCallS *, ud);
840 cl = luaF_newCclosure(L, 0, getcurrenv(L));
842 setclvalue(L, L->top, cl); /* push function */
844 setpvalue(L->top, c->ud); /* push only argument */
846 luaD_call(L, L->top - 2, 0);
850 LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {
856 status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
862 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
863 const char *chunkname) {
867 if (!chunkname) chunkname = "?";
868 luaZ_init(L, &z, reader, data);
869 status = luaD_protectedparser(L, &z, chunkname);
875 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {
879 api_checknelems(L, 1);
882 status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);
890 LUA_API int lua_status (lua_State *L) {
896 ** Garbage-collection function
899 LUA_API int lua_gc (lua_State *L, int what, int data) {
906 g->GCthreshold = MAX_LUMEM;
909 case LUA_GCRESTART: {
910 g->GCthreshold = g->totalbytes;
913 case LUA_GCCOLLECT: {
918 /* GC values are expressed in Kbytes: #bytes/2^10 */
919 res = cast_int(g->totalbytes >> 10);
923 res = cast_int(g->totalbytes & 0x3ff);
927 lu_mem a = (cast(lu_mem, data) << 10);
928 if (a <= g->totalbytes)
929 g->GCthreshold = g->totalbytes - a;
932 while (g->GCthreshold <= g->totalbytes) {
934 if (g->gcstate == GCSpause) { /* end of cycle? */
935 res = 1; /* signal it */
941 case LUA_GCSETPAUSE: {
946 case LUA_GCSETSTEPMUL: {
951 default: res = -1; /* invalid option */
960 ** miscellaneous functions
964 LUA_API int lua_error (lua_State *L) {
966 api_checknelems(L, 1);
969 return 0; /* to avoid warnings */
973 LUA_API int lua_next (lua_State *L, int idx) {
977 t = index2adr(L, idx);
978 api_check(L, ttistable(t));
979 more = luaH_next(L, hvalue(t), L->top - 1);
983 else /* no more elements */
984 L->top -= 1; /* remove key */
990 LUA_API void lua_concat (lua_State *L, int n) {
992 api_checknelems(L, n);
995 luaV_concat(L, n, cast_int(L->top - L->base) - 1);
998 else if (n == 0) { /* push empty string */
999 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1002 /* else n == 1; nothing to do */
1007 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1010 if (ud) *ud = G(L)->ud;
1017 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1025 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1029 u = luaS_newudata(L, size, getcurrenv(L));
1030 setuvalue(L, L->top, u);
1039 static const char *aux_upvalue (StkId fi, int n, TValue **val) {
1041 if (!ttisfunction(fi)) return NULL;
1044 if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1045 *val = &f->c.upvalue[n-1];
1050 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1051 *val = f->l.upvals[n-1]->v;
1052 return getstr(p->upvalues[n-1]);
1057 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1061 name = aux_upvalue(index2adr(L, funcindex), n, &val);
1063 setobj2s(L, L->top, val);
1071 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1076 fi = index2adr(L, funcindex);
1077 api_checknelems(L, 1);
1078 name = aux_upvalue(fi, n, &val);
1081 setobj(L, val, L->top);
1082 luaC_barrier(L, clvalue(fi), L->top);