2 ** $Id: lapi.c,v 2.171 2013/03/16 21:10:18 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 /* test for pseudo index */
44 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
46 /* test for valid but not pseudo index */
47 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
49 #define api_checkvalidindex(L, o) api_check(L, isvalid(o), "invalid index")
51 #define api_checkstackindex(L, i, o) \
52 api_check(L, isstackindex(i, o), "index not in the stack")
55 static TValue
*index2addr(lua_State
*L
, int idx
) {
58 TValue
*o
= ci
->func
+ idx
;
59 api_check(L
, idx
<= ci
->top
- (ci
->func
+ 1), "unacceptable index");
60 if (o
>= L
->top
) return NONVALIDVALUE
;
62 } else if (!ispseudo(idx
)) { /* negative index */
63 api_check(L
, idx
!= 0 && -idx
<= L
->top
- (ci
->func
+ 1), "invalid index");
65 } else if (idx
== LUA_REGISTRYINDEX
)
66 return &G(L
)->l_registry
;
68 idx
= LUA_REGISTRYINDEX
- idx
;
69 api_check(L
, idx
<= MAXUPVAL
+ 1, "upvalue index too large");
70 if (ttislcf(ci
->func
)) /* light C function? */
71 return NONVALIDVALUE
; /* it has no upvalues */
73 CClosure
*func
= clCvalue(ci
->func
);
74 return (idx
<= func
->nupvalues
) ? &func
->upvalue
[idx
- 1] : NONVALIDVALUE
;
81 ** to be called by 'lua_checkstack' in protected mode, to grow stack
82 ** capturing memory errors
84 static void growstack(lua_State
*L
, void *ud
) {
85 int size
= *(int *)ud
;
86 luaD_growstack(L
, size
);
90 LUA_API
int lua_checkstack(lua_State
*L
, int size
) {
94 if (L
->stack_last
- L
->top
> size
) /* stack large enough? */
95 res
= 1; /* yes; check is OK */
96 else { /* no; need to grow stack */
97 int inuse
= cast_int(L
->top
- L
->stack
) + EXTRA_STACK
;
98 if (inuse
> LUAI_MAXSTACK
- size
) /* can grow without overflow? */
100 else /* try to grow stack */
101 res
= (luaD_rawrunprotected(L
, &growstack
, &size
) == LUA_OK
);
103 if (res
&& ci
->top
< L
->top
+ size
)
104 ci
->top
= L
->top
+ size
; /* adjust frame top */
110 LUA_API
void lua_xmove(lua_State
*from
, lua_State
*to
, int n
) {
112 if (from
== to
) return;
114 api_checknelems(from
, n
);
115 api_check(from
, G(from
) == G(to
), "moving among independent states");
116 api_check(from
, to
->ci
->top
- to
->top
>= n
, "not enough elements to move");
118 for (i
= 0; i
< n
; i
++) {
119 setobj2s(to
, to
->top
++, from
->top
+ i
);
125 LUA_API lua_CFunction
lua_atpanic(lua_State
*L
, lua_CFunction panicf
) {
129 G(L
)->panic
= panicf
;
135 LUA_API
const lua_Number
*lua_version(lua_State
*L
) {
136 static const lua_Number version
= LUA_VERSION_NUM
;
137 if (L
== NULL
) return &version
;
138 else return G(L
)->version
;
144 ** basic stack manipulation
149 ** convert an acceptable stack index into an absolute index
151 LUA_API
int lua_absindex(lua_State
*L
, int idx
) {
152 return (idx
> 0 || ispseudo(idx
))
154 : cast_int(L
->top
- L
->ci
->func
+ idx
);
158 LUA_API
int lua_gettop(lua_State
*L
) {
159 return cast_int(L
->top
- (L
->ci
->func
+ 1));
163 LUA_API
void lua_settop(lua_State
*L
, int idx
) {
164 StkId func
= L
->ci
->func
;
167 api_check(L
, idx
<= L
->stack_last
- (func
+ 1), "new top too large");
168 while (L
->top
< (func
+ 1) + idx
)
169 setnilvalue(L
->top
++);
170 L
->top
= (func
+ 1) + idx
;
172 api_check(L
, -(idx
+ 1) <= (L
->top
- (func
+ 1)), "invalid new top");
173 L
->top
+= idx
+ 1; /* `subtract' index (index is negative) */
179 LUA_API
void lua_remove(lua_State
*L
, int idx
) {
182 p
= index2addr(L
, idx
);
183 api_checkstackindex(L
, idx
, p
);
184 while (++p
< L
->top
) setobjs2s(L
, p
- 1, p
);
190 LUA_API
void lua_insert(lua_State
*L
, int idx
) {
194 p
= index2addr(L
, idx
);
195 api_checkstackindex(L
, idx
, p
);
196 for (q
= L
->top
; q
> p
; q
--) /* use L->top as a temporary */
197 setobjs2s(L
, q
, q
- 1);
198 setobjs2s(L
, p
, L
->top
);
203 static void moveto(lua_State
*L
, TValue
*fr
, int idx
) {
204 TValue
*to
= index2addr(L
, idx
);
205 api_checkvalidindex(L
, to
);
207 if (idx
< LUA_REGISTRYINDEX
) /* function upvalue? */
208 luaC_barrier(L
, clCvalue(L
->ci
->func
), fr
);
209 /* LUA_REGISTRYINDEX does not need gc barrier
210 (collector revisits it before finishing collection) */
214 LUA_API
void lua_replace(lua_State
*L
, int idx
) {
216 api_checknelems(L
, 1);
217 moveto(L
, L
->top
- 1, idx
);
223 LUA_API
void lua_copy(lua_State
*L
, int fromidx
, int toidx
) {
226 fr
= index2addr(L
, fromidx
);
227 moveto(L
, fr
, toidx
);
232 LUA_API
void lua_pushvalue(lua_State
*L
, int idx
) {
234 setobj2s(L
, L
->top
, index2addr(L
, idx
));
242 ** access functions (stack -> C)
246 LUA_API
int lua_type(lua_State
*L
, int idx
) {
247 StkId o
= index2addr(L
, idx
);
248 return (isvalid(o
) ? ttypenv(o
) : LUA_TNONE
);
252 LUA_API
const char *lua_typename(lua_State
*L
, int t
) {
258 LUA_API
int lua_iscfunction(lua_State
*L
, int idx
) {
259 StkId o
= index2addr(L
, idx
);
260 return (ttislcf(o
) || (ttisCclosure(o
)));
264 LUA_API
int lua_isnumber(lua_State
*L
, int idx
) {
266 const TValue
*o
= index2addr(L
, idx
);
267 return tonumber(o
, &n
);
271 LUA_API
int lua_isstring(lua_State
*L
, int idx
) {
272 int t
= lua_type(L
, idx
);
273 return (t
== LUA_TSTRING
|| t
== LUA_TNUMBER
);
277 LUA_API
int lua_isuserdata(lua_State
*L
, int idx
) {
278 const TValue
*o
= index2addr(L
, idx
);
279 return (ttisuserdata(o
) || ttislightuserdata(o
));
283 LUA_API
int lua_rawequal(lua_State
*L
, int index1
, int index2
) {
284 StkId o1
= index2addr(L
, index1
);
285 StkId o2
= index2addr(L
, index2
);
286 return (isvalid(o1
) && isvalid(o2
)) ? luaV_rawequalobj(o1
, o2
) : 0;
290 LUA_API
void lua_arith(lua_State
*L
, int op
) {
291 StkId o1
; /* 1st operand */
292 StkId o2
; /* 2nd operand */
294 if (op
!= LUA_OPUNM
) /* all other operations expect two operands */
295 api_checknelems(L
, 2);
296 else { /* for unary minus, add fake 2nd operand */
297 api_checknelems(L
, 1);
298 setobjs2s(L
, L
->top
, L
->top
- 1);
303 if (ttisnumber(o1
) && ttisnumber(o2
)) {
304 setnvalue(o1
, luaO_arith(op
, nvalue(o1
), nvalue(o2
)));
306 luaV_arith(L
, o1
, o1
, o2
, cast(TMS
, op
- LUA_OPADD
+ TM_ADD
));
312 LUA_API
int lua_compare(lua_State
*L
, int index1
, int index2
, int op
) {
315 lua_lock(L
); /* may call tag method */
316 o1
= index2addr(L
, index1
);
317 o2
= index2addr(L
, index2
);
318 if (isvalid(o1
) && isvalid(o2
)) {
321 i
= equalobj(L
, o1
, o2
);
324 i
= luaV_lessthan(L
, o1
, o2
);
327 i
= luaV_lessequal(L
, o1
, o2
);
330 api_check(L
, 0, "invalid option");
338 LUA_API lua_Number
lua_tonumberx(lua_State
*L
, int idx
, int *isnum
) {
340 const TValue
*o
= index2addr(L
, idx
);
341 if (tonumber(o
, &n
)) {
342 if (isnum
) *isnum
= 1;
345 if (isnum
) *isnum
= 0;
351 LUA_API lua_Integer
lua_tointegerx(lua_State
*L
, int idx
, int *isnum
) {
353 const TValue
*o
= index2addr(L
, idx
);
354 if (tonumber(o
, &n
)) {
356 lua_Number num
= nvalue(o
);
357 lua_number2integer(res
, num
);
358 if (isnum
) *isnum
= 1;
361 if (isnum
) *isnum
= 0;
367 LUA_API lua_Unsigned
lua_tounsignedx(lua_State
*L
, int idx
, int *isnum
) {
369 const TValue
*o
= index2addr(L
, idx
);
370 if (tonumber(o
, &n
)) {
372 lua_Number num
= nvalue(o
);
373 lua_number2unsigned(res
, num
);
374 if (isnum
) *isnum
= 1;
377 if (isnum
) *isnum
= 0;
383 LUA_API
int lua_toboolean(lua_State
*L
, int idx
) {
384 const TValue
*o
= index2addr(L
, idx
);
385 return !l_isfalse(o
);
389 LUA_API
const char *lua_tolstring(lua_State
*L
, int idx
, size_t *len
) {
390 StkId o
= index2addr(L
, idx
);
391 if (!ttisstring(o
)) {
392 lua_lock(L
); /* `luaV_tostring' may create a new string */
393 if (!luaV_tostring(L
, o
)) { /* conversion failed? */
394 if (len
!= NULL
) *len
= 0;
399 o
= index2addr(L
, idx
); /* previous call may reallocate the stack */
402 if (len
!= NULL
) *len
= tsvalue(o
)->len
;
407 LUA_API
size_t lua_rawlen(lua_State
*L
, int idx
) {
408 StkId o
= index2addr(L
, idx
);
409 switch (ttypenv(o
)) {
411 return tsvalue(o
)->len
;
413 return uvalue(o
)->len
;
415 return luaH_getn(hvalue(o
));
422 LUA_API lua_CFunction
lua_tocfunction(lua_State
*L
, int idx
) {
423 StkId o
= index2addr(L
, idx
);
424 if (ttislcf(o
)) return fvalue(o
);
425 else if (ttisCclosure(o
))
426 return clCvalue(o
)->f
;
427 else return NULL
; /* not a C function */
431 LUA_API
void *lua_touserdata(lua_State
*L
, int idx
) {
432 StkId o
= index2addr(L
, idx
);
433 switch (ttypenv(o
)) {
435 return (rawuvalue(o
) + 1);
436 case LUA_TLIGHTUSERDATA
:
444 LUA_API lua_State
*lua_tothread(lua_State
*L
, int idx
) {
445 StkId o
= index2addr(L
, idx
);
446 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
450 LUA_API
const void *lua_topointer(lua_State
*L
, int idx
) {
451 StkId o
= index2addr(L
, idx
);
460 return cast(void *, cast(size_t, fvalue(o
)));
464 case LUA_TLIGHTUSERDATA
:
465 return lua_touserdata(L
, idx
);
474 ** push functions (C -> stack)
478 LUA_API
void lua_pushnil(lua_State
*L
) {
486 LUA_API
void lua_pushnumber(lua_State
*L
, lua_Number n
) {
488 setnvalue(L
->top
, n
);
489 luai_checknum(L
, L
->top
,
490 luaG_runerror(L
, "C API - attempt to push a signaling NaN"));
496 LUA_API
void lua_pushinteger(lua_State
*L
, lua_Integer n
) {
498 setnvalue(L
->top
, cast_num(n
));
504 LUA_API
void lua_pushunsigned(lua_State
*L
, lua_Unsigned u
) {
507 n
= lua_unsigned2number(u
);
508 setnvalue(L
->top
, n
);
514 LUA_API
const char *lua_pushlstring(lua_State
*L
, const char *s
, size_t len
) {
518 ts
= luaS_newlstr(L
, s
, len
);
519 setsvalue2s(L
, L
->top
, ts
);
526 LUA_API
const char *lua_pushstring(lua_State
*L
, const char *s
) {
535 setsvalue2s(L
, L
->top
, ts
);
543 LUA_API
const char *lua_pushvfstring(lua_State
*L
, const char *fmt
,
548 ret
= luaO_pushvfstring(L
, fmt
, argp
);
554 LUA_API
const char *lua_pushfstring(lua_State
*L
, const char *fmt
, ...) {
560 ret
= luaO_pushvfstring(L
, fmt
, argp
);
567 LUA_API
void lua_pushcclosure(lua_State
*L
, lua_CFunction fn
, int n
) {
570 setfvalue(L
->top
, fn
);
573 api_checknelems(L
, n
);
574 api_check(L
, n
<= MAXUPVAL
, "upvalue index too large");
576 cl
= luaF_newCclosure(L
, n
);
580 setobj2n(L
, &cl
->c
.upvalue
[n
], L
->top
+ n
);
581 setclCvalue(L
, L
->top
, cl
);
588 LUA_API
void lua_pushboolean(lua_State
*L
, int b
) {
590 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
596 LUA_API
void lua_pushlightuserdata(lua_State
*L
, void *p
) {
598 setpvalue(L
->top
, p
);
604 LUA_API
int lua_pushthread(lua_State
*L
) {
606 setthvalue(L
, L
->top
, L
);
609 return (G(L
)->mainthread
== L
);
615 ** get functions (Lua -> stack)
619 LUA_API
void lua_getglobal(lua_State
*L
, const char *var
) {
620 Table
*reg
= hvalue(&G(L
)->l_registry
);
621 const TValue
*gt
; /* global table */
623 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
624 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
625 luaV_gettable(L
, gt
, L
->top
- 1, L
->top
- 1);
630 LUA_API
void lua_gettable(lua_State
*L
, int idx
) {
633 t
= index2addr(L
, idx
);
634 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
639 LUA_API
void lua_getfield(lua_State
*L
, int idx
, const char *k
) {
642 t
= index2addr(L
, idx
);
643 setsvalue2s(L
, L
->top
, luaS_new(L
, k
));
645 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
650 LUA_API
void lua_rawget(lua_State
*L
, int idx
) {
653 t
= index2addr(L
, idx
);
654 api_check(L
, ttistable(t
), "table expected");
655 setobj2s(L
, L
->top
- 1, luaH_get(hvalue(t
), L
->top
- 1));
660 LUA_API
void lua_rawgeti(lua_State
*L
, int idx
, int n
) {
663 t
= index2addr(L
, idx
);
664 api_check(L
, ttistable(t
), "table expected");
665 setobj2s(L
, L
->top
, luaH_getint(hvalue(t
), n
));
671 LUA_API
void lua_rawgetp(lua_State
*L
, int idx
, const void *p
) {
675 t
= index2addr(L
, idx
);
676 api_check(L
, ttistable(t
), "table expected");
677 setpvalue(&k
, cast(void *, p
));
678 setobj2s(L
, L
->top
, luaH_get(hvalue(t
), &k
));
684 LUA_API
void lua_createtable(lua_State
*L
, int narray
, int nrec
) {
689 sethvalue(L
, L
->top
, t
);
691 if (narray
> 0 || nrec
> 0)
692 luaH_resize(L
, t
, narray
, nrec
);
697 LUA_API
int lua_getmetatable(lua_State
*L
, int objindex
) {
702 obj
= index2addr(L
, objindex
);
703 switch (ttypenv(obj
)) {
705 mt
= hvalue(obj
)->metatable
;
708 mt
= uvalue(obj
)->metatable
;
711 mt
= G(L
)->mt
[ttypenv(obj
)];
717 sethvalue(L
, L
->top
, mt
);
726 LUA_API
void lua_getuservalue(lua_State
*L
, int idx
) {
729 o
= index2addr(L
, idx
);
730 api_check(L
, ttisuserdata(o
), "userdata expected");
731 if (uvalue(o
)->env
) {
732 sethvalue(L
, L
->top
, uvalue(o
)->env
);
741 ** set functions (stack -> Lua)
745 LUA_API
void lua_setglobal(lua_State
*L
, const char *var
) {
746 Table
*reg
= hvalue(&G(L
)->l_registry
);
747 const TValue
*gt
; /* global table */
749 api_checknelems(L
, 1);
750 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
751 setsvalue2s(L
, L
->top
++, luaS_new(L
, var
));
752 luaV_settable(L
, gt
, L
->top
- 1, L
->top
- 2);
753 L
->top
-= 2; /* pop value and key */
758 LUA_API
void lua_settable(lua_State
*L
, int idx
) {
761 api_checknelems(L
, 2);
762 t
= index2addr(L
, idx
);
763 luaV_settable(L
, t
, L
->top
- 2, L
->top
- 1);
764 L
->top
-= 2; /* pop index and value */
769 LUA_API
void lua_setfield(lua_State
*L
, int idx
, const char *k
) {
772 api_checknelems(L
, 1);
773 t
= index2addr(L
, idx
);
774 setsvalue2s(L
, L
->top
++, luaS_new(L
, k
));
775 luaV_settable(L
, t
, L
->top
- 1, L
->top
- 2);
776 L
->top
-= 2; /* pop value and key */
781 LUA_API
void lua_rawset(lua_State
*L
, int idx
) {
784 api_checknelems(L
, 2);
785 t
= index2addr(L
, idx
);
786 api_check(L
, ttistable(t
), "table expected");
787 setobj2t(L
, luaH_set(L
, hvalue(t
), L
->top
- 2), L
->top
- 1);
788 invalidateTMcache(hvalue(t
));
789 luaC_barrierback(L
, gcvalue(t
), L
->top
- 1);
795 LUA_API
void lua_rawseti(lua_State
*L
, int idx
, int n
) {
798 api_checknelems(L
, 1);
799 t
= index2addr(L
, idx
);
800 api_check(L
, ttistable(t
), "table expected");
801 luaH_setint(L
, hvalue(t
), n
, L
->top
- 1);
802 luaC_barrierback(L
, gcvalue(t
), L
->top
- 1);
808 LUA_API
void lua_rawsetp(lua_State
*L
, int idx
, const void *p
) {
812 api_checknelems(L
, 1);
813 t
= index2addr(L
, idx
);
814 api_check(L
, ttistable(t
), "table expected");
815 setpvalue(&k
, cast(void *, p
));
816 setobj2t(L
, luaH_set(L
, hvalue(t
), &k
), L
->top
- 1);
817 luaC_barrierback(L
, gcvalue(t
), L
->top
- 1);
823 LUA_API
int lua_setmetatable(lua_State
*L
, int objindex
) {
827 api_checknelems(L
, 1);
828 obj
= index2addr(L
, objindex
);
829 if (ttisnil(L
->top
- 1))
832 api_check(L
, ttistable(L
->top
- 1), "table expected");
833 mt
= hvalue(L
->top
- 1);
835 switch (ttypenv(obj
)) {
837 hvalue(obj
)->metatable
= mt
;
839 luaC_objbarrierback(L
, gcvalue(obj
), mt
);
840 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
844 case LUA_TUSERDATA
: {
845 uvalue(obj
)->metatable
= mt
;
847 luaC_objbarrier(L
, rawuvalue(obj
), mt
);
848 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
853 G(L
)->mt
[ttypenv(obj
)] = mt
;
863 LUA_API
void lua_setuservalue(lua_State
*L
, int idx
) {
866 api_checknelems(L
, 1);
867 o
= index2addr(L
, idx
);
868 api_check(L
, ttisuserdata(o
), "userdata expected");
869 if (ttisnil(L
->top
- 1))
870 uvalue(o
)->env
= NULL
;
872 api_check(L
, ttistable(L
->top
- 1), "table expected");
873 uvalue(o
)->env
= hvalue(L
->top
- 1);
874 luaC_objbarrier(L
, gcvalue(o
), hvalue(L
->top
- 1));
882 ** `load' and `call' functions (run Lua code)
886 #define checkresults(L,na,nr) \
887 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
888 "results from function overflow current stack size")
891 LUA_API
int lua_getctx(lua_State
*L
, int *ctx
) {
892 if (L
->ci
->callstatus
& CIST_YIELDED
) {
893 if (ctx
) *ctx
= L
->ci
->u
.c
.ctx
;
894 return L
->ci
->u
.c
.status
;
895 } else return LUA_OK
;
899 LUA_API
void lua_callk(lua_State
*L
, int nargs
, int nresults
, int ctx
,
903 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
904 "cannot use continuations inside hooks");
905 api_checknelems(L
, nargs
+ 1);
906 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
907 checkresults(L
, nargs
, nresults
);
908 func
= L
->top
- (nargs
+ 1);
909 if (k
!= NULL
&& L
->nny
== 0) { /* need to prepare continuation? */
910 L
->ci
->u
.c
.k
= k
; /* save continuation */
911 L
->ci
->u
.c
.ctx
= ctx
; /* save context */
912 luaD_call(L
, func
, nresults
, 1); /* do the call */
913 } else /* no continuation or no yieldable */
914 luaD_call(L
, func
, nresults
, 0); /* just do the call */
915 adjustresults(L
, nresults
);
922 ** Execute a protected call.
924 struct CallS
{ /* data to `f_call' */
930 static void f_call(lua_State
*L
, void *ud
) {
931 struct CallS
*c
= cast(struct CallS
*, ud
);
932 luaD_call(L
, c
->func
, c
->nresults
, 0);
937 LUA_API
int lua_pcallk(lua_State
*L
, int nargs
, int nresults
, int errfunc
,
938 int ctx
, lua_CFunction k
) {
943 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
944 "cannot use continuations inside hooks");
945 api_checknelems(L
, nargs
+ 1);
946 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
947 checkresults(L
, nargs
, nresults
);
951 StkId o
= index2addr(L
, errfunc
);
952 api_checkstackindex(L
, errfunc
, o
);
953 func
= savestack(L
, o
);
955 c
.func
= L
->top
- (nargs
+ 1); /* function to be called */
956 if (k
== NULL
|| L
->nny
> 0) { /* no continuation or no yieldable? */
957 c
.nresults
= nresults
; /* do a 'conventional' protected call */
958 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
959 } else { /* prepare continuation (call is already protected by 'resume') */
960 CallInfo
*ci
= L
->ci
;
961 ci
->u
.c
.k
= k
; /* save continuation */
962 ci
->u
.c
.ctx
= ctx
; /* save context */
963 /* save information for error recovery */
964 ci
->extra
= savestack(L
, c
.func
);
965 ci
->u
.c
.old_allowhook
= L
->allowhook
;
966 ci
->u
.c
.old_errfunc
= L
->errfunc
;
968 /* mark that function may do error recovery */
969 ci
->callstatus
|= CIST_YPCALL
;
970 luaD_call(L
, c
.func
, nresults
, 1); /* do the call */
971 ci
->callstatus
&= ~CIST_YPCALL
;
972 L
->errfunc
= ci
->u
.c
.old_errfunc
;
973 status
= LUA_OK
; /* if it is here, there were no errors */
975 adjustresults(L
, nresults
);
981 LUA_API
int lua_load(lua_State
*L
, lua_Reader reader
, void *data
,
982 const char *chunkname
, const char *mode
) {
986 if (!chunkname
) chunkname
= "?";
987 luaZ_init(L
, &z
, reader
, data
);
988 status
= luaD_protectedparser(L
, &z
, chunkname
, mode
);
989 if (status
== LUA_OK
) { /* no errors? */
990 LClosure
*f
= clLvalue(L
->top
- 1); /* get newly created function */
991 if (f
->nupvalues
== 1) { /* does it have one upvalue? */
992 /* get global table from registry */
993 Table
*reg
= hvalue(&G(L
)->l_registry
);
994 const TValue
*gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
995 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
996 setobj(L
, f
->upvals
[0]->v
, gt
);
997 luaC_barrier(L
, f
->upvals
[0], gt
);
1005 LUA_API
int lua_dump(lua_State
*L
, lua_Writer writer
, void *data
) {
1009 api_checknelems(L
, 1);
1012 status
= luaU_dump(L
, getproto(o
), writer
, data
, 0);
1020 LUA_API
int lua_status(lua_State
*L
) {
1026 ** Garbage-collection function
1029 LUA_API
int lua_gc(lua_State
*L
, int what
, int data
) {
1039 case LUA_GCRESTART
: {
1044 case LUA_GCCOLLECT
: {
1049 /* GC values are expressed in Kbytes: #bytes/2^10 */
1050 res
= cast_int(gettotalbytes(g
) >> 10);
1053 case LUA_GCCOUNTB
: {
1054 res
= cast_int(gettotalbytes(g
) & 0x3ff);
1058 if (g
->gckind
== KGC_GEN
) { /* generational mode? */
1059 res
= (g
->GCestimate
== 0); /* true if it will do major collection */
1060 luaC_forcestep(L
); /* do a single step */
1062 lu_mem debt
= cast(lu_mem
, data
) * 1024 - GCSTEPSIZE
;
1064 debt
+= g
->GCdebt
; /* include current debt */
1065 luaE_setdebt(g
, debt
);
1067 if (g
->gcstate
== GCSpause
) /* end of cycle? */
1068 res
= 1; /* signal it */
1072 case LUA_GCSETPAUSE
: {
1077 case LUA_GCSETMAJORINC
: {
1078 res
= g
->gcmajorinc
;
1079 g
->gcmajorinc
= data
;
1082 case LUA_GCSETSTEPMUL
: {
1084 g
->gcstepmul
= data
;
1087 case LUA_GCISRUNNING
: {
1091 case LUA_GCGEN
: { /* change collector to generational mode */
1092 luaC_changemode(L
, KGC_GEN
);
1095 case LUA_GCINC
: { /* change collector to incremental mode */
1096 luaC_changemode(L
, KGC_NORMAL
);
1100 res
= -1; /* invalid option */
1109 ** miscellaneous functions
1113 LUA_API
int lua_error(lua_State
*L
) {
1115 api_checknelems(L
, 1);
1117 /* code unreachable; will unlock when control actually leaves the kernel */
1118 return 0; /* to avoid warnings */
1122 LUA_API
int lua_next(lua_State
*L
, int idx
) {
1126 t
= index2addr(L
, idx
);
1127 api_check(L
, ttistable(t
), "table expected");
1128 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
1131 } else /* no more elements */
1132 L
->top
-= 1; /* remove key */
1138 LUA_API
void lua_concat(lua_State
*L
, int n
) {
1140 api_checknelems(L
, n
);
1144 } else if (n
== 0) { /* push empty string */
1145 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1148 /* else n == 1; nothing to do */
1153 LUA_API
void lua_len(lua_State
*L
, int idx
) {
1156 t
= index2addr(L
, idx
);
1157 luaV_objlen(L
, L
->top
, t
);
1163 LUA_API lua_Alloc
lua_getallocf(lua_State
*L
, void **ud
) {
1166 if (ud
) *ud
= G(L
)->ud
;
1173 LUA_API
void lua_setallocf(lua_State
*L
, lua_Alloc f
, void *ud
) {
1181 LUA_API
void *lua_newuserdata(lua_State
*L
, size_t size
) {
1185 u
= luaS_newudata(L
, size
, NULL
);
1186 setuvalue(L
, L
->top
, u
);
1194 static const char *aux_upvalue(StkId fi
, int n
, TValue
**val
,
1196 switch (ttype(fi
)) {
1197 case LUA_TCCL
: { /* C closure */
1198 CClosure
*f
= clCvalue(fi
);
1199 if (!(1 <= n
&& n
<= f
->nupvalues
)) return NULL
;
1200 *val
= &f
->upvalue
[n
- 1];
1201 if (owner
) *owner
= obj2gco(f
);
1204 case LUA_TLCL
: { /* Lua closure */
1205 LClosure
*f
= clLvalue(fi
);
1208 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1209 *val
= f
->upvals
[n
- 1]->v
;
1210 if (owner
) *owner
= obj2gco(f
->upvals
[n
- 1]);
1211 name
= p
->upvalues
[n
- 1].name
;
1212 return (name
== NULL
) ? "" : getstr(name
);
1215 return NULL
; /* not a closure */
1220 LUA_API
const char *lua_getupvalue(lua_State
*L
, int funcindex
, int n
) {
1222 TValue
*val
= NULL
; /* to avoid warnings */
1224 name
= aux_upvalue(index2addr(L
, funcindex
), n
, &val
, NULL
);
1226 setobj2s(L
, L
->top
, val
);
1234 LUA_API
const char *lua_setupvalue(lua_State
*L
, int funcindex
, int n
) {
1236 TValue
*val
= NULL
; /* to avoid warnings */
1237 GCObject
*owner
= NULL
; /* to avoid warnings */
1240 fi
= index2addr(L
, funcindex
);
1241 api_checknelems(L
, 1);
1242 name
= aux_upvalue(fi
, n
, &val
, &owner
);
1245 setobj(L
, val
, L
->top
);
1246 luaC_barrier(L
, owner
, L
->top
);
1253 static UpVal
**getupvalref(lua_State
*L
, int fidx
, int n
, LClosure
**pf
) {
1255 StkId fi
= index2addr(L
, fidx
);
1256 api_check(L
, ttisLclosure(fi
), "Lua function expected");
1258 api_check(L
, (1 <= n
&& n
<= f
->p
->sizeupvalues
), "invalid upvalue index");
1260 return &f
->upvals
[n
- 1]; /* get its upvalue pointer */
1264 LUA_API
void *lua_upvalueid(lua_State
*L
, int fidx
, int n
) {
1265 StkId fi
= index2addr(L
, fidx
);
1266 switch (ttype(fi
)) {
1267 case LUA_TLCL
: { /* lua closure */
1268 return *getupvalref(L
, fidx
, n
, NULL
);
1270 case LUA_TCCL
: { /* C closure */
1271 CClosure
*f
= clCvalue(fi
);
1272 api_check(L
, 1 <= n
&& n
<= f
->nupvalues
, "invalid upvalue index");
1273 return &f
->upvalue
[n
- 1];
1276 api_check(L
, 0, "closure expected");
1283 LUA_API
void lua_upvaluejoin(lua_State
*L
, int fidx1
, int n1
,
1284 int fidx2
, int n2
) {
1286 UpVal
**up1
= getupvalref(L
, fidx1
, n1
, &f1
);
1287 UpVal
**up2
= getupvalref(L
, fidx2
, n2
, NULL
);
1289 luaC_objbarrier(L
, f1
, *up2
);