2 ** $Id: lapi.c,v 2.171.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** See Copyright Notice in lua.h
11 #include <sys/lua/lua.h>
28 const char lua_ident
[] =
29 "$LuaVersion: " LUA_COPYRIGHT
" $"
30 "$LuaAuthors: " LUA_AUTHORS
" $";
33 /* value at a non-valid index */
34 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
36 /* corresponding test */
37 #define isvalid(o) ((o) != luaO_nilobject)
39 /* test for pseudo index */
40 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
42 /* test for valid but not pseudo index */
43 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
45 #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
47 #define api_checkstackindex(L, i, o) \
48 api_check(L, isstackindex(i, o), "index not in the stack")
51 static TValue
*index2addr (lua_State
*L
, int idx
) {
54 TValue
*o
= ci
->func
+ idx
;
55 api_check(L
, idx
<= ci
->top
- (ci
->func
+ 1), "unacceptable index");
56 if (o
>= L
->top
) return NONVALIDVALUE
;
59 else if (!ispseudo(idx
)) { /* negative index */
60 api_check(L
, idx
!= 0 && -idx
<= L
->top
- (ci
->func
+ 1), "invalid index");
63 else if (idx
== LUA_REGISTRYINDEX
)
64 return &G(L
)->l_registry
;
66 idx
= LUA_REGISTRYINDEX
- idx
;
67 api_check(L
, idx
<= MAXUPVAL
+ 1, "upvalue index too large");
68 if (ttislcf(ci
->func
)) /* light C function? */
69 return NONVALIDVALUE
; /* it has no upvalues */
71 CClosure
*func
= clCvalue(ci
->func
);
72 return (idx
<= func
->nupvalues
) ? &func
->upvalue
[idx
-1] : NONVALIDVALUE
;
79 ** to be called by 'lua_checkstack' in protected mode, to grow stack
80 ** capturing memory errors
82 static void growstack (lua_State
*L
, void *ud
) {
83 int size
= *(int *)ud
;
84 luaD_growstack(L
, size
);
88 LUA_API
int lua_checkstack (lua_State
*L
, int size
) {
92 if (L
->stack_last
- L
->top
> size
) /* stack large enough? */
93 res
= 1; /* yes; check is OK */
94 else { /* no; need to grow stack */
95 int inuse
= cast_int(L
->top
- L
->stack
) + EXTRA_STACK
;
96 if (inuse
> LUAI_MAXSTACK
- size
) /* can grow without overflow? */
98 else /* try to grow stack */
99 res
= (luaD_rawrunprotected(L
, &growstack
, &size
) == LUA_OK
);
101 if (res
&& ci
->top
< L
->top
+ size
)
102 ci
->top
= L
->top
+ size
; /* adjust frame top */
108 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
110 if (from
== to
) return;
112 api_checknelems(from
, n
);
113 api_check(from
, G(from
) == G(to
), "moving among independent states");
114 api_check(from
, to
->ci
->top
- to
->top
>= n
, "not enough elements to move");
116 for (i
= 0; i
< n
; i
++) {
117 setobj2s(to
, to
->top
++, from
->top
+ i
);
123 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
127 G(L
)->panic
= panicf
;
133 LUA_API
const lua_Number
*lua_version (lua_State
*L
) {
134 static const lua_Number version
= LUA_VERSION_NUM
;
135 if (L
== NULL
) return &version
;
136 else return G(L
)->version
;
142 ** basic stack manipulation
147 ** convert an acceptable stack index into an absolute index
149 LUA_API
int lua_absindex (lua_State
*L
, int idx
) {
150 return (idx
> 0 || ispseudo(idx
))
152 : cast_int(L
->top
- L
->ci
->func
+ idx
);
156 LUA_API
int lua_gettop (lua_State
*L
) {
157 return cast_int(L
->top
- (L
->ci
->func
+ 1));
161 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
162 StkId func
= L
->ci
->func
;
165 api_check(L
, idx
<= L
->stack_last
- (func
+ 1), "new top too large");
166 while (L
->top
< (func
+ 1) + idx
)
167 setnilvalue(L
->top
++);
168 L
->top
= (func
+ 1) + idx
;
171 api_check(L
, -(idx
+1) <= (L
->top
- (func
+ 1)), "invalid new top");
172 L
->top
+= idx
+1; /* `subtract' index (index is negative) */
178 LUA_API
void lua_remove (lua_State
*L
, int idx
) {
181 p
= index2addr(L
, idx
);
182 api_checkstackindex(L
, idx
, p
);
183 while (++p
< L
->top
) setobjs2s(L
, p
-1, p
);
189 LUA_API
void lua_insert (lua_State
*L
, int idx
) {
193 p
= index2addr(L
, idx
);
194 api_checkstackindex(L
, idx
, p
);
195 for (q
= L
->top
; q
> p
; q
--) /* use L->top as a temporary */
196 setobjs2s(L
, q
, q
- 1);
197 setobjs2s(L
, p
, L
->top
);
202 static void moveto (lua_State
*L
, TValue
*fr
, int idx
) {
203 TValue
*to
= index2addr(L
, idx
);
204 api_checkvalidindex(L
, to
);
206 if (idx
< LUA_REGISTRYINDEX
) /* function upvalue? */
207 luaC_barrier(L
, clCvalue(L
->ci
->func
), fr
);
208 /* LUA_REGISTRYINDEX does not need gc barrier
209 (collector revisits it before finishing collection) */
213 LUA_API
void lua_replace (lua_State
*L
, int idx
) {
215 api_checknelems(L
, 1);
216 moveto(L
, L
->top
- 1, idx
);
222 LUA_API
void lua_copy (lua_State
*L
, int fromidx
, int toidx
) {
225 fr
= index2addr(L
, fromidx
);
226 moveto(L
, fr
, toidx
);
231 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
233 setobj2s(L
, L
->top
, index2addr(L
, idx
));
241 ** access functions (stack -> C)
245 LUA_API
int lua_type (lua_State
*L
, int idx
) {
246 StkId o
= index2addr(L
, idx
);
247 return (isvalid(o
) ? ttypenv(o
) : LUA_TNONE
);
251 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
254 return "internal_type_error";
259 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
260 StkId o
= index2addr(L
, idx
);
261 return (ttislcf(o
) || (ttisCclosure(o
)));
265 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
267 const TValue
*o
= index2addr(L
, idx
);
268 return tonumber(o
, &n
);
272 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
273 int t
= lua_type(L
, idx
);
274 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
278 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
279 const TValue
*o
= index2addr(L
, idx
);
280 return (ttisuserdata(o
) || ttislightuserdata(o
));
284 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
285 StkId o1
= index2addr(L
, index1
);
286 StkId o2
= index2addr(L
, index2
);
287 return (isvalid(o1
) && isvalid(o2
)) ? luaV_rawequalobj(o1
, o2
) : 0;
291 LUA_API
void lua_arith (lua_State
*L
, int op
) {
292 StkId o1
; /* 1st operand */
293 StkId o2
; /* 2nd operand */
295 if (op
!= LUA_OPUNM
) /* all other operations expect two operands */
296 api_checknelems(L
, 2);
297 else { /* for unary minus, add fake 2nd operand */
298 api_checknelems(L
, 1);
299 setobjs2s(L
, L
->top
, L
->top
- 1);
304 if (ttisnumber(o1
) && ttisnumber(o2
)) {
305 setnvalue(o1
, luaO_arith(op
, nvalue(o1
), nvalue(o2
)));
308 luaV_arith(L
, o1
, o1
, o2
, cast(TMS
, op
- LUA_OPADD
+ TM_ADD
));
314 LUA_API
int lua_compare (lua_State
*L
, int index1
, int index2
, int op
) {
317 lua_lock(L
); /* may call tag method */
318 o1
= index2addr(L
, index1
);
319 o2
= index2addr(L
, index2
);
320 if (isvalid(o1
) && isvalid(o2
)) {
322 case LUA_OPEQ
: i
= equalobj(L
, o1
, o2
); break;
323 case LUA_OPLT
: i
= luaV_lessthan(L
, o1
, o2
); break;
324 case LUA_OPLE
: i
= luaV_lessequal(L
, o1
, o2
); break;
325 default: api_check(L
, 0, "invalid option");
333 LUA_API lua_Number
lua_tonumberx (lua_State
*L
, int idx
, int *isnum
) {
335 const TValue
*o
= index2addr(L
, idx
);
336 if (tonumber(o
, &n
)) {
337 if (isnum
) *isnum
= 1;
341 if (isnum
) *isnum
= 0;
347 LUA_API lua_Integer
lua_tointegerx (lua_State
*L
, int idx
, int *isnum
) {
349 const TValue
*o
= index2addr(L
, idx
);
350 if (tonumber(o
, &n
)) {
352 lua_Number num
= nvalue(o
);
353 lua_number2integer(res
, num
);
354 if (isnum
) *isnum
= 1;
358 if (isnum
) *isnum
= 0;
364 LUA_API lua_Unsigned
lua_tounsignedx (lua_State
*L
, int idx
, int *isnum
) {
366 const TValue
*o
= index2addr(L
, idx
);
367 if (tonumber(o
, &n
)) {
369 lua_Number num
= nvalue(o
);
370 lua_number2unsigned(res
, num
);
371 if (isnum
) *isnum
= 1;
375 if (isnum
) *isnum
= 0;
381 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
382 const TValue
*o
= index2addr(L
, idx
);
383 return !l_isfalse(o
);
387 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
388 StkId o
= index2addr(L
, idx
);
389 if (!ttisstring(o
)) {
390 lua_lock(L
); /* `luaV_tostring' may create a new string */
391 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
392 if (len
!= NULL
) *len
= 0;
397 o
= index2addr(L
, idx
); /* previous call may reallocate the stack */
400 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
405 LUA_API
size_t lua_rawlen (lua_State
*L
, int idx
) {
406 StkId o
= index2addr(L
, idx
);
407 switch (ttypenv(o
)) {
408 case LUA_TSTRING
: return tsvalue(o
)->len
;
409 case LUA_TUSERDATA
: return uvalue(o
)->len
;
410 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
416 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
417 StkId o
= index2addr(L
, idx
);
418 if (ttislcf(o
)) return fvalue(o
);
419 else if (ttisCclosure(o
))
420 return clCvalue(o
)->f
;
421 else return NULL
; /* not a C function */
425 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
426 StkId o
= index2addr(L
, idx
);
427 switch (ttypenv(o
)) {
428 case LUA_TUSERDATA
: return ((void *)(rawuvalue(o
) + 1));
429 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
430 default: return NULL
;
435 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
436 StkId o
= index2addr(L
, idx
);
437 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
441 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
442 StkId o
= index2addr(L
, idx
);
444 case LUA_TTABLE
: return hvalue(o
);
445 case LUA_TLCL
: return clLvalue(o
);
446 case LUA_TCCL
: return clCvalue(o
);
447 case LUA_TLCF
: return cast(void *, cast(uintptr_t, fvalue(o
)));
448 case LUA_TTHREAD
: return thvalue(o
);
450 case LUA_TLIGHTUSERDATA
:
451 return lua_touserdata(L
, idx
);
452 default: return NULL
;
459 ** push functions (C -> stack)
463 LUA_API
void lua_pushnil (lua_State
*L
) {
471 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
473 setnvalue(L
->top
, n
);
474 luai_checknum(L
, L
->top
,
475 luaG_runerror(L
, "C API - attempt to push a signaling NaN"));
481 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
483 setnvalue(L
->top
, cast_num(n
));
489 LUA_API
void lua_pushunsigned (lua_State
*L
, lua_Unsigned u
) {
492 n
= lua_unsigned2number(u
);
493 setnvalue(L
->top
, n
);
499 LUA_API
const char *lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
503 ts
= luaS_newlstr(L
, s
, len
);
504 setsvalue2s(L
, L
->top
, ts
);
511 LUA_API
const char *lua_pushstring (lua_State
*L
, const char *s
) {
521 setsvalue2s(L
, L
->top
, ts
);
529 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
534 ret
= luaO_pushvfstring(L
, fmt
, argp
);
540 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
546 ret
= luaO_pushvfstring(L
, fmt
, argp
);
553 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
556 setfvalue(L
->top
, fn
);
560 api_checknelems(L
, n
);
561 api_check(L
, n
<= MAXUPVAL
, "upvalue index too large");
563 cl
= luaF_newCclosure(L
, n
);
567 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+ n
);
568 setclCvalue(L
, L
->top
, cl
);
575 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
577 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
583 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
585 setpvalue(L
->top
, p
);
591 LUA_API
int lua_pushthread (lua_State
*L
) {
593 setthvalue(L
, L
->top
, L
);
596 return (G(L
)->mainthread
== L
);
602 ** get functions (Lua -> stack)
606 LUA_API
void lua_getglobal (lua_State
*L
, const char *var
) {
607 Table
*reg
= hvalue(&G(L
)->l_registry
);
608 const TValue
*gt
; /* global table */
610 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
611 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
612 luaV_gettable(L
, gt
, L
->top
- 1, L
->top
- 1);
617 LUA_API
void lua_gettable (lua_State
*L
, int idx
) {
620 t
= index2addr(L
, idx
);
621 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
626 LUA_API
void lua_getfield (lua_State
*L
, int idx
, const char *k
) {
629 t
= index2addr(L
, idx
);
630 setsvalue2s(L
, L
->top
, luaS_new(L
, k
));
632 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
637 LUA_API
void lua_rawget (lua_State
*L
, int idx
) {
640 t
= index2addr(L
, idx
);
641 api_check(L
, ttistable(t
), "table expected");
642 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
647 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
) {
650 t
= index2addr(L
, idx
);
651 api_check(L
, ttistable(t
), "table expected");
652 setobj2s(L
, L
->top
, luaH_getint(hvalue(t
), n
));
658 LUA_API
void lua_rawgetp (lua_State
*L
, int idx
, const void *p
) {
662 t
= index2addr(L
, idx
);
663 api_check(L
, ttistable(t
), "table expected");
664 setpvalue(&k
, cast(void *, p
));
665 setobj2s(L
, L
->top
, luaH_get(hvalue(t
), &k
));
671 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
676 sethvalue(L
, L
->top
, t
);
678 if (narray
> 0 || nrec
> 0)
679 luaH_resize(L
, t
, narray
, nrec
);
684 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
689 obj
= index2addr(L
, objindex
);
690 switch (ttypenv(obj
)) {
692 mt
= hvalue(obj
)->metatable
;
695 mt
= uvalue(obj
)->metatable
;
698 mt
= G(L
)->mt
[ttypenv(obj
)];
704 sethvalue(L
, L
->top
, mt
);
713 LUA_API
void lua_getuservalue (lua_State
*L
, int idx
) {
716 o
= index2addr(L
, idx
);
717 api_check(L
, ttisuserdata(o
), "userdata expected");
718 if (uvalue(o
)->env
) {
719 sethvalue(L
, L
->top
, uvalue(o
)->env
);
728 ** set functions (stack -> Lua)
732 LUA_API
void lua_setglobal (lua_State
*L
, const char *var
) {
733 Table
*reg
= hvalue(&G(L
)->l_registry
);
734 const TValue
*gt
; /* global table */
736 api_checknelems(L
, 1);
737 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
738 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
739 luaV_settable(L
, gt
, L
->top
- 1, L
->top
- 2);
740 L
->top
-= 2; /* pop value and key */
745 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
748 api_checknelems(L
, 2);
749 t
= index2addr(L
, idx
);
750 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
751 L
->top
-= 2; /* pop index and value */
756 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
759 api_checknelems(L
, 1);
760 t
= index2addr(L
, idx
);
761 setsvalue2s(L
, L
->top
++, luaS_new(L
, k
));
762 luaV_settable(L
, t
, L
->top
- 1, L
->top
- 2);
763 L
->top
-= 2; /* pop value and key */
768 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
771 api_checknelems(L
, 2);
772 t
= index2addr(L
, idx
);
773 api_check(L
, ttistable(t
), "table expected");
774 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
-2), L
->top
-1);
775 invalidateTMcache(hvalue(t
));
776 luaC_barrierback(L
, gcvalue(t
), L
->top
-1);
782 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
) {
785 api_checknelems(L
, 1);
786 t
= index2addr(L
, idx
);
787 api_check(L
, ttistable(t
), "table expected");
788 luaH_setint(L
, hvalue(t
), n
, L
->top
- 1);
789 luaC_barrierback(L
, gcvalue(t
), L
->top
-1);
795 LUA_API
void lua_rawsetp (lua_State
*L
, int idx
, const void *p
) {
799 api_checknelems(L
, 1);
800 t
= index2addr(L
, idx
);
801 api_check(L
, ttistable(t
), "table expected");
802 setpvalue(&k
, cast(void *, p
));
803 setobj2t(L
, luaH_set(L
, hvalue(t
), &k
), L
->top
- 1);
804 luaC_barrierback(L
, gcvalue(t
), L
->top
- 1);
810 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
814 api_checknelems(L
, 1);
815 obj
= index2addr(L
, objindex
);
816 if (ttisnil(L
->top
- 1))
819 api_check(L
, ttistable(L
->top
- 1), "table expected");
820 mt
= hvalue(L
->top
- 1);
822 switch (ttypenv(obj
)) {
824 hvalue(obj
)->metatable
= mt
;
826 luaC_objbarrierback(L
, gcvalue(obj
), mt
);
827 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
831 case LUA_TUSERDATA
: {
832 uvalue(obj
)->metatable
= mt
;
834 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
835 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
840 G(L
)->mt
[ttypenv(obj
)] = mt
;
850 LUA_API
void lua_setuservalue (lua_State
*L
, int idx
) {
853 api_checknelems(L
, 1);
854 o
= index2addr(L
, idx
);
855 api_check(L
, ttisuserdata(o
), "userdata expected");
856 if (ttisnil(L
->top
- 1))
857 uvalue(o
)->env
= NULL
;
859 api_check(L
, ttistable(L
->top
- 1), "table expected");
860 uvalue(o
)->env
= hvalue(L
->top
- 1);
861 luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
869 ** `load' and `call' functions (run Lua code)
873 #define checkresults(L,na,nr) \
874 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
875 "results from function overflow current stack size")
878 LUA_API
int lua_getctx (lua_State
*L
, int *ctx
) {
879 if (L
->ci
->callstatus
& CIST_YIELDED
) {
880 if (ctx
) *ctx
= L
->ci
->u
.c
.ctx
;
881 return L
->ci
->u
.c
.status
;
887 LUA_API
void lua_callk (lua_State
*L
, int nargs
, int nresults
, int ctx
,
891 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
892 "cannot use continuations inside hooks");
893 api_checknelems(L
, nargs
+1);
894 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
895 checkresults(L
, nargs
, nresults
);
896 func
= L
->top
- (nargs
+1);
897 if (k
!= NULL
&& L
->nny
== 0) { /* need to prepare continuation? */
898 L
->ci
->u
.c
.k
= k
; /* save continuation */
899 L
->ci
->u
.c
.ctx
= ctx
; /* save context */
900 luaD_call(L
, func
, nresults
, 1); /* do the call */
902 else /* no continuation or no yieldable */
903 luaD_call(L
, func
, nresults
, 0); /* just do the call */
904 adjustresults(L
, nresults
);
911 ** Execute a protected call.
913 struct CallS
{ /* data to `f_call' */
919 static void f_call (lua_State
*L
, void *ud
) {
920 struct CallS
*c
= cast(struct CallS
*, ud
);
921 luaD_call(L
, c
->func
, c
->nresults
, 0);
926 LUA_API
int lua_pcallk (lua_State
*L
, int nargs
, int nresults
, int errfunc
,
927 int ctx
, lua_CFunction k
) {
932 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
933 "cannot use continuations inside hooks");
934 api_checknelems(L
, nargs
+1);
935 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
936 checkresults(L
, nargs
, nresults
);
940 StkId o
= index2addr(L
, errfunc
);
941 api_checkstackindex(L
, errfunc
, o
);
942 func
= savestack(L
, o
);
944 c
.func
= L
->top
- (nargs
+1); /* function to be called */
945 if (k
== NULL
|| L
->nny
> 0) { /* no continuation or no yieldable? */
946 c
.nresults
= nresults
; /* do a 'conventional' protected call */
947 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
949 else { /* prepare continuation (call is already protected by 'resume') */
950 CallInfo
*ci
= L
->ci
;
951 ci
->u
.c
.k
= k
; /* save continuation */
952 ci
->u
.c
.ctx
= ctx
; /* save context */
953 /* save information for error recovery */
954 ci
->extra
= savestack(L
, c
.func
);
955 ci
->u
.c
.old_allowhook
= L
->allowhook
;
956 ci
->u
.c
.old_errfunc
= L
->errfunc
;
958 /* mark that function may do error recovery */
959 ci
->callstatus
|= CIST_YPCALL
;
960 luaD_call(L
, c
.func
, nresults
, 1); /* do the call */
961 ci
->callstatus
&= ~CIST_YPCALL
;
962 L
->errfunc
= ci
->u
.c
.old_errfunc
;
963 status
= LUA_OK
; /* if it is here, there were no errors */
965 adjustresults(L
, nresults
);
971 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
972 const char *chunkname
, const char *mode
) {
976 if (!chunkname
) chunkname
= "?";
977 luaZ_init(L
, &z
, reader
, data
);
978 status
= luaD_protectedparser(L
, &z
, chunkname
, mode
);
979 if (status
== LUA_OK
) { /* no errors? */
980 LClosure
*f
= clLvalue(L
->top
- 1); /* get newly created function */
981 if (f
->nupvalues
== 1) { /* does it have one upvalue? */
982 /* get global table from registry */
983 Table
*reg
= hvalue(&G(L
)->l_registry
);
984 const TValue
*gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
985 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
986 setobj(L
, f
->upvals
[0]->v
, gt
);
987 luaC_barrier(L
, f
->upvals
[0], gt
);
994 #if defined(LUA_USE_DUMP)
995 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
) {
999 api_checknelems(L
, 1);
1002 status
= luaU_dump(L
, getproto(o
), writer
, data
, 0);
1010 LUA_API
int lua_status (lua_State
*L
) {
1016 ** Garbage-collection function
1019 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
1029 case LUA_GCRESTART
: {
1034 case LUA_GCCOLLECT
: {
1039 /* GC values are expressed in Kbytes: #bytes/2^10 */
1040 res
= cast_int(gettotalbytes(g
) >> 10);
1043 case LUA_GCCOUNTB
: {
1044 res
= cast_int(gettotalbytes(g
) & 0x3ff);
1048 if (g
->gckind
== KGC_GEN
) { /* generational mode? */
1049 res
= (g
->GCestimate
== 0); /* true if it will do major collection */
1050 luaC_forcestep(L
); /* do a single step */
1053 lu_mem debt
= cast(lu_mem
, data
) * 1024 - GCSTEPSIZE
;
1055 debt
+= g
->GCdebt
; /* include current debt */
1056 luaE_setdebt(g
, debt
);
1058 if (g
->gcstate
== GCSpause
) /* end of cycle? */
1059 res
= 1; /* signal it */
1063 case LUA_GCSETPAUSE
: {
1068 case LUA_GCSETMAJORINC
: {
1069 res
= g
->gcmajorinc
;
1070 g
->gcmajorinc
= data
;
1073 case LUA_GCSETSTEPMUL
: {
1075 g
->gcstepmul
= data
;
1078 case LUA_GCISRUNNING
: {
1082 case LUA_GCGEN
: { /* change collector to generational mode */
1083 luaC_changemode(L
, KGC_GEN
);
1086 case LUA_GCINC
: { /* change collector to incremental mode */
1087 luaC_changemode(L
, KGC_NORMAL
);
1090 default: res
= -1; /* invalid option */
1099 ** miscellaneous functions
1103 LUA_API
int lua_error (lua_State
*L
) {
1105 api_checknelems(L
, 1);
1107 /* code unreachable; will unlock when control actually leaves the kernel */
1108 return 0; /* to avoid warnings */
1112 LUA_API
int lua_next (lua_State
*L
, int idx
) {
1116 t
= index2addr(L
, idx
);
1117 api_check(L
, ttistable(t
), "table expected");
1118 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
1122 else /* no more elements */
1123 L
->top
-= 1; /* remove key */
1129 LUA_API
void lua_concat (lua_State
*L
, int n
) {
1131 api_checknelems(L
, n
);
1136 else if (n
== 0) { /* push empty string */
1137 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1140 /* else n == 1; nothing to do */
1145 LUA_API
void lua_len (lua_State
*L
, int idx
) {
1148 t
= index2addr(L
, idx
);
1149 luaV_objlen(L
, L
->top
, t
);
1155 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1158 if (ud
) *ud
= G(L
)->ud
;
1165 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1173 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1177 u
= luaS_newudata(L
, size
, NULL
);
1178 setuvalue(L
, L
->top
, u
);
1186 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
,
1188 switch (ttype(fi
)) {
1189 case LUA_TCCL
: { /* C closure */
1190 CClosure
*f
= clCvalue(fi
);
1191 if (!(1 <= n
&& n
<= f
->nupvalues
)) return NULL
;
1192 *val
= &f
->upvalue
[n
-1];
1193 if (owner
) *owner
= obj2gco(f
);
1196 case LUA_TLCL
: { /* Lua closure */
1197 LClosure
*f
= clLvalue(fi
);
1200 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1201 *val
= f
->upvals
[n
-1]->v
;
1202 if (owner
) *owner
= obj2gco(f
->upvals
[n
- 1]);
1203 name
= p
->upvalues
[n
-1].name
;
1204 return (name
== NULL
) ? "" : getstr(name
);
1206 default: return NULL
; /* not a closure */
1211 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1213 TValue
*val
= NULL
; /* to avoid warnings */
1215 name
= aux_upvalue(index2addr(L
, funcindex
), n
, &val
, NULL
);
1217 setobj2s(L
, L
->top
, val
);
1225 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1227 TValue
*val
= NULL
; /* to avoid warnings */
1228 GCObject
*owner
= NULL
; /* to avoid warnings */
1231 fi
= index2addr(L
, funcindex
);
1232 api_checknelems(L
, 1);
1233 name
= aux_upvalue(fi
, n
, &val
, &owner
);
1236 setobj(L
, val
, L
->top
);
1237 luaC_barrier(L
, owner
, L
->top
);
1244 static UpVal
**getupvalref (lua_State
*L
, int fidx
, int n
, LClosure
**pf
) {
1246 StkId fi
= index2addr(L
, fidx
);
1247 api_check(L
, ttisLclosure(fi
), "Lua function expected");
1249 api_check(L
, (1 <= n
&& n
<= f
->p
->sizeupvalues
), "invalid upvalue index");
1251 return &f
->upvals
[n
- 1]; /* get its upvalue pointer */
1255 LUA_API
void *lua_upvalueid (lua_State
*L
, int fidx
, int n
) {
1256 StkId fi
= index2addr(L
, fidx
);
1257 switch (ttype(fi
)) {
1258 case LUA_TLCL
: { /* lua closure */
1259 return *getupvalref(L
, fidx
, n
, NULL
);
1261 case LUA_TCCL
: { /* C closure */
1262 CClosure
*f
= clCvalue(fi
);
1263 api_check(L
, 1 <= n
&& n
<= f
->nupvalues
, "invalid upvalue index");
1264 return &f
->upvalue
[n
- 1];
1267 api_check(L
, 0, "closure expected");
1274 LUA_API
void lua_upvaluejoin (lua_State
*L
, int fidx1
, int n1
,
1275 int fidx2
, int n2
) {
1277 UpVal
**up1
= getupvalref(L
, fidx1
, n1
, &f1
);
1278 UpVal
**up2
= getupvalref(L
, fidx2
, n2
, NULL
);
1280 luaC_objbarrier(L
, f1
, *up2
);
1283 EXPORT_SYMBOL(lua_absindex
);
1284 EXPORT_SYMBOL(lua_atpanic
);
1285 EXPORT_SYMBOL(lua_checkstack
);
1286 EXPORT_SYMBOL(lua_close
);
1287 EXPORT_SYMBOL(lua_createtable
);
1288 EXPORT_SYMBOL(lua_error
);
1289 EXPORT_SYMBOL(lua_getfield
);
1290 EXPORT_SYMBOL(lua_gettable
);
1291 EXPORT_SYMBOL(lua_gettop
);
1292 EXPORT_SYMBOL(lua_isnumber
);
1293 EXPORT_SYMBOL(lua_isstring
);
1294 EXPORT_SYMBOL(lua_newstate
);
1295 EXPORT_SYMBOL(lua_newuserdata
);
1296 EXPORT_SYMBOL(lua_next
);
1297 EXPORT_SYMBOL(lua_pcallk
);
1298 EXPORT_SYMBOL(lua_pushboolean
);
1299 EXPORT_SYMBOL(lua_pushcclosure
);
1300 EXPORT_SYMBOL(lua_pushfstring
);
1301 EXPORT_SYMBOL(lua_pushinteger
);
1302 EXPORT_SYMBOL(lua_pushlightuserdata
);
1303 EXPORT_SYMBOL(lua_pushnil
);
1304 EXPORT_SYMBOL(lua_pushnumber
);
1305 EXPORT_SYMBOL(lua_pushstring
);
1306 EXPORT_SYMBOL(lua_pushvalue
);
1307 EXPORT_SYMBOL(lua_pushvfstring
);
1308 EXPORT_SYMBOL(lua_remove
);
1309 EXPORT_SYMBOL(lua_replace
);
1310 EXPORT_SYMBOL(lua_setfield
);
1311 EXPORT_SYMBOL(lua_setglobal
);
1312 EXPORT_SYMBOL(lua_sethook
);
1313 EXPORT_SYMBOL(lua_setmetatable
);
1314 EXPORT_SYMBOL(lua_settable
);
1315 EXPORT_SYMBOL(lua_settop
);
1316 EXPORT_SYMBOL(lua_toboolean
);
1317 EXPORT_SYMBOL(lua_tointegerx
);
1318 EXPORT_SYMBOL(lua_tolstring
);
1319 EXPORT_SYMBOL(lua_tonumberx
);
1320 EXPORT_SYMBOL(lua_touserdata
);
1321 EXPORT_SYMBOL(lua_type
);
1322 EXPORT_SYMBOL(lua_typename
);