4 ** See Copyright Notice in lua.h
35 const char lua_ident
[] =
36 "$LuaVersion: " LUA_COPYRIGHT
" $"
37 "$LuaAuthors: " LUA_AUTHORS
" $";
42 ** Test for a valid index (one that is not the 'nilvalue').
43 ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
44 ** However, it covers the most common cases in a faster way.
46 #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
49 /* test for pseudo index */
50 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
52 /* test for upvalue */
53 #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
57 ** Convert an acceptable index to a pointer to its respective value.
58 ** Non-valid indices return the special nil value 'G(L)->nilvalue'.
60 static TValue
*index2value (lua_State
*L
, int idx
) {
63 StkId o
= ci
->func
.p
+ idx
;
64 api_check(L
, idx
<= ci
->top
.p
- (ci
->func
.p
+ 1), "unacceptable index");
65 if (o
>= L
->top
.p
) return &G(L
)->nilvalue
;
68 else if (!ispseudo(idx
)) { /* negative index */
69 api_check(L
, idx
!= 0 && -idx
<= L
->top
.p
- (ci
->func
.p
+ 1),
71 return s2v(L
->top
.p
+ idx
);
73 else if (idx
== LUA_REGISTRYINDEX
)
74 return &G(L
)->l_registry
;
76 idx
= LUA_REGISTRYINDEX
- idx
;
77 api_check(L
, idx
<= MAXUPVAL
+ 1, "upvalue index too large");
78 if (ttisCclosure(s2v(ci
->func
.p
))) { /* C closure? */
79 CClosure
*func
= clCvalue(s2v(ci
->func
.p
));
80 return (idx
<= func
->nupvalues
) ? &func
->upvalue
[idx
-1]
83 else { /* light C function or Lua function (through a hook)?) */
84 api_check(L
, ttislcf(s2v(ci
->func
.p
)), "caller not a C function");
85 return &G(L
)->nilvalue
; /* no upvalues */
93 ** Convert a valid actual index (not a pseudo-index) to its address.
95 l_sinline StkId
index2stack (lua_State
*L
, int idx
) {
98 StkId o
= ci
->func
.p
+ idx
;
99 api_check(L
, o
< L
->top
.p
, "invalid index");
102 else { /* non-positive index */
103 api_check(L
, idx
!= 0 && -idx
<= L
->top
.p
- (ci
->func
.p
+ 1),
105 api_check(L
, !ispseudo(idx
), "invalid index");
106 return L
->top
.p
+ idx
;
111 LUA_API
int lua_checkstack (lua_State
*L
, int n
) {
116 api_check(L
, n
>= 0, "negative 'n'");
117 if (L
->stack_last
.p
- L
->top
.p
> n
) /* stack large enough? */
118 res
= 1; /* yes; check is OK */
119 else /* need to grow stack */
120 res
= luaD_growstack(L
, n
, 0);
121 if (res
&& ci
->top
.p
< L
->top
.p
+ n
)
122 ci
->top
.p
= L
->top
.p
+ n
; /* adjust frame top */
128 LUA_API
void lua_xmove (lua_State
*from
, lua_State
*to
, int n
) {
130 if (from
== to
) return;
132 api_checknelems(from
, n
);
133 api_check(from
, G(from
) == G(to
), "moving among independent states");
134 api_check(from
, to
->ci
->top
.p
- to
->top
.p
>= n
, "stack overflow");
136 for (i
= 0; i
< n
; i
++) {
137 setobjs2s(to
, to
->top
.p
, from
->top
.p
+ i
);
138 to
->top
.p
++; /* stack already checked by previous 'api_check' */
144 LUA_API lua_CFunction
lua_atpanic (lua_State
*L
, lua_CFunction panicf
) {
148 G(L
)->panic
= panicf
;
154 LUA_API lua_Number
lua_version (lua_State
*L
) {
156 return LUA_VERSION_NUM
;
162 ** basic stack manipulation
167 ** convert an acceptable stack index into an absolute index
169 LUA_API
int lua_absindex (lua_State
*L
, int idx
) {
170 return (idx
> 0 || ispseudo(idx
))
172 : cast_int(L
->top
.p
- L
->ci
->func
.p
) + idx
;
176 LUA_API
int lua_gettop (lua_State
*L
) {
177 return cast_int(L
->top
.p
- (L
->ci
->func
.p
+ 1));
181 LUA_API
void lua_settop (lua_State
*L
, int idx
) {
184 ptrdiff_t diff
; /* difference for new top */
189 api_check(L
, idx
<= ci
->top
.p
- (func
+ 1), "new top too large");
190 diff
= ((func
+ 1) + idx
) - L
->top
.p
;
191 for (; diff
> 0; diff
--)
192 setnilvalue(s2v(L
->top
.p
++)); /* clear new slots */
195 api_check(L
, -(idx
+1) <= (L
->top
.p
- (func
+ 1)), "invalid new top");
196 diff
= idx
+ 1; /* will "subtract" index (as it is negative) */
198 api_check(L
, L
->tbclist
.p
< L
->top
.p
, "previous pop of an unclosed slot");
199 newtop
= L
->top
.p
+ diff
;
200 if (diff
< 0 && L
->tbclist
.p
>= newtop
) {
201 lua_assert(hastocloseCfunc(ci
->nresults
));
202 newtop
= luaF_close(L
, newtop
, CLOSEKTOP
, 0);
204 L
->top
.p
= newtop
; /* correct top only after closing any upvalue */
209 LUA_API
void lua_closeslot (lua_State
*L
, int idx
) {
212 level
= index2stack(L
, idx
);
213 api_check(L
, hastocloseCfunc(L
->ci
->nresults
) && L
->tbclist
.p
== level
,
214 "no variable to close at given level");
215 level
= luaF_close(L
, level
, CLOSEKTOP
, 0);
216 setnilvalue(s2v(level
));
222 ** Reverse the stack segment from 'from' to 'to'
223 ** (auxiliary to 'lua_rotate')
224 ** Note that we move(copy) only the value inside the stack.
225 ** (We do not move additional fields that may exist.)
227 l_sinline
void reverse (lua_State
*L
, StkId from
, StkId to
) {
228 for (; from
< to
; from
++, to
--) {
230 setobj(L
, &temp
, s2v(from
));
231 setobjs2s(L
, from
, to
);
232 setobj2s(L
, to
, &temp
);
238 ** Let x = AB, where A is a prefix of length 'n'. Then,
239 ** rotate x n == BA. But BA == (A^r . B^r)^r.
241 LUA_API
void lua_rotate (lua_State
*L
, int idx
, int n
) {
244 t
= L
->top
.p
- 1; /* end of stack segment being rotated */
245 p
= index2stack(L
, idx
); /* start of segment */
246 api_check(L
, (n
>= 0 ? n
: -n
) <= (t
- p
+ 1), "invalid 'n'");
247 m
= (n
>= 0 ? t
- n
: p
- n
- 1); /* end of prefix */
248 reverse(L
, p
, m
); /* reverse the prefix with length 'n' */
249 reverse(L
, m
+ 1, t
); /* reverse the suffix */
250 reverse(L
, p
, t
); /* reverse the entire segment */
255 LUA_API
void lua_copy (lua_State
*L
, int fromidx
, int toidx
) {
258 fr
= index2value(L
, fromidx
);
259 to
= index2value(L
, toidx
);
260 api_check(L
, isvalid(L
, to
), "invalid index");
262 if (isupvalue(toidx
)) /* function upvalue? */
263 luaC_barrier(L
, clCvalue(s2v(L
->ci
->func
.p
)), fr
);
264 /* LUA_REGISTRYINDEX does not need gc barrier
265 (collector revisits it before finishing collection) */
270 LUA_API
void lua_pushvalue (lua_State
*L
, int idx
) {
272 setobj2s(L
, L
->top
.p
, index2value(L
, idx
));
280 ** access functions (stack -> C)
284 LUA_API
int lua_type (lua_State
*L
, int idx
) {
285 const TValue
*o
= index2value(L
, idx
);
286 return (isvalid(L
, o
) ? ttype(o
) : LUA_TNONE
);
290 LUA_API
const char *lua_typename (lua_State
*L
, int t
) {
292 api_check(L
, LUA_TNONE
<= t
&& t
< LUA_NUMTYPES
, "invalid type");
297 LUA_API
int lua_iscfunction (lua_State
*L
, int idx
) {
298 const TValue
*o
= index2value(L
, idx
);
299 return (ttislcf(o
) || (ttisCclosure(o
)));
303 LUA_API
int lua_isinteger (lua_State
*L
, int idx
) {
304 const TValue
*o
= index2value(L
, idx
);
305 return ttisinteger(o
);
309 LUA_API
int lua_isnumber (lua_State
*L
, int idx
) {
311 const TValue
*o
= index2value(L
, idx
);
312 return tonumber(o
, &n
);
316 LUA_API
int lua_isstring (lua_State
*L
, int idx
) {
317 const TValue
*o
= index2value(L
, idx
);
318 return (ttisstring(o
) || cvt2str(o
));
322 LUA_API
int lua_isuserdata (lua_State
*L
, int idx
) {
323 const TValue
*o
= index2value(L
, idx
);
324 return (ttisfulluserdata(o
) || ttislightuserdata(o
));
328 LUA_API
int lua_rawequal (lua_State
*L
, int index1
, int index2
) {
329 const TValue
*o1
= index2value(L
, index1
);
330 const TValue
*o2
= index2value(L
, index2
);
331 return (isvalid(L
, o1
) && isvalid(L
, o2
)) ? luaV_rawequalobj(o1
, o2
) : 0;
335 LUA_API
void lua_arith (lua_State
*L
, int op
) {
337 if (op
!= LUA_OPUNM
&& op
!= LUA_OPBNOT
)
338 api_checknelems(L
, 2); /* all other operations expect two operands */
339 else { /* for unary operations, add fake 2nd operand */
340 api_checknelems(L
, 1);
341 setobjs2s(L
, L
->top
.p
, L
->top
.p
- 1);
344 /* first operand at top - 2, second at top - 1; result go to top - 2 */
345 luaO_arith(L
, op
, s2v(L
->top
.p
- 2), s2v(L
->top
.p
- 1), L
->top
.p
- 2);
346 L
->top
.p
--; /* remove second operand */
351 LUA_API
int lua_compare (lua_State
*L
, int index1
, int index2
, int op
) {
355 lua_lock(L
); /* may call tag method */
356 o1
= index2value(L
, index1
);
357 o2
= index2value(L
, index2
);
358 if (isvalid(L
, o1
) && isvalid(L
, o2
)) {
360 case LUA_OPEQ
: i
= luaV_equalobj(L
, o1
, o2
); break;
361 case LUA_OPLT
: i
= luaV_lessthan(L
, o1
, o2
); break;
362 case LUA_OPLE
: i
= luaV_lessequal(L
, o1
, o2
); break;
363 default: api_check(L
, 0, "invalid option");
371 LUA_API
size_t lua_stringtonumber (lua_State
*L
, const char *s
) {
372 size_t sz
= luaO_str2num(s
, s2v(L
->top
.p
));
379 LUA_API lua_Number
lua_tonumberx (lua_State
*L
, int idx
, int *pisnum
) {
381 const TValue
*o
= index2value(L
, idx
);
382 int isnum
= tonumber(o
, &n
);
389 LUA_API lua_Integer
lua_tointegerx (lua_State
*L
, int idx
, int *pisnum
) {
391 const TValue
*o
= index2value(L
, idx
);
392 int isnum
= tointeger(o
, &res
);
399 LUA_API
int lua_toboolean (lua_State
*L
, int idx
) {
400 const TValue
*o
= index2value(L
, idx
);
401 return !l_isfalse(o
);
405 LUA_API
const char *lua_tolstring (lua_State
*L
, int idx
, size_t *len
) {
408 o
= index2value(L
, idx
);
409 if (!ttisstring(o
)) {
410 if (!cvt2str(o
)) { /* not convertible? */
411 if (len
!= NULL
) *len
= 0;
417 o
= index2value(L
, idx
); /* previous call may reallocate the stack */
426 LUA_API lua_Unsigned
lua_rawlen (lua_State
*L
, int idx
) {
427 const TValue
*o
= index2value(L
, idx
);
428 switch (ttypetag(o
)) {
429 case LUA_VSHRSTR
: return tsvalue(o
)->shrlen
;
430 case LUA_VLNGSTR
: return tsvalue(o
)->u
.lnglen
;
431 case LUA_VUSERDATA
: return uvalue(o
)->len
;
432 case LUA_VTABLE
: return luaH_getn(hvalue(o
));
438 LUA_API lua_CFunction
lua_tocfunction (lua_State
*L
, int idx
) {
439 const TValue
*o
= index2value(L
, idx
);
440 if (ttislcf(o
)) return fvalue(o
);
441 else if (ttisCclosure(o
))
442 return clCvalue(o
)->f
;
443 else return NULL
; /* not a C function */
447 l_sinline
void *touserdata (const TValue
*o
) {
449 case LUA_TUSERDATA
: return getudatamem(uvalue(o
));
450 case LUA_TLIGHTUSERDATA
: return pvalue(o
);
451 default: return NULL
;
456 LUA_API
void *lua_touserdata (lua_State
*L
, int idx
) {
457 const TValue
*o
= index2value(L
, idx
);
458 return touserdata(o
);
462 LUA_API lua_State
*lua_tothread (lua_State
*L
, int idx
) {
463 const TValue
*o
= index2value(L
, idx
);
464 return (!ttisthread(o
)) ? NULL
: thvalue(o
);
469 ** Returns a pointer to the internal representation of an object.
470 ** Note that ANSI C does not allow the conversion of a pointer to
471 ** function to a 'void*', so the conversion here goes through
472 ** a 'size_t'. (As the returned pointer is only informative, this
473 ** conversion should not be a problem.)
475 LUA_API
const void *lua_topointer (lua_State
*L
, int idx
) {
476 const TValue
*o
= index2value(L
, idx
);
477 switch (ttypetag(o
)) {
478 case LUA_VLCF
: return cast_voidp(cast_sizet(fvalue(o
)));
479 case LUA_VUSERDATA
: case LUA_VLIGHTUSERDATA
:
480 return touserdata(o
);
482 if (iscollectable(o
))
493 ** push functions (C -> stack)
497 LUA_API
void lua_pushnil (lua_State
*L
) {
499 setnilvalue(s2v(L
->top
.p
));
505 LUA_API
void lua_pushnumber (lua_State
*L
, lua_Number n
) {
507 setfltvalue(s2v(L
->top
.p
), n
);
513 LUA_API
void lua_pushinteger (lua_State
*L
, lua_Integer n
) {
515 setivalue(s2v(L
->top
.p
), n
);
522 ** Pushes on the stack a string with given length. Avoid using 's' when
523 ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
524 ** 'memcmp' and 'memcpy'.
526 LUA_API
const char *lua_pushlstring (lua_State
*L
, const char *s
, size_t len
) {
529 ts
= (len
== 0) ? luaS_new(L
, "") : luaS_newlstr(L
, s
, len
);
530 setsvalue2s(L
, L
->top
.p
, ts
);
538 LUA_API
const char *lua_pushstring (lua_State
*L
, const char *s
) {
541 setnilvalue(s2v(L
->top
.p
));
545 setsvalue2s(L
, L
->top
.p
, ts
);
546 s
= getstr(ts
); /* internal copy's address */
555 LUA_API
const char *lua_pushvfstring (lua_State
*L
, const char *fmt
,
559 ret
= luaO_pushvfstring(L
, fmt
, argp
);
566 LUA_API
const char *lua_pushfstring (lua_State
*L
, const char *fmt
, ...) {
571 ret
= luaO_pushvfstring(L
, fmt
, argp
);
579 LUA_API
void lua_pushcclosure (lua_State
*L
, lua_CFunction fn
, int n
) {
582 setfvalue(s2v(L
->top
.p
), fn
);
587 api_checknelems(L
, n
);
588 api_check(L
, n
<= MAXUPVAL
, "upvalue index too large");
589 cl
= luaF_newCclosure(L
, n
);
593 setobj2n(L
, &cl
->upvalue
[n
], s2v(L
->top
.p
+ n
));
594 /* does not need barrier because closure is white */
595 lua_assert(iswhite(cl
));
597 setclCvalue(L
, s2v(L
->top
.p
), cl
);
605 LUA_API
void lua_pushboolean (lua_State
*L
, int b
) {
608 setbtvalue(s2v(L
->top
.p
));
610 setbfvalue(s2v(L
->top
.p
));
616 LUA_API
void lua_pushlightuserdata (lua_State
*L
, void *p
) {
618 setpvalue(s2v(L
->top
.p
), p
);
624 LUA_API
int lua_pushthread (lua_State
*L
) {
626 setthvalue(L
, s2v(L
->top
.p
), L
);
629 return (G(L
)->mainthread
== L
);
635 ** get functions (Lua -> stack)
639 l_sinline
int auxgetstr (lua_State
*L
, const TValue
*t
, const char *k
) {
641 TString
*str
= luaS_new(L
, k
);
642 if (luaV_fastget(L
, t
, str
, slot
, luaH_getstr
)) {
643 setobj2s(L
, L
->top
.p
, slot
);
647 setsvalue2s(L
, L
->top
.p
, str
);
649 luaV_finishget(L
, t
, s2v(L
->top
.p
- 1), L
->top
.p
- 1, slot
);
652 return ttype(s2v(L
->top
.p
- 1));
657 ** Get the global table in the registry. Since all predefined
658 ** indices in the registry were inserted right when the registry
659 ** was created and never removed, they must always be in the array
660 ** part of the registry.
662 #define getGtable(L) \
663 (&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
666 LUA_API
int lua_getglobal (lua_State
*L
, const char *name
) {
670 return auxgetstr(L
, G
, name
);
674 LUA_API
int lua_gettable (lua_State
*L
, int idx
) {
678 t
= index2value(L
, idx
);
679 if (luaV_fastget(L
, t
, s2v(L
->top
.p
- 1), slot
, luaH_get
)) {
680 setobj2s(L
, L
->top
.p
- 1, slot
);
683 luaV_finishget(L
, t
, s2v(L
->top
.p
- 1), L
->top
.p
- 1, slot
);
685 return ttype(s2v(L
->top
.p
- 1));
689 LUA_API
int lua_getfield (lua_State
*L
, int idx
, const char *k
) {
691 return auxgetstr(L
, index2value(L
, idx
), k
);
695 LUA_API
int lua_geti (lua_State
*L
, int idx
, lua_Integer n
) {
699 t
= index2value(L
, idx
);
700 if (luaV_fastgeti(L
, t
, n
, slot
)) {
701 setobj2s(L
, L
->top
.p
, slot
);
706 luaV_finishget(L
, t
, &aux
, L
->top
.p
, slot
);
710 return ttype(s2v(L
->top
.p
- 1));
714 l_sinline
int finishrawget (lua_State
*L
, const TValue
*val
) {
715 if (isempty(val
)) /* avoid copying empty items to the stack */
716 setnilvalue(s2v(L
->top
.p
));
718 setobj2s(L
, L
->top
.p
, val
);
721 return ttype(s2v(L
->top
.p
- 1));
725 static Table
*gettable (lua_State
*L
, int idx
) {
726 TValue
*t
= index2value(L
, idx
);
727 api_check(L
, ttistable(t
), "table expected");
732 LUA_API
int lua_rawget (lua_State
*L
, int idx
) {
736 api_checknelems(L
, 1);
737 t
= gettable(L
, idx
);
738 val
= luaH_get(t
, s2v(L
->top
.p
- 1));
739 L
->top
.p
--; /* remove key */
740 return finishrawget(L
, val
);
744 LUA_API
int lua_rawgeti (lua_State
*L
, int idx
, lua_Integer n
) {
747 t
= gettable(L
, idx
);
748 return finishrawget(L
, luaH_getint(t
, n
));
752 LUA_API
int lua_rawgetp (lua_State
*L
, int idx
, const void *p
) {
756 t
= gettable(L
, idx
);
757 setpvalue(&k
, cast_voidp(p
));
758 return finishrawget(L
, luaH_get(t
, &k
));
762 LUA_API
void lua_createtable (lua_State
*L
, int narray
, int nrec
) {
766 sethvalue2s(L
, L
->top
.p
, t
);
768 if (narray
> 0 || nrec
> 0)
769 luaH_resize(L
, t
, narray
, nrec
);
775 LUA_API
int lua_getmetatable (lua_State
*L
, int objindex
) {
780 obj
= index2value(L
, objindex
);
781 switch (ttype(obj
)) {
783 mt
= hvalue(obj
)->metatable
;
786 mt
= uvalue(obj
)->metatable
;
789 mt
= G(L
)->mt
[ttype(obj
)];
793 sethvalue2s(L
, L
->top
.p
, mt
);
802 LUA_API
int lua_getiuservalue (lua_State
*L
, int idx
, int n
) {
806 o
= index2value(L
, idx
);
807 api_check(L
, ttisfulluserdata(o
), "full userdata expected");
808 if (n
<= 0 || n
> uvalue(o
)->nuvalue
) {
809 setnilvalue(s2v(L
->top
.p
));
813 setobj2s(L
, L
->top
.p
, &uvalue(o
)->uv
[n
- 1].uv
);
814 t
= ttype(s2v(L
->top
.p
));
823 ** set functions (stack -> Lua)
827 ** t[k] = value at the top of the stack (where 'k' is a string)
829 static void auxsetstr (lua_State
*L
, const TValue
*t
, const char *k
) {
831 TString
*str
= luaS_new(L
, k
);
832 api_checknelems(L
, 1);
833 if (luaV_fastget(L
, t
, str
, slot
, luaH_getstr
)) {
834 luaV_finishfastset(L
, t
, slot
, s2v(L
->top
.p
- 1));
835 L
->top
.p
--; /* pop value */
838 setsvalue2s(L
, L
->top
.p
, str
); /* push 'str' (to make it a TValue) */
840 luaV_finishset(L
, t
, s2v(L
->top
.p
- 1), s2v(L
->top
.p
- 2), slot
);
841 L
->top
.p
-= 2; /* pop value and key */
843 lua_unlock(L
); /* lock done by caller */
847 LUA_API
void lua_setglobal (lua_State
*L
, const char *name
) {
849 lua_lock(L
); /* unlock done in 'auxsetstr' */
851 auxsetstr(L
, G
, name
);
855 LUA_API
void lua_settable (lua_State
*L
, int idx
) {
859 api_checknelems(L
, 2);
860 t
= index2value(L
, idx
);
861 if (luaV_fastget(L
, t
, s2v(L
->top
.p
- 2), slot
, luaH_get
)) {
862 luaV_finishfastset(L
, t
, slot
, s2v(L
->top
.p
- 1));
865 luaV_finishset(L
, t
, s2v(L
->top
.p
- 2), s2v(L
->top
.p
- 1), slot
);
866 L
->top
.p
-= 2; /* pop index and value */
871 LUA_API
void lua_setfield (lua_State
*L
, int idx
, const char *k
) {
872 lua_lock(L
); /* unlock done in 'auxsetstr' */
873 auxsetstr(L
, index2value(L
, idx
), k
);
877 LUA_API
void lua_seti (lua_State
*L
, int idx
, lua_Integer n
) {
881 api_checknelems(L
, 1);
882 t
= index2value(L
, idx
);
883 if (luaV_fastgeti(L
, t
, n
, slot
)) {
884 luaV_finishfastset(L
, t
, slot
, s2v(L
->top
.p
- 1));
889 luaV_finishset(L
, t
, &aux
, s2v(L
->top
.p
- 1), slot
);
891 L
->top
.p
--; /* pop value */
896 static void aux_rawset (lua_State
*L
, int idx
, TValue
*key
, int n
) {
899 api_checknelems(L
, n
);
900 t
= gettable(L
, idx
);
901 luaH_set(L
, t
, key
, s2v(L
->top
.p
- 1));
902 invalidateTMcache(t
);
903 luaC_barrierback(L
, obj2gco(t
), s2v(L
->top
.p
- 1));
909 LUA_API
void lua_rawset (lua_State
*L
, int idx
) {
910 aux_rawset(L
, idx
, s2v(L
->top
.p
- 2), 2);
914 LUA_API
void lua_rawsetp (lua_State
*L
, int idx
, const void *p
) {
916 setpvalue(&k
, cast_voidp(p
));
917 aux_rawset(L
, idx
, &k
, 1);
921 LUA_API
void lua_rawseti (lua_State
*L
, int idx
, lua_Integer n
) {
924 api_checknelems(L
, 1);
925 t
= gettable(L
, idx
);
926 luaH_setint(L
, t
, n
, s2v(L
->top
.p
- 1));
927 luaC_barrierback(L
, obj2gco(t
), s2v(L
->top
.p
- 1));
933 LUA_API
int lua_setmetatable (lua_State
*L
, int objindex
) {
937 api_checknelems(L
, 1);
938 obj
= index2value(L
, objindex
);
939 if (ttisnil(s2v(L
->top
.p
- 1)))
942 api_check(L
, ttistable(s2v(L
->top
.p
- 1)), "table expected");
943 mt
= hvalue(s2v(L
->top
.p
- 1));
945 switch (ttype(obj
)) {
947 hvalue(obj
)->metatable
= mt
;
949 luaC_objbarrier(L
, gcvalue(obj
), mt
);
950 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
954 case LUA_TUSERDATA
: {
955 uvalue(obj
)->metatable
= mt
;
957 luaC_objbarrier(L
, uvalue(obj
), mt
);
958 luaC_checkfinalizer(L
, gcvalue(obj
), mt
);
963 G(L
)->mt
[ttype(obj
)] = mt
;
973 LUA_API
int lua_setiuservalue (lua_State
*L
, int idx
, int n
) {
977 api_checknelems(L
, 1);
978 o
= index2value(L
, idx
);
979 api_check(L
, ttisfulluserdata(o
), "full userdata expected");
980 if (!(cast_uint(n
) - 1u < cast_uint(uvalue(o
)->nuvalue
)))
981 res
= 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
983 setobj(L
, &uvalue(o
)->uv
[n
- 1].uv
, s2v(L
->top
.p
- 1));
984 luaC_barrierback(L
, gcvalue(o
), s2v(L
->top
.p
- 1));
994 ** 'load' and 'call' functions (run Lua code)
998 #define checkresults(L,na,nr) \
999 api_check(L, (nr) == LUA_MULTRET \
1000 || (L->ci->top.p - L->top.p >= (nr) - (na)), \
1001 "results from function overflow current stack size")
1004 LUA_API
void lua_callk (lua_State
*L
, int nargs
, int nresults
,
1005 lua_KContext ctx
, lua_KFunction k
) {
1008 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
1009 "cannot use continuations inside hooks");
1010 api_checknelems(L
, nargs
+1);
1011 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
1012 checkresults(L
, nargs
, nresults
);
1013 func
= L
->top
.p
- (nargs
+1);
1014 if (k
!= NULL
&& yieldable(L
)) { /* need to prepare continuation? */
1015 L
->ci
->u
.c
.k
= k
; /* save continuation */
1016 L
->ci
->u
.c
.ctx
= ctx
; /* save context */
1017 luaD_call(L
, func
, nresults
); /* do the call */
1019 else /* no continuation or no yieldable */
1020 luaD_callnoyield(L
, func
, nresults
); /* just do the call */
1021 adjustresults(L
, nresults
);
1028 ** Execute a protected call.
1030 struct CallS
{ /* data to 'f_call' */
1036 static void f_call (lua_State
*L
, void *ud
) {
1037 struct CallS
*c
= cast(struct CallS
*, ud
);
1038 luaD_callnoyield(L
, c
->func
, c
->nresults
);
1043 LUA_API
int lua_pcallk (lua_State
*L
, int nargs
, int nresults
, int errfunc
,
1044 lua_KContext ctx
, lua_KFunction k
) {
1049 api_check(L
, k
== NULL
|| !isLua(L
->ci
),
1050 "cannot use continuations inside hooks");
1051 api_checknelems(L
, nargs
+1);
1052 api_check(L
, L
->status
== LUA_OK
, "cannot do calls on non-normal thread");
1053 checkresults(L
, nargs
, nresults
);
1057 StkId o
= index2stack(L
, errfunc
);
1058 api_check(L
, ttisfunction(s2v(o
)), "error handler must be a function");
1059 func
= savestack(L
, o
);
1061 c
.func
= L
->top
.p
- (nargs
+1); /* function to be called */
1062 if (k
== NULL
|| !yieldable(L
)) { /* no continuation or no yieldable? */
1063 c
.nresults
= nresults
; /* do a 'conventional' protected call */
1064 status
= luaD_pcall(L
, f_call
, &c
, savestack(L
, c
.func
), func
);
1066 else { /* prepare continuation (call is already protected by 'resume') */
1067 CallInfo
*ci
= L
->ci
;
1068 ci
->u
.c
.k
= k
; /* save continuation */
1069 ci
->u
.c
.ctx
= ctx
; /* save context */
1070 /* save information for error recovery */
1071 ci
->u2
.funcidx
= cast_int(savestack(L
, c
.func
));
1072 ci
->u
.c
.old_errfunc
= L
->errfunc
;
1074 setoah(ci
->callstatus
, L
->allowhook
); /* save value of 'allowhook' */
1075 ci
->callstatus
|= CIST_YPCALL
; /* function can do error recovery */
1076 luaD_call(L
, c
.func
, nresults
); /* do the call */
1077 ci
->callstatus
&= ~CIST_YPCALL
;
1078 L
->errfunc
= ci
->u
.c
.old_errfunc
;
1079 status
= LUA_OK
; /* if it is here, there were no errors */
1081 adjustresults(L
, nresults
);
1087 LUA_API
int lua_load (lua_State
*L
, lua_Reader reader
, void *data
,
1088 const char *chunkname
, const char *mode
) {
1092 if (!chunkname
) chunkname
= "?";
1093 luaZ_init(L
, &z
, reader
, data
);
1094 status
= luaD_protectedparser(L
, &z
, chunkname
, mode
);
1095 if (status
== LUA_OK
) { /* no errors? */
1096 LClosure
*f
= clLvalue(s2v(L
->top
.p
- 1)); /* get new function */
1097 if (f
->nupvalues
>= 1) { /* does it have an upvalue? */
1098 /* get global table from registry */
1099 const TValue
*gt
= getGtable(L
);
1100 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
1101 setobj(L
, f
->upvals
[0]->v
.p
, gt
);
1102 luaC_barrier(L
, f
->upvals
[0], gt
);
1110 LUA_API
int lua_dump (lua_State
*L
, lua_Writer writer
, void *data
, int strip
) {
1114 api_checknelems(L
, 1);
1115 o
= s2v(L
->top
.p
- 1);
1117 status
= luaU_dump(L
, getproto(o
), writer
, data
, strip
);
1125 LUA_API
int lua_status (lua_State
*L
) {
1131 ** Garbage-collection function
1133 LUA_API
int lua_gc (lua_State
*L
, int what
, ...) {
1136 global_State
*g
= G(L
);
1137 if (g
->gcstp
& GCSTPGC
) /* internal stop? */
1138 return -1; /* all options are invalid when stopped */
1140 va_start(argp
, what
);
1143 g
->gcstp
= GCSTPUSR
; /* stopped by the user */
1146 case LUA_GCRESTART
: {
1148 g
->gcstp
= 0; /* (GCSTPGC must be already zero here) */
1151 case LUA_GCCOLLECT
: {
1156 /* GC values are expressed in Kbytes: #bytes/2^10 */
1157 res
= cast_int(gettotalbytes(g
) >> 10);
1160 case LUA_GCCOUNTB
: {
1161 res
= cast_int(gettotalbytes(g
) & 0x3ff);
1165 int data
= va_arg(argp
, int);
1166 l_mem debt
= 1; /* =1 to signal that it did an actual step */
1167 lu_byte oldstp
= g
->gcstp
;
1168 g
->gcstp
= 0; /* allow GC to run (GCSTPGC must be zero here) */
1170 luaE_setdebt(g
, 0); /* do a basic step */
1173 else { /* add 'data' to total debt */
1174 debt
= cast(l_mem
, data
) * 1024 + g
->GCdebt
;
1175 luaE_setdebt(g
, debt
);
1178 g
->gcstp
= oldstp
; /* restore previous state */
1179 if (debt
> 0 && g
->gcstate
== GCSpause
) /* end of cycle? */
1180 res
= 1; /* signal it */
1183 case LUA_GCSETPAUSE
: {
1184 int data
= va_arg(argp
, int);
1185 res
= getgcparam(g
->gcpause
);
1186 setgcparam(g
->gcpause
, data
);
1189 case LUA_GCSETSTEPMUL
: {
1190 int data
= va_arg(argp
, int);
1191 res
= getgcparam(g
->gcstepmul
);
1192 setgcparam(g
->gcstepmul
, data
);
1195 case LUA_GCISRUNNING
: {
1200 int minormul
= va_arg(argp
, int);
1201 int majormul
= va_arg(argp
, int);
1202 res
= isdecGCmodegen(g
) ? LUA_GCGEN
: LUA_GCINC
;
1204 g
->genminormul
= minormul
;
1206 setgcparam(g
->genmajormul
, majormul
);
1207 luaC_changemode(L
, KGC_GEN
);
1211 int pause
= va_arg(argp
, int);
1212 int stepmul
= va_arg(argp
, int);
1213 int stepsize
= va_arg(argp
, int);
1214 res
= isdecGCmodegen(g
) ? LUA_GCGEN
: LUA_GCINC
;
1216 setgcparam(g
->gcpause
, pause
);
1218 setgcparam(g
->gcstepmul
, stepmul
);
1220 g
->gcstepsize
= stepsize
;
1221 luaC_changemode(L
, KGC_INC
);
1224 default: res
= -1; /* invalid option */
1234 ** miscellaneous functions
1238 LUA_API
int lua_error (lua_State
*L
) {
1241 errobj
= s2v(L
->top
.p
- 1);
1242 api_checknelems(L
, 1);
1243 /* error object is the memory error message? */
1244 if (ttisshrstring(errobj
) && eqshrstr(tsvalue(errobj
), G(L
)->memerrmsg
))
1245 luaM_error(L
); /* raise a memory error */
1247 luaG_errormsg(L
); /* raise a regular error */
1248 /* code unreachable; will unlock when control actually leaves the kernel */
1249 return 0; /* to avoid warnings */
1253 LUA_API
int lua_next (lua_State
*L
, int idx
) {
1257 api_checknelems(L
, 1);
1258 t
= gettable(L
, idx
);
1259 more
= luaH_next(L
, t
, L
->top
.p
- 1);
1263 else /* no more elements */
1264 L
->top
.p
-= 1; /* remove key */
1270 LUA_API
void lua_toclose (lua_State
*L
, int idx
) {
1274 o
= index2stack(L
, idx
);
1275 nresults
= L
->ci
->nresults
;
1276 api_check(L
, L
->tbclist
.p
< o
, "given index below or equal a marked one");
1277 luaF_newtbcupval(L
, o
); /* create new to-be-closed upvalue */
1278 if (!hastocloseCfunc(nresults
)) /* function not marked yet? */
1279 L
->ci
->nresults
= codeNresults(nresults
); /* mark it */
1280 lua_assert(hastocloseCfunc(L
->ci
->nresults
));
1285 LUA_API
void lua_concat (lua_State
*L
, int n
) {
1287 api_checknelems(L
, n
);
1290 else { /* nothing to concatenate */
1291 setsvalue2s(L
, L
->top
.p
, luaS_newlstr(L
, "", 0)); /* push empty string */
1299 LUA_API
void lua_len (lua_State
*L
, int idx
) {
1302 t
= index2value(L
, idx
);
1303 luaV_objlen(L
, L
->top
.p
, t
);
1309 LUA_API lua_Alloc
lua_getallocf (lua_State
*L
, void **ud
) {
1312 if (ud
) *ud
= G(L
)->ud
;
1319 LUA_API
void lua_setallocf (lua_State
*L
, lua_Alloc f
, void *ud
) {
1327 void lua_setwarnf (lua_State
*L
, lua_WarnFunction f
, void *ud
) {
1335 void lua_warning (lua_State
*L
, const char *msg
, int tocont
) {
1337 luaE_warning(L
, msg
, tocont
);
1343 LUA_API
void *lua_newuserdatauv (lua_State
*L
, size_t size
, int nuvalue
) {
1346 api_check(L
, 0 <= nuvalue
&& nuvalue
< USHRT_MAX
, "invalid value");
1347 u
= luaS_newudata(L
, size
, nuvalue
);
1348 setuvalue(L
, s2v(L
->top
.p
), u
);
1352 return getudatamem(u
);
1357 static const char *aux_upvalue (TValue
*fi
, int n
, TValue
**val
,
1359 switch (ttypetag(fi
)) {
1360 case LUA_VCCL
: { /* C closure */
1361 CClosure
*f
= clCvalue(fi
);
1362 if (!(cast_uint(n
) - 1u < cast_uint(f
->nupvalues
)))
1363 return NULL
; /* 'n' not in [1, f->nupvalues] */
1364 *val
= &f
->upvalue
[n
-1];
1365 if (owner
) *owner
= obj2gco(f
);
1368 case LUA_VLCL
: { /* Lua closure */
1369 LClosure
*f
= clLvalue(fi
);
1372 if (!(cast_uint(n
) - 1u < cast_uint(p
->sizeupvalues
)))
1373 return NULL
; /* 'n' not in [1, p->sizeupvalues] */
1374 *val
= f
->upvals
[n
-1]->v
.p
;
1375 if (owner
) *owner
= obj2gco(f
->upvals
[n
- 1]);
1376 name
= p
->upvalues
[n
-1].name
;
1377 return (name
== NULL
) ? "(no name)" : getstr(name
);
1379 default: return NULL
; /* not a closure */
1384 LUA_API
const char *lua_getupvalue (lua_State
*L
, int funcindex
, int n
) {
1386 TValue
*val
= NULL
; /* to avoid warnings */
1388 name
= aux_upvalue(index2value(L
, funcindex
), n
, &val
, NULL
);
1390 setobj2s(L
, L
->top
.p
, val
);
1398 LUA_API
const char *lua_setupvalue (lua_State
*L
, int funcindex
, int n
) {
1400 TValue
*val
= NULL
; /* to avoid warnings */
1401 GCObject
*owner
= NULL
; /* to avoid warnings */
1404 fi
= index2value(L
, funcindex
);
1405 api_checknelems(L
, 1);
1406 name
= aux_upvalue(fi
, n
, &val
, &owner
);
1409 setobj(L
, val
, s2v(L
->top
.p
));
1410 luaC_barrier(L
, owner
, val
);
1417 static UpVal
**getupvalref (lua_State
*L
, int fidx
, int n
, LClosure
**pf
) {
1418 static const UpVal
*const nullup
= NULL
;
1420 TValue
*fi
= index2value(L
, fidx
);
1421 api_check(L
, ttisLclosure(fi
), "Lua function expected");
1424 if (1 <= n
&& n
<= f
->p
->sizeupvalues
)
1425 return &f
->upvals
[n
- 1]; /* get its upvalue pointer */
1427 return (UpVal
**)&nullup
;
1431 LUA_API
void *lua_upvalueid (lua_State
*L
, int fidx
, int n
) {
1432 TValue
*fi
= index2value(L
, fidx
);
1433 switch (ttypetag(fi
)) {
1434 case LUA_VLCL
: { /* lua closure */
1435 return *getupvalref(L
, fidx
, n
, NULL
);
1437 case LUA_VCCL
: { /* C closure */
1438 CClosure
*f
= clCvalue(fi
);
1439 if (1 <= n
&& n
<= f
->nupvalues
)
1440 return &f
->upvalue
[n
- 1];
1444 return NULL
; /* light C functions have no upvalues */
1446 api_check(L
, 0, "function expected");
1453 LUA_API
void lua_upvaluejoin (lua_State
*L
, int fidx1
, int n1
,
1454 int fidx2
, int n2
) {
1456 UpVal
**up1
= getupvalref(L
, fidx1
, n1
, &f1
);
1457 UpVal
**up2
= getupvalref(L
, fidx2
, n2
, NULL
);
1458 api_check(L
, *up1
!= NULL
&& *up2
!= NULL
, "invalid upvalue index");
1460 luaC_objbarrier(L
, f1
, *up1
);