2 ** $Id: lapi.c,v 2.159 2011/11/30 12:32:05 roberto Exp $
4 ** See Copyright Notice in lua.h
32 const char lua_ident
[] =
33 "$LuaVersion: " LUA_COPYRIGHT
" $"
34 "$LuaAuthors: " LUA_AUTHORS
" $";
37 /* value at a non-valid index */
38 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
40 /* corresponding test */
41 #define isvalid(o) ((o) != luaO_nilobject)
43 #define api_checkvalidindex(L, i) api_check(L, isvalid(i), "invalid index")
46 static TValue
*index2addr (lua_State
*L
, int idx
) {
49 TValue
*o
= ci
->func
+ idx
;
50 api_check(L
, idx
<= ci
->top
- (ci
->func
+ 1), "unacceptable index");
51 if (o
>= L
->top
) return NONVALIDVALUE
;
54 else if (idx
> LUA_REGISTRYINDEX
) {
55 api_check(L
, idx
!= 0 && -idx
<= L
->top
- (ci
->func
+ 1), "invalid index");
58 else if (idx
== LUA_REGISTRYINDEX
)
59 return &G(L
)->l_registry
;
61 idx
= LUA_REGISTRYINDEX
- idx
;
62 api_check(L
, idx
<= MAXUPVAL
+ 1, "upvalue index too large");
63 if (ttislcf(ci
->func
)) /* light C function? */
64 return NONVALIDVALUE
; /* it has no upvalues */
66 CClosure
*func
= clCvalue(ci
->func
);
67 return (idx
<= func
->nupvalues
) ? &func
->upvalue
[idx
-1] : NONVALIDVALUE
;
74 ** to be called by 'lua_checkstack' in protected mode, to grow stack
75 ** capturing memory errors
77 static void growstack (lua_State
*L
, void *ud
) {
78 int size
= *(int *)ud
;
79 luaD_growstack(L
, size
);
83 LUA_API
int lua_checkstack (lua_State
*L
, int size
) {
87 if (L
->stack_last
- L
->top
> size
) /* stack large enough? */
88 res
= 1; /* yes; check is OK */
89 else { /* no; need to grow stack */
90 int inuse
= cast_int(L
->top
- L
->stack
) + EXTRA_STACK
;
91 if (inuse
> LUAI_MAXSTACK
- size
) /* can grow without overflow? */
93 else /* try to grow stack */
94 res
= (luaD_rawrunprotected(L
, &growstack
, &size
) == LUA_OK
);
96 if (res
&& ci
->top
< L
->top
+ size
)
97 ci
->top
= L
->top
+ size
; /* adjust frame top */
103 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
105 if (from
== to
) return;
107 api_checknelems(from
, n
);
108 api_check(from
, G(from
) == G(to
), "moving among independent states");
109 api_check(from
, to
->ci
->top
- to
->top
>= n
, "not enough elements to move");
111 for (i
= 0; i
< n
; i
++) {
112 setobj2s(to
, to
->top
++, from
->top
+ i
);
118 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
122 G(L
)->panic
= panicf
;
128 LUA_API
const lua_Number
*lua_version (lua_State
*L
) {
129 static const lua_Number version
= LUA_VERSION_NUM
;
130 if (L
== NULL
) return &version
;
131 else return G(L
)->version
;
137 ** basic stack manipulation
142 ** convert an acceptable stack index into an absolute index
144 LUA_API
int lua_absindex (lua_State
*L
, int idx
) {
145 return (idx
> 0 || idx
<= LUA_REGISTRYINDEX
)
147 : cast_int(L
->top
- L
->ci
->func
+ idx
);
151 LUA_API
int lua_gettop (lua_State
*L
) {
152 return cast_int(L
->top
- (L
->ci
->func
+ 1));
156 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
157 StkId func
= L
->ci
->func
;
160 api_check(L
, idx
<= L
->stack_last
- (func
+ 1), "new top too large");
161 while (L
->top
< (func
+ 1) + idx
)
162 setnilvalue(L
->top
++);
163 L
->top
= (func
+ 1) + idx
;
166 api_check(L
, -(idx
+1) <= (L
->top
- (func
+ 1)), "invalid new top");
167 L
->top
+= idx
+1; /* `subtract' index (index is negative) */
173 LUA_API
void lua_remove (lua_State
*L
, int idx
) {
176 p
= index2addr(L
, idx
);
177 api_checkvalidindex(L
, p
);
178 while (++p
< L
->top
) setobjs2s(L
, p
-1, p
);
184 LUA_API
void lua_insert (lua_State
*L
, int idx
) {
188 p
= index2addr(L
, idx
);
189 api_checkvalidindex(L
, p
);
190 for (q
= L
->top
; q
>p
; q
--) setobjs2s(L
, q
, q
-1);
191 setobjs2s(L
, p
, L
->top
);
196 static void moveto (lua_State
*L
, TValue
*fr
, int idx
) {
197 TValue
*to
= index2addr(L
, idx
);
198 api_checkvalidindex(L
, to
);
200 if (idx
< LUA_REGISTRYINDEX
) /* function upvalue? */
201 luaC_barrier(L
, clCvalue(L
->ci
->func
), fr
);
202 /* LUA_REGISTRYINDEX does not need gc barrier
203 (collector revisits it before finishing collection) */
207 LUA_API
void lua_replace (lua_State
*L
, int idx
) {
209 api_checknelems(L
, 1);
210 moveto(L
, L
->top
- 1, idx
);
216 LUA_API
void lua_copy (lua_State
*L
, int fromidx
, int toidx
) {
219 fr
= index2addr(L
, fromidx
);
220 api_checkvalidindex(L
, fr
);
221 moveto(L
, fr
, toidx
);
226 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
228 setobj2s(L
, L
->top
, index2addr(L
, idx
));
236 ** access functions (stack -> C)
240 LUA_API
int lua_type (lua_State
*L
, int idx
) {
241 StkId o
= index2addr(L
, idx
);
242 return (isvalid(o
) ? ttypenv(o
) : LUA_TNONE
);
246 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
252 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
253 StkId o
= index2addr(L
, idx
);
254 return (ttislcf(o
) || (ttisCclosure(o
)));
258 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
260 const TValue
*o
= index2addr(L
, idx
);
261 return tonumber(o
, &n
);
265 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
266 int t
= lua_type(L
, idx
);
267 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
271 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
272 const TValue
*o
= index2addr(L
, idx
);
273 return (ttisuserdata(o
) || ttislightuserdata(o
));
277 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
278 StkId o1
= index2addr(L
, index1
);
279 StkId o2
= index2addr(L
, index2
);
280 return (isvalid(o1
) && isvalid(o2
)) ? luaV_rawequalobj(o1
, o2
) : 0;
284 LUA_API
void lua_arith (lua_State
*L
, int op
) {
285 StkId o1
; /* 1st operand */
286 StkId o2
; /* 2nd operand */
288 if (op
!= LUA_OPUNM
) /* all other operations expect two operands */
289 api_checknelems(L
, 2);
290 else { /* for unary minus, add fake 2nd operand */
291 api_checknelems(L
, 1);
292 setobjs2s(L
, L
->top
, L
->top
- 1);
297 if (ttisnumber(o1
) && ttisnumber(o2
)) {
298 changenvalue(o1
, luaO_arith(op
, nvalue(o1
), nvalue(o2
)));
301 luaV_arith(L
, o1
, o1
, o2
, cast(TMS
, op
- LUA_OPADD
+ TM_ADD
));
307 LUA_API
int lua_compare (lua_State
*L
, int index1
, int index2
, int op
) {
310 lua_lock(L
); /* may call tag method */
311 o1
= index2addr(L
, index1
);
312 o2
= index2addr(L
, index2
);
313 if (isvalid(o1
) && isvalid(o2
)) {
315 case LUA_OPEQ
: i
= equalobj(L
, o1
, o2
); break;
316 case LUA_OPLT
: i
= luaV_lessthan(L
, o1
, o2
); break;
317 case LUA_OPLE
: i
= luaV_lessequal(L
, o1
, o2
); break;
318 default: api_check(L
, 0, "invalid option");
326 LUA_API lua_Number
lua_tonumberx (lua_State
*L
, int idx
, int *isnum
) {
328 const TValue
*o
= index2addr(L
, idx
);
329 if (tonumber(o
, &n
)) {
330 if (isnum
) *isnum
= 1;
334 if (isnum
) *isnum
= 0;
340 LUA_API lua_Integer
lua_tointegerx (lua_State
*L
, int idx
, int *isnum
) {
342 const TValue
*o
= index2addr(L
, idx
);
343 if (tonumber(o
, &n
)) {
345 lua_Number num
= nvalue(o
);
346 lua_number2integer(res
, num
);
347 if (isnum
) *isnum
= 1;
351 if (isnum
) *isnum
= 0;
357 LUA_API lua_Unsigned
lua_tounsignedx (lua_State
*L
, int idx
, int *isnum
) {
359 const TValue
*o
= index2addr(L
, idx
);
360 if (tonumber(o
, &n
)) {
362 lua_Number num
= nvalue(o
);
363 lua_number2unsigned(res
, num
);
364 if (isnum
) *isnum
= 1;
368 if (isnum
) *isnum
= 0;
374 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
375 const TValue
*o
= index2addr(L
, idx
);
376 return !l_isfalse(o
);
380 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
381 StkId o
= index2addr(L
, idx
);
382 if (!ttisstring(o
)) {
383 lua_lock(L
); /* `luaV_tostring' may create a new string */
384 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
385 if (len
!= NULL
) *len
= 0;
390 o
= index2addr(L
, idx
); /* previous call may reallocate the stack */
393 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
398 LUA_API
size_t lua_rawlen (lua_State
*L
, int idx
) {
399 StkId o
= index2addr(L
, idx
);
400 switch (ttypenv(o
)) {
401 case LUA_TSTRING
: return tsvalue(o
)->len
;
402 case LUA_TUSERDATA
: return uvalue(o
)->len
;
403 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
409 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
410 StkId o
= index2addr(L
, idx
);
411 if (ttislcf(o
)) return fvalue(o
);
412 else if (ttisCclosure(o
))
413 return clCvalue(o
)->f
;
414 else return NULL
; /* not a C function */
418 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
419 StkId o
= index2addr(L
, idx
);
420 switch (ttypenv(o
)) {
421 case LUA_TUSERDATA
: return (rawuvalue(o
) + 1);
422 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
423 default: return NULL
;
428 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
429 StkId o
= index2addr(L
, idx
);
430 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
434 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
435 StkId o
= index2addr(L
, idx
);
437 case LUA_TTABLE
: return hvalue(o
);
438 case LUA_TLCL
: return clLvalue(o
);
439 case LUA_TCCL
: return clCvalue(o
);
440 case LUA_TLCF
: return cast(void *, cast(size_t, fvalue(o
)));
441 case LUA_TTHREAD
: return thvalue(o
);
443 case LUA_TLIGHTUSERDATA
:
444 return lua_touserdata(L
, idx
);
445 default: return NULL
;
452 ** push functions (C -> stack)
456 LUA_API
void lua_pushnil (lua_State
*L
) {
464 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
466 setnvalue(L
->top
, n
);
467 luai_checknum(L
, L
->top
,
468 luaG_runerror(L
, "C API - attempt to push a signaling NaN"));
474 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
476 setnvalue(L
->top
, cast_num(n
));
482 LUA_API
void lua_pushunsigned (lua_State
*L
, lua_Unsigned u
) {
485 n
= lua_unsigned2number(u
);
486 setnvalue(L
->top
, n
);
492 LUA_API
const char *lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
496 ts
= luaS_newlstr(L
, s
, len
);
497 setsvalue2s(L
, L
->top
, ts
);
504 LUA_API
const char *lua_pushstring (lua_State
*L
, const char *s
) {
514 setsvalue2s(L
, L
->top
, ts
);
522 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
527 ret
= luaO_pushvfstring(L
, fmt
, argp
);
533 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
539 ret
= luaO_pushvfstring(L
, fmt
, argp
);
546 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
549 setfvalue(L
->top
, fn
);
553 api_checknelems(L
, n
);
554 api_check(L
, n
<= MAXUPVAL
, "upvalue index too large");
556 cl
= luaF_newCclosure(L
, n
);
560 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+ n
);
561 setclCvalue(L
, L
->top
, cl
);
568 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
570 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
576 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
578 setpvalue(L
->top
, p
);
584 LUA_API
int lua_pushthread (lua_State
*L
) {
586 setthvalue(L
, L
->top
, L
);
589 return (G(L
)->mainthread
== L
);
595 ** get functions (Lua -> stack)
599 LUA_API
void lua_getglobal (lua_State
*L
, const char *var
) {
600 Table
*reg
= hvalue(&G(L
)->l_registry
);
601 const TValue
*gt
; /* global table */
603 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
604 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
605 luaV_gettable(L
, gt
, L
->top
- 1, L
->top
- 1);
610 LUA_API
void lua_gettable (lua_State
*L
, int idx
) {
613 t
= index2addr(L
, idx
);
614 api_checkvalidindex(L
, t
);
615 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
620 LUA_API
void lua_getfield (lua_State
*L
, int idx
, const char *k
) {
623 t
= index2addr(L
, idx
);
624 api_checkvalidindex(L
, t
);
625 setsvalue2s(L
, L
->top
, luaS_new(L
, k
));
627 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
632 LUA_API
void lua_rawget (lua_State
*L
, int idx
) {
635 t
= index2addr(L
, idx
);
636 api_check(L
, ttistable(t
), "table expected");
637 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
642 LUA_API
void lua_rawgeti (lua_State
*L
, int idx
, int n
) {
645 t
= index2addr(L
, idx
);
646 api_check(L
, ttistable(t
), "table expected");
647 setobj2s(L
, L
->top
, luaH_getint(hvalue(t
), n
));
653 LUA_API
void lua_rawgetp (lua_State
*L
, int idx
, const void *p
) {
657 t
= index2addr(L
, idx
);
658 api_check(L
, ttistable(t
), "table expected");
659 setpvalue(&k
, cast(void *, p
));
660 setobj2s(L
, L
->top
, luaH_get(hvalue(t
), &k
));
666 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
671 sethvalue(L
, L
->top
, t
);
673 if (narray
> 0 || nrec
> 0)
674 luaH_resize(L
, t
, narray
, nrec
);
679 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
684 obj
= index2addr(L
, objindex
);
685 switch (ttypenv(obj
)) {
687 mt
= hvalue(obj
)->metatable
;
690 mt
= uvalue(obj
)->metatable
;
693 mt
= G(L
)->mt
[ttypenv(obj
)];
699 sethvalue(L
, L
->top
, mt
);
708 LUA_API
void lua_getuservalue (lua_State
*L
, int idx
) {
711 o
= index2addr(L
, idx
);
712 api_checkvalidindex(L
, o
);
713 api_check(L
, ttisuserdata(o
), "userdata expected");
714 if (uvalue(o
)->env
) {
715 sethvalue(L
, L
->top
, uvalue(o
)->env
);
724 ** set functions (stack -> Lua)
728 LUA_API
void lua_setglobal (lua_State
*L
, const char *var
) {
729 Table
*reg
= hvalue(&G(L
)->l_registry
);
730 const TValue
*gt
; /* global table */
732 api_checknelems(L
, 1);
733 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
734 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
735 luaV_settable(L
, gt
, L
->top
- 1, L
->top
- 2);
736 L
->top
-= 2; /* pop value and key */
741 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
744 api_checknelems(L
, 2);
745 t
= index2addr(L
, idx
);
746 api_checkvalidindex(L
, t
);
747 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
748 L
->top
-= 2; /* pop index and value */
753 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
756 api_checknelems(L
, 1);
757 t
= index2addr(L
, idx
);
758 api_checkvalidindex(L
, t
);
759 setsvalue2s(L
, L
->top
++, luaS_new(L
, k
));
760 luaV_settable(L
, t
, L
->top
- 1, L
->top
- 2);
761 L
->top
-= 2; /* pop value and key */
766 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
769 api_checknelems(L
, 2);
770 t
= index2addr(L
, idx
);
771 api_check(L
, ttistable(t
), "table expected");
772 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
-2), L
->top
-1);
773 invalidateTMcache(hvalue(t
));
774 luaC_barrierback(L
, gcvalue(t
), L
->top
-1);
780 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, int n
) {
783 api_checknelems(L
, 1);
784 t
= index2addr(L
, idx
);
785 api_check(L
, ttistable(t
), "table expected");
786 luaH_setint(L
, hvalue(t
), n
, L
->top
- 1);
787 luaC_barrierback(L
, gcvalue(t
), L
->top
-1);
793 LUA_API
void lua_rawsetp (lua_State
*L
, int idx
, const void *p
) {
797 api_checknelems(L
, 1);
798 t
= index2addr(L
, idx
);
799 api_check(L
, ttistable(t
), "table expected");
800 setpvalue(&k
, cast(void *, p
));
801 setobj2t(L
, luaH_set(L
, hvalue(t
), &k
), L
->top
- 1);
802 luaC_barrierback(L
, gcvalue(t
), L
->top
- 1);
808 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
812 api_checknelems(L
, 1);
813 obj
= index2addr(L
, objindex
);
814 api_checkvalidindex(L
, obj
);
815 if (ttisnil(L
->top
- 1))
818 api_check(L
, ttistable(L
->top
- 1), "table expected");
819 mt
= hvalue(L
->top
- 1);
821 switch (ttypenv(obj
)) {
823 hvalue(obj
)->metatable
= mt
;
825 luaC_objbarrierback(L
, gcvalue(obj
), mt
);
826 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
829 case LUA_TUSERDATA
: {
830 uvalue(obj
)->metatable
= mt
;
832 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
833 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
838 G(L
)->mt
[ttypenv(obj
)] = mt
;
848 LUA_API
void lua_setuservalue (lua_State
*L
, int idx
) {
851 api_checknelems(L
, 1);
852 o
= index2addr(L
, idx
);
853 api_checkvalidindex(L
, o
);
854 api_check(L
, ttisuserdata(o
), "userdata expected");
855 if (ttisnil(L
->top
- 1))
856 uvalue(o
)->env
= NULL
;
858 api_check(L
, ttistable(L
->top
- 1), "table expected");
859 uvalue(o
)->env
= hvalue(L
->top
- 1);
860 luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
868 ** `load' and `call' functions (run Lua code)
872 #define checkresults(L,na,nr) \
873 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
874 "results from function overflow current stack size")
877 LUA_API
int lua_getctx (lua_State
*L
, int *ctx
) {
878 if (L
->ci
->callstatus
& CIST_YIELDED
) {
879 if (ctx
) *ctx
= L
->ci
->u
.c
.ctx
;
880 return L
->ci
->u
.c
.status
;
886 LUA_API
void lua_callk (lua_State
*L
, int nargs
, int nresults
, int ctx
,
890 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
891 "cannot use continuations inside hooks");
892 api_checknelems(L
, nargs
+1);
893 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
894 checkresults(L
, nargs
, nresults
);
895 func
= L
->top
- (nargs
+1);
896 if (k
!= NULL
&& L
->nny
== 0) { /* need to prepare continuation? */
897 L
->ci
->u
.c
.k
= k
; /* save continuation */
898 L
->ci
->u
.c
.ctx
= ctx
; /* save context */
899 luaD_call(L
, func
, nresults
, 1); /* do the call */
901 else /* no continuation or no yieldable */
902 luaD_call(L
, func
, nresults
, 0); /* just do the call */
903 adjustresults(L
, nresults
);
910 ** Execute a protected call.
912 struct CallS
{ /* data to `f_call' */
918 static void f_call (lua_State
*L
, void *ud
) {
919 struct CallS
*c
= cast(struct CallS
*, ud
);
920 luaD_call(L
, c
->func
, c
->nresults
, 0);
925 LUA_API
int lua_pcallk (lua_State
*L
, int nargs
, int nresults
, int errfunc
,
926 int ctx
, lua_CFunction k
) {
931 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
932 "cannot use continuations inside hooks");
933 api_checknelems(L
, nargs
+1);
934 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
935 checkresults(L
, nargs
, nresults
);
939 StkId o
= index2addr(L
, errfunc
);
940 api_checkvalidindex(L
, o
);
941 func
= savestack(L
, o
);
943 c
.func
= L
->top
- (nargs
+1); /* function to be called */
944 if (k
== NULL
|| L
->nny
> 0) { /* no continuation or no yieldable? */
945 c
.nresults
= nresults
; /* do a 'conventional' protected call */
946 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
948 else { /* prepare continuation (call is already protected by 'resume') */
949 CallInfo
*ci
= L
->ci
;
950 ci
->u
.c
.k
= k
; /* save continuation */
951 ci
->u
.c
.ctx
= ctx
; /* save context */
952 /* save information for error recovery */
953 ci
->u
.c
.extra
= savestack(L
, c
.func
);
954 ci
->u
.c
.old_allowhook
= L
->allowhook
;
955 ci
->u
.c
.old_errfunc
= L
->errfunc
;
957 /* mark that function may do error recovery */
958 ci
->callstatus
|= CIST_YPCALL
;
959 luaD_call(L
, c
.func
, nresults
, 1); /* do the call */
960 ci
->callstatus
&= ~CIST_YPCALL
;
961 L
->errfunc
= ci
->u
.c
.old_errfunc
;
962 status
= LUA_OK
; /* if it is here, there were no errors */
964 adjustresults(L
, nresults
);
970 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
971 const char *chunkname
, const char *mode
) {
975 if (!chunkname
) chunkname
= "?";
976 luaZ_init(L
, &z
, reader
, data
);
977 status
= luaD_protectedparser(L
, &z
, chunkname
, mode
);
978 if (status
== LUA_OK
) { /* no errors? */
979 LClosure
*f
= clLvalue(L
->top
- 1); /* get newly created function */
980 if (f
->nupvalues
== 1) { /* does it have one upvalue? */
981 /* get global table from registry */
982 Table
*reg
= hvalue(&G(L
)->l_registry
);
983 const TValue
*gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
984 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
985 setobj(L
, f
->upvals
[0]->v
, gt
);
986 luaC_barrier(L
, f
->upvals
[0], gt
);
994 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
) {
998 api_checknelems(L
, 1);
1001 status
= luaU_dump(L
, getproto(o
), writer
, data
, 0);
1009 LUA_API
int lua_status (lua_State
*L
) {
1015 ** Garbage-collection function
1018 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
1028 case LUA_GCRESTART
: {
1033 case LUA_GCCOLLECT
: {
1038 /* GC values are expressed in Kbytes: #bytes/2^10 */
1039 res
= cast_int(gettotalbytes(g
) >> 10);
1042 case LUA_GCCOUNTB
: {
1043 res
= cast_int(gettotalbytes(g
) & 0x3ff);
1047 if (g
->gckind
== KGC_GEN
) { /* generational mode? */
1048 res
= (g
->lastmajormem
== 0); /* 1 if will do major collection */
1049 luaC_forcestep(L
); /* do a single step */
1052 while (data
-- >= 0) {
1054 if (g
->gcstate
== GCSpause
) { /* end of cycle? */
1055 res
= 1; /* signal it */
1062 case LUA_GCSETPAUSE
: {
1067 case LUA_GCSETMAJORINC
: {
1068 res
= g
->gcmajorinc
;
1069 g
->gcmajorinc
= data
;
1072 case LUA_GCSETSTEPMUL
: {
1074 g
->gcstepmul
= data
;
1077 case LUA_GCISRUNNING
: {
1081 case LUA_GCGEN
: { /* change collector to generational mode */
1082 luaC_changemode(L
, KGC_GEN
);
1085 case LUA_GCINC
: { /* change collector to incremental mode */
1086 luaC_changemode(L
, KGC_NORMAL
);
1089 default: res
= -1; /* invalid option */
1098 ** miscellaneous functions
1102 LUA_API
int lua_error (lua_State
*L
) {
1104 api_checknelems(L
, 1);
1107 return 0; /* to avoid warnings */
1111 LUA_API
int lua_next (lua_State
*L
, int idx
) {
1115 t
= index2addr(L
, idx
);
1116 api_check(L
, ttistable(t
), "table expected");
1117 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
1121 else /* no more elements */
1122 L
->top
-= 1; /* remove key */
1128 LUA_API
void lua_concat (lua_State
*L
, int n
) {
1130 api_checknelems(L
, n
);
1135 else if (n
== 0) { /* push empty string */
1136 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1139 /* else n == 1; nothing to do */
1144 LUA_API
void lua_len (lua_State
*L
, int idx
) {
1147 t
= index2addr(L
, idx
);
1148 luaV_objlen(L
, L
->top
, t
);
1154 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1157 if (ud
) *ud
= G(L
)->ud
;
1164 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1172 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1176 u
= luaS_newudata(L
, size
, NULL
);
1177 setuvalue(L
, L
->top
, u
);
1185 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
,
1187 switch (ttype(fi
)) {
1188 case LUA_TCCL
: { /* C closure */
1189 CClosure
*f
= clCvalue(fi
);
1190 if (!(1 <= n
&& n
<= f
->nupvalues
)) return NULL
;
1191 *val
= &f
->upvalue
[n
-1];
1192 if (owner
) *owner
= obj2gco(f
);
1195 case LUA_TLCL
: { /* Lua closure */
1196 LClosure
*f
= clLvalue(fi
);
1199 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1200 *val
= f
->upvals
[n
-1]->v
;
1201 if (owner
) *owner
= obj2gco(f
->upvals
[n
- 1]);
1202 name
= p
->upvalues
[n
-1].name
;
1203 return (name
== NULL
) ? "" : getstr(name
);
1205 default: return NULL
; /* not a closure */
1210 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1212 TValue
*val
= NULL
; /* to avoid warnings */
1214 name
= aux_upvalue(index2addr(L
, funcindex
), n
, &val
, NULL
);
1216 setobj2s(L
, L
->top
, val
);
1224 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1226 TValue
*val
= NULL
; /* to avoid warnings */
1227 GCObject
*owner
= NULL
; /* to avoid warnings */
1230 fi
= index2addr(L
, funcindex
);
1231 api_checknelems(L
, 1);
1232 name
= aux_upvalue(fi
, n
, &val
, &owner
);
1235 setobj(L
, val
, L
->top
);
1236 luaC_barrier(L
, owner
, L
->top
);
1243 static UpVal
**getupvalref (lua_State
*L
, int fidx
, int n
, LClosure
**pf
) {
1245 StkId fi
= index2addr(L
, fidx
);
1246 api_check(L
, ttisLclosure(fi
), "Lua function expected");
1248 api_check(L
, (1 <= n
&& n
<= f
->p
->sizeupvalues
), "invalid upvalue index");
1250 return &f
->upvals
[n
- 1]; /* get its upvalue pointer */
1254 LUA_API
void *lua_upvalueid (lua_State
*L
, int fidx
, int n
) {
1255 StkId fi
= index2addr(L
, fidx
);
1256 switch (ttype(fi
)) {
1257 case LUA_TLCL
: { /* lua closure */
1258 return *getupvalref(L
, fidx
, n
, NULL
);
1260 case LUA_TCCL
: { /* C closure */
1261 CClosure
*f
= clCvalue(fi
);
1262 api_check(L
, 1 <= n
&& n
<= f
->nupvalues
, "invalid upvalue index");
1263 return &f
->upvalue
[n
- 1];
1266 api_check(L
, 0, "closure expected");
1273 LUA_API
void lua_upvaluejoin (lua_State
*L
, int fidx1
, int n1
,
1274 int fidx2
, int n2
) {
1276 UpVal
**up1
= getupvalref(L
, fidx1
, n1
, &f1
);
1277 UpVal
**up2
= getupvalref(L
, fidx2
, n2
, NULL
);
1279 luaC_objbarrier(L
, f1
, *up2
);