2 ** $Id: lapi.c,v 2.55 2006/06/07 12:37:17 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 ((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
;
111 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
113 if (from
== to
) return;
115 api_checknelems(from
, n
);
116 api_check(from
, G(from
) == G(to
));
117 api_check(from
, to
->ci
->top
- to
->top
>= n
);
119 for (i
= 0; i
< n
; i
++) {
120 setobj2s(to
, to
->top
++, from
->top
+ i
);
126 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
130 G(L
)->panic
= panicf
;
136 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
140 L1
= luaE_newthread(L
);
141 setthvalue(L
, L
->top
, L1
);
144 luai_userstatethread(L
, L1
);
151 ** basic stack manipulation
155 LUA_API
int lua_gettop (lua_State
*L
) {
156 return cast_int(L
->top
- L
->base
);
160 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
163 api_check(L
, idx
<= L
->stack_last
- L
->base
);
164 while (L
->top
< L
->base
+ idx
)
165 setnilvalue(L
->top
++);
166 L
->top
= L
->base
+ idx
;
169 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
170 L
->top
+= idx
+1; /* `subtract' index (index is negative) */
176 LUA_API
void lua_remove (lua_State
*L
, int idx
) {
179 p
= index2adr(L
, idx
);
180 api_checkvalidindex(L
, p
);
181 while (++p
< L
->top
) setobjs2s(L
, p
-1, p
);
187 LUA_API
void lua_insert (lua_State
*L
, int idx
) {
191 p
= index2adr(L
, idx
);
192 api_checkvalidindex(L
, p
);
193 for (q
= L
->top
; q
>p
; q
--) setobjs2s(L
, q
, q
-1);
194 setobjs2s(L
, p
, L
->top
);
199 LUA_API
void lua_replace (lua_State
*L
, int idx
) {
202 /* explicit test for incompatible code */
203 if (idx
== LUA_ENVIRONINDEX
&& L
->ci
== L
->base_ci
)
204 luaG_runerror(L
, "no calling environment");
205 api_checknelems(L
, 1);
206 o
= index2adr(L
, idx
);
207 api_checkvalidindex(L
, o
);
208 if (idx
== LUA_ENVIRONINDEX
) {
209 Closure
*func
= curr_func(L
);
210 api_check(L
, ttistable(L
->top
- 1));
211 func
->c
.env
= hvalue(L
->top
- 1);
212 luaC_barrier(L
, func
, L
->top
- 1);
215 setobj(L
, o
, L
->top
- 1);
216 if (idx
< LUA_GLOBALSINDEX
) /* function upvalue? */
217 luaC_barrier(L
, curr_func(L
), L
->top
- 1);
224 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
226 setobj2s(L
, L
->top
, index2adr(L
, idx
));
234 ** access functions (stack -> C)
238 LUA_API
int lua_type (lua_State
*L
, int idx
) {
239 StkId o
= index2adr(L
, idx
);
240 return (o
== luaO_nilobject
) ? LUA_TNONE
: ttype(o
);
244 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
246 return (t
== LUA_TNONE
) ? "no value" : luaT_typenames
[t
];
250 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
251 StkId o
= index2adr(L
, idx
);
252 return iscfunction(o
);
256 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
258 const TValue
*o
= index2adr(L
, idx
);
259 return tonumber(o
, &n
);
263 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
264 int t
= lua_type(L
, idx
);
265 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
269 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
270 const TValue
*o
= index2adr(L
, idx
);
271 return (ttisuserdata(o
) || ttislightuserdata(o
));
275 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
276 StkId o1
= index2adr(L
, index1
);
277 StkId o2
= index2adr(L
, index2
);
278 return (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
279 : luaO_rawequalObj(o1
, o2
);
283 LUA_API
int lua_equal (lua_State
*L
, int index1
, int index2
) {
286 lua_lock(L
); /* may call tag method */
287 o1
= index2adr(L
, index1
);
288 o2
= index2adr(L
, index2
);
289 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0 : equalobj(L
, o1
, o2
);
295 LUA_API
int lua_lessthan (lua_State
*L
, int index1
, int index2
) {
298 lua_lock(L
); /* may call tag method */
299 o1
= index2adr(L
, index1
);
300 o2
= index2adr(L
, index2
);
301 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
302 : luaV_lessthan(L
, o1
, o2
);
309 LUA_API lua_Number
lua_tonumber (lua_State
*L
, int idx
) {
311 const TValue
*o
= index2adr(L
, idx
);
319 LUA_API lua_Integer
lua_tointeger (lua_State
*L
, int idx
) {
321 const TValue
*o
= index2adr(L
, idx
);
322 if (tonumber(o
, &n
)) {
324 lua_Number num
= nvalue(o
);
325 lua_number2integer(res
, num
);
333 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
334 const TValue
*o
= index2adr(L
, idx
);
335 return !l_isfalse(o
);
339 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
340 StkId o
= index2adr(L
, idx
);
341 if (!ttisstring(o
)) {
342 lua_lock(L
); /* `luaV_tostring' may create a new string */
343 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
344 if (len
!= NULL
) *len
= 0;
349 o
= index2adr(L
, idx
); /* previous call may reallocate the stack */
352 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
357 LUA_API
size_t lua_objlen (lua_State
*L
, int idx
) {
358 StkId o
= index2adr(L
, idx
);
360 case LUA_TSTRING
: return tsvalue(o
)->len
;
361 case LUA_TUSERDATA
: return uvalue(o
)->len
;
362 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
365 lua_lock(L
); /* `luaV_tostring' may create a new string */
366 l
= (luaV_tostring(L
, o
) ? tsvalue(o
)->len
: 0);
375 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
376 StkId o
= index2adr(L
, idx
);
377 return (!iscfunction(o
)) ? NULL
: clvalue(o
)->c
.f
;
381 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
382 StkId o
= index2adr(L
, idx
);
384 case LUA_TUSERDATA
: return (rawuvalue(o
) + 1);
385 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
386 default: return NULL
;
391 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
392 StkId o
= index2adr(L
, idx
);
393 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
397 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
398 StkId o
= index2adr(L
, idx
);
400 case LUA_TTABLE
: return hvalue(o
);
401 case LUA_TFUNCTION
: return clvalue(o
);
402 case LUA_TTHREAD
: return thvalue(o
);
404 case LUA_TLIGHTUSERDATA
:
405 return lua_touserdata(L
, idx
);
406 default: return NULL
;
413 ** push functions (C -> stack)
417 LUA_API
void lua_pushnil (lua_State
*L
) {
425 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
427 setnvalue(L
->top
, n
);
433 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
435 setnvalue(L
->top
, cast_num(n
));
441 LUA_API
void lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
444 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, s
, len
));
450 LUA_API
void lua_pushstring (lua_State
*L
, const char *s
) {
454 lua_pushlstring(L
, s
, strlen(s
));
458 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
463 ret
= luaO_pushvfstring(L
, fmt
, argp
);
469 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
475 ret
= luaO_pushvfstring(L
, fmt
, argp
);
482 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
486 api_checknelems(L
, n
);
487 cl
= luaF_newCclosure(L
, n
, getcurrenv(L
));
491 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+n
);
492 setclvalue(L
, L
->top
, cl
);
493 lua_assert(iswhite(obj2gco(cl
)));
499 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
501 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
507 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
509 setpvalue(L
->top
, p
);
515 LUA_API
int lua_pushthread (lua_State
*L
) {
517 setthvalue(L
, L
->top
, L
);
520 return (G(L
)->mainthread
== L
);
526 ** get functions (Lua -> stack)
530 LUA_API
void lua_gettable (lua_State
*L
, int idx
) {
533 t
= index2adr(L
, idx
);
534 api_checkvalidindex(L
, t
);
535 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
540 LUA_API
void lua_getfield (lua_State
*L
, int idx
, const char *k
) {
544 t
= index2adr(L
, idx
);
545 api_checkvalidindex(L
, t
);
546 setsvalue(L
, &key
, luaS_new(L
, k
));
547 luaV_gettable(L
, t
, &key
, L
->top
);
553 LUA_API
void lua_rawget (lua_State
*L
, int idx
) {
556 t
= index2adr(L
, idx
);
557 api_check(L
, ttistable(t
));
558 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
563 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
) {
566 o
= index2adr(L
, idx
);
567 api_check(L
, ttistable(o
));
568 setobj2s(L
, L
->top
, luaH_getnum(hvalue(o
), n
));
574 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
577 sethvalue(L
, L
->top
, luaH_new(L
, narray
, nrec
));
583 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
588 obj
= index2adr(L
, objindex
);
589 switch (ttype(obj
)) {
591 mt
= hvalue(obj
)->metatable
;
594 mt
= uvalue(obj
)->metatable
;
597 mt
= G(L
)->mt
[ttype(obj
)];
603 sethvalue(L
, L
->top
, mt
);
612 LUA_API
void lua_getfenv (lua_State
*L
, int idx
) {
615 o
= index2adr(L
, idx
);
616 api_checkvalidindex(L
, o
);
619 sethvalue(L
, L
->top
, clvalue(o
)->c
.env
);
622 sethvalue(L
, L
->top
, uvalue(o
)->env
);
625 setobj2s(L
, L
->top
, gt(thvalue(o
)));
637 ** set functions (stack -> Lua)
641 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
644 api_checknelems(L
, 2);
645 t
= index2adr(L
, idx
);
646 api_checkvalidindex(L
, t
);
647 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
648 L
->top
-= 2; /* pop index and value */
653 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
657 api_checknelems(L
, 1);
658 t
= index2adr(L
, idx
);
659 api_checkvalidindex(L
, t
);
660 setsvalue(L
, &key
, luaS_new(L
, k
));
661 luaV_settable(L
, t
, &key
, L
->top
- 1);
662 L
->top
--; /* pop value */
667 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
670 api_checknelems(L
, 2);
671 t
= index2adr(L
, idx
);
672 api_check(L
, ttistable(t
));
673 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
-2), L
->top
-1);
674 luaC_barriert(L
, hvalue(t
), L
->top
-1);
680 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
) {
683 api_checknelems(L
, 1);
684 o
= index2adr(L
, idx
);
685 api_check(L
, ttistable(o
));
686 setobj2t(L
, luaH_setnum(L
, hvalue(o
), n
), L
->top
-1);
687 luaC_barriert(L
, hvalue(o
), L
->top
-1);
693 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
697 api_checknelems(L
, 1);
698 obj
= index2adr(L
, objindex
);
699 api_checkvalidindex(L
, obj
);
700 if (ttisnil(L
->top
- 1))
703 api_check(L
, ttistable(L
->top
- 1));
704 mt
= hvalue(L
->top
- 1);
706 switch (ttype(obj
)) {
708 hvalue(obj
)->metatable
= mt
;
710 luaC_objbarriert(L
, hvalue(obj
), mt
);
713 case LUA_TUSERDATA
: {
714 uvalue(obj
)->metatable
= mt
;
716 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
720 G(L
)->mt
[ttype(obj
)] = mt
;
730 LUA_API
int lua_setfenv (lua_State
*L
, int idx
) {
734 api_checknelems(L
, 1);
735 o
= index2adr(L
, idx
);
736 api_checkvalidindex(L
, o
);
737 api_check(L
, ttistable(L
->top
- 1));
740 clvalue(o
)->c
.env
= hvalue(L
->top
- 1);
743 uvalue(o
)->env
= hvalue(L
->top
- 1);
746 sethvalue(L
, gt(thvalue(o
)), hvalue(L
->top
- 1));
752 luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
760 ** `load' and `call' functions (run Lua code)
764 #define adjustresults(L,nres) \
765 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
768 #define checkresults(L,na,nr) \
769 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
772 LUA_API
void lua_call (lua_State
*L
, int nargs
, int nresults
) {
775 api_checknelems(L
, nargs
+1);
776 checkresults(L
, nargs
, nresults
);
777 func
= L
->top
- (nargs
+1);
778 luaD_call(L
, func
, nresults
);
779 adjustresults(L
, nresults
);
786 ** Execute a protected call.
788 struct CallS
{ /* data to `f_call' */
794 static void f_call (lua_State
*L
, void *ud
) {
795 struct CallS
*c
= cast(struct CallS
*, ud
);
796 luaD_call(L
, c
->func
, c
->nresults
);
801 LUA_API
int lua_pcall (lua_State
*L
, int nargs
, int nresults
, int errfunc
) {
806 api_checknelems(L
, nargs
+1);
807 checkresults(L
, nargs
, nresults
);
811 StkId o
= index2adr(L
, errfunc
);
812 api_checkvalidindex(L
, o
);
813 func
= savestack(L
, o
);
815 c
.func
= L
->top
- (nargs
+1); /* function to be called */
816 c
.nresults
= nresults
;
817 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
818 adjustresults(L
, nresults
);
825 ** Execute a protected C call.
827 struct CCallS
{ /* data to `f_Ccall' */
833 static void f_Ccall (lua_State
*L
, void *ud
) {
834 struct CCallS
*c
= cast(struct CCallS
*, ud
);
836 cl
= luaF_newCclosure(L
, 0, getcurrenv(L
));
838 setclvalue(L
, L
->top
, cl
); /* push function */
840 setpvalue(L
->top
, c
->ud
); /* push only argument */
842 luaD_call(L
, L
->top
- 2, 0);
846 LUA_API
int lua_cpcall (lua_State
*L
, lua_CFunction func
, void *ud
) {
852 status
= luaD_pcall(L
, f_Ccall
, &c
, savestack(L
, L
->top
), 0);
858 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
859 const char *chunkname
) {
863 if (!chunkname
) chunkname
= "?";
864 luaZ_init(L
, &z
, reader
, data
);
865 status
= luaD_protectedparser(L
, &z
, chunkname
);
871 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
) {
875 api_checknelems(L
, 1);
878 status
= luaU_dump(L
, clvalue(o
)->l
.p
, writer
, data
, 0);
886 LUA_API
int lua_status (lua_State
*L
) {
892 ** Garbage-collection function
895 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
902 g
->GCthreshold
= MAX_LUMEM
;
905 case LUA_GCRESTART
: {
906 g
->GCthreshold
= g
->totalbytes
;
909 case LUA_GCCOLLECT
: {
914 /* GC values are expressed in Kbytes: #bytes/2^10 */
915 res
= cast_int(g
->totalbytes
>> 10);
919 res
= cast_int(g
->totalbytes
& 0x3ff);
923 lu_mem a
= (cast(lu_mem
, data
) << 10);
924 if (a
<= g
->totalbytes
)
925 g
->GCthreshold
= g
->totalbytes
- a
;
928 while (g
->GCthreshold
<= g
->totalbytes
)
930 if (g
->gcstate
== GCSpause
) /* end of cycle? */
931 res
= 1; /* signal it */
934 case LUA_GCSETPAUSE
: {
939 case LUA_GCSETSTEPMUL
: {
944 default: res
= -1; /* invalid option */
953 ** miscellaneous functions
957 LUA_API
int lua_error (lua_State
*L
) {
959 api_checknelems(L
, 1);
962 return 0; /* to avoid warnings */
966 LUA_API
int lua_next (lua_State
*L
, int idx
) {
970 t
= index2adr(L
, idx
);
971 api_check(L
, ttistable(t
));
972 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
976 else /* no more elements */
977 L
->top
-= 1; /* remove key */
983 LUA_API
void lua_concat (lua_State
*L
, int n
) {
985 api_checknelems(L
, n
);
988 luaV_concat(L
, n
, cast_int(L
->top
- L
->base
) - 1);
991 else if (n
== 0) { /* push empty string */
992 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
995 /* else n == 1; nothing to do */
1000 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1003 if (ud
) *ud
= G(L
)->ud
;
1010 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1018 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1022 u
= luaS_newudata(L
, size
, getcurrenv(L
));
1023 setuvalue(L
, L
->top
, u
);
1032 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
) {
1034 if (!ttisfunction(fi
)) return NULL
;
1037 if (!(1 <= n
&& n
<= f
->c
.nupvalues
)) return NULL
;
1038 *val
= &f
->c
.upvalue
[n
-1];
1043 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1044 *val
= f
->l
.upvals
[n
-1]->v
;
1045 return getstr(p
->upvalues
[n
-1]);
1050 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1054 name
= aux_upvalue(index2adr(L
, funcindex
), n
, &val
);
1056 setobj2s(L
, L
->top
, val
);
1064 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1069 fi
= index2adr(L
, funcindex
);
1070 api_checknelems(L
, 1);
1071 name
= aux_upvalue(fi
, n
, &val
);
1074 setobj(L
, val
, L
->top
);
1075 luaC_barrier(L
, clvalue(fi
), L
->top
);