2 ** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $
4 ** See Copyright Notice in lua.h
35 const char lua_ident
[] =
36 "$Lua: " LUA_RELEASE
" " LUA_COPYRIGHT
" $\n"
37 "$Authors: " LUA_AUTHORS
" $\n"
38 "$URL: www.lua.org $\n";
42 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
44 #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
46 #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
50 static TValue
*index2adr (lua_State
*L
, int idx
) {
52 TValue
*o
= L
->base
+ (idx
- 1);
53 api_check(L
, idx
<= L
->ci
->top
- L
->base
);
54 if (o
>= L
->top
) return cast(TValue
*, luaO_nilobject
);
57 else if (idx
> LUA_REGISTRYINDEX
) {
58 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
61 else switch (idx
) { /* pseudo-indices */
62 case LUA_REGISTRYINDEX
: return registry(L
);
63 case LUA_ENVIRONINDEX
: {
64 Closure
*func
= curr_func(L
);
65 sethvalue(L
, &L
->env
, func
->c
.env
);
68 case LUA_GLOBALSINDEX
: return gt(L
);
70 Closure
*func
= curr_func(L
);
71 idx
= LUA_GLOBALSINDEX
- idx
;
72 return (idx
<= func
->c
.nupvalues
)
73 ? &func
->c
.upvalue
[idx
-1]
74 : cast(TValue
*, luaO_nilobject
);
80 static Table
*getcurrenv (lua_State
*L
) {
81 if (L
->ci
== L
->base_ci
) /* no enclosing function? */
82 return hvalue(gt(L
)); /* use global table as environment */
84 Closure
*func
= curr_func(L
);
90 void luaA_pushobject (lua_State
*L
, const TValue
*o
) {
91 setobj2s(L
, L
->top
, o
);
96 LUA_API
int lua_checkstack (lua_State
*L
, int size
) {
99 if (size
> LUAI_MAXCSTACK
|| (L
->top
- L
->base
+ size
) > LUAI_MAXCSTACK
)
100 res
= 0; /* stack overflow */
102 luaD_checkstack(L
, size
);
103 if (L
->ci
->top
< L
->top
+ size
)
104 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
void lua_setlevel (lua_State
*from
, lua_State
*to
) {
127 to
->nCcalls
= from
->nCcalls
;
131 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
135 G(L
)->panic
= panicf
;
141 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
145 L1
= luaE_newthread(L
);
146 setthvalue(L
, L
->top
, L1
);
149 luai_userstatethread(L
, L1
);
156 ** basic stack manipulation
160 LUA_API
int lua_gettop (lua_State
*L
) {
161 return cast_int(L
->top
- L
->base
);
165 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
168 api_check(L
, idx
<= L
->stack_last
- L
->base
);
169 while (L
->top
< L
->base
+ idx
)
170 setnilvalue(L
->top
++);
171 L
->top
= L
->base
+ idx
;
174 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
175 L
->top
+= idx
+1; /* `subtract' index (index is negative) */
181 LUA_API
void lua_remove (lua_State
*L
, int idx
) {
184 p
= index2adr(L
, idx
);
185 api_checkvalidindex(L
, p
);
186 while (++p
< L
->top
) setobjs2s(L
, p
-1, p
);
192 LUA_API
void lua_insert (lua_State
*L
, int idx
) {
196 p
= index2adr(L
, idx
);
197 api_checkvalidindex(L
, p
);
198 for (q
= L
->top
; q
>p
; q
--) setobjs2s(L
, q
, q
-1);
199 setobjs2s(L
, p
, L
->top
);
204 LUA_API
void lua_replace (lua_State
*L
, int idx
) {
207 /* explicit test for incompatible code */
208 if (idx
== LUA_ENVIRONINDEX
&& L
->ci
== L
->base_ci
)
209 luaG_runerror(L
, "no calling environment");
210 api_checknelems(L
, 1);
211 o
= index2adr(L
, idx
);
212 api_checkvalidindex(L
, o
);
213 if (idx
== LUA_ENVIRONINDEX
) {
214 Closure
*func
= curr_func(L
);
215 api_check(L
, ttistable(L
->top
- 1));
216 func
->c
.env
= hvalue(L
->top
- 1);
217 luaC_barrier(L
, func
, L
->top
- 1);
220 setobj(L
, o
, L
->top
- 1);
221 if (idx
< LUA_GLOBALSINDEX
) /* function upvalue? */
222 luaC_barrier(L
, curr_func(L
), L
->top
- 1);
229 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
231 setobj2s(L
, L
->top
, index2adr(L
, idx
));
239 ** access functions (stack -> C)
243 LUA_API
int lua_type (lua_State
*L
, int idx
) {
244 StkId o
= index2adr(L
, idx
);
245 return (o
== luaO_nilobject
) ? LUA_TNONE
: ttype(o
);
249 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
251 return (t
== LUA_TNONE
) ? "no value" : luaT_typenames
[t
];
255 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
256 StkId o
= index2adr(L
, idx
);
257 return iscfunction(o
);
261 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
263 const TValue
*o
= index2adr(L
, idx
);
264 return tonumber(o
, &n
);
268 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
269 int t
= lua_type(L
, idx
);
270 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
274 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
275 const TValue
*o
= index2adr(L
, idx
);
276 return (ttisuserdata(o
) || ttislightuserdata(o
));
280 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
281 StkId o1
= index2adr(L
, index1
);
282 StkId o2
= index2adr(L
, index2
);
283 return (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
284 : luaO_rawequalObj(o1
, o2
);
288 LUA_API
int lua_equal (lua_State
*L
, int index1
, int index2
) {
291 lua_lock(L
); /* may call tag method */
292 o1
= index2adr(L
, index1
);
293 o2
= index2adr(L
, index2
);
294 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0 : equalobj(L
, o1
, o2
);
300 LUA_API
int lua_lessthan (lua_State
*L
, int index1
, int index2
) {
303 lua_lock(L
); /* may call tag method */
304 o1
= index2adr(L
, index1
);
305 o2
= index2adr(L
, index2
);
306 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
307 : luaV_lessthan(L
, o1
, o2
);
314 LUA_API lua_Number
lua_tonumber (lua_State
*L
, int idx
) {
316 const TValue
*o
= index2adr(L
, idx
);
324 LUA_API lua_Integer
lua_tointeger (lua_State
*L
, int idx
) {
326 const TValue
*o
= index2adr(L
, idx
);
327 if (tonumber(o
, &n
)) {
329 lua_Number num
= nvalue(o
);
330 lua_number2integer(res
, num
);
338 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
339 const TValue
*o
= index2adr(L
, idx
);
340 return !l_isfalse(o
);
344 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
345 StkId o
= index2adr(L
, idx
);
346 if (!ttisstring(o
)) {
347 lua_lock(L
); /* `luaV_tostring' may create a new string */
348 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
349 if (len
!= NULL
) *len
= 0;
354 o
= index2adr(L
, idx
); /* previous call may reallocate the stack */
357 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
362 LUA_API
size_t lua_objlen (lua_State
*L
, int idx
) {
363 StkId o
= index2adr(L
, idx
);
365 case LUA_TSTRING
: return tsvalue(o
)->len
;
366 case LUA_TUSERDATA
: return uvalue(o
)->len
;
367 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
370 lua_lock(L
); /* `luaV_tostring' may create a new string */
371 l
= (luaV_tostring(L
, o
) ? tsvalue(o
)->len
: 0);
380 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
381 StkId o
= index2adr(L
, idx
);
382 return (!iscfunction(o
)) ? NULL
: clvalue(o
)->c
.f
;
386 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
387 StkId o
= index2adr(L
, idx
);
389 case LUA_TUSERDATA
: return (rawuvalue(o
) + 1);
390 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
391 default: return NULL
;
396 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
397 StkId o
= index2adr(L
, idx
);
398 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
402 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
403 StkId o
= index2adr(L
, idx
);
405 case LUA_TTABLE
: return hvalue(o
);
406 case LUA_TFUNCTION
: return clvalue(o
);
407 case LUA_TTHREAD
: return thvalue(o
);
409 case LUA_TLIGHTUSERDATA
:
410 return lua_touserdata(L
, idx
);
411 default: return NULL
;
418 ** push functions (C -> stack)
422 LUA_API
void lua_pushnil (lua_State
*L
) {
430 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
432 setnvalue(L
->top
, n
);
438 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
440 setnvalue(L
->top
, cast_num(n
));
446 LUA_API
void lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
449 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, s
, len
));
455 LUA_API
void lua_pushstring (lua_State
*L
, const char *s
) {
459 lua_pushlstring(L
, s
, strlen(s
));
463 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
468 ret
= luaO_pushvfstring(L
, fmt
, argp
);
474 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
480 ret
= luaO_pushvfstring(L
, fmt
, argp
);
487 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
491 api_checknelems(L
, n
);
492 cl
= luaF_newCclosure(L
, n
, getcurrenv(L
));
496 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+n
);
497 setclvalue(L
, L
->top
, cl
);
498 lua_assert(iswhite(obj2gco(cl
)));
504 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
506 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
512 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
514 setpvalue(L
->top
, p
);
520 LUA_API
int lua_pushthread (lua_State
*L
) {
522 setthvalue(L
, L
->top
, L
);
525 return (G(L
)->mainthread
== L
);
531 ** get functions (Lua -> stack)
535 LUA_API
void lua_gettable (lua_State
*L
, int idx
) {
538 t
= index2adr(L
, idx
);
539 api_checkvalidindex(L
, t
);
540 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
545 LUA_API
void lua_getfield (lua_State
*L
, int idx
, const char *k
) {
549 t
= index2adr(L
, idx
);
550 api_checkvalidindex(L
, t
);
551 setsvalue(L
, &key
, luaS_new(L
, k
));
552 luaV_gettable(L
, t
, &key
, L
->top
);
558 LUA_API
void lua_rawget (lua_State
*L
, int idx
) {
561 t
= index2adr(L
, idx
);
562 api_check(L
, ttistable(t
));
563 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
568 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
) {
571 o
= index2adr(L
, idx
);
572 api_check(L
, ttistable(o
));
573 setobj2s(L
, L
->top
, luaH_getnum(hvalue(o
), n
));
579 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
582 sethvalue(L
, L
->top
, luaH_new(L
, narray
, nrec
));
588 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
593 obj
= index2adr(L
, objindex
);
594 switch (ttype(obj
)) {
596 mt
= hvalue(obj
)->metatable
;
599 mt
= uvalue(obj
)->metatable
;
602 mt
= G(L
)->mt
[ttype(obj
)];
608 sethvalue(L
, L
->top
, mt
);
617 LUA_API
void lua_getfenv (lua_State
*L
, int idx
) {
620 o
= index2adr(L
, idx
);
621 api_checkvalidindex(L
, o
);
624 sethvalue(L
, L
->top
, clvalue(o
)->c
.env
);
627 sethvalue(L
, L
->top
, uvalue(o
)->env
);
630 setobj2s(L
, L
->top
, gt(thvalue(o
)));
642 ** set functions (stack -> Lua)
646 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
649 api_checknelems(L
, 2);
650 t
= index2adr(L
, idx
);
651 api_checkvalidindex(L
, t
);
652 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
653 L
->top
-= 2; /* pop index and value */
658 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
662 api_checknelems(L
, 1);
663 t
= index2adr(L
, idx
);
664 api_checkvalidindex(L
, t
);
665 setsvalue(L
, &key
, luaS_new(L
, k
));
666 luaV_settable(L
, t
, &key
, L
->top
- 1);
667 L
->top
--; /* pop value */
672 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
675 api_checknelems(L
, 2);
676 t
= index2adr(L
, idx
);
677 api_check(L
, ttistable(t
));
678 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
-2), L
->top
-1);
679 luaC_barriert(L
, hvalue(t
), L
->top
-1);
685 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
) {
688 api_checknelems(L
, 1);
689 o
= index2adr(L
, idx
);
690 api_check(L
, ttistable(o
));
691 setobj2t(L
, luaH_setnum(L
, hvalue(o
), n
), L
->top
-1);
692 luaC_barriert(L
, hvalue(o
), L
->top
-1);
698 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
702 api_checknelems(L
, 1);
703 obj
= index2adr(L
, objindex
);
704 api_checkvalidindex(L
, obj
);
705 if (ttisnil(L
->top
- 1))
708 api_check(L
, ttistable(L
->top
- 1));
709 mt
= hvalue(L
->top
- 1);
711 switch (ttype(obj
)) {
713 hvalue(obj
)->metatable
= mt
;
715 luaC_objbarriert(L
, hvalue(obj
), mt
);
718 case LUA_TUSERDATA
: {
719 uvalue(obj
)->metatable
= mt
;
721 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
725 G(L
)->mt
[ttype(obj
)] = mt
;
735 LUA_API
int lua_setfenv (lua_State
*L
, int idx
) {
739 api_checknelems(L
, 1);
740 o
= index2adr(L
, idx
);
741 api_checkvalidindex(L
, o
);
742 api_check(L
, ttistable(L
->top
- 1));
745 clvalue(o
)->c
.env
= hvalue(L
->top
- 1);
748 uvalue(o
)->env
= hvalue(L
->top
- 1);
751 sethvalue(L
, gt(thvalue(o
)), hvalue(L
->top
- 1));
757 if (res
) luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
765 ** `load' and `call' functions (run Lua code)
769 #define adjustresults(L,nres) \
770 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
773 #define checkresults(L,na,nr) \
774 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
777 LUA_API
void lua_call (lua_State
*L
, int nargs
, int nresults
) {
780 api_checknelems(L
, nargs
+1);
781 checkresults(L
, nargs
, nresults
);
782 func
= L
->top
- (nargs
+1);
783 luaD_call(L
, func
, nresults
);
784 adjustresults(L
, nresults
);
791 ** Execute a protected call.
793 struct CallS
{ /* data to `f_call' */
799 static void f_call (lua_State
*L
, void *ud
) {
800 struct CallS
*c
= cast(struct CallS
*, ud
);
801 luaD_call(L
, c
->func
, c
->nresults
);
806 LUA_API
int lua_pcall (lua_State
*L
, int nargs
, int nresults
, int errfunc
) {
811 api_checknelems(L
, nargs
+1);
812 checkresults(L
, nargs
, nresults
);
816 StkId o
= index2adr(L
, errfunc
);
817 api_checkvalidindex(L
, o
);
818 func
= savestack(L
, o
);
820 c
.func
= L
->top
- (nargs
+1); /* function to be called */
821 c
.nresults
= nresults
;
822 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
823 adjustresults(L
, nresults
);
830 ** Execute a protected C call.
832 struct CCallS
{ /* data to `f_Ccall' */
838 static void f_Ccall (lua_State
*L
, void *ud
) {
839 struct CCallS
*c
= cast(struct CCallS
*, ud
);
841 cl
= luaF_newCclosure(L
, 0, getcurrenv(L
));
843 setclvalue(L
, L
->top
, cl
); /* push function */
845 setpvalue(L
->top
, c
->ud
); /* push only argument */
847 luaD_call(L
, L
->top
- 2, 0);
851 LUA_API
int lua_cpcall (lua_State
*L
, lua_CFunction func
, void *ud
) {
857 status
= luaD_pcall(L
, f_Ccall
, &c
, savestack(L
, L
->top
), 0);
863 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
864 const char *chunkname
) {
868 if (!chunkname
) chunkname
= "?";
869 luaZ_init(L
, &z
, reader
, data
);
870 status
= luaD_protectedparser(L
, &z
, chunkname
);
876 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
) {
880 api_checknelems(L
, 1);
883 status
= luaU_dump(L
, clvalue(o
)->l
.p
, writer
, data
, 0);
891 LUA_API
int lua_status (lua_State
*L
) {
897 ** Garbage-collection function
900 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
907 g
->GCthreshold
= MAX_LUMEM
;
910 case LUA_GCRESTART
: {
911 g
->GCthreshold
= g
->totalbytes
;
914 case LUA_GCCOLLECT
: {
919 /* GC values are expressed in Kbytes: #bytes/2^10 */
920 res
= cast_int(g
->totalbytes
>> 10);
924 res
= cast_int(g
->totalbytes
& 0x3ff);
928 lu_mem a
= (cast(lu_mem
, data
) << 10);
929 if (a
<= g
->totalbytes
)
930 g
->GCthreshold
= g
->totalbytes
- a
;
933 while (g
->GCthreshold
<= g
->totalbytes
) {
935 if (g
->gcstate
== GCSpause
) { /* end of cycle? */
936 res
= 1; /* signal it */
942 case LUA_GCSETPAUSE
: {
947 case LUA_GCSETSTEPMUL
: {
952 default: res
= -1; /* invalid option */
961 ** miscellaneous functions
965 LUA_API
int lua_error (lua_State
*L
) {
967 api_checknelems(L
, 1);
970 return 0; /* to avoid warnings */
974 LUA_API
int lua_next (lua_State
*L
, int idx
) {
978 t
= index2adr(L
, idx
);
979 api_check(L
, ttistable(t
));
980 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
984 else /* no more elements */
985 L
->top
-= 1; /* remove key */
991 LUA_API
void lua_concat (lua_State
*L
, int n
) {
993 api_checknelems(L
, n
);
996 luaV_concat(L
, n
, cast_int(L
->top
- L
->base
) - 1);
999 else if (n
== 0) { /* push empty string */
1000 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1003 /* else n == 1; nothing to do */
1008 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1011 if (ud
) *ud
= G(L
)->ud
;
1018 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1026 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1030 u
= luaS_newudata(L
, size
, getcurrenv(L
));
1031 setuvalue(L
, L
->top
, u
);
1040 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
) {
1042 if (!ttisfunction(fi
)) return NULL
;
1045 if (!(1 <= n
&& n
<= f
->c
.nupvalues
)) return NULL
;
1046 *val
= &f
->c
.upvalue
[n
-1];
1051 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1052 *val
= f
->l
.upvals
[n
-1]->v
;
1053 return getstr(p
->upvalues
[n
-1]);
1058 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1062 name
= aux_upvalue(index2adr(L
, funcindex
), n
, &val
);
1064 setobj2s(L
, L
->top
, val
);
1072 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1077 fi
= index2adr(L
, funcindex
);
1078 api_checknelems(L
, 1);
1079 name
= aux_upvalue(fi
, n
, &val
);
1082 setobj(L
, val
, L
->top
);
1083 luaC_barrier(L
, clvalue(fi
), L
->top
);