1 /* $NetBSD: lapi.c,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */
4 ** Id: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp
6 ** See Copyright Notice in lua.h
38 const char lua_ident
[] =
39 "$LuaVersion: " LUA_COPYRIGHT
" $"
40 "$LuaAuthors: " LUA_AUTHORS
" $";
43 /* value at a non-valid index */
44 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
46 /* corresponding test */
47 #define isvalid(o) ((o) != luaO_nilobject)
49 /* test for pseudo index */
50 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
52 /* test for upvalue */
53 #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
55 /* test for valid but not pseudo index */
56 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
58 #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
60 #define api_checkstackindex(l, i, o) \
61 api_check(l, isstackindex(i, o), "index not in the stack")
64 static TValue
*index2addr (lua_State
*L
, int idx
) {
67 TValue
*o
= ci
->func
+ idx
;
68 api_check(L
, idx
<= ci
->top
- (ci
->func
+ 1), "unacceptable index");
69 if (o
>= L
->top
) return NONVALIDVALUE
;
72 else if (!ispseudo(idx
)) { /* negative index */
73 api_check(L
, idx
!= 0 && -idx
<= L
->top
- (ci
->func
+ 1), "invalid index");
76 else if (idx
== LUA_REGISTRYINDEX
)
77 return &G(L
)->l_registry
;
79 idx
= LUA_REGISTRYINDEX
- idx
;
80 api_check(L
, idx
<= MAXUPVAL
+ 1, "upvalue index too large");
81 if (ttislcf(ci
->func
)) /* light C function? */
82 return NONVALIDVALUE
; /* it has no upvalues */
84 CClosure
*func
= clCvalue(ci
->func
);
85 return (idx
<= func
->nupvalues
) ? &func
->upvalue
[idx
-1] : NONVALIDVALUE
;
92 ** to be called by 'lua_checkstack' in protected mode, to grow stack
93 ** capturing memory errors
95 static void growstack (lua_State
*L
, void *ud
) {
96 int size
= *(int *)ud
;
97 luaD_growstack(L
, size
);
101 LUA_API
int lua_checkstack (lua_State
*L
, int n
) {
103 CallInfo
*ci
= L
->ci
;
105 api_check(L
, n
>= 0, "negative 'n'");
106 if (L
->stack_last
- L
->top
> n
) /* stack large enough? */
107 res
= 1; /* yes; check is OK */
108 else { /* no; need to grow stack */
109 int inuse
= cast_int(L
->top
- L
->stack
) + EXTRA_STACK
;
110 if (inuse
> LUAI_MAXSTACK
- n
) /* can grow without overflow? */
112 else /* try to grow stack */
113 res
= (luaD_rawrunprotected(L
, &growstack
, &n
) == LUA_OK
);
115 if (res
&& ci
->top
< L
->top
+ n
)
116 ci
->top
= L
->top
+ n
; /* adjust frame top */
122 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
124 if (from
== to
) return;
126 api_checknelems(from
, n
);
127 api_check(from
, G(from
) == G(to
), "moving among independent states");
128 api_check(from
, to
->ci
->top
- to
->top
>= n
, "not enough elements to move");
130 for (i
= 0; i
< n
; i
++) {
131 setobj2s(to
, to
->top
, from
->top
+ i
);
138 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
142 G(L
)->panic
= panicf
;
148 LUA_API
const lua_Number
*lua_version (lua_State
*L
) {
149 static const lua_Number version
= LUA_VERSION_NUM
;
150 if (L
== NULL
) return &version
;
151 else return G(L
)->version
;
157 ** basic stack manipulation
162 ** convert an acceptable stack index into an absolute index
164 LUA_API
int lua_absindex (lua_State
*L
, int idx
) {
165 return (idx
> 0 || ispseudo(idx
))
167 : cast_int(L
->top
- L
->ci
->func
) + idx
;
171 LUA_API
int lua_gettop (lua_State
*L
) {
172 return cast_int(L
->top
- (L
->ci
->func
+ 1));
176 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
177 StkId func
= L
->ci
->func
;
180 api_check(L
, idx
<= L
->stack_last
- (func
+ 1), "new top too large");
181 while (L
->top
< (func
+ 1) + idx
)
182 setnilvalue(L
->top
++);
183 L
->top
= (func
+ 1) + idx
;
186 api_check(L
, -(idx
+1) <= (L
->top
- (func
+ 1)), "invalid new top");
187 L
->top
+= idx
+1; /* 'subtract' index (index is negative) */
194 ** Reverse the stack segment from 'from' to 'to'
195 ** (auxiliary to 'lua_rotate')
197 static void reverse (lua_State
*L
, StkId from
, StkId to
) {
198 for (; from
< to
; from
++, to
--) {
200 setobj(L
, &temp
, from
);
201 setobjs2s(L
, from
, to
);
202 setobj2s(L
, to
, &temp
);
208 ** Let x = AB, where A is a prefix of length 'n'. Then,
209 ** rotate x n == BA. But BA == (A^r . B^r)^r.
211 LUA_API
void lua_rotate (lua_State
*L
, int idx
, int n
) {
214 t
= L
->top
- 1; /* end of stack segment being rotated */
215 p
= index2addr(L
, idx
); /* start of segment */
216 api_checkstackindex(L
, idx
, p
);
217 api_check(L
, (n
>= 0 ? n
: -n
) <= (t
- p
+ 1), "invalid 'n'");
218 m
= (n
>= 0 ? t
- n
: p
- n
- 1); /* end of prefix */
219 reverse(L
, p
, m
); /* reverse the prefix with length 'n' */
220 reverse(L
, m
+ 1, t
); /* reverse the suffix */
221 reverse(L
, p
, t
); /* reverse the entire segment */
226 LUA_API
void lua_copy (lua_State
*L
, int fromidx
, int toidx
) {
229 fr
= index2addr(L
, fromidx
);
230 to
= index2addr(L
, toidx
);
231 api_checkvalidindex(L
, to
);
233 if (isupvalue(toidx
)) /* function upvalue? */
234 luaC_barrier(L
, clCvalue(L
->ci
->func
), fr
);
235 /* LUA_REGISTRYINDEX does not need gc barrier
236 (collector revisits it before finishing collection) */
241 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
243 setobj2s(L
, L
->top
, index2addr(L
, idx
));
251 ** access functions (stack -> C)
255 LUA_API
int lua_type (lua_State
*L
, int idx
) {
256 StkId o
= index2addr(L
, idx
);
257 return (isvalid(o
) ? ttnov(o
) : LUA_TNONE
);
261 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
263 api_check(L
, LUA_TNONE
<= t
&& t
< LUA_NUMTAGS
, "invalid tag");
268 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
269 StkId o
= index2addr(L
, idx
);
270 return (ttislcf(o
) || (ttisCclosure(o
)));
274 LUA_API
int lua_isinteger (lua_State
*L
, int idx
) {
275 StkId o
= index2addr(L
, idx
);
276 return ttisinteger(o
);
280 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
282 const TValue
*o
= index2addr(L
, idx
);
283 return tonumber(o
, &n
);
287 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
288 const TValue
*o
= index2addr(L
, idx
);
289 return (ttisstring(o
) || cvt2str(o
));
293 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
294 const TValue
*o
= index2addr(L
, idx
);
295 return (ttisfulluserdata(o
) || ttislightuserdata(o
));
299 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
300 StkId o1
= index2addr(L
, index1
);
301 StkId o2
= index2addr(L
, index2
);
302 return (isvalid(o1
) && isvalid(o2
)) ? luaV_rawequalobj(o1
, o2
) : 0;
306 LUA_API
void lua_arith (lua_State
*L
, int op
) {
308 if (op
!= LUA_OPUNM
&& op
!= LUA_OPBNOT
)
309 api_checknelems(L
, 2); /* all other operations expect two operands */
310 else { /* for unary operations, add fake 2nd operand */
311 api_checknelems(L
, 1);
312 setobjs2s(L
, L
->top
, L
->top
- 1);
315 /* first operand at top - 2, second at top - 1; result go to top - 2 */
316 luaO_arith(L
, op
, L
->top
- 2, L
->top
- 1, L
->top
- 2);
317 L
->top
--; /* remove second operand */
322 LUA_API
int lua_compare (lua_State
*L
, int index1
, int index2
, int op
) {
325 lua_lock(L
); /* may call tag method */
326 o1
= index2addr(L
, index1
);
327 o2
= index2addr(L
, index2
);
328 if (isvalid(o1
) && isvalid(o2
)) {
330 case LUA_OPEQ
: i
= luaV_equalobj(L
, o1
, o2
); break;
331 case LUA_OPLT
: i
= luaV_lessthan(L
, o1
, o2
); break;
332 case LUA_OPLE
: i
= luaV_lessequal(L
, o1
, o2
); break;
333 default: api_check(L
, 0, "invalid option");
341 LUA_API
size_t lua_stringtonumber (lua_State
*L
, const char *s
) {
342 size_t sz
= luaO_str2num(s
, L
->top
);
350 LUA_API lua_Number
lua_tonumberx (lua_State
*L
, int idx
, int *pisnum
) {
352 const TValue
*o
= index2addr(L
, idx
);
353 int isnum
= tonumber(o
, &n
);
355 n
= 0; /* call to 'tonumber' may change 'n' even if it fails */
356 if (pisnum
) *pisnum
= isnum
;
362 LUA_API lua_Integer
lua_tointegerx (lua_State
*L
, int idx
, int *pisnum
) {
364 const TValue
*o
= index2addr(L
, idx
);
365 int isnum
= tointeger(o
, &res
);
367 res
= 0; /* call to 'tointeger' may change 'n' even if it fails */
368 if (pisnum
) *pisnum
= isnum
;
373 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
374 const TValue
*o
= index2addr(L
, idx
);
375 return !l_isfalse(o
);
379 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
380 StkId o
= index2addr(L
, idx
);
381 if (!ttisstring(o
)) {
382 if (!cvt2str(o
)) { /* not convertible? */
383 if (len
!= NULL
) *len
= 0;
386 lua_lock(L
); /* 'luaO_tostring' may create a new string */
388 o
= index2addr(L
, idx
); /* previous call may reallocate the stack */
398 LUA_API
size_t lua_rawlen (lua_State
*L
, int idx
) {
399 StkId o
= index2addr(L
, idx
);
401 case LUA_TSHRSTR
: return tsvalue(o
)->shrlen
;
402 case LUA_TLNGSTR
: return tsvalue(o
)->u
.lnglen
;
403 case LUA_TUSERDATA
: return uvalue(o
)->len
;
404 case LUA_TTABLE
: return luaH_getn(hvalue(o
));
410 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
411 StkId o
= index2addr(L
, idx
);
412 if (ttislcf(o
)) return fvalue(o
);
413 else if (ttisCclosure(o
))
414 return clCvalue(o
)->f
;
415 else return NULL
; /* not a C function */
419 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
420 StkId o
= index2addr(L
, idx
);
422 case LUA_TUSERDATA
: return getudatamem(uvalue(o
));
423 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
424 default: return NULL
;
429 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
430 StkId o
= index2addr(L
, idx
);
431 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
435 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
436 StkId o
= index2addr(L
, idx
);
438 case LUA_TTABLE
: return hvalue(o
);
439 case LUA_TLCL
: return clLvalue(o
);
440 case LUA_TCCL
: return clCvalue(o
);
441 case LUA_TLCF
: return cast(void *, cast(size_t, fvalue(o
)));
442 case LUA_TTHREAD
: return thvalue(o
);
443 case LUA_TUSERDATA
: return getudatamem(uvalue(o
));
444 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
445 default: return NULL
;
452 ** push functions (C -> stack)
456 LUA_API
void lua_pushnil (lua_State
*L
) {
465 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
467 setfltvalue(L
->top
, n
);
474 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
476 setivalue(L
->top
, n
);
482 LUA_API
const char *lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
486 ts
= luaS_newlstr(L
, s
, len
);
487 setsvalue2s(L
, L
->top
, ts
);
494 LUA_API
const char *lua_pushstring (lua_State
*L
, const char *s
) {
502 setsvalue2s(L
, L
->top
, ts
);
503 s
= getstr(ts
); /* internal copy's address */
511 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
516 ret
= luaO_pushvfstring(L
, fmt
, argp
);
522 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
528 ret
= luaO_pushvfstring(L
, fmt
, argp
);
535 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
538 setfvalue(L
->top
, fn
);
542 api_checknelems(L
, n
);
543 api_check(L
, n
<= MAXUPVAL
, "upvalue index too large");
545 cl
= luaF_newCclosure(L
, n
);
549 setobj2n(L
, &cl
->upvalue
[n
], L
->top
+ n
);
550 /* does not need barrier because closure is white */
552 setclCvalue(L
, L
->top
, cl
);
559 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
561 setbvalue(L
->top
, (b
!= 0)); /* ensure that true is 1 */
567 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
569 setpvalue(L
->top
, p
);
575 LUA_API
int lua_pushthread (lua_State
*L
) {
577 setthvalue(L
, L
->top
, L
);
580 return (G(L
)->mainthread
== L
);
586 ** get functions (Lua -> stack)
590 LUA_API
int lua_getglobal (lua_State
*L
, const char *name
) {
591 Table
*reg
= hvalue(&G(L
)->l_registry
);
592 const TValue
*gt
; /* global table */
594 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
595 setsvalue2s(L
, L
->top
, luaS_new(L
, name
));
597 luaV_gettable(L
, gt
, L
->top
- 1, L
->top
- 1);
599 return ttnov(L
->top
- 1);
603 LUA_API
int lua_gettable (lua_State
*L
, int idx
) {
606 t
= index2addr(L
, idx
);
607 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
609 return ttnov(L
->top
- 1);
613 LUA_API
int lua_getfield (lua_State
*L
, int idx
, const char *k
) {
616 t
= index2addr(L
, idx
);
617 setsvalue2s(L
, L
->top
, luaS_new(L
, k
));
619 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
621 return ttnov(L
->top
- 1);
625 LUA_API
int lua_geti (lua_State
*L
, int idx
, lua_Integer n
) {
628 t
= index2addr(L
, idx
);
629 setivalue(L
->top
, n
);
631 luaV_gettable(L
, t
, L
->top
- 1, L
->top
- 1);
633 return ttnov(L
->top
- 1);
637 LUA_API
int 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));
644 return ttnov(L
->top
- 1);
648 LUA_API
int lua_rawgeti (lua_State
*L
, int idx
, lua_Integer n
) {
651 t
= index2addr(L
, idx
);
652 api_check(L
, ttistable(t
), "table expected");
653 setobj2s(L
, L
->top
, luaH_getint(hvalue(t
), n
));
656 return ttnov(L
->top
- 1);
660 LUA_API
int lua_rawgetp (lua_State
*L
, int idx
, const void *p
) {
664 t
= index2addr(L
, idx
);
665 api_check(L
, ttistable(t
), "table expected");
666 setpvalue(&k
, cast(void *, p
));
667 setobj2s(L
, L
->top
, luaH_get(hvalue(t
), &k
));
670 return ttnov(L
->top
- 1);
674 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
679 sethvalue(L
, L
->top
, t
);
681 if (narray
> 0 || nrec
> 0)
682 luaH_resize(L
, t
, narray
, nrec
);
687 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
692 obj
= index2addr(L
, objindex
);
693 switch (ttnov(obj
)) {
695 mt
= hvalue(obj
)->metatable
;
698 mt
= uvalue(obj
)->metatable
;
701 mt
= G(L
)->mt
[ttnov(obj
)];
705 sethvalue(L
, L
->top
, mt
);
714 LUA_API
int lua_getuservalue (lua_State
*L
, int idx
) {
717 o
= index2addr(L
, idx
);
718 api_check(L
, ttisfulluserdata(o
), "full userdata expected");
719 getuservalue(L
, uvalue(o
), L
->top
);
722 return ttnov(L
->top
- 1);
727 ** set functions (stack -> Lua)
731 LUA_API
void lua_setglobal (lua_State
*L
, const char *name
) {
732 Table
*reg
= hvalue(&G(L
)->l_registry
);
733 const TValue
*gt
; /* global table */
735 api_checknelems(L
, 1);
736 gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
737 setsvalue2s(L
, L
->top
, luaS_new(L
, name
));
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
));
763 luaV_settable(L
, t
, L
->top
- 1, L
->top
- 2);
764 L
->top
-= 2; /* pop value and key */
769 LUA_API
void lua_seti (lua_State
*L
, int idx
, lua_Integer n
) {
772 api_checknelems(L
, 1);
773 t
= index2addr(L
, idx
);
774 setivalue(L
->top
, n
);
776 luaV_settable(L
, t
, L
->top
- 1, L
->top
- 2);
777 L
->top
-= 2; /* pop value and key */
782 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
786 api_checknelems(L
, 2);
787 o
= index2addr(L
, idx
);
788 api_check(L
, ttistable(o
), "table expected");
790 setobj2t(L
, luaH_set(L
, t
, L
->top
-2), L
->top
-1);
791 invalidateTMcache(t
);
792 luaC_barrierback(L
, t
, L
->top
-1);
798 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, lua_Integer n
) {
802 api_checknelems(L
, 1);
803 o
= index2addr(L
, idx
);
804 api_check(L
, ttistable(o
), "table expected");
806 luaH_setint(L
, t
, n
, L
->top
- 1);
807 luaC_barrierback(L
, t
, L
->top
-1);
813 LUA_API
void lua_rawsetp (lua_State
*L
, int idx
, const void *p
) {
818 api_checknelems(L
, 1);
819 o
= index2addr(L
, idx
);
820 api_check(L
, ttistable(o
), "table expected");
822 setpvalue(&k
, cast(void *, p
));
823 setobj2t(L
, luaH_set(L
, t
, &k
), L
->top
- 1);
824 luaC_barrierback(L
, t
, L
->top
- 1);
830 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
834 api_checknelems(L
, 1);
835 obj
= index2addr(L
, objindex
);
836 if (ttisnil(L
->top
- 1))
839 api_check(L
, ttistable(L
->top
- 1), "table expected");
840 mt
= hvalue(L
->top
- 1);
842 switch (ttnov(obj
)) {
844 hvalue(obj
)->metatable
= mt
;
846 luaC_objbarrier(L
, gcvalue(obj
), mt
);
847 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
851 case LUA_TUSERDATA
: {
852 uvalue(obj
)->metatable
= mt
;
854 luaC_objbarrier(L
, uvalue(obj
), mt
);
855 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
860 G(L
)->mt
[ttnov(obj
)] = mt
;
870 LUA_API
void lua_setuservalue (lua_State
*L
, int idx
) {
873 api_checknelems(L
, 1);
874 o
= index2addr(L
, idx
);
875 api_check(L
, ttisfulluserdata(o
), "full userdata expected");
876 setuservalue(L
, uvalue(o
), L
->top
- 1);
877 luaC_barrier(L
, gcvalue(o
), L
->top
- 1);
884 ** 'load' and 'call' functions (run Lua code)
888 #define checkresults(L,na,nr) \
889 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
890 "results from function overflow current stack size")
893 LUA_API
void lua_callk (lua_State
*L
, int nargs
, int nresults
,
894 lua_KContext ctx
, lua_KFunction k
) {
897 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
898 "cannot use continuations inside hooks");
899 api_checknelems(L
, nargs
+1);
900 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
901 checkresults(L
, nargs
, nresults
);
902 func
= L
->top
- (nargs
+1);
903 if (k
!= NULL
&& L
->nny
== 0) { /* need to prepare continuation? */
904 L
->ci
->u
.c
.k
= k
; /* save continuation */
905 L
->ci
->u
.c
.ctx
= ctx
; /* save context */
906 luaD_call(L
, func
, nresults
, 1); /* do the call */
908 else /* no continuation or no yieldable */
909 luaD_call(L
, func
, nresults
, 0); /* just do the call */
910 adjustresults(L
, nresults
);
917 ** Execute a protected call.
919 struct CallS
{ /* data to 'f_call' */
925 static void f_call (lua_State
*L
, void *ud
) {
926 struct CallS
*c
= cast(struct CallS
*, ud
);
927 luaD_call(L
, c
->func
, c
->nresults
, 0);
932 LUA_API
int lua_pcallk (lua_State
*L
, int nargs
, int nresults
, int errfunc
,
933 lua_KContext ctx
, lua_KFunction k
) {
938 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
939 "cannot use continuations inside hooks");
940 api_checknelems(L
, nargs
+1);
941 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
942 checkresults(L
, nargs
, nresults
);
946 StkId o
= index2addr(L
, errfunc
);
947 api_checkstackindex(L
, errfunc
, o
);
948 func
= savestack(L
, o
);
950 c
.func
= L
->top
- (nargs
+1); /* function to be called */
951 if (k
== NULL
|| L
->nny
> 0) { /* no continuation or no yieldable? */
952 c
.nresults
= nresults
; /* do a 'conventional' protected call */
953 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
955 else { /* prepare continuation (call is already protected by 'resume') */
956 CallInfo
*ci
= L
->ci
;
957 ci
->u
.c
.k
= k
; /* save continuation */
958 ci
->u
.c
.ctx
= ctx
; /* save context */
959 /* save information for error recovery */
960 ci
->extra
= savestack(L
, c
.func
);
961 ci
->u
.c
.old_errfunc
= L
->errfunc
;
963 setoah(ci
->callstatus
, L
->allowhook
); /* save value of 'allowhook' */
964 ci
->callstatus
|= CIST_YPCALL
; /* function can do error recovery */
965 luaD_call(L
, c
.func
, nresults
, 1); /* do the call */
966 ci
->callstatus
&= ~CIST_YPCALL
;
967 L
->errfunc
= ci
->u
.c
.old_errfunc
;
968 status
= LUA_OK
; /* if it is here, there were no errors */
970 adjustresults(L
, nresults
);
976 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
977 const char *chunkname
, const char *mode
) {
981 if (!chunkname
) chunkname
= "?";
982 luaZ_init(L
, &z
, reader
, data
);
983 status
= luaD_protectedparser(L
, &z
, chunkname
, mode
);
984 if (status
== LUA_OK
) { /* no errors? */
985 LClosure
*f
= clLvalue(L
->top
- 1); /* get newly created function */
986 if (f
->nupvalues
>= 1) { /* does it have an upvalue? */
987 /* get global table from registry */
988 Table
*reg
= hvalue(&G(L
)->l_registry
);
989 const TValue
*gt
= luaH_getint(reg
, LUA_RIDX_GLOBALS
);
990 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
991 setobj(L
, f
->upvals
[0]->v
, gt
);
992 luaC_upvalbarrier(L
, f
->upvals
[0]);
1000 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
, int strip
) {
1004 api_checknelems(L
, 1);
1007 status
= luaU_dump(L
, getproto(o
), writer
, data
, strip
);
1015 LUA_API
int lua_status (lua_State
*L
) {
1021 ** Garbage-collection function
1024 LUA_API
int lua_gc (lua_State
*L
, int what
, int data
) {
1034 case LUA_GCRESTART
: {
1039 case LUA_GCCOLLECT
: {
1044 /* GC values are expressed in Kbytes: #bytes/2^10 */
1045 res
= cast_int(gettotalbytes(g
) >> 10);
1048 case LUA_GCCOUNTB
: {
1049 res
= cast_int(gettotalbytes(g
) & 0x3ff);
1053 l_mem debt
= 1; /* =1 to signal that it did an actual step */
1054 int oldrunning
= g
->gcrunning
;
1055 g
->gcrunning
= 1; /* allow GC to run */
1057 luaE_setdebt(g
, -GCSTEPSIZE
); /* to do a "small" step */
1060 else { /* add 'data' to total debt */
1061 debt
= cast(l_mem
, data
) * 1024 + g
->GCdebt
;
1062 luaE_setdebt(g
, debt
);
1065 g
->gcrunning
= oldrunning
; /* restore previous state */
1066 if (debt
> 0 && g
->gcstate
== GCSpause
) /* end of cycle? */
1067 res
= 1; /* signal it */
1070 case LUA_GCSETPAUSE
: {
1075 case LUA_GCSETSTEPMUL
: {
1077 if (data
< 40) data
= 40; /* avoid ridiculous low values (and 0) */
1078 g
->gcstepmul
= data
;
1081 case LUA_GCISRUNNING
: {
1085 default: res
= -1; /* invalid option */
1094 ** miscellaneous functions
1098 LUA_API
int lua_error (lua_State
*L
) {
1100 api_checknelems(L
, 1);
1102 /* code unreachable; will unlock when control actually leaves the kernel */
1103 return 0; /* to avoid warnings */
1107 LUA_API
int lua_next (lua_State
*L
, int idx
) {
1111 t
= index2addr(L
, idx
);
1112 api_check(L
, ttistable(t
), "table expected");
1113 more
= luaH_next(L
, hvalue(t
), L
->top
- 1);
1117 else /* no more elements */
1118 L
->top
-= 1; /* remove key */
1124 LUA_API
void lua_concat (lua_State
*L
, int n
) {
1126 api_checknelems(L
, n
);
1131 else if (n
== 0) { /* push empty string */
1132 setsvalue2s(L
, L
->top
, luaS_newlstr(L
, "", 0));
1135 /* else n == 1; nothing to do */
1140 LUA_API
void lua_len (lua_State
*L
, int idx
) {
1143 t
= index2addr(L
, idx
);
1144 luaV_objlen(L
, L
->top
, t
);
1150 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1153 if (ud
) *ud
= G(L
)->ud
;
1160 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1168 LUA_API
void *lua_newuserdata (lua_State
*L
, size_t size
) {
1172 u
= luaS_newudata(L
, size
);
1173 setuvalue(L
, L
->top
, u
);
1176 return getudatamem(u
);
1181 static const char *aux_upvalue (StkId fi
, int n
, TValue
**val
,
1182 CClosure
**owner
, UpVal
**uv
) {
1183 switch (ttype(fi
)) {
1184 case LUA_TCCL
: { /* C closure */
1185 CClosure
*f
= clCvalue(fi
);
1186 if (!(1 <= n
&& n
<= f
->nupvalues
)) return NULL
;
1187 *val
= &f
->upvalue
[n
-1];
1188 if (owner
) *owner
= f
;
1191 case LUA_TLCL
: { /* Lua closure */
1192 LClosure
*f
= clLvalue(fi
);
1195 if (!(1 <= n
&& n
<= p
->sizeupvalues
)) return NULL
;
1196 *val
= f
->upvals
[n
-1]->v
;
1197 if (uv
) *uv
= f
->upvals
[n
- 1];
1198 name
= p
->upvalues
[n
-1].name
;
1199 return (name
== NULL
) ? "(*no name)" : getstr(name
);
1201 default: return NULL
; /* not a closure */
1206 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1208 TValue
*val
= NULL
; /* to avoid warnings */
1210 name
= aux_upvalue(index2addr(L
, funcindex
), n
, &val
, NULL
, NULL
);
1212 setobj2s(L
, L
->top
, val
);
1220 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1222 TValue
*val
= NULL
; /* to avoid warnings */
1223 CClosure
*owner
= NULL
;
1227 fi
= index2addr(L
, funcindex
);
1228 api_checknelems(L
, 1);
1229 name
= aux_upvalue(fi
, n
, &val
, &owner
, &uv
);
1232 setobj(L
, val
, L
->top
);
1233 if (owner
) { luaC_barrier(L
, owner
, L
->top
); }
1234 else if (uv
) { luaC_upvalbarrier(L
, uv
); }
1241 static UpVal
**getupvalref (lua_State
*L
, int fidx
, int n
, LClosure
**pf
) {
1243 StkId fi
= index2addr(L
, fidx
);
1244 api_check(L
, ttisLclosure(fi
), "Lua function expected");
1246 api_check(L
, (1 <= n
&& n
<= f
->p
->sizeupvalues
), "invalid upvalue index");
1248 return &f
->upvals
[n
- 1]; /* get its upvalue pointer */
1252 LUA_API
void *lua_upvalueid (lua_State
*L
, int fidx
, int n
) {
1253 StkId fi
= index2addr(L
, fidx
);
1254 switch (ttype(fi
)) {
1255 case LUA_TLCL
: { /* lua closure */
1256 return *getupvalref(L
, fidx
, n
, NULL
);
1258 case LUA_TCCL
: { /* C closure */
1259 CClosure
*f
= clCvalue(fi
);
1260 api_check(L
, 1 <= n
&& n
<= f
->nupvalues
, "invalid upvalue index");
1261 return &f
->upvalue
[n
- 1];
1264 api_check(L
, 0, "closure expected");
1271 LUA_API
void lua_upvaluejoin (lua_State
*L
, int fidx1
, int n1
,
1272 int fidx2
, int n2
) {
1274 UpVal
**up1
= getupvalref(L
, fidx1
, n1
, &f1
);
1275 UpVal
**up2
= getupvalref(L
, fidx2
, n2
, NULL
);
1276 luaC_upvdeccount(L
, *up1
);
1279 if (upisopen(*up1
)) (*up1
)->u
.open
.touched
= 1;
1280 luaC_upvalbarrier(L
, *up1
);