1*0a6a1f1dSLionel Sambuc /* $NetBSD: lapi.c,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc ** Id: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp
511be35a1SLionel Sambuc ** Lua API
611be35a1SLionel Sambuc ** See Copyright Notice in lua.h
711be35a1SLionel Sambuc */
811be35a1SLionel Sambuc
911be35a1SLionel Sambuc #define lapi_c
1011be35a1SLionel Sambuc #define LUA_CORE
1111be35a1SLionel Sambuc
12*0a6a1f1dSLionel Sambuc #include "lprefix.h"
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc
15*0a6a1f1dSLionel Sambuc #include <stdarg.h>
16*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
17*0a6a1f1dSLionel Sambuc #include <string.h>
18*0a6a1f1dSLionel Sambuc #endif
19*0a6a1f1dSLionel Sambuc
2011be35a1SLionel Sambuc #include "lua.h"
2111be35a1SLionel Sambuc
2211be35a1SLionel Sambuc #include "lapi.h"
2311be35a1SLionel Sambuc #include "ldebug.h"
2411be35a1SLionel Sambuc #include "ldo.h"
2511be35a1SLionel Sambuc #include "lfunc.h"
2611be35a1SLionel Sambuc #include "lgc.h"
2711be35a1SLionel Sambuc #include "lmem.h"
2811be35a1SLionel Sambuc #include "lobject.h"
2911be35a1SLionel Sambuc #include "lstate.h"
3011be35a1SLionel Sambuc #include "lstring.h"
3111be35a1SLionel Sambuc #include "ltable.h"
3211be35a1SLionel Sambuc #include "ltm.h"
3311be35a1SLionel Sambuc #include "lundump.h"
3411be35a1SLionel Sambuc #include "lvm.h"
3511be35a1SLionel Sambuc
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc const char lua_ident[] =
39*0a6a1f1dSLionel Sambuc "$LuaVersion: " LUA_COPYRIGHT " $"
40*0a6a1f1dSLionel Sambuc "$LuaAuthors: " LUA_AUTHORS " $";
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc
43*0a6a1f1dSLionel Sambuc /* value at a non-valid index */
44*0a6a1f1dSLionel Sambuc #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
4511be35a1SLionel Sambuc
46*0a6a1f1dSLionel Sambuc /* corresponding test */
47*0a6a1f1dSLionel Sambuc #define isvalid(o) ((o) != luaO_nilobject)
4811be35a1SLionel Sambuc
49*0a6a1f1dSLionel Sambuc /* test for pseudo index */
50*0a6a1f1dSLionel Sambuc #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
5111be35a1SLionel Sambuc
52*0a6a1f1dSLionel Sambuc /* test for upvalue */
53*0a6a1f1dSLionel Sambuc #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc /* test for valid but not pseudo index */
56*0a6a1f1dSLionel Sambuc #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc #define api_checkstackindex(l, i, o) \
61*0a6a1f1dSLionel Sambuc api_check(l, isstackindex(i, o), "index not in the stack")
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc
index2addr(lua_State * L,int idx)64*0a6a1f1dSLionel Sambuc static TValue *index2addr (lua_State *L, int idx) {
65*0a6a1f1dSLionel Sambuc CallInfo *ci = L->ci;
6611be35a1SLionel Sambuc if (idx > 0) {
67*0a6a1f1dSLionel Sambuc TValue *o = ci->func + idx;
68*0a6a1f1dSLionel Sambuc api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
69*0a6a1f1dSLionel Sambuc if (o >= L->top) return NONVALIDVALUE;
7011be35a1SLionel Sambuc else return o;
7111be35a1SLionel Sambuc }
72*0a6a1f1dSLionel Sambuc else if (!ispseudo(idx)) { /* negative index */
73*0a6a1f1dSLionel Sambuc api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
7411be35a1SLionel Sambuc return L->top + idx;
7511be35a1SLionel Sambuc }
76*0a6a1f1dSLionel Sambuc else if (idx == LUA_REGISTRYINDEX)
77*0a6a1f1dSLionel Sambuc return &G(L)->l_registry;
78*0a6a1f1dSLionel Sambuc else { /* upvalues */
79*0a6a1f1dSLionel Sambuc idx = LUA_REGISTRYINDEX - idx;
80*0a6a1f1dSLionel Sambuc api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
81*0a6a1f1dSLionel Sambuc if (ttislcf(ci->func)) /* light C function? */
82*0a6a1f1dSLionel Sambuc return NONVALIDVALUE; /* it has no upvalues */
8311be35a1SLionel Sambuc else {
84*0a6a1f1dSLionel Sambuc CClosure *func = clCvalue(ci->func);
85*0a6a1f1dSLionel Sambuc return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
86*0a6a1f1dSLionel Sambuc }
8711be35a1SLionel Sambuc }
8811be35a1SLionel Sambuc }
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc
91*0a6a1f1dSLionel Sambuc /*
92*0a6a1f1dSLionel Sambuc ** to be called by 'lua_checkstack' in protected mode, to grow stack
93*0a6a1f1dSLionel Sambuc ** capturing memory errors
94*0a6a1f1dSLionel Sambuc */
growstack(lua_State * L,void * ud)95*0a6a1f1dSLionel Sambuc static void growstack (lua_State *L, void *ud) {
96*0a6a1f1dSLionel Sambuc int size = *(int *)ud;
97*0a6a1f1dSLionel Sambuc luaD_growstack(L, size);
9811be35a1SLionel Sambuc }
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc
lua_checkstack(lua_State * L,int n)101*0a6a1f1dSLionel Sambuc LUA_API int lua_checkstack (lua_State *L, int n) {
102*0a6a1f1dSLionel Sambuc int res;
103*0a6a1f1dSLionel Sambuc CallInfo *ci = L->ci;
10411be35a1SLionel Sambuc lua_lock(L);
105*0a6a1f1dSLionel Sambuc api_check(L, n >= 0, "negative 'n'");
106*0a6a1f1dSLionel Sambuc if (L->stack_last - L->top > n) /* stack large enough? */
107*0a6a1f1dSLionel Sambuc res = 1; /* yes; check is OK */
108*0a6a1f1dSLionel Sambuc else { /* no; need to grow stack */
109*0a6a1f1dSLionel Sambuc int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
110*0a6a1f1dSLionel Sambuc if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
111*0a6a1f1dSLionel Sambuc res = 0; /* no */
112*0a6a1f1dSLionel Sambuc else /* try to grow stack */
113*0a6a1f1dSLionel Sambuc res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
11411be35a1SLionel Sambuc }
115*0a6a1f1dSLionel Sambuc if (res && ci->top < L->top + n)
116*0a6a1f1dSLionel Sambuc ci->top = L->top + n; /* adjust frame top */
11711be35a1SLionel Sambuc lua_unlock(L);
11811be35a1SLionel Sambuc return res;
11911be35a1SLionel Sambuc }
12011be35a1SLionel Sambuc
12111be35a1SLionel Sambuc
lua_xmove(lua_State * from,lua_State * to,int n)12211be35a1SLionel Sambuc LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
12311be35a1SLionel Sambuc int i;
12411be35a1SLionel Sambuc if (from == to) return;
12511be35a1SLionel Sambuc lua_lock(to);
12611be35a1SLionel Sambuc api_checknelems(from, n);
127*0a6a1f1dSLionel Sambuc api_check(from, G(from) == G(to), "moving among independent states");
128*0a6a1f1dSLionel Sambuc api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
12911be35a1SLionel Sambuc from->top -= n;
13011be35a1SLionel Sambuc for (i = 0; i < n; i++) {
131*0a6a1f1dSLionel Sambuc setobj2s(to, to->top, from->top + i);
132*0a6a1f1dSLionel Sambuc api_incr_top(to);
13311be35a1SLionel Sambuc }
13411be35a1SLionel Sambuc lua_unlock(to);
13511be35a1SLionel Sambuc }
13611be35a1SLionel Sambuc
13711be35a1SLionel Sambuc
lua_atpanic(lua_State * L,lua_CFunction panicf)13811be35a1SLionel Sambuc LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
13911be35a1SLionel Sambuc lua_CFunction old;
14011be35a1SLionel Sambuc lua_lock(L);
14111be35a1SLionel Sambuc old = G(L)->panic;
14211be35a1SLionel Sambuc G(L)->panic = panicf;
14311be35a1SLionel Sambuc lua_unlock(L);
14411be35a1SLionel Sambuc return old;
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc
lua_version(lua_State * L)148*0a6a1f1dSLionel Sambuc LUA_API const lua_Number *lua_version (lua_State *L) {
149*0a6a1f1dSLionel Sambuc static const lua_Number version = LUA_VERSION_NUM;
150*0a6a1f1dSLionel Sambuc if (L == NULL) return &version;
151*0a6a1f1dSLionel Sambuc else return G(L)->version;
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc
15511be35a1SLionel Sambuc
15611be35a1SLionel Sambuc /*
15711be35a1SLionel Sambuc ** basic stack manipulation
15811be35a1SLionel Sambuc */
15911be35a1SLionel Sambuc
16011be35a1SLionel Sambuc
161*0a6a1f1dSLionel Sambuc /*
162*0a6a1f1dSLionel Sambuc ** convert an acceptable stack index into an absolute index
163*0a6a1f1dSLionel Sambuc */
lua_absindex(lua_State * L,int idx)164*0a6a1f1dSLionel Sambuc LUA_API int lua_absindex (lua_State *L, int idx) {
165*0a6a1f1dSLionel Sambuc return (idx > 0 || ispseudo(idx))
166*0a6a1f1dSLionel Sambuc ? idx
167*0a6a1f1dSLionel Sambuc : cast_int(L->top - L->ci->func) + idx;
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc
170*0a6a1f1dSLionel Sambuc
lua_gettop(lua_State * L)17111be35a1SLionel Sambuc LUA_API int lua_gettop (lua_State *L) {
172*0a6a1f1dSLionel Sambuc return cast_int(L->top - (L->ci->func + 1));
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc
17511be35a1SLionel Sambuc
lua_settop(lua_State * L,int idx)17611be35a1SLionel Sambuc LUA_API void lua_settop (lua_State *L, int idx) {
177*0a6a1f1dSLionel Sambuc StkId func = L->ci->func;
17811be35a1SLionel Sambuc lua_lock(L);
17911be35a1SLionel Sambuc if (idx >= 0) {
180*0a6a1f1dSLionel Sambuc api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
181*0a6a1f1dSLionel Sambuc while (L->top < (func + 1) + idx)
18211be35a1SLionel Sambuc setnilvalue(L->top++);
183*0a6a1f1dSLionel Sambuc L->top = (func + 1) + idx;
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc else {
186*0a6a1f1dSLionel Sambuc api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
187*0a6a1f1dSLionel Sambuc L->top += idx+1; /* 'subtract' index (index is negative) */
18811be35a1SLionel Sambuc }
18911be35a1SLionel Sambuc lua_unlock(L);
19011be35a1SLionel Sambuc }
19111be35a1SLionel Sambuc
19211be35a1SLionel Sambuc
193*0a6a1f1dSLionel Sambuc /*
194*0a6a1f1dSLionel Sambuc ** Reverse the stack segment from 'from' to 'to'
195*0a6a1f1dSLionel Sambuc ** (auxiliary to 'lua_rotate')
196*0a6a1f1dSLionel Sambuc */
reverse(lua_State * L,StkId from,StkId to)197*0a6a1f1dSLionel Sambuc static void reverse (lua_State *L, StkId from, StkId to) {
198*0a6a1f1dSLionel Sambuc for (; from < to; from++, to--) {
199*0a6a1f1dSLionel Sambuc TValue temp;
200*0a6a1f1dSLionel Sambuc setobj(L, &temp, from);
201*0a6a1f1dSLionel Sambuc setobjs2s(L, from, to);
202*0a6a1f1dSLionel Sambuc setobj2s(L, to, &temp);
203*0a6a1f1dSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc /*
208*0a6a1f1dSLionel Sambuc ** Let x = AB, where A is a prefix of length 'n'. Then,
209*0a6a1f1dSLionel Sambuc ** rotate x n == BA. But BA == (A^r . B^r)^r.
210*0a6a1f1dSLionel Sambuc */
lua_rotate(lua_State * L,int idx,int n)211*0a6a1f1dSLionel Sambuc LUA_API void lua_rotate (lua_State *L, int idx, int n) {
212*0a6a1f1dSLionel Sambuc StkId p, t, m;
21311be35a1SLionel Sambuc lua_lock(L);
214*0a6a1f1dSLionel Sambuc t = L->top - 1; /* end of stack segment being rotated */
215*0a6a1f1dSLionel Sambuc p = index2addr(L, idx); /* start of segment */
216*0a6a1f1dSLionel Sambuc api_checkstackindex(L, idx, p);
217*0a6a1f1dSLionel Sambuc api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
218*0a6a1f1dSLionel Sambuc m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
219*0a6a1f1dSLionel Sambuc reverse(L, p, m); /* reverse the prefix with length 'n' */
220*0a6a1f1dSLionel Sambuc reverse(L, m + 1, t); /* reverse the suffix */
221*0a6a1f1dSLionel Sambuc reverse(L, p, t); /* reverse the entire segment */
22211be35a1SLionel Sambuc lua_unlock(L);
22311be35a1SLionel Sambuc }
22411be35a1SLionel Sambuc
22511be35a1SLionel Sambuc
lua_copy(lua_State * L,int fromidx,int toidx)226*0a6a1f1dSLionel Sambuc LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
227*0a6a1f1dSLionel Sambuc TValue *fr, *to;
22811be35a1SLionel Sambuc lua_lock(L);
229*0a6a1f1dSLionel Sambuc fr = index2addr(L, fromidx);
230*0a6a1f1dSLionel Sambuc to = index2addr(L, toidx);
231*0a6a1f1dSLionel Sambuc api_checkvalidindex(L, to);
232*0a6a1f1dSLionel Sambuc setobj(L, to, fr);
233*0a6a1f1dSLionel Sambuc if (isupvalue(toidx)) /* function upvalue? */
234*0a6a1f1dSLionel Sambuc luaC_barrier(L, clCvalue(L->ci->func), fr);
235*0a6a1f1dSLionel Sambuc /* LUA_REGISTRYINDEX does not need gc barrier
236*0a6a1f1dSLionel Sambuc (collector revisits it before finishing collection) */
23711be35a1SLionel Sambuc lua_unlock(L);
23811be35a1SLionel Sambuc }
23911be35a1SLionel Sambuc
24011be35a1SLionel Sambuc
lua_pushvalue(lua_State * L,int idx)24111be35a1SLionel Sambuc LUA_API void lua_pushvalue (lua_State *L, int idx) {
24211be35a1SLionel Sambuc lua_lock(L);
243*0a6a1f1dSLionel Sambuc setobj2s(L, L->top, index2addr(L, idx));
24411be35a1SLionel Sambuc api_incr_top(L);
24511be35a1SLionel Sambuc lua_unlock(L);
24611be35a1SLionel Sambuc }
24711be35a1SLionel Sambuc
24811be35a1SLionel Sambuc
24911be35a1SLionel Sambuc
25011be35a1SLionel Sambuc /*
25111be35a1SLionel Sambuc ** access functions (stack -> C)
25211be35a1SLionel Sambuc */
25311be35a1SLionel Sambuc
25411be35a1SLionel Sambuc
lua_type(lua_State * L,int idx)25511be35a1SLionel Sambuc LUA_API int lua_type (lua_State *L, int idx) {
256*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
257*0a6a1f1dSLionel Sambuc return (isvalid(o) ? ttnov(o) : LUA_TNONE);
25811be35a1SLionel Sambuc }
25911be35a1SLionel Sambuc
26011be35a1SLionel Sambuc
lua_typename(lua_State * L,int t)26111be35a1SLionel Sambuc LUA_API const char *lua_typename (lua_State *L, int t) {
26211be35a1SLionel Sambuc UNUSED(L);
263*0a6a1f1dSLionel Sambuc api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
264*0a6a1f1dSLionel Sambuc return ttypename(t);
26511be35a1SLionel Sambuc }
26611be35a1SLionel Sambuc
26711be35a1SLionel Sambuc
lua_iscfunction(lua_State * L,int idx)26811be35a1SLionel Sambuc LUA_API int lua_iscfunction (lua_State *L, int idx) {
269*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
270*0a6a1f1dSLionel Sambuc return (ttislcf(o) || (ttisCclosure(o)));
271*0a6a1f1dSLionel Sambuc }
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc
lua_isinteger(lua_State * L,int idx)274*0a6a1f1dSLionel Sambuc LUA_API int lua_isinteger (lua_State *L, int idx) {
275*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
276*0a6a1f1dSLionel Sambuc return ttisinteger(o);
27711be35a1SLionel Sambuc }
27811be35a1SLionel Sambuc
27911be35a1SLionel Sambuc
lua_isnumber(lua_State * L,int idx)28011be35a1SLionel Sambuc LUA_API int lua_isnumber (lua_State *L, int idx) {
281*0a6a1f1dSLionel Sambuc lua_Number n;
282*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
28311be35a1SLionel Sambuc return tonumber(o, &n);
28411be35a1SLionel Sambuc }
28511be35a1SLionel Sambuc
28611be35a1SLionel Sambuc
lua_isstring(lua_State * L,int idx)28711be35a1SLionel Sambuc LUA_API int lua_isstring (lua_State *L, int idx) {
288*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
289*0a6a1f1dSLionel Sambuc return (ttisstring(o) || cvt2str(o));
29011be35a1SLionel Sambuc }
29111be35a1SLionel Sambuc
29211be35a1SLionel Sambuc
lua_isuserdata(lua_State * L,int idx)29311be35a1SLionel Sambuc LUA_API int lua_isuserdata (lua_State *L, int idx) {
294*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
295*0a6a1f1dSLionel Sambuc return (ttisfulluserdata(o) || ttislightuserdata(o));
29611be35a1SLionel Sambuc }
29711be35a1SLionel Sambuc
29811be35a1SLionel Sambuc
lua_rawequal(lua_State * L,int index1,int index2)29911be35a1SLionel Sambuc LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
300*0a6a1f1dSLionel Sambuc StkId o1 = index2addr(L, index1);
301*0a6a1f1dSLionel Sambuc StkId o2 = index2addr(L, index2);
302*0a6a1f1dSLionel Sambuc return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
30311be35a1SLionel Sambuc }
30411be35a1SLionel Sambuc
30511be35a1SLionel Sambuc
lua_arith(lua_State * L,int op)306*0a6a1f1dSLionel Sambuc LUA_API void lua_arith (lua_State *L, int op) {
307*0a6a1f1dSLionel Sambuc lua_lock(L);
308*0a6a1f1dSLionel Sambuc if (op != LUA_OPUNM && op != LUA_OPBNOT)
309*0a6a1f1dSLionel Sambuc api_checknelems(L, 2); /* all other operations expect two operands */
310*0a6a1f1dSLionel Sambuc else { /* for unary operations, add fake 2nd operand */
311*0a6a1f1dSLionel Sambuc api_checknelems(L, 1);
312*0a6a1f1dSLionel Sambuc setobjs2s(L, L->top, L->top - 1);
313*0a6a1f1dSLionel Sambuc api_incr_top(L);
314*0a6a1f1dSLionel Sambuc }
315*0a6a1f1dSLionel Sambuc /* first operand at top - 2, second at top - 1; result go to top - 2 */
316*0a6a1f1dSLionel Sambuc luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
317*0a6a1f1dSLionel Sambuc L->top--; /* remove second operand */
318*0a6a1f1dSLionel Sambuc lua_unlock(L);
319*0a6a1f1dSLionel Sambuc }
320*0a6a1f1dSLionel Sambuc
321*0a6a1f1dSLionel Sambuc
lua_compare(lua_State * L,int index1,int index2,int op)322*0a6a1f1dSLionel Sambuc LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
32311be35a1SLionel Sambuc StkId o1, o2;
324*0a6a1f1dSLionel Sambuc int i = 0;
32511be35a1SLionel Sambuc lua_lock(L); /* may call tag method */
326*0a6a1f1dSLionel Sambuc o1 = index2addr(L, index1);
327*0a6a1f1dSLionel Sambuc o2 = index2addr(L, index2);
328*0a6a1f1dSLionel Sambuc if (isvalid(o1) && isvalid(o2)) {
329*0a6a1f1dSLionel Sambuc switch (op) {
330*0a6a1f1dSLionel Sambuc case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
331*0a6a1f1dSLionel Sambuc case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
332*0a6a1f1dSLionel Sambuc case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
333*0a6a1f1dSLionel Sambuc default: api_check(L, 0, "invalid option");
334*0a6a1f1dSLionel Sambuc }
335*0a6a1f1dSLionel Sambuc }
33611be35a1SLionel Sambuc lua_unlock(L);
33711be35a1SLionel Sambuc return i;
33811be35a1SLionel Sambuc }
33911be35a1SLionel Sambuc
34011be35a1SLionel Sambuc
lua_stringtonumber(lua_State * L,const char * s)341*0a6a1f1dSLionel Sambuc LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
342*0a6a1f1dSLionel Sambuc size_t sz = luaO_str2num(s, L->top);
343*0a6a1f1dSLionel Sambuc if (sz != 0)
344*0a6a1f1dSLionel Sambuc api_incr_top(L);
345*0a6a1f1dSLionel Sambuc return sz;
34611be35a1SLionel Sambuc }
34711be35a1SLionel Sambuc
34811be35a1SLionel Sambuc
349*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
lua_tonumberx(lua_State * L,int idx,int * pisnum)350*0a6a1f1dSLionel Sambuc LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
351*0a6a1f1dSLionel Sambuc lua_Number n;
352*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
353*0a6a1f1dSLionel Sambuc int isnum = tonumber(o, &n);
354*0a6a1f1dSLionel Sambuc if (!isnum)
355*0a6a1f1dSLionel Sambuc n = 0; /* call to 'tonumber' may change 'n' even if it fails */
356*0a6a1f1dSLionel Sambuc if (pisnum) *pisnum = isnum;
357*0a6a1f1dSLionel Sambuc return n;
35811be35a1SLionel Sambuc }
359*0a6a1f1dSLionel Sambuc #endif
36011be35a1SLionel Sambuc
36111be35a1SLionel Sambuc
lua_tointegerx(lua_State * L,int idx,int * pisnum)362*0a6a1f1dSLionel Sambuc LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
36311be35a1SLionel Sambuc lua_Integer res;
364*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
365*0a6a1f1dSLionel Sambuc int isnum = tointeger(o, &res);
366*0a6a1f1dSLionel Sambuc if (!isnum)
367*0a6a1f1dSLionel Sambuc res = 0; /* call to 'tointeger' may change 'n' even if it fails */
368*0a6a1f1dSLionel Sambuc if (pisnum) *pisnum = isnum;
36911be35a1SLionel Sambuc return res;
37011be35a1SLionel Sambuc }
37111be35a1SLionel Sambuc
37211be35a1SLionel Sambuc
lua_toboolean(lua_State * L,int idx)37311be35a1SLionel Sambuc LUA_API int lua_toboolean (lua_State *L, int idx) {
374*0a6a1f1dSLionel Sambuc const TValue *o = index2addr(L, idx);
37511be35a1SLionel Sambuc return !l_isfalse(o);
37611be35a1SLionel Sambuc }
37711be35a1SLionel Sambuc
37811be35a1SLionel Sambuc
lua_tolstring(lua_State * L,int idx,size_t * len)37911be35a1SLionel Sambuc LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
380*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
38111be35a1SLionel Sambuc if (!ttisstring(o)) {
382*0a6a1f1dSLionel Sambuc if (!cvt2str(o)) { /* not convertible? */
38311be35a1SLionel Sambuc if (len != NULL) *len = 0;
38411be35a1SLionel Sambuc return NULL;
38511be35a1SLionel Sambuc }
386*0a6a1f1dSLionel Sambuc lua_lock(L); /* 'luaO_tostring' may create a new string */
38711be35a1SLionel Sambuc luaC_checkGC(L);
388*0a6a1f1dSLionel Sambuc o = index2addr(L, idx); /* previous call may reallocate the stack */
389*0a6a1f1dSLionel Sambuc luaO_tostring(L, o);
39011be35a1SLionel Sambuc lua_unlock(L);
39111be35a1SLionel Sambuc }
392*0a6a1f1dSLionel Sambuc if (len != NULL)
393*0a6a1f1dSLionel Sambuc *len = vslen(o);
39411be35a1SLionel Sambuc return svalue(o);
39511be35a1SLionel Sambuc }
39611be35a1SLionel Sambuc
39711be35a1SLionel Sambuc
lua_rawlen(lua_State * L,int idx)398*0a6a1f1dSLionel Sambuc LUA_API size_t lua_rawlen (lua_State *L, int idx) {
399*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
40011be35a1SLionel Sambuc switch (ttype(o)) {
401*0a6a1f1dSLionel Sambuc case LUA_TSHRSTR: return tsvalue(o)->shrlen;
402*0a6a1f1dSLionel Sambuc case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
40311be35a1SLionel Sambuc case LUA_TUSERDATA: return uvalue(o)->len;
40411be35a1SLionel Sambuc case LUA_TTABLE: return luaH_getn(hvalue(o));
40511be35a1SLionel Sambuc default: return 0;
40611be35a1SLionel Sambuc }
40711be35a1SLionel Sambuc }
40811be35a1SLionel Sambuc
40911be35a1SLionel Sambuc
lua_tocfunction(lua_State * L,int idx)41011be35a1SLionel Sambuc LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
411*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
412*0a6a1f1dSLionel Sambuc if (ttislcf(o)) return fvalue(o);
413*0a6a1f1dSLionel Sambuc else if (ttisCclosure(o))
414*0a6a1f1dSLionel Sambuc return clCvalue(o)->f;
415*0a6a1f1dSLionel Sambuc else return NULL; /* not a C function */
41611be35a1SLionel Sambuc }
41711be35a1SLionel Sambuc
41811be35a1SLionel Sambuc
lua_touserdata(lua_State * L,int idx)41911be35a1SLionel Sambuc LUA_API void *lua_touserdata (lua_State *L, int idx) {
420*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
421*0a6a1f1dSLionel Sambuc switch (ttnov(o)) {
422*0a6a1f1dSLionel Sambuc case LUA_TUSERDATA: return getudatamem(uvalue(o));
42311be35a1SLionel Sambuc case LUA_TLIGHTUSERDATA: return pvalue(o);
42411be35a1SLionel Sambuc default: return NULL;
42511be35a1SLionel Sambuc }
42611be35a1SLionel Sambuc }
42711be35a1SLionel Sambuc
42811be35a1SLionel Sambuc
lua_tothread(lua_State * L,int idx)42911be35a1SLionel Sambuc LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
430*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
43111be35a1SLionel Sambuc return (!ttisthread(o)) ? NULL : thvalue(o);
43211be35a1SLionel Sambuc }
43311be35a1SLionel Sambuc
43411be35a1SLionel Sambuc
lua_topointer(lua_State * L,int idx)43511be35a1SLionel Sambuc LUA_API const void *lua_topointer (lua_State *L, int idx) {
436*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, idx);
43711be35a1SLionel Sambuc switch (ttype(o)) {
43811be35a1SLionel Sambuc case LUA_TTABLE: return hvalue(o);
439*0a6a1f1dSLionel Sambuc case LUA_TLCL: return clLvalue(o);
440*0a6a1f1dSLionel Sambuc case LUA_TCCL: return clCvalue(o);
441*0a6a1f1dSLionel Sambuc case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
44211be35a1SLionel Sambuc case LUA_TTHREAD: return thvalue(o);
443*0a6a1f1dSLionel Sambuc case LUA_TUSERDATA: return getudatamem(uvalue(o));
444*0a6a1f1dSLionel Sambuc case LUA_TLIGHTUSERDATA: return pvalue(o);
44511be35a1SLionel Sambuc default: return NULL;
44611be35a1SLionel Sambuc }
44711be35a1SLionel Sambuc }
44811be35a1SLionel Sambuc
44911be35a1SLionel Sambuc
45011be35a1SLionel Sambuc
45111be35a1SLionel Sambuc /*
45211be35a1SLionel Sambuc ** push functions (C -> stack)
45311be35a1SLionel Sambuc */
45411be35a1SLionel Sambuc
45511be35a1SLionel Sambuc
lua_pushnil(lua_State * L)45611be35a1SLionel Sambuc LUA_API void lua_pushnil (lua_State *L) {
45711be35a1SLionel Sambuc lua_lock(L);
45811be35a1SLionel Sambuc setnilvalue(L->top);
45911be35a1SLionel Sambuc api_incr_top(L);
46011be35a1SLionel Sambuc lua_unlock(L);
46111be35a1SLionel Sambuc }
46211be35a1SLionel Sambuc
46311be35a1SLionel Sambuc
464*0a6a1f1dSLionel Sambuc #ifndef _KERNEL
lua_pushnumber(lua_State * L,lua_Number n)46511be35a1SLionel Sambuc LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
46611be35a1SLionel Sambuc lua_lock(L);
467*0a6a1f1dSLionel Sambuc setfltvalue(L->top, n);
46811be35a1SLionel Sambuc api_incr_top(L);
46911be35a1SLionel Sambuc lua_unlock(L);
47011be35a1SLionel Sambuc }
471*0a6a1f1dSLionel Sambuc #endif
47211be35a1SLionel Sambuc
47311be35a1SLionel Sambuc
lua_pushinteger(lua_State * L,lua_Integer n)47411be35a1SLionel Sambuc LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
47511be35a1SLionel Sambuc lua_lock(L);
476*0a6a1f1dSLionel Sambuc setivalue(L->top, n);
47711be35a1SLionel Sambuc api_incr_top(L);
47811be35a1SLionel Sambuc lua_unlock(L);
47911be35a1SLionel Sambuc }
48011be35a1SLionel Sambuc
48111be35a1SLionel Sambuc
lua_pushlstring(lua_State * L,const char * s,size_t len)482*0a6a1f1dSLionel Sambuc LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
483*0a6a1f1dSLionel Sambuc TString *ts;
48411be35a1SLionel Sambuc lua_lock(L);
48511be35a1SLionel Sambuc luaC_checkGC(L);
486*0a6a1f1dSLionel Sambuc ts = luaS_newlstr(L, s, len);
487*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, ts);
48811be35a1SLionel Sambuc api_incr_top(L);
48911be35a1SLionel Sambuc lua_unlock(L);
490*0a6a1f1dSLionel Sambuc return getstr(ts);
49111be35a1SLionel Sambuc }
49211be35a1SLionel Sambuc
49311be35a1SLionel Sambuc
lua_pushstring(lua_State * L,const char * s)494*0a6a1f1dSLionel Sambuc LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
495*0a6a1f1dSLionel Sambuc lua_lock(L);
49611be35a1SLionel Sambuc if (s == NULL)
497*0a6a1f1dSLionel Sambuc setnilvalue(L->top);
498*0a6a1f1dSLionel Sambuc else {
499*0a6a1f1dSLionel Sambuc TString *ts;
500*0a6a1f1dSLionel Sambuc luaC_checkGC(L);
501*0a6a1f1dSLionel Sambuc ts = luaS_new(L, s);
502*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, ts);
503*0a6a1f1dSLionel Sambuc s = getstr(ts); /* internal copy's address */
504*0a6a1f1dSLionel Sambuc }
505*0a6a1f1dSLionel Sambuc api_incr_top(L);
506*0a6a1f1dSLionel Sambuc lua_unlock(L);
507*0a6a1f1dSLionel Sambuc return s;
50811be35a1SLionel Sambuc }
50911be35a1SLionel Sambuc
51011be35a1SLionel Sambuc
lua_pushvfstring(lua_State * L,const char * fmt,va_list argp)51111be35a1SLionel Sambuc LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
51211be35a1SLionel Sambuc va_list argp) {
51311be35a1SLionel Sambuc const char *ret;
51411be35a1SLionel Sambuc lua_lock(L);
51511be35a1SLionel Sambuc luaC_checkGC(L);
51611be35a1SLionel Sambuc ret = luaO_pushvfstring(L, fmt, argp);
51711be35a1SLionel Sambuc lua_unlock(L);
51811be35a1SLionel Sambuc return ret;
51911be35a1SLionel Sambuc }
52011be35a1SLionel Sambuc
52111be35a1SLionel Sambuc
lua_pushfstring(lua_State * L,const char * fmt,...)52211be35a1SLionel Sambuc LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
52311be35a1SLionel Sambuc const char *ret;
52411be35a1SLionel Sambuc va_list argp;
52511be35a1SLionel Sambuc lua_lock(L);
52611be35a1SLionel Sambuc luaC_checkGC(L);
52711be35a1SLionel Sambuc va_start(argp, fmt);
52811be35a1SLionel Sambuc ret = luaO_pushvfstring(L, fmt, argp);
52911be35a1SLionel Sambuc va_end(argp);
53011be35a1SLionel Sambuc lua_unlock(L);
53111be35a1SLionel Sambuc return ret;
53211be35a1SLionel Sambuc }
53311be35a1SLionel Sambuc
53411be35a1SLionel Sambuc
lua_pushcclosure(lua_State * L,lua_CFunction fn,int n)53511be35a1SLionel Sambuc LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
53611be35a1SLionel Sambuc lua_lock(L);
537*0a6a1f1dSLionel Sambuc if (n == 0) {
538*0a6a1f1dSLionel Sambuc setfvalue(L->top, fn);
539*0a6a1f1dSLionel Sambuc }
540*0a6a1f1dSLionel Sambuc else {
541*0a6a1f1dSLionel Sambuc CClosure *cl;
54211be35a1SLionel Sambuc api_checknelems(L, n);
543*0a6a1f1dSLionel Sambuc api_check(L, n <= MAXUPVAL, "upvalue index too large");
544*0a6a1f1dSLionel Sambuc luaC_checkGC(L);
545*0a6a1f1dSLionel Sambuc cl = luaF_newCclosure(L, n);
546*0a6a1f1dSLionel Sambuc cl->f = fn;
54711be35a1SLionel Sambuc L->top -= n;
548*0a6a1f1dSLionel Sambuc while (n--) {
549*0a6a1f1dSLionel Sambuc setobj2n(L, &cl->upvalue[n], L->top + n);
550*0a6a1f1dSLionel Sambuc /* does not need barrier because closure is white */
551*0a6a1f1dSLionel Sambuc }
552*0a6a1f1dSLionel Sambuc setclCvalue(L, L->top, cl);
553*0a6a1f1dSLionel Sambuc }
55411be35a1SLionel Sambuc api_incr_top(L);
55511be35a1SLionel Sambuc lua_unlock(L);
55611be35a1SLionel Sambuc }
55711be35a1SLionel Sambuc
55811be35a1SLionel Sambuc
lua_pushboolean(lua_State * L,int b)55911be35a1SLionel Sambuc LUA_API void lua_pushboolean (lua_State *L, int b) {
56011be35a1SLionel Sambuc lua_lock(L);
56111be35a1SLionel Sambuc setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
56211be35a1SLionel Sambuc api_incr_top(L);
56311be35a1SLionel Sambuc lua_unlock(L);
56411be35a1SLionel Sambuc }
56511be35a1SLionel Sambuc
56611be35a1SLionel Sambuc
lua_pushlightuserdata(lua_State * L,void * p)56711be35a1SLionel Sambuc LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
56811be35a1SLionel Sambuc lua_lock(L);
56911be35a1SLionel Sambuc setpvalue(L->top, p);
57011be35a1SLionel Sambuc api_incr_top(L);
57111be35a1SLionel Sambuc lua_unlock(L);
57211be35a1SLionel Sambuc }
57311be35a1SLionel Sambuc
57411be35a1SLionel Sambuc
lua_pushthread(lua_State * L)57511be35a1SLionel Sambuc LUA_API int lua_pushthread (lua_State *L) {
57611be35a1SLionel Sambuc lua_lock(L);
57711be35a1SLionel Sambuc setthvalue(L, L->top, L);
57811be35a1SLionel Sambuc api_incr_top(L);
57911be35a1SLionel Sambuc lua_unlock(L);
58011be35a1SLionel Sambuc return (G(L)->mainthread == L);
58111be35a1SLionel Sambuc }
58211be35a1SLionel Sambuc
58311be35a1SLionel Sambuc
58411be35a1SLionel Sambuc
58511be35a1SLionel Sambuc /*
58611be35a1SLionel Sambuc ** get functions (Lua -> stack)
58711be35a1SLionel Sambuc */
58811be35a1SLionel Sambuc
58911be35a1SLionel Sambuc
lua_getglobal(lua_State * L,const char * name)590*0a6a1f1dSLionel Sambuc LUA_API int lua_getglobal (lua_State *L, const char *name) {
591*0a6a1f1dSLionel Sambuc Table *reg = hvalue(&G(L)->l_registry);
592*0a6a1f1dSLionel Sambuc const TValue *gt; /* global table */
593*0a6a1f1dSLionel Sambuc lua_lock(L);
594*0a6a1f1dSLionel Sambuc gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
595*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, luaS_new(L, name));
596*0a6a1f1dSLionel Sambuc api_incr_top(L);
597*0a6a1f1dSLionel Sambuc luaV_gettable(L, gt, L->top - 1, L->top - 1);
598*0a6a1f1dSLionel Sambuc lua_unlock(L);
599*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
600*0a6a1f1dSLionel Sambuc }
601*0a6a1f1dSLionel Sambuc
602*0a6a1f1dSLionel Sambuc
lua_gettable(lua_State * L,int idx)603*0a6a1f1dSLionel Sambuc LUA_API int lua_gettable (lua_State *L, int idx) {
60411be35a1SLionel Sambuc StkId t;
60511be35a1SLionel Sambuc lua_lock(L);
606*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
60711be35a1SLionel Sambuc luaV_gettable(L, t, L->top - 1, L->top - 1);
60811be35a1SLionel Sambuc lua_unlock(L);
609*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
61011be35a1SLionel Sambuc }
61111be35a1SLionel Sambuc
61211be35a1SLionel Sambuc
lua_getfield(lua_State * L,int idx,const char * k)613*0a6a1f1dSLionel Sambuc LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
61411be35a1SLionel Sambuc StkId t;
61511be35a1SLionel Sambuc lua_lock(L);
616*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
617*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, luaS_new(L, k));
61811be35a1SLionel Sambuc api_incr_top(L);
619*0a6a1f1dSLionel Sambuc luaV_gettable(L, t, L->top - 1, L->top - 1);
62011be35a1SLionel Sambuc lua_unlock(L);
621*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
62211be35a1SLionel Sambuc }
62311be35a1SLionel Sambuc
62411be35a1SLionel Sambuc
lua_geti(lua_State * L,int idx,lua_Integer n)625*0a6a1f1dSLionel Sambuc LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
62611be35a1SLionel Sambuc StkId t;
62711be35a1SLionel Sambuc lua_lock(L);
628*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
629*0a6a1f1dSLionel Sambuc setivalue(L->top, n);
630*0a6a1f1dSLionel Sambuc api_incr_top(L);
631*0a6a1f1dSLionel Sambuc luaV_gettable(L, t, L->top - 1, L->top - 1);
632*0a6a1f1dSLionel Sambuc lua_unlock(L);
633*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
634*0a6a1f1dSLionel Sambuc }
635*0a6a1f1dSLionel Sambuc
636*0a6a1f1dSLionel Sambuc
lua_rawget(lua_State * L,int idx)637*0a6a1f1dSLionel Sambuc LUA_API int lua_rawget (lua_State *L, int idx) {
638*0a6a1f1dSLionel Sambuc StkId t;
639*0a6a1f1dSLionel Sambuc lua_lock(L);
640*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
641*0a6a1f1dSLionel Sambuc api_check(L, ttistable(t), "table expected");
64211be35a1SLionel Sambuc setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
64311be35a1SLionel Sambuc lua_unlock(L);
644*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
64511be35a1SLionel Sambuc }
64611be35a1SLionel Sambuc
64711be35a1SLionel Sambuc
lua_rawgeti(lua_State * L,int idx,lua_Integer n)648*0a6a1f1dSLionel Sambuc LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
649*0a6a1f1dSLionel Sambuc StkId t;
65011be35a1SLionel Sambuc lua_lock(L);
651*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
652*0a6a1f1dSLionel Sambuc api_check(L, ttistable(t), "table expected");
653*0a6a1f1dSLionel Sambuc setobj2s(L, L->top, luaH_getint(hvalue(t), n));
65411be35a1SLionel Sambuc api_incr_top(L);
65511be35a1SLionel Sambuc lua_unlock(L);
656*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
657*0a6a1f1dSLionel Sambuc }
658*0a6a1f1dSLionel Sambuc
659*0a6a1f1dSLionel Sambuc
lua_rawgetp(lua_State * L,int idx,const void * p)660*0a6a1f1dSLionel Sambuc LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
661*0a6a1f1dSLionel Sambuc StkId t;
662*0a6a1f1dSLionel Sambuc TValue k;
663*0a6a1f1dSLionel Sambuc lua_lock(L);
664*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
665*0a6a1f1dSLionel Sambuc api_check(L, ttistable(t), "table expected");
666*0a6a1f1dSLionel Sambuc setpvalue(&k, cast(void *, p));
667*0a6a1f1dSLionel Sambuc setobj2s(L, L->top, luaH_get(hvalue(t), &k));
668*0a6a1f1dSLionel Sambuc api_incr_top(L);
669*0a6a1f1dSLionel Sambuc lua_unlock(L);
670*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
67111be35a1SLionel Sambuc }
67211be35a1SLionel Sambuc
67311be35a1SLionel Sambuc
lua_createtable(lua_State * L,int narray,int nrec)67411be35a1SLionel Sambuc LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
675*0a6a1f1dSLionel Sambuc Table *t;
67611be35a1SLionel Sambuc lua_lock(L);
67711be35a1SLionel Sambuc luaC_checkGC(L);
678*0a6a1f1dSLionel Sambuc t = luaH_new(L);
679*0a6a1f1dSLionel Sambuc sethvalue(L, L->top, t);
68011be35a1SLionel Sambuc api_incr_top(L);
681*0a6a1f1dSLionel Sambuc if (narray > 0 || nrec > 0)
682*0a6a1f1dSLionel Sambuc luaH_resize(L, t, narray, nrec);
68311be35a1SLionel Sambuc lua_unlock(L);
68411be35a1SLionel Sambuc }
68511be35a1SLionel Sambuc
68611be35a1SLionel Sambuc
lua_getmetatable(lua_State * L,int objindex)68711be35a1SLionel Sambuc LUA_API int lua_getmetatable (lua_State *L, int objindex) {
68811be35a1SLionel Sambuc const TValue *obj;
689*0a6a1f1dSLionel Sambuc Table *mt;
690*0a6a1f1dSLionel Sambuc int res = 0;
69111be35a1SLionel Sambuc lua_lock(L);
692*0a6a1f1dSLionel Sambuc obj = index2addr(L, objindex);
693*0a6a1f1dSLionel Sambuc switch (ttnov(obj)) {
69411be35a1SLionel Sambuc case LUA_TTABLE:
69511be35a1SLionel Sambuc mt = hvalue(obj)->metatable;
69611be35a1SLionel Sambuc break;
69711be35a1SLionel Sambuc case LUA_TUSERDATA:
69811be35a1SLionel Sambuc mt = uvalue(obj)->metatable;
69911be35a1SLionel Sambuc break;
70011be35a1SLionel Sambuc default:
701*0a6a1f1dSLionel Sambuc mt = G(L)->mt[ttnov(obj)];
70211be35a1SLionel Sambuc break;
70311be35a1SLionel Sambuc }
704*0a6a1f1dSLionel Sambuc if (mt != NULL) {
70511be35a1SLionel Sambuc sethvalue(L, L->top, mt);
70611be35a1SLionel Sambuc api_incr_top(L);
70711be35a1SLionel Sambuc res = 1;
70811be35a1SLionel Sambuc }
70911be35a1SLionel Sambuc lua_unlock(L);
71011be35a1SLionel Sambuc return res;
71111be35a1SLionel Sambuc }
71211be35a1SLionel Sambuc
71311be35a1SLionel Sambuc
lua_getuservalue(lua_State * L,int idx)714*0a6a1f1dSLionel Sambuc LUA_API int lua_getuservalue (lua_State *L, int idx) {
71511be35a1SLionel Sambuc StkId o;
71611be35a1SLionel Sambuc lua_lock(L);
717*0a6a1f1dSLionel Sambuc o = index2addr(L, idx);
718*0a6a1f1dSLionel Sambuc api_check(L, ttisfulluserdata(o), "full userdata expected");
719*0a6a1f1dSLionel Sambuc getuservalue(L, uvalue(o), L->top);
72011be35a1SLionel Sambuc api_incr_top(L);
72111be35a1SLionel Sambuc lua_unlock(L);
722*0a6a1f1dSLionel Sambuc return ttnov(L->top - 1);
72311be35a1SLionel Sambuc }
72411be35a1SLionel Sambuc
72511be35a1SLionel Sambuc
72611be35a1SLionel Sambuc /*
72711be35a1SLionel Sambuc ** set functions (stack -> Lua)
72811be35a1SLionel Sambuc */
72911be35a1SLionel Sambuc
73011be35a1SLionel Sambuc
lua_setglobal(lua_State * L,const char * name)731*0a6a1f1dSLionel Sambuc LUA_API void lua_setglobal (lua_State *L, const char *name) {
732*0a6a1f1dSLionel Sambuc Table *reg = hvalue(&G(L)->l_registry);
733*0a6a1f1dSLionel Sambuc const TValue *gt; /* global table */
734*0a6a1f1dSLionel Sambuc lua_lock(L);
735*0a6a1f1dSLionel Sambuc api_checknelems(L, 1);
736*0a6a1f1dSLionel Sambuc gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
737*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, luaS_new(L, name));
738*0a6a1f1dSLionel Sambuc api_incr_top(L);
739*0a6a1f1dSLionel Sambuc luaV_settable(L, gt, L->top - 1, L->top - 2);
740*0a6a1f1dSLionel Sambuc L->top -= 2; /* pop value and key */
741*0a6a1f1dSLionel Sambuc lua_unlock(L);
742*0a6a1f1dSLionel Sambuc }
743*0a6a1f1dSLionel Sambuc
744*0a6a1f1dSLionel Sambuc
lua_settable(lua_State * L,int idx)74511be35a1SLionel Sambuc LUA_API void lua_settable (lua_State *L, int idx) {
74611be35a1SLionel Sambuc StkId t;
74711be35a1SLionel Sambuc lua_lock(L);
74811be35a1SLionel Sambuc api_checknelems(L, 2);
749*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
75011be35a1SLionel Sambuc luaV_settable(L, t, L->top - 2, L->top - 1);
75111be35a1SLionel Sambuc L->top -= 2; /* pop index and value */
75211be35a1SLionel Sambuc lua_unlock(L);
75311be35a1SLionel Sambuc }
75411be35a1SLionel Sambuc
75511be35a1SLionel Sambuc
lua_setfield(lua_State * L,int idx,const char * k)75611be35a1SLionel Sambuc LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
75711be35a1SLionel Sambuc StkId t;
75811be35a1SLionel Sambuc lua_lock(L);
75911be35a1SLionel Sambuc api_checknelems(L, 1);
760*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
761*0a6a1f1dSLionel Sambuc setsvalue2s(L, L->top, luaS_new(L, k));
762*0a6a1f1dSLionel Sambuc api_incr_top(L);
763*0a6a1f1dSLionel Sambuc luaV_settable(L, t, L->top - 1, L->top - 2);
764*0a6a1f1dSLionel Sambuc L->top -= 2; /* pop value and key */
765*0a6a1f1dSLionel Sambuc lua_unlock(L);
766*0a6a1f1dSLionel Sambuc }
767*0a6a1f1dSLionel Sambuc
768*0a6a1f1dSLionel Sambuc
lua_seti(lua_State * L,int idx,lua_Integer n)769*0a6a1f1dSLionel Sambuc LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
770*0a6a1f1dSLionel Sambuc StkId t;
771*0a6a1f1dSLionel Sambuc lua_lock(L);
772*0a6a1f1dSLionel Sambuc api_checknelems(L, 1);
773*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
774*0a6a1f1dSLionel Sambuc setivalue(L->top, n);
775*0a6a1f1dSLionel Sambuc api_incr_top(L);
776*0a6a1f1dSLionel Sambuc luaV_settable(L, t, L->top - 1, L->top - 2);
777*0a6a1f1dSLionel Sambuc L->top -= 2; /* pop value and key */
77811be35a1SLionel Sambuc lua_unlock(L);
77911be35a1SLionel Sambuc }
78011be35a1SLionel Sambuc
78111be35a1SLionel Sambuc
lua_rawset(lua_State * L,int idx)78211be35a1SLionel Sambuc LUA_API void lua_rawset (lua_State *L, int idx) {
783*0a6a1f1dSLionel Sambuc StkId o;
784*0a6a1f1dSLionel Sambuc Table *t;
78511be35a1SLionel Sambuc lua_lock(L);
78611be35a1SLionel Sambuc api_checknelems(L, 2);
787*0a6a1f1dSLionel Sambuc o = index2addr(L, idx);
788*0a6a1f1dSLionel Sambuc api_check(L, ttistable(o), "table expected");
789*0a6a1f1dSLionel Sambuc t = hvalue(o);
790*0a6a1f1dSLionel Sambuc setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
791*0a6a1f1dSLionel Sambuc invalidateTMcache(t);
792*0a6a1f1dSLionel Sambuc luaC_barrierback(L, t, L->top-1);
79311be35a1SLionel Sambuc L->top -= 2;
79411be35a1SLionel Sambuc lua_unlock(L);
79511be35a1SLionel Sambuc }
79611be35a1SLionel Sambuc
79711be35a1SLionel Sambuc
lua_rawseti(lua_State * L,int idx,lua_Integer n)798*0a6a1f1dSLionel Sambuc LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
79911be35a1SLionel Sambuc StkId o;
800*0a6a1f1dSLionel Sambuc Table *t;
80111be35a1SLionel Sambuc lua_lock(L);
80211be35a1SLionel Sambuc api_checknelems(L, 1);
803*0a6a1f1dSLionel Sambuc o = index2addr(L, idx);
804*0a6a1f1dSLionel Sambuc api_check(L, ttistable(o), "table expected");
805*0a6a1f1dSLionel Sambuc t = hvalue(o);
806*0a6a1f1dSLionel Sambuc luaH_setint(L, t, n, L->top - 1);
807*0a6a1f1dSLionel Sambuc luaC_barrierback(L, t, L->top-1);
808*0a6a1f1dSLionel Sambuc L->top--;
809*0a6a1f1dSLionel Sambuc lua_unlock(L);
810*0a6a1f1dSLionel Sambuc }
811*0a6a1f1dSLionel Sambuc
812*0a6a1f1dSLionel Sambuc
lua_rawsetp(lua_State * L,int idx,const void * p)813*0a6a1f1dSLionel Sambuc LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
814*0a6a1f1dSLionel Sambuc StkId o;
815*0a6a1f1dSLionel Sambuc Table *t;
816*0a6a1f1dSLionel Sambuc TValue k;
817*0a6a1f1dSLionel Sambuc lua_lock(L);
818*0a6a1f1dSLionel Sambuc api_checknelems(L, 1);
819*0a6a1f1dSLionel Sambuc o = index2addr(L, idx);
820*0a6a1f1dSLionel Sambuc api_check(L, ttistable(o), "table expected");
821*0a6a1f1dSLionel Sambuc t = hvalue(o);
822*0a6a1f1dSLionel Sambuc setpvalue(&k, cast(void *, p));
823*0a6a1f1dSLionel Sambuc setobj2t(L, luaH_set(L, t, &k), L->top - 1);
824*0a6a1f1dSLionel Sambuc luaC_barrierback(L, t, L->top - 1);
82511be35a1SLionel Sambuc L->top--;
82611be35a1SLionel Sambuc lua_unlock(L);
82711be35a1SLionel Sambuc }
82811be35a1SLionel Sambuc
82911be35a1SLionel Sambuc
lua_setmetatable(lua_State * L,int objindex)83011be35a1SLionel Sambuc LUA_API int lua_setmetatable (lua_State *L, int objindex) {
83111be35a1SLionel Sambuc TValue *obj;
83211be35a1SLionel Sambuc Table *mt;
83311be35a1SLionel Sambuc lua_lock(L);
83411be35a1SLionel Sambuc api_checknelems(L, 1);
835*0a6a1f1dSLionel Sambuc obj = index2addr(L, objindex);
83611be35a1SLionel Sambuc if (ttisnil(L->top - 1))
83711be35a1SLionel Sambuc mt = NULL;
83811be35a1SLionel Sambuc else {
839*0a6a1f1dSLionel Sambuc api_check(L, ttistable(L->top - 1), "table expected");
84011be35a1SLionel Sambuc mt = hvalue(L->top - 1);
84111be35a1SLionel Sambuc }
842*0a6a1f1dSLionel Sambuc switch (ttnov(obj)) {
84311be35a1SLionel Sambuc case LUA_TTABLE: {
84411be35a1SLionel Sambuc hvalue(obj)->metatable = mt;
845*0a6a1f1dSLionel Sambuc if (mt) {
846*0a6a1f1dSLionel Sambuc luaC_objbarrier(L, gcvalue(obj), mt);
847*0a6a1f1dSLionel Sambuc luaC_checkfinalizer(L, gcvalue(obj), mt);
848*0a6a1f1dSLionel Sambuc }
84911be35a1SLionel Sambuc break;
85011be35a1SLionel Sambuc }
85111be35a1SLionel Sambuc case LUA_TUSERDATA: {
85211be35a1SLionel Sambuc uvalue(obj)->metatable = mt;
853*0a6a1f1dSLionel Sambuc if (mt) {
854*0a6a1f1dSLionel Sambuc luaC_objbarrier(L, uvalue(obj), mt);
855*0a6a1f1dSLionel Sambuc luaC_checkfinalizer(L, gcvalue(obj), mt);
856*0a6a1f1dSLionel Sambuc }
85711be35a1SLionel Sambuc break;
85811be35a1SLionel Sambuc }
85911be35a1SLionel Sambuc default: {
860*0a6a1f1dSLionel Sambuc G(L)->mt[ttnov(obj)] = mt;
86111be35a1SLionel Sambuc break;
86211be35a1SLionel Sambuc }
86311be35a1SLionel Sambuc }
86411be35a1SLionel Sambuc L->top--;
86511be35a1SLionel Sambuc lua_unlock(L);
86611be35a1SLionel Sambuc return 1;
86711be35a1SLionel Sambuc }
86811be35a1SLionel Sambuc
86911be35a1SLionel Sambuc
lua_setuservalue(lua_State * L,int idx)870*0a6a1f1dSLionel Sambuc LUA_API void lua_setuservalue (lua_State *L, int idx) {
87111be35a1SLionel Sambuc StkId o;
87211be35a1SLionel Sambuc lua_lock(L);
87311be35a1SLionel Sambuc api_checknelems(L, 1);
874*0a6a1f1dSLionel Sambuc o = index2addr(L, idx);
875*0a6a1f1dSLionel Sambuc api_check(L, ttisfulluserdata(o), "full userdata expected");
876*0a6a1f1dSLionel Sambuc setuservalue(L, uvalue(o), L->top - 1);
877*0a6a1f1dSLionel Sambuc luaC_barrier(L, gcvalue(o), L->top - 1);
87811be35a1SLionel Sambuc L->top--;
87911be35a1SLionel Sambuc lua_unlock(L);
88011be35a1SLionel Sambuc }
88111be35a1SLionel Sambuc
88211be35a1SLionel Sambuc
88311be35a1SLionel Sambuc /*
884*0a6a1f1dSLionel Sambuc ** 'load' and 'call' functions (run Lua code)
88511be35a1SLionel Sambuc */
88611be35a1SLionel Sambuc
88711be35a1SLionel Sambuc
88811be35a1SLionel Sambuc #define checkresults(L,na,nr) \
889*0a6a1f1dSLionel Sambuc api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
890*0a6a1f1dSLionel Sambuc "results from function overflow current stack size")
89111be35a1SLionel Sambuc
89211be35a1SLionel Sambuc
lua_callk(lua_State * L,int nargs,int nresults,lua_KContext ctx,lua_KFunction k)893*0a6a1f1dSLionel Sambuc LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
894*0a6a1f1dSLionel Sambuc lua_KContext ctx, lua_KFunction k) {
89511be35a1SLionel Sambuc StkId func;
89611be35a1SLionel Sambuc lua_lock(L);
897*0a6a1f1dSLionel Sambuc api_check(L, k == NULL || !isLua(L->ci),
898*0a6a1f1dSLionel Sambuc "cannot use continuations inside hooks");
89911be35a1SLionel Sambuc api_checknelems(L, nargs+1);
900*0a6a1f1dSLionel Sambuc api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
90111be35a1SLionel Sambuc checkresults(L, nargs, nresults);
90211be35a1SLionel Sambuc func = L->top - (nargs+1);
903*0a6a1f1dSLionel Sambuc if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
904*0a6a1f1dSLionel Sambuc L->ci->u.c.k = k; /* save continuation */
905*0a6a1f1dSLionel Sambuc L->ci->u.c.ctx = ctx; /* save context */
906*0a6a1f1dSLionel Sambuc luaD_call(L, func, nresults, 1); /* do the call */
907*0a6a1f1dSLionel Sambuc }
908*0a6a1f1dSLionel Sambuc else /* no continuation or no yieldable */
909*0a6a1f1dSLionel Sambuc luaD_call(L, func, nresults, 0); /* just do the call */
91011be35a1SLionel Sambuc adjustresults(L, nresults);
91111be35a1SLionel Sambuc lua_unlock(L);
91211be35a1SLionel Sambuc }
91311be35a1SLionel Sambuc
91411be35a1SLionel Sambuc
91511be35a1SLionel Sambuc
91611be35a1SLionel Sambuc /*
91711be35a1SLionel Sambuc ** Execute a protected call.
91811be35a1SLionel Sambuc */
919*0a6a1f1dSLionel Sambuc struct CallS { /* data to 'f_call' */
92011be35a1SLionel Sambuc StkId func;
92111be35a1SLionel Sambuc int nresults;
92211be35a1SLionel Sambuc };
92311be35a1SLionel Sambuc
92411be35a1SLionel Sambuc
f_call(lua_State * L,void * ud)92511be35a1SLionel Sambuc static void f_call (lua_State *L, void *ud) {
92611be35a1SLionel Sambuc struct CallS *c = cast(struct CallS *, ud);
927*0a6a1f1dSLionel Sambuc luaD_call(L, c->func, c->nresults, 0);
92811be35a1SLionel Sambuc }
92911be35a1SLionel Sambuc
93011be35a1SLionel Sambuc
93111be35a1SLionel Sambuc
lua_pcallk(lua_State * L,int nargs,int nresults,int errfunc,lua_KContext ctx,lua_KFunction k)932*0a6a1f1dSLionel Sambuc LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
933*0a6a1f1dSLionel Sambuc lua_KContext ctx, lua_KFunction k) {
93411be35a1SLionel Sambuc struct CallS c;
93511be35a1SLionel Sambuc int status;
93611be35a1SLionel Sambuc ptrdiff_t func;
93711be35a1SLionel Sambuc lua_lock(L);
938*0a6a1f1dSLionel Sambuc api_check(L, k == NULL || !isLua(L->ci),
939*0a6a1f1dSLionel Sambuc "cannot use continuations inside hooks");
94011be35a1SLionel Sambuc api_checknelems(L, nargs+1);
941*0a6a1f1dSLionel Sambuc api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
94211be35a1SLionel Sambuc checkresults(L, nargs, nresults);
94311be35a1SLionel Sambuc if (errfunc == 0)
94411be35a1SLionel Sambuc func = 0;
94511be35a1SLionel Sambuc else {
946*0a6a1f1dSLionel Sambuc StkId o = index2addr(L, errfunc);
947*0a6a1f1dSLionel Sambuc api_checkstackindex(L, errfunc, o);
94811be35a1SLionel Sambuc func = savestack(L, o);
94911be35a1SLionel Sambuc }
95011be35a1SLionel Sambuc c.func = L->top - (nargs+1); /* function to be called */
951*0a6a1f1dSLionel Sambuc if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
952*0a6a1f1dSLionel Sambuc c.nresults = nresults; /* do a 'conventional' protected call */
95311be35a1SLionel Sambuc status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
954*0a6a1f1dSLionel Sambuc }
955*0a6a1f1dSLionel Sambuc else { /* prepare continuation (call is already protected by 'resume') */
956*0a6a1f1dSLionel Sambuc CallInfo *ci = L->ci;
957*0a6a1f1dSLionel Sambuc ci->u.c.k = k; /* save continuation */
958*0a6a1f1dSLionel Sambuc ci->u.c.ctx = ctx; /* save context */
959*0a6a1f1dSLionel Sambuc /* save information for error recovery */
960*0a6a1f1dSLionel Sambuc ci->extra = savestack(L, c.func);
961*0a6a1f1dSLionel Sambuc ci->u.c.old_errfunc = L->errfunc;
962*0a6a1f1dSLionel Sambuc L->errfunc = func;
963*0a6a1f1dSLionel Sambuc setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
964*0a6a1f1dSLionel Sambuc ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
965*0a6a1f1dSLionel Sambuc luaD_call(L, c.func, nresults, 1); /* do the call */
966*0a6a1f1dSLionel Sambuc ci->callstatus &= ~CIST_YPCALL;
967*0a6a1f1dSLionel Sambuc L->errfunc = ci->u.c.old_errfunc;
968*0a6a1f1dSLionel Sambuc status = LUA_OK; /* if it is here, there were no errors */
969*0a6a1f1dSLionel Sambuc }
97011be35a1SLionel Sambuc adjustresults(L, nresults);
97111be35a1SLionel Sambuc lua_unlock(L);
97211be35a1SLionel Sambuc return status;
97311be35a1SLionel Sambuc }
97411be35a1SLionel Sambuc
97511be35a1SLionel Sambuc
lua_load(lua_State * L,lua_Reader reader,void * data,const char * chunkname,const char * mode)97611be35a1SLionel Sambuc LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
977*0a6a1f1dSLionel Sambuc const char *chunkname, const char *mode) {
97811be35a1SLionel Sambuc ZIO z;
97911be35a1SLionel Sambuc int status;
98011be35a1SLionel Sambuc lua_lock(L);
98111be35a1SLionel Sambuc if (!chunkname) chunkname = "?";
98211be35a1SLionel Sambuc luaZ_init(L, &z, reader, data);
983*0a6a1f1dSLionel Sambuc status = luaD_protectedparser(L, &z, chunkname, mode);
984*0a6a1f1dSLionel Sambuc if (status == LUA_OK) { /* no errors? */
985*0a6a1f1dSLionel Sambuc LClosure *f = clLvalue(L->top - 1); /* get newly created function */
986*0a6a1f1dSLionel Sambuc if (f->nupvalues >= 1) { /* does it have an upvalue? */
987*0a6a1f1dSLionel Sambuc /* get global table from registry */
988*0a6a1f1dSLionel Sambuc Table *reg = hvalue(&G(L)->l_registry);
989*0a6a1f1dSLionel Sambuc const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
990*0a6a1f1dSLionel Sambuc /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
991*0a6a1f1dSLionel Sambuc setobj(L, f->upvals[0]->v, gt);
992*0a6a1f1dSLionel Sambuc luaC_upvalbarrier(L, f->upvals[0]);
993*0a6a1f1dSLionel Sambuc }
994*0a6a1f1dSLionel Sambuc }
99511be35a1SLionel Sambuc lua_unlock(L);
99611be35a1SLionel Sambuc return status;
99711be35a1SLionel Sambuc }
99811be35a1SLionel Sambuc
99911be35a1SLionel Sambuc
lua_dump(lua_State * L,lua_Writer writer,void * data,int strip)1000*0a6a1f1dSLionel Sambuc LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
100111be35a1SLionel Sambuc int status;
100211be35a1SLionel Sambuc TValue *o;
100311be35a1SLionel Sambuc lua_lock(L);
100411be35a1SLionel Sambuc api_checknelems(L, 1);
100511be35a1SLionel Sambuc o = L->top - 1;
100611be35a1SLionel Sambuc if (isLfunction(o))
1007*0a6a1f1dSLionel Sambuc status = luaU_dump(L, getproto(o), writer, data, strip);
100811be35a1SLionel Sambuc else
100911be35a1SLionel Sambuc status = 1;
101011be35a1SLionel Sambuc lua_unlock(L);
101111be35a1SLionel Sambuc return status;
101211be35a1SLionel Sambuc }
101311be35a1SLionel Sambuc
101411be35a1SLionel Sambuc
lua_status(lua_State * L)101511be35a1SLionel Sambuc LUA_API int lua_status (lua_State *L) {
101611be35a1SLionel Sambuc return L->status;
101711be35a1SLionel Sambuc }
101811be35a1SLionel Sambuc
101911be35a1SLionel Sambuc
102011be35a1SLionel Sambuc /*
102111be35a1SLionel Sambuc ** Garbage-collection function
102211be35a1SLionel Sambuc */
102311be35a1SLionel Sambuc
lua_gc(lua_State * L,int what,int data)102411be35a1SLionel Sambuc LUA_API int lua_gc (lua_State *L, int what, int data) {
102511be35a1SLionel Sambuc int res = 0;
102611be35a1SLionel Sambuc global_State *g;
102711be35a1SLionel Sambuc lua_lock(L);
102811be35a1SLionel Sambuc g = G(L);
102911be35a1SLionel Sambuc switch (what) {
103011be35a1SLionel Sambuc case LUA_GCSTOP: {
1031*0a6a1f1dSLionel Sambuc g->gcrunning = 0;
103211be35a1SLionel Sambuc break;
103311be35a1SLionel Sambuc }
103411be35a1SLionel Sambuc case LUA_GCRESTART: {
1035*0a6a1f1dSLionel Sambuc luaE_setdebt(g, 0);
1036*0a6a1f1dSLionel Sambuc g->gcrunning = 1;
103711be35a1SLionel Sambuc break;
103811be35a1SLionel Sambuc }
103911be35a1SLionel Sambuc case LUA_GCCOLLECT: {
1040*0a6a1f1dSLionel Sambuc luaC_fullgc(L, 0);
104111be35a1SLionel Sambuc break;
104211be35a1SLionel Sambuc }
104311be35a1SLionel Sambuc case LUA_GCCOUNT: {
104411be35a1SLionel Sambuc /* GC values are expressed in Kbytes: #bytes/2^10 */
1045*0a6a1f1dSLionel Sambuc res = cast_int(gettotalbytes(g) >> 10);
104611be35a1SLionel Sambuc break;
104711be35a1SLionel Sambuc }
104811be35a1SLionel Sambuc case LUA_GCCOUNTB: {
1049*0a6a1f1dSLionel Sambuc res = cast_int(gettotalbytes(g) & 0x3ff);
105011be35a1SLionel Sambuc break;
105111be35a1SLionel Sambuc }
105211be35a1SLionel Sambuc case LUA_GCSTEP: {
1053*0a6a1f1dSLionel Sambuc l_mem debt = 1; /* =1 to signal that it did an actual step */
1054*0a6a1f1dSLionel Sambuc int oldrunning = g->gcrunning;
1055*0a6a1f1dSLionel Sambuc g->gcrunning = 1; /* allow GC to run */
1056*0a6a1f1dSLionel Sambuc if (data == 0) {
1057*0a6a1f1dSLionel Sambuc luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
105811be35a1SLionel Sambuc luaC_step(L);
1059*0a6a1f1dSLionel Sambuc }
1060*0a6a1f1dSLionel Sambuc else { /* add 'data' to total debt */
1061*0a6a1f1dSLionel Sambuc debt = cast(l_mem, data) * 1024 + g->GCdebt;
1062*0a6a1f1dSLionel Sambuc luaE_setdebt(g, debt);
1063*0a6a1f1dSLionel Sambuc luaC_checkGC(L);
1064*0a6a1f1dSLionel Sambuc }
1065*0a6a1f1dSLionel Sambuc g->gcrunning = oldrunning; /* restore previous state */
1066*0a6a1f1dSLionel Sambuc if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
106711be35a1SLionel Sambuc res = 1; /* signal it */
106811be35a1SLionel Sambuc break;
106911be35a1SLionel Sambuc }
107011be35a1SLionel Sambuc case LUA_GCSETPAUSE: {
107111be35a1SLionel Sambuc res = g->gcpause;
107211be35a1SLionel Sambuc g->gcpause = data;
107311be35a1SLionel Sambuc break;
107411be35a1SLionel Sambuc }
107511be35a1SLionel Sambuc case LUA_GCSETSTEPMUL: {
107611be35a1SLionel Sambuc res = g->gcstepmul;
1077*0a6a1f1dSLionel Sambuc if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
107811be35a1SLionel Sambuc g->gcstepmul = data;
107911be35a1SLionel Sambuc break;
108011be35a1SLionel Sambuc }
1081*0a6a1f1dSLionel Sambuc case LUA_GCISRUNNING: {
1082*0a6a1f1dSLionel Sambuc res = g->gcrunning;
1083*0a6a1f1dSLionel Sambuc break;
1084*0a6a1f1dSLionel Sambuc }
108511be35a1SLionel Sambuc default: res = -1; /* invalid option */
108611be35a1SLionel Sambuc }
108711be35a1SLionel Sambuc lua_unlock(L);
108811be35a1SLionel Sambuc return res;
108911be35a1SLionel Sambuc }
109011be35a1SLionel Sambuc
109111be35a1SLionel Sambuc
109211be35a1SLionel Sambuc
109311be35a1SLionel Sambuc /*
109411be35a1SLionel Sambuc ** miscellaneous functions
109511be35a1SLionel Sambuc */
109611be35a1SLionel Sambuc
109711be35a1SLionel Sambuc
lua_error(lua_State * L)109811be35a1SLionel Sambuc LUA_API int lua_error (lua_State *L) {
109911be35a1SLionel Sambuc lua_lock(L);
110011be35a1SLionel Sambuc api_checknelems(L, 1);
110111be35a1SLionel Sambuc luaG_errormsg(L);
1102*0a6a1f1dSLionel Sambuc /* code unreachable; will unlock when control actually leaves the kernel */
110311be35a1SLionel Sambuc return 0; /* to avoid warnings */
110411be35a1SLionel Sambuc }
110511be35a1SLionel Sambuc
110611be35a1SLionel Sambuc
lua_next(lua_State * L,int idx)110711be35a1SLionel Sambuc LUA_API int lua_next (lua_State *L, int idx) {
110811be35a1SLionel Sambuc StkId t;
110911be35a1SLionel Sambuc int more;
111011be35a1SLionel Sambuc lua_lock(L);
1111*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
1112*0a6a1f1dSLionel Sambuc api_check(L, ttistable(t), "table expected");
111311be35a1SLionel Sambuc more = luaH_next(L, hvalue(t), L->top - 1);
111411be35a1SLionel Sambuc if (more) {
111511be35a1SLionel Sambuc api_incr_top(L);
111611be35a1SLionel Sambuc }
111711be35a1SLionel Sambuc else /* no more elements */
111811be35a1SLionel Sambuc L->top -= 1; /* remove key */
111911be35a1SLionel Sambuc lua_unlock(L);
112011be35a1SLionel Sambuc return more;
112111be35a1SLionel Sambuc }
112211be35a1SLionel Sambuc
112311be35a1SLionel Sambuc
lua_concat(lua_State * L,int n)112411be35a1SLionel Sambuc LUA_API void lua_concat (lua_State *L, int n) {
112511be35a1SLionel Sambuc lua_lock(L);
112611be35a1SLionel Sambuc api_checknelems(L, n);
112711be35a1SLionel Sambuc if (n >= 2) {
112811be35a1SLionel Sambuc luaC_checkGC(L);
1129*0a6a1f1dSLionel Sambuc luaV_concat(L, n);
113011be35a1SLionel Sambuc }
113111be35a1SLionel Sambuc else if (n == 0) { /* push empty string */
113211be35a1SLionel Sambuc setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
113311be35a1SLionel Sambuc api_incr_top(L);
113411be35a1SLionel Sambuc }
113511be35a1SLionel Sambuc /* else n == 1; nothing to do */
113611be35a1SLionel Sambuc lua_unlock(L);
113711be35a1SLionel Sambuc }
113811be35a1SLionel Sambuc
113911be35a1SLionel Sambuc
lua_len(lua_State * L,int idx)1140*0a6a1f1dSLionel Sambuc LUA_API void lua_len (lua_State *L, int idx) {
1141*0a6a1f1dSLionel Sambuc StkId t;
1142*0a6a1f1dSLionel Sambuc lua_lock(L);
1143*0a6a1f1dSLionel Sambuc t = index2addr(L, idx);
1144*0a6a1f1dSLionel Sambuc luaV_objlen(L, L->top, t);
1145*0a6a1f1dSLionel Sambuc api_incr_top(L);
1146*0a6a1f1dSLionel Sambuc lua_unlock(L);
1147*0a6a1f1dSLionel Sambuc }
1148*0a6a1f1dSLionel Sambuc
1149*0a6a1f1dSLionel Sambuc
lua_getallocf(lua_State * L,void ** ud)115011be35a1SLionel Sambuc LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
115111be35a1SLionel Sambuc lua_Alloc f;
115211be35a1SLionel Sambuc lua_lock(L);
115311be35a1SLionel Sambuc if (ud) *ud = G(L)->ud;
115411be35a1SLionel Sambuc f = G(L)->frealloc;
115511be35a1SLionel Sambuc lua_unlock(L);
115611be35a1SLionel Sambuc return f;
115711be35a1SLionel Sambuc }
115811be35a1SLionel Sambuc
115911be35a1SLionel Sambuc
lua_setallocf(lua_State * L,lua_Alloc f,void * ud)116011be35a1SLionel Sambuc LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
116111be35a1SLionel Sambuc lua_lock(L);
116211be35a1SLionel Sambuc G(L)->ud = ud;
116311be35a1SLionel Sambuc G(L)->frealloc = f;
116411be35a1SLionel Sambuc lua_unlock(L);
116511be35a1SLionel Sambuc }
116611be35a1SLionel Sambuc
116711be35a1SLionel Sambuc
lua_newuserdata(lua_State * L,size_t size)116811be35a1SLionel Sambuc LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
116911be35a1SLionel Sambuc Udata *u;
117011be35a1SLionel Sambuc lua_lock(L);
117111be35a1SLionel Sambuc luaC_checkGC(L);
1172*0a6a1f1dSLionel Sambuc u = luaS_newudata(L, size);
117311be35a1SLionel Sambuc setuvalue(L, L->top, u);
117411be35a1SLionel Sambuc api_incr_top(L);
117511be35a1SLionel Sambuc lua_unlock(L);
1176*0a6a1f1dSLionel Sambuc return getudatamem(u);
117711be35a1SLionel Sambuc }
117811be35a1SLionel Sambuc
117911be35a1SLionel Sambuc
118011be35a1SLionel Sambuc
aux_upvalue(StkId fi,int n,TValue ** val,CClosure ** owner,UpVal ** uv)1181*0a6a1f1dSLionel Sambuc static const char *aux_upvalue (StkId fi, int n, TValue **val,
1182*0a6a1f1dSLionel Sambuc CClosure **owner, UpVal **uv) {
1183*0a6a1f1dSLionel Sambuc switch (ttype(fi)) {
1184*0a6a1f1dSLionel Sambuc case LUA_TCCL: { /* C closure */
1185*0a6a1f1dSLionel Sambuc CClosure *f = clCvalue(fi);
1186*0a6a1f1dSLionel Sambuc if (!(1 <= n && n <= f->nupvalues)) return NULL;
1187*0a6a1f1dSLionel Sambuc *val = &f->upvalue[n-1];
1188*0a6a1f1dSLionel Sambuc if (owner) *owner = f;
118911be35a1SLionel Sambuc return "";
119011be35a1SLionel Sambuc }
1191*0a6a1f1dSLionel Sambuc case LUA_TLCL: { /* Lua closure */
1192*0a6a1f1dSLionel Sambuc LClosure *f = clLvalue(fi);
1193*0a6a1f1dSLionel Sambuc TString *name;
1194*0a6a1f1dSLionel Sambuc Proto *p = f->p;
119511be35a1SLionel Sambuc if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1196*0a6a1f1dSLionel Sambuc *val = f->upvals[n-1]->v;
1197*0a6a1f1dSLionel Sambuc if (uv) *uv = f->upvals[n - 1];
1198*0a6a1f1dSLionel Sambuc name = p->upvalues[n-1].name;
1199*0a6a1f1dSLionel Sambuc return (name == NULL) ? "(*no name)" : getstr(name);
1200*0a6a1f1dSLionel Sambuc }
1201*0a6a1f1dSLionel Sambuc default: return NULL; /* not a closure */
120211be35a1SLionel Sambuc }
120311be35a1SLionel Sambuc }
120411be35a1SLionel Sambuc
120511be35a1SLionel Sambuc
lua_getupvalue(lua_State * L,int funcindex,int n)120611be35a1SLionel Sambuc LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
120711be35a1SLionel Sambuc const char *name;
1208*0a6a1f1dSLionel Sambuc TValue *val = NULL; /* to avoid warnings */
120911be35a1SLionel Sambuc lua_lock(L);
1210*0a6a1f1dSLionel Sambuc name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
121111be35a1SLionel Sambuc if (name) {
121211be35a1SLionel Sambuc setobj2s(L, L->top, val);
121311be35a1SLionel Sambuc api_incr_top(L);
121411be35a1SLionel Sambuc }
121511be35a1SLionel Sambuc lua_unlock(L);
121611be35a1SLionel Sambuc return name;
121711be35a1SLionel Sambuc }
121811be35a1SLionel Sambuc
121911be35a1SLionel Sambuc
lua_setupvalue(lua_State * L,int funcindex,int n)122011be35a1SLionel Sambuc LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
122111be35a1SLionel Sambuc const char *name;
1222*0a6a1f1dSLionel Sambuc TValue *val = NULL; /* to avoid warnings */
1223*0a6a1f1dSLionel Sambuc CClosure *owner = NULL;
1224*0a6a1f1dSLionel Sambuc UpVal *uv = NULL;
122511be35a1SLionel Sambuc StkId fi;
122611be35a1SLionel Sambuc lua_lock(L);
1227*0a6a1f1dSLionel Sambuc fi = index2addr(L, funcindex);
122811be35a1SLionel Sambuc api_checknelems(L, 1);
1229*0a6a1f1dSLionel Sambuc name = aux_upvalue(fi, n, &val, &owner, &uv);
123011be35a1SLionel Sambuc if (name) {
123111be35a1SLionel Sambuc L->top--;
123211be35a1SLionel Sambuc setobj(L, val, L->top);
1233*0a6a1f1dSLionel Sambuc if (owner) { luaC_barrier(L, owner, L->top); }
1234*0a6a1f1dSLionel Sambuc else if (uv) { luaC_upvalbarrier(L, uv); }
123511be35a1SLionel Sambuc }
123611be35a1SLionel Sambuc lua_unlock(L);
123711be35a1SLionel Sambuc return name;
123811be35a1SLionel Sambuc }
123911be35a1SLionel Sambuc
1240*0a6a1f1dSLionel Sambuc
getupvalref(lua_State * L,int fidx,int n,LClosure ** pf)1241*0a6a1f1dSLionel Sambuc static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1242*0a6a1f1dSLionel Sambuc LClosure *f;
1243*0a6a1f1dSLionel Sambuc StkId fi = index2addr(L, fidx);
1244*0a6a1f1dSLionel Sambuc api_check(L, ttisLclosure(fi), "Lua function expected");
1245*0a6a1f1dSLionel Sambuc f = clLvalue(fi);
1246*0a6a1f1dSLionel Sambuc api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1247*0a6a1f1dSLionel Sambuc if (pf) *pf = f;
1248*0a6a1f1dSLionel Sambuc return &f->upvals[n - 1]; /* get its upvalue pointer */
1249*0a6a1f1dSLionel Sambuc }
1250*0a6a1f1dSLionel Sambuc
1251*0a6a1f1dSLionel Sambuc
lua_upvalueid(lua_State * L,int fidx,int n)1252*0a6a1f1dSLionel Sambuc LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1253*0a6a1f1dSLionel Sambuc StkId fi = index2addr(L, fidx);
1254*0a6a1f1dSLionel Sambuc switch (ttype(fi)) {
1255*0a6a1f1dSLionel Sambuc case LUA_TLCL: { /* lua closure */
1256*0a6a1f1dSLionel Sambuc return *getupvalref(L, fidx, n, NULL);
1257*0a6a1f1dSLionel Sambuc }
1258*0a6a1f1dSLionel Sambuc case LUA_TCCL: { /* C closure */
1259*0a6a1f1dSLionel Sambuc CClosure *f = clCvalue(fi);
1260*0a6a1f1dSLionel Sambuc api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1261*0a6a1f1dSLionel Sambuc return &f->upvalue[n - 1];
1262*0a6a1f1dSLionel Sambuc }
1263*0a6a1f1dSLionel Sambuc default: {
1264*0a6a1f1dSLionel Sambuc api_check(L, 0, "closure expected");
1265*0a6a1f1dSLionel Sambuc return NULL;
1266*0a6a1f1dSLionel Sambuc }
1267*0a6a1f1dSLionel Sambuc }
1268*0a6a1f1dSLionel Sambuc }
1269*0a6a1f1dSLionel Sambuc
1270*0a6a1f1dSLionel Sambuc
lua_upvaluejoin(lua_State * L,int fidx1,int n1,int fidx2,int n2)1271*0a6a1f1dSLionel Sambuc LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1272*0a6a1f1dSLionel Sambuc int fidx2, int n2) {
1273*0a6a1f1dSLionel Sambuc LClosure *f1;
1274*0a6a1f1dSLionel Sambuc UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1275*0a6a1f1dSLionel Sambuc UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1276*0a6a1f1dSLionel Sambuc luaC_upvdeccount(L, *up1);
1277*0a6a1f1dSLionel Sambuc *up1 = *up2;
1278*0a6a1f1dSLionel Sambuc (*up1)->refcount++;
1279*0a6a1f1dSLionel Sambuc if (upisopen(*up1)) (*up1)->u.open.touched = 1;
1280*0a6a1f1dSLionel Sambuc luaC_upvalbarrier(L, *up1);
1281*0a6a1f1dSLionel Sambuc }
1282*0a6a1f1dSLionel Sambuc
1283*0a6a1f1dSLionel Sambuc
1284