1 /* $NetBSD: lapi.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $ */
4 ** $Id: lapi.c,v 1.1.1.2 2012/03/15 00:08:10 alnsn Exp $
6 ** See Copyright Notice in lua.h
36 const char lua_ident
[] =
37 "$Lua: " LUA_RELEASE
" " LUA_COPYRIGHT
" $\n"
38 "$Authors: " LUA_AUTHORS
" $\n"
39 "$URL: www.lua.org $\n";
43 #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base))
45 #define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject)
47 #define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;}
51 static TValue
*index2adr (lua_State
*L
, int idx
) {
53 TValue
*o
= L
->base
+ (idx
- 1);
54 api_check(L
, idx
<= L
->ci
->top
- L
->base
);
55 if (o
>= L
->top
) return cast(TValue
*, luaO_nilobject
);
58 else if (idx
> LUA_REGISTRYINDEX
) {
59 api_check(L
, idx
!= 0 && -idx
<= L
->top
- L
->base
);
62 else switch (idx
) { /* pseudo-indices */
63 case LUA_REGISTRYINDEX
: return registry(L
);
64 case LUA_ENVIRONINDEX
: {
65 Closure
*func
= curr_func(L
);
66 sethvalue(L
, &L
->env
, func
->c
.env
);
69 case LUA_GLOBALSINDEX
: return gt(L
);
71 Closure
*func
= curr_func(L
);
72 idx
= LUA_GLOBALSINDEX
- idx
;
73 return (idx
<= func
->c
.nupvalues
)
74 ? &func
->c
.upvalue
[idx
-1]
75 : cast(TValue
*, luaO_nilobject
);
81 static Table
*getcurrenv (lua_State
*L
) {
82 if (L
->ci
== L
->base_ci
) /* no enclosing function? */
83 return hvalue(gt(L
)); /* use global table as environment */
85 Closure
*func
= curr_func(L
);
91 void luaA_pushobject (lua_State
*L
, const TValue
*o
) {
92 setobj2s(L
, L
->top
, o
);
97 LUA_API
int lua_checkstack (lua_State
*L
, int size
) {
100 if (size
> LUAI_MAXCSTACK
|| (L
->top
- L
->base
+ size
) > LUAI_MAXCSTACK
)
101 res
= 0; /* stack overflow */
103 luaD_checkstack(L
, size
);
104 if (L
->ci
->top
< L
->top
+ size
)
105 L
->ci
->top
= L
->top
+ size
;
112 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
114 if (from
== to
) return;
116 api_checknelems(from
, n
);
117 api_check(from
, G(from
) == G(to
));
118 api_check(from
, to
->ci
->top
- to
->top
>= n
);
120 for (i
= 0; i
< n
; i
++) {
121 setobj2s(to
, to
->top
++, from
->top
+ i
);
127 LUA_API
void lua_setlevel (lua_State
*from
, lua_State
*to
) {
128 to
->nCcalls
= from
->nCcalls
;
132 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
136 G(L
)->panic
= panicf
;
142 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
146 L1
= luaE_newthread(L
);
147 setthvalue(L
, L
->top
, L1
);
150 luai_userstatethread(L
, L1
);
157 ** basic stack manipulation
161 LUA_API
int lua_gettop (lua_State
*L
) {
162 return cast_int(L
->top
- L
->base
);
166 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
169 api_check(L
, idx
<= L
->stack_last
- L
->base
);
170 while (L
->top
< L
->base
+ idx
)
171 setnilvalue(L
->top
++);
172 L
->top
= L
->base
+ idx
;
175 api_check(L
, -(idx
+1) <= (L
->top
- L
->base
));
176 L
->top
+= idx
+1; /* `subtract' index (index is negative) */
182 LUA_API
void lua_remove (lua_State
*L
, int idx
) {
185 p
= index2adr(L
, idx
);
186 api_checkvalidindex(L
, p
);
187 while (++p
< L
->top
) setobjs2s(L
, p
-1, p
);
193 LUA_API
void lua_insert (lua_State
*L
, int idx
) {
197 p
= index2adr(L
, idx
);
198 api_checkvalidindex(L
, p
);
199 for (q
= L
->top
; q
>p
; q
--) setobjs2s(L
, q
, q
-1);
200 setobjs2s(L
, p
, L
->top
);
205 LUA_API
void lua_replace (lua_State
*L
, int idx
) {
208 /* explicit test for incompatible code */
209 if (idx
== LUA_ENVIRONINDEX
&& L
->ci
== L
->base_ci
)
210 luaG_runerror(L
, "no calling environment");
211 api_checknelems(L
, 1);
212 o
= index2adr(L
, idx
);
213 api_checkvalidindex(L
, o
);
214 if (idx
== LUA_ENVIRONINDEX
) {
215 Closure
*func
= curr_func(L
);
216 api_check(L
, ttistable(L
->top
- 1));
217 func
->c
.env
= hvalue(L
->top
- 1);
218 luaC_barrier(L
, func
, L
->top
- 1);
221 setobj(L
, o
, L
->top
- 1);
222 if (idx
< LUA_GLOBALSINDEX
) /* function upvalue? */
223 luaC_barrier(L
, curr_func(L
), L
->top
- 1);
230 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
232 setobj2s(L
, L
->top
, index2adr(L
, idx
));
240 ** access functions (stack -> C)
244 LUA_API
int lua_type (lua_State
*L
, int idx
) {
245 StkId o
= index2adr(L
, idx
);
246 return (o
== luaO_nilobject
) ? LUA_TNONE
: ttype(o
);
250 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
252 return (t
== LUA_TNONE
) ? "no value" : luaT_typenames
[t
];
256 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
257 StkId o
= index2adr(L
, idx
);
258 return iscfunction(o
);
262 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
264 const TValue
*o
= index2adr(L
, idx
);
265 return tonumber(o
, &n
);
269 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
270 int t
= lua_type(L
, idx
);
271 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
275 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
276 const TValue
*o
= index2adr(L
, idx
);
277 return (ttisuserdata(o
) || ttislightuserdata(o
));
281 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
282 StkId o1
= index2adr(L
, index1
);
283 StkId o2
= index2adr(L
, index2
);
284 return (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
285 : luaO_rawequalObj(o1
, o2
);
289 LUA_API
int lua_equal (lua_State
*L
, int index1
, int index2
) {
292 lua_lock(L
); /* may call tag method */
293 o1
= index2adr(L
, index1
);
294 o2
= index2adr(L
, index2
);
295 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0 : equalobj(L
, o1
, o2
);
301 LUA_API
int lua_lessthan (lua_State
*L
, int index1
, int index2
) {
304 lua_lock(L
); /* may call tag method */
305 o1
= index2adr(L
, index1
);
306 o2
= index2adr(L
, index2
);
307 i
= (o1
== luaO_nilobject
|| o2
== luaO_nilobject
) ? 0
308 : luaV_lessthan(L
, o1
, o2
);
315 LUA_API lua_Number
lua_tonumber (lua_State
*L
, int idx
) {
317 const TValue
*o
= index2adr(L
, idx
);
325 LUA_API lua_Integer
lua_tointeger (lua_State
*L
, int idx
) {
327 const TValue
*o
= index2adr(L
, idx
);
328 if (tonumber(o
, &n
)) {
330 lua_Number num
= nvalue(o
);
331 lua_number2integer(res
, num
);
339 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
340 const TValue
*o
= index2adr(L
, idx
);
341 return !l_isfalse(o
);
345 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
346 StkId o
= index2adr(L
, idx
);
347 if (!ttisstring(o
)) {
348 lua_lock(L
); /* `luaV_tostring' may create a new string */
349 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
350 if (len
!= NULL
) *len
= 0;
355 o
= index2adr(L
, idx
); /* previous call may reallocate the stack */
358 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
363 LUA_API
size_t lua_objlen (lua_State
*L
, int idx
) {
364 StkId o
= index2adr(L
, idx
);
366 case LUA_TSTRING
: return tsvalue(o
)->len
;
367 case LUA_TUSERDATA
: return uvalue(o
)->len
;
368 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
371 lua_lock(L
); /* `luaV_tostring' may create a new string */
372 l
= (luaV_tostring(L
, o
) ? tsvalue(o
)->len
: 0);
381 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
382 StkId o
= index2adr(L
, idx
);
383 return (!iscfunction(o
)) ? NULL
: clvalue(o
)->c
.f
;
387 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
388 StkId o
= index2adr(L
, idx
);
390 case LUA_TUSERDATA
: return (rawuvalue(o
) + 1);
391 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
392 default: return NULL
;
397 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
398 StkId o
= index2adr(L
, idx
);
399 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
403 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
404 StkId o
= index2adr(L
, idx
);
406 case LUA_TTABLE
: return hvalue(o
);
407 case LUA_TFUNCTION
: return clvalue(o
);
408 case LUA_TTHREAD
: return thvalue(o
);
410 case LUA_TLIGHTUSERDATA
:
411 return lua_touserdata(L
, idx
);
412 default: return NULL
;
419 ** push functions (C -> stack)
423 LUA_API
void lua_pushnil (lua_State
*L
) {
431 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
433 setnvalue(L
->top
, n
);
439 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
441 setnvalue(L
->top
, cast_num(n
));
447 LUA_API
void lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
450 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, s
, len
));
456 LUA_API
void lua_pushstring (lua_State
*L
, const char *s
) {
460 lua_pushlstring(L
, s
, strlen(s
));
464 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
469 ret
= luaO_pushvfstring(L
, fmt
, argp
);
475 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
481 ret
= luaO_pushvfstring(L
, fmt
, argp
);
488 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
492 api_checknelems(L
, n
);
493 cl
= luaF_newCclosure(L
, n
, getcurrenv(L
));
497 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+n
);
498 setclvalue(L
, L
->top
, cl
);
499 lua_assert(iswhite(obj2gco(cl
)));
505 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
507 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
513 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
515 setpvalue(L
->top
, p
);
521 LUA_API
int lua_pushthread (lua_State
*L
) {
523 setthvalue(L
, L
->top
, L
);
526 return (G(L
)->mainthread
== L
);
532 ** get functions (Lua -> stack)
536 LUA_API
void lua_gettable (lua_State
*L
, int idx
) {
539 t
= index2adr(L
, idx
);
540 api_checkvalidindex(L
, t
);
541 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
546 LUA_API
void lua_getfield (lua_State
*L
, int idx
, const char *k
) {
550 t
= index2adr(L
, idx
);
551 api_checkvalidindex(L
, t
);
552 setsvalue(L
, &key
, luaS_new(L
, k
));
553 luaV_gettable(L
, t
, &key
, L
->top
);
559 LUA_API
void lua_rawget (lua_State
*L
, int idx
) {
562 t
= index2adr(L
, idx
);
563 api_check(L
, ttistable(t
));
564 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
569 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
) {
572 o
= index2adr(L
, idx
);
573 api_check(L
, ttistable(o
));
574 setobj2s(L
, L
->top
, luaH_getnum(hvalue(o
), n
));
580 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
583 sethvalue(L
, L
->top
, luaH_new(L
, narray
, nrec
));
589 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
594 obj
= index2adr(L
, objindex
);
595 switch (ttype(obj
)) {
597 mt
= hvalue(obj
)->metatable
;
600 mt
= uvalue(obj
)->metatable
;
603 mt
= G(L
)->mt
[ttype(obj
)];
609 sethvalue(L
, L
->top
, mt
);
618 LUA_API
void lua_getfenv (lua_State
*L
, int idx
) {
621 o
= index2adr(L
, idx
);
622 api_checkvalidindex(L
, o
);
625 sethvalue(L
, L
->top
, clvalue(o
)->c
.env
);
628 sethvalue(L
, L
->top
, uvalue(o
)->env
);
631 setobj2s(L
, L
->top
, gt(thvalue(o
)));
643 ** set functions (stack -> Lua)
647 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
650 api_checknelems(L
, 2);
651 t
= index2adr(L
, idx
);
652 api_checkvalidindex(L
, t
);
653 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
654 L
->top
-= 2; /* pop index and value */
659 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
663 api_checknelems(L
, 1);
664 t
= index2adr(L
, idx
);
665 api_checkvalidindex(L
, t
);
666 setsvalue(L
, &key
, luaS_new(L
, k
));
667 luaV_settable(L
, t
, &key
, L
->top
- 1);
668 L
->top
--; /* pop value */
673 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
676 api_checknelems(L
, 2);
677 t
= index2adr(L
, idx
);
678 api_check(L
, ttistable(t
));
679 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
-2), L
->top
-1);
680 luaC_barriert(L
, hvalue(t
), L
->top
-1);
686 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
) {
689 api_checknelems(L
, 1);
690 o
= index2adr(L
, idx
);
691 api_check(L
, ttistable(o
));
692 setobj2t(L
, luaH_setnum(L
, hvalue(o
), n
), L
->top
-1);
693 luaC_barriert(L
, hvalue(o
), L
->top
-1);
699 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
703 api_checknelems(L
, 1);
704 obj
= index2adr(L
, objindex
);
705 api_checkvalidindex(L
, obj
);
706 if (ttisnil(L
->top
- 1))
709 api_check(L
, ttistable(L
->top
- 1));
710 mt
= hvalue(L
->top
- 1);
712 switch (ttype(obj
)) {
714 hvalue(obj
)->metatable
= mt
;
716 luaC_objbarriert(L
, hvalue(obj
), mt
);
719 case LUA_TUSERDATA
: {
720 uvalue(obj
)->metatable
= mt
;
722 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
726 G(L
)->mt
[ttype(obj
)] = mt
;
736 LUA_API
int lua_setfenv (lua_State
*L
, int idx
) {
740 api_checknelems(L
, 1);
741 o
= index2adr(L
, idx
);
742 api_checkvalidindex(L
, o
);
743 api_check(L
, ttistable(L
->top
- 1));
746 clvalue(o
)->c
.env
= hvalue(L
->top
- 1);
749 uvalue(o
)->env
= hvalue(L
->top
- 1);
752 sethvalue(L
, gt(thvalue(o
)), hvalue(L
->top
- 1));
758 if (res
) luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
766 ** `load' and `call' functions (run Lua code)
770 #define adjustresults(L,nres) \
771 { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
774 #define checkresults(L,na,nr) \
775 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
778 LUA_API
void lua_call (lua_State
*L
, int nargs
, int nresults
) {
781 api_checknelems(L
, nargs
+1);
782 checkresults(L
, nargs
, nresults
);
783 func
= L
->top
- (nargs
+1);
784 luaD_call(L
, func
, nresults
);
785 adjustresults(L
, nresults
);
792 ** Execute a protected call.
794 struct CallS
{ /* data to `f_call' */
800 static void f_call (lua_State
*L
, void *ud
) {
801 struct CallS
*c
= cast(struct CallS
*, ud
);
802 luaD_call(L
, c
->func
, c
->nresults
);
807 LUA_API
int lua_pcall (lua_State
*L
, int nargs
, int nresults
, int errfunc
) {
812 api_checknelems(L
, nargs
+1);
813 checkresults(L
, nargs
, nresults
);
817 StkId o
= index2adr(L
, errfunc
);
818 api_checkvalidindex(L
, o
);
819 func
= savestack(L
, o
);
821 c
.func
= L
->top
- (nargs
+1); /* function to be called */
822 c
.nresults
= nresults
;
823 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
824 adjustresults(L
, nresults
);
831 ** Execute a protected C call.
833 struct CCallS
{ /* data to `f_Ccall' */
839 static void f_Ccall (lua_State
*L
, void *ud
) {
840 struct CCallS
*c
= cast(struct CCallS
*, ud
);
842 cl
= luaF_newCclosure(L
, 0, getcurrenv(L
));
844 setclvalue(L
, L
->top
, cl
); /* push function */
846 setpvalue(L
->top
, c
->ud
); /* push only argument */
848 luaD_call(L
, L
->top
- 2, 0);
852 LUA_API
int lua_cpcall (lua_State
*L
, lua_CFunction func
, void *ud
) {
858 status
= luaD_pcall(L
, f_Ccall
, &c
, savestack(L
, L
->top
), 0);
864 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
865 const char *chunkname
) {
869 if (!chunkname
) chunkname
= "?";
870 luaZ_init(L
, &z
, reader
, data
);
871 status
= luaD_protectedparser(L
, &z
, chunkname
);
877 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
) {
881 api_checknelems(L
, 1);
884 status
= luaU_dump(L
, clvalue(o
)->l
.p
, writer
, data
, 0);
892 LUA_API
int lua_status (lua_State
*L
) {
898 ** Garbage-collection function
901 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
908 g
->GCthreshold
= MAX_LUMEM
;
911 case LUA_GCRESTART
: {
912 g
->GCthreshold
= g
->totalbytes
;
915 case LUA_GCCOLLECT
: {
920 /* GC values are expressed in Kbytes: #bytes/2^10 */
921 res
= cast_int(g
->totalbytes
>> 10);
925 res
= cast_int(g
->totalbytes
& 0x3ff);
929 lu_mem a
= (cast(lu_mem
, data
) << 10);
930 if (a
<= g
->totalbytes
)
931 g
->GCthreshold
= g
->totalbytes
- a
;
934 while (g
->GCthreshold
<= g
->totalbytes
) {
936 if (g
->gcstate
== GCSpause
) { /* end of cycle? */
937 res
= 1; /* signal it */
943 case LUA_GCSETPAUSE
: {
948 case LUA_GCSETSTEPMUL
: {
953 default: res
= -1; /* invalid option */
962 ** miscellaneous functions
966 LUA_API
int lua_error (lua_State
*L
) {
968 api_checknelems(L
, 1);
971 return 0; /* to avoid warnings */
975 LUA_API
int lua_next (lua_State
*L
, int idx
) {
979 t
= index2adr(L
, idx
);
980 api_check(L
, ttistable(t
));
981 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
985 else /* no more elements */
986 L
->top
-= 1; /* remove key */
992 LUA_API
void lua_concat (lua_State
*L
, int n
) {
994 api_checknelems(L
, n
);
997 luaV_concat(L
, n
, cast_int(L
->top
- L
->base
) - 1);
1000 else if (n
== 0) { /* push empty string */
1001 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1004 /* else n == 1; nothing to do */
1009 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1012 if (ud
) *ud
= G(L
)->ud
;
1019 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1027 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1031 u
= luaS_newudata(L
, size
, getcurrenv(L
));
1032 setuvalue(L
, L
->top
, u
);
1041 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
) {
1043 if (!ttisfunction(fi
)) return NULL
;
1046 if (!(1 <= n
&& n
<= f
->c
.nupvalues
)) return NULL
;
1047 *val
= &f
->c
.upvalue
[n
-1];
1052 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1053 *val
= f
->l
.upvals
[n
-1]->v
;
1054 return getstr(p
->upvalues
[n
-1]);
1059 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1063 name
= aux_upvalue(index2adr(L
, funcindex
), n
, &val
);
1065 setobj2s(L
, L
->top
, val
);
1073 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1078 fi
= index2adr(L
, funcindex
);
1079 api_checknelems(L
, 1);
1080 name
= aux_upvalue(fi
, n
, &val
);
1083 setobj(L
, val
, L
->top
);
1084 luaC_barrier(L
, clvalue(fi
), L
->top
);