1 /*
2 ** $Lua: lapi.c,v 2.249 2015/04/06 12:23:48 roberto Exp $
3 ** Lua API
4 ** See Copyright Notice in lua.h
5 */
6
7 #define lapi_c
8 #define LUA_CORE
9
10 #include "lprefix.h"
11
12
13 #include <stdarg.h>
14 #include <string.h>
15
16 #include "lua.h"
17
18 #include "lapi.h"
19 #include "ldebug.h"
20 #include "ldo.h"
21 #include "lfunc.h"
22 #include "lgc.h"
23 #include "lmem.h"
24 #include "lobject.h"
25 #include "lstate.h"
26 #include "lstring.h"
27 #include "ltable.h"
28 #include "ltm.h"
29 #include "lundump.h"
30 #include "lvm.h"
31
32
33
34 const char lua_ident[] =
35 "$LuaVersion: " LUA_COPYRIGHT " $"
36 "$LuaAuthors: " LUA_AUTHORS " $";
37
38
39 /* value at a non-valid index */
40 #define NONVALIDVALUE cast(TValue *, luaO_nilobject)
41
42 /* corresponding test */
43 #define isvalid(o) ((o) != luaO_nilobject)
44
45 /* test for pseudo index */
46 #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
47
48 /* test for upvalue */
49 #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
50
51 /* test for valid but not pseudo index */
52 #define isstackindex(i, o) (isvalid(o) && !ispseudo(i))
53
54 #define api_checkvalidindex(l,o) api_check(l, isvalid(o), "invalid index")
55
56 #define api_checkstackindex(l, i, o) \
57 api_check(l, isstackindex(i, o), "index not in the stack")
58
59
index2addr(lua_State * L,int idx)60 static TValue *index2addr (lua_State *L, int idx) {
61 CallInfo *ci = L->ci;
62 if (idx > 0) {
63 TValue *o = ci->func + idx;
64 api_check(L, idx <= ci->top - (ci->func + 1), "unacceptable index");
65 if (o >= L->top) return NONVALIDVALUE;
66 else return o;
67 }
68 else if (!ispseudo(idx)) { /* negative index */
69 api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
70 return L->top + idx;
71 }
72 else if (idx == LUA_REGISTRYINDEX)
73 return &G(L)->l_registry;
74 else { /* upvalues */
75 idx = LUA_REGISTRYINDEX - idx;
76 api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
77 if (ttislcf(ci->func)) /* light C function? */
78 return NONVALIDVALUE; /* it has no upvalues */
79 else {
80 CClosure *func = clCvalue(ci->func);
81 return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : NONVALIDVALUE;
82 }
83 }
84 }
85
86
87 /*
88 ** to be called by 'lua_checkstack' in protected mode, to grow stack
89 ** capturing memory errors
90 */
growstack(lua_State * L,void * ud)91 static void growstack (lua_State *L, void *ud) {
92 int size = *(int *)ud;
93 luaD_growstack(L, size);
94 }
95
96
lua_checkstack(lua_State * L,int n)97 LUA_API int lua_checkstack (lua_State *L, int n) {
98 int res;
99 CallInfo *ci = L->ci;
100 lua_lock(L);
101 api_check(L, n >= 0, "negative 'n'");
102 if (L->stack_last - L->top > n) /* stack large enough? */
103 res = 1; /* yes; check is OK */
104 else { /* no; need to grow stack */
105 int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
106 if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
107 res = 0; /* no */
108 else /* try to grow stack */
109 res = (luaD_rawrunprotected(L, &growstack, &n) == LUA_OK);
110 }
111 if (res && ci->top < L->top + n)
112 ci->top = L->top + n; /* adjust frame top */
113 lua_unlock(L);
114 return res;
115 }
116
117
lua_xmove(lua_State * from,lua_State * to,int n)118 LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
119 int i;
120 if (from == to) return;
121 lua_lock(to);
122 api_checknelems(from, n);
123 api_check(from, G(from) == G(to), "moving among independent states");
124 api_check(from, to->ci->top - to->top >= n, "not enough elements to move");
125 from->top -= n;
126 for (i = 0; i < n; i++) {
127 setobj2s(to, to->top, from->top + i);
128 api_incr_top(to);
129 }
130 lua_unlock(to);
131 }
132
133
lua_atpanic(lua_State * L,lua_CFunction panicf)134 LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
135 lua_CFunction old;
136 lua_lock(L);
137 old = G(L)->panic;
138 G(L)->panic = panicf;
139 lua_unlock(L);
140 return old;
141 }
142
143
lua_version(lua_State * L)144 LUA_API const lua_Number *lua_version (lua_State *L) {
145 static const lua_Number version = LUA_VERSION_NUM;
146 if (L == NULL) return &version;
147 else return G(L)->version;
148 }
149
150
151
152 /*
153 ** basic stack manipulation
154 */
155
156
157 /*
158 ** convert an acceptable stack index into an absolute index
159 */
lua_absindex(lua_State * L,int idx)160 LUA_API int lua_absindex (lua_State *L, int idx) {
161 return (idx > 0 || ispseudo(idx))
162 ? idx
163 : cast_int(L->top - L->ci->func) + idx;
164 }
165
166
lua_gettop(lua_State * L)167 LUA_API int lua_gettop (lua_State *L) {
168 return cast_int(L->top - (L->ci->func + 1));
169 }
170
171
lua_settop(lua_State * L,int idx)172 LUA_API void lua_settop (lua_State *L, int idx) {
173 StkId func = L->ci->func;
174 lua_lock(L);
175 if (idx >= 0) {
176 api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
177 while (L->top < (func + 1) + idx)
178 setnilvalue(L->top++);
179 L->top = (func + 1) + idx;
180 }
181 else {
182 api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
183 L->top += idx+1; /* 'subtract' index (index is negative) */
184 }
185 lua_unlock(L);
186 }
187
188
189 /*
190 ** Reverse the stack segment from 'from' to 'to'
191 ** (auxiliary to 'lua_rotate')
192 */
reverse(lua_State * L,StkId from,StkId to)193 static void reverse (lua_State *L, StkId from, StkId to) {
194 for (; from < to; from++, to--) {
195 TValue temp;
196 setobj(L, &temp, from);
197 setobjs2s(L, from, to);
198 setobj2s(L, to, &temp);
199 }
200 }
201
202
203 /*
204 ** Let x = AB, where A is a prefix of length 'n'. Then,
205 ** rotate x n == BA. But BA == (A^r . B^r)^r.
206 */
lua_rotate(lua_State * L,int idx,int n)207 LUA_API void lua_rotate (lua_State *L, int idx, int n) {
208 StkId p, t, m;
209 lua_lock(L);
210 t = L->top - 1; /* end of stack segment being rotated */
211 p = index2addr(L, idx); /* start of segment */
212 api_checkstackindex(L, idx, p);
213 api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
214 m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
215 reverse(L, p, m); /* reverse the prefix with length 'n' */
216 reverse(L, m + 1, t); /* reverse the suffix */
217 reverse(L, p, t); /* reverse the entire segment */
218 lua_unlock(L);
219 }
220
221
lua_copy(lua_State * L,int fromidx,int toidx)222 LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
223 TValue *fr, *to;
224 lua_lock(L);
225 fr = index2addr(L, fromidx);
226 to = index2addr(L, toidx);
227 api_checkvalidindex(L, to);
228 setobj(L, to, fr);
229 if (isupvalue(toidx)) /* function upvalue? */
230 luaC_barrier(L, clCvalue(L->ci->func), fr);
231 /* LUA_REGISTRYINDEX does not need gc barrier
232 (collector revisits it before finishing collection) */
233 lua_unlock(L);
234 }
235
236
lua_pushvalue(lua_State * L,int idx)237 LUA_API void lua_pushvalue (lua_State *L, int idx) {
238 lua_lock(L);
239 setobj2s(L, L->top, index2addr(L, idx));
240 api_incr_top(L);
241 lua_unlock(L);
242 }
243
244
245
246 /*
247 ** access functions (stack -> C)
248 */
249
250
lua_type(lua_State * L,int idx)251 LUA_API int lua_type (lua_State *L, int idx) {
252 StkId o = index2addr(L, idx);
253 return (isvalid(o) ? ttnov(o) : LUA_TNONE);
254 }
255
256
lua_typename(lua_State * L,int t)257 LUA_API const char *lua_typename (lua_State *L, int t) {
258 UNUSED(L);
259 api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
260 return ttypename(t);
261 }
262
263
lua_iscfunction(lua_State * L,int idx)264 LUA_API int lua_iscfunction (lua_State *L, int idx) {
265 StkId o = index2addr(L, idx);
266 return (ttislcf(o) || (ttisCclosure(o)));
267 }
268
269
lua_isinteger(lua_State * L,int idx)270 LUA_API int lua_isinteger (lua_State *L, int idx) {
271 StkId o = index2addr(L, idx);
272 return ttisinteger(o);
273 }
274
275
lua_isnumber(lua_State * L,int idx)276 LUA_API int lua_isnumber (lua_State *L, int idx) {
277 lua_Number n;
278 const TValue *o = index2addr(L, idx);
279 return tonumber(o, &n);
280 }
281
282
lua_isstring(lua_State * L,int idx)283 LUA_API int lua_isstring (lua_State *L, int idx) {
284 const TValue *o = index2addr(L, idx);
285 return (ttisstring(o) || cvt2str(o));
286 }
287
288
lua_isuserdata(lua_State * L,int idx)289 LUA_API int lua_isuserdata (lua_State *L, int idx) {
290 const TValue *o = index2addr(L, idx);
291 return (ttisfulluserdata(o) || ttislightuserdata(o));
292 }
293
294
lua_rawequal(lua_State * L,int index1,int index2)295 LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
296 StkId o1 = index2addr(L, index1);
297 StkId o2 = index2addr(L, index2);
298 return (isvalid(o1) && isvalid(o2)) ? luaV_rawequalobj(o1, o2) : 0;
299 }
300
301
lua_arith(lua_State * L,int op)302 LUA_API void lua_arith (lua_State *L, int op) {
303 lua_lock(L);
304 if (op != LUA_OPUNM && op != LUA_OPBNOT)
305 api_checknelems(L, 2); /* all other operations expect two operands */
306 else { /* for unary operations, add fake 2nd operand */
307 api_checknelems(L, 1);
308 setobjs2s(L, L->top, L->top - 1);
309 api_incr_top(L);
310 }
311 /* first operand at top - 2, second at top - 1; result go to top - 2 */
312 luaO_arith(L, op, L->top - 2, L->top - 1, L->top - 2);
313 L->top--; /* remove second operand */
314 lua_unlock(L);
315 }
316
317
lua_compare(lua_State * L,int index1,int index2,int op)318 LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
319 StkId o1, o2;
320 int i = 0;
321 lua_lock(L); /* may call tag method */
322 o1 = index2addr(L, index1);
323 o2 = index2addr(L, index2);
324 if (isvalid(o1) && isvalid(o2)) {
325 switch (op) {
326 case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
327 case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
328 case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
329 default: api_check(L, 0, "invalid option");
330 }
331 }
332 lua_unlock(L);
333 return i;
334 }
335
336
lua_stringtonumber(lua_State * L,const char * s)337 LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
338 size_t sz = luaO_str2num(s, L->top);
339 if (sz != 0)
340 api_incr_top(L);
341 return sz;
342 }
343
344
lua_tonumberx(lua_State * L,int idx,int * pisnum)345 LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
346 lua_Number n;
347 const TValue *o = index2addr(L, idx);
348 int isnum = tonumber(o, &n);
349 if (!isnum)
350 n = 0; /* call to 'tonumber' may change 'n' even if it fails */
351 if (pisnum) *pisnum = isnum;
352 return n;
353 }
354
355
lua_tointegerx(lua_State * L,int idx,int * pisnum)356 LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
357 lua_Integer res;
358 const TValue *o = index2addr(L, idx);
359 int isnum = tointeger(o, &res);
360 if (!isnum)
361 res = 0; /* call to 'tointeger' may change 'n' even if it fails */
362 if (pisnum) *pisnum = isnum;
363 return res;
364 }
365
366
lua_toboolean(lua_State * L,int idx)367 LUA_API int lua_toboolean (lua_State *L, int idx) {
368 const TValue *o = index2addr(L, idx);
369 return !l_isfalse(o);
370 }
371
372
lua_tolstring(lua_State * L,int idx,size_t * len)373 LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
374 StkId o = index2addr(L, idx);
375 if (!ttisstring(o)) {
376 if (!cvt2str(o)) { /* not convertible? */
377 if (len != NULL) *len = 0;
378 return NULL;
379 }
380 lua_lock(L); /* 'luaO_tostring' may create a new string */
381 luaC_checkGC(L);
382 o = index2addr(L, idx); /* previous call may reallocate the stack */
383 luaO_tostring(L, o);
384 lua_unlock(L);
385 }
386 if (len != NULL)
387 *len = vslen(o);
388 return svalue(o);
389 }
390
391
lua_rawlen(lua_State * L,int idx)392 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
393 StkId o = index2addr(L, idx);
394 switch (ttype(o)) {
395 case LUA_TSHRSTR: return tsvalue(o)->shrlen;
396 case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
397 case LUA_TUSERDATA: return uvalue(o)->len;
398 case LUA_TTABLE: return luaH_getn(hvalue(o));
399 default: return 0;
400 }
401 }
402
403
lua_tocfunction(lua_State * L,int idx)404 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
405 StkId o = index2addr(L, idx);
406 if (ttislcf(o)) return fvalue(o);
407 else if (ttisCclosure(o))
408 return clCvalue(o)->f;
409 else return NULL; /* not a C function */
410 }
411
412
lua_touserdata(lua_State * L,int idx)413 LUA_API void *lua_touserdata (lua_State *L, int idx) {
414 StkId o = index2addr(L, idx);
415 switch (ttnov(o)) {
416 case LUA_TUSERDATA: return getudatamem(uvalue(o));
417 case LUA_TLIGHTUSERDATA: return pvalue(o);
418 default: return NULL;
419 }
420 }
421
422
lua_tothread(lua_State * L,int idx)423 LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
424 StkId o = index2addr(L, idx);
425 return (!ttisthread(o)) ? NULL : thvalue(o);
426 }
427
428
lua_topointer(lua_State * L,int idx)429 LUA_API const void *lua_topointer (lua_State *L, int idx) {
430 StkId o = index2addr(L, idx);
431 switch (ttype(o)) {
432 case LUA_TTABLE: return hvalue(o);
433 case LUA_TLCL: return clLvalue(o);
434 case LUA_TCCL: return clCvalue(o);
435 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
436 case LUA_TTHREAD: return thvalue(o);
437 case LUA_TUSERDATA: return getudatamem(uvalue(o));
438 case LUA_TLIGHTUSERDATA: return pvalue(o);
439 default: return NULL;
440 }
441 }
442
443
444
445 /*
446 ** push functions (C -> stack)
447 */
448
449
lua_pushnil(lua_State * L)450 LUA_API void lua_pushnil (lua_State *L) {
451 lua_lock(L);
452 setnilvalue(L->top);
453 api_incr_top(L);
454 lua_unlock(L);
455 }
456
457
lua_pushnumber(lua_State * L,lua_Number n)458 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
459 lua_lock(L);
460 setfltvalue(L->top, n);
461 api_incr_top(L);
462 lua_unlock(L);
463 }
464
465
lua_pushinteger(lua_State * L,lua_Integer n)466 LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
467 lua_lock(L);
468 setivalue(L->top, n);
469 api_incr_top(L);
470 lua_unlock(L);
471 }
472
473
lua_pushlstring(lua_State * L,const char * s,size_t len)474 LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
475 TString *ts;
476 lua_lock(L);
477 luaC_checkGC(L);
478 ts = luaS_newlstr(L, s, len);
479 setsvalue2s(L, L->top, ts);
480 api_incr_top(L);
481 lua_unlock(L);
482 return getstr(ts);
483 }
484
485
lua_pushstring(lua_State * L,const char * s)486 LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
487 lua_lock(L);
488 if (s == NULL)
489 setnilvalue(L->top);
490 else {
491 TString *ts;
492 luaC_checkGC(L);
493 ts = luaS_new(L, s);
494 setsvalue2s(L, L->top, ts);
495 s = getstr(ts); /* internal copy's address */
496 }
497 api_incr_top(L);
498 lua_unlock(L);
499 return s;
500 }
501
502
lua_pushvfstring(lua_State * L,const char * fmt,va_list argp)503 LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
504 va_list argp) {
505 const char *ret;
506 lua_lock(L);
507 luaC_checkGC(L);
508 ret = luaO_pushvfstring(L, fmt, argp);
509 lua_unlock(L);
510 return ret;
511 }
512
513
lua_pushfstring(lua_State * L,const char * fmt,...)514 LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
515 const char *ret;
516 va_list argp;
517 lua_lock(L);
518 luaC_checkGC(L);
519 va_start(argp, fmt);
520 ret = luaO_pushvfstring(L, fmt, argp);
521 va_end(argp);
522 lua_unlock(L);
523 return ret;
524 }
525
526
lua_pushcclosure(lua_State * L,lua_CFunction fn,int n)527 LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
528 lua_lock(L);
529 if (n == 0) {
530 setfvalue(L->top, fn);
531 }
532 else {
533 CClosure *cl;
534 api_checknelems(L, n);
535 api_check(L, n <= MAXUPVAL, "upvalue index too large");
536 luaC_checkGC(L);
537 cl = luaF_newCclosure(L, n);
538 cl->f = fn;
539 L->top -= n;
540 while (n--) {
541 setobj2n(L, &cl->upvalue[n], L->top + n);
542 /* does not need barrier because closure is white */
543 }
544 setclCvalue(L, L->top, cl);
545 }
546 api_incr_top(L);
547 lua_unlock(L);
548 }
549
550
lua_pushboolean(lua_State * L,int b)551 LUA_API void lua_pushboolean (lua_State *L, int b) {
552 lua_lock(L);
553 setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
554 api_incr_top(L);
555 lua_unlock(L);
556 }
557
558
lua_pushlightuserdata(lua_State * L,void * p)559 LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
560 lua_lock(L);
561 setpvalue(L->top, p);
562 api_incr_top(L);
563 lua_unlock(L);
564 }
565
566
lua_pushthread(lua_State * L)567 LUA_API int lua_pushthread (lua_State *L) {
568 lua_lock(L);
569 setthvalue(L, L->top, L);
570 api_incr_top(L);
571 lua_unlock(L);
572 return (G(L)->mainthread == L);
573 }
574
575
576
577 /*
578 ** get functions (Lua -> stack)
579 */
580
581
lua_getglobal(lua_State * L,const char * name)582 LUA_API int lua_getglobal (lua_State *L, const char *name) {
583 Table *reg = hvalue(&G(L)->l_registry);
584 const TValue *gt; /* global table */
585 lua_lock(L);
586 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
587 setsvalue2s(L, L->top, luaS_new(L, name));
588 api_incr_top(L);
589 luaV_gettable(L, gt, L->top - 1, L->top - 1);
590 lua_unlock(L);
591 return ttnov(L->top - 1);
592 }
593
594
lua_gettable(lua_State * L,int idx)595 LUA_API int lua_gettable (lua_State *L, int idx) {
596 StkId t;
597 lua_lock(L);
598 t = index2addr(L, idx);
599 luaV_gettable(L, t, L->top - 1, L->top - 1);
600 lua_unlock(L);
601 return ttnov(L->top - 1);
602 }
603
604
lua_getfield(lua_State * L,int idx,const char * k)605 LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
606 StkId t;
607 lua_lock(L);
608 t = index2addr(L, idx);
609 setsvalue2s(L, L->top, luaS_new(L, k));
610 api_incr_top(L);
611 luaV_gettable(L, t, L->top - 1, L->top - 1);
612 lua_unlock(L);
613 return ttnov(L->top - 1);
614 }
615
616
lua_geti(lua_State * L,int idx,lua_Integer n)617 LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
618 StkId t;
619 lua_lock(L);
620 t = index2addr(L, idx);
621 setivalue(L->top, n);
622 api_incr_top(L);
623 luaV_gettable(L, t, L->top - 1, L->top - 1);
624 lua_unlock(L);
625 return ttnov(L->top - 1);
626 }
627
628
lua_rawget(lua_State * L,int idx)629 LUA_API int lua_rawget (lua_State *L, int idx) {
630 StkId t;
631 lua_lock(L);
632 t = index2addr(L, idx);
633 api_check(L, ttistable(t), "table expected");
634 setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));
635 lua_unlock(L);
636 return ttnov(L->top - 1);
637 }
638
639
lua_rawgeti(lua_State * L,int idx,lua_Integer n)640 LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
641 StkId t;
642 lua_lock(L);
643 t = index2addr(L, idx);
644 api_check(L, ttistable(t), "table expected");
645 setobj2s(L, L->top, luaH_getint(hvalue(t), n));
646 api_incr_top(L);
647 lua_unlock(L);
648 return ttnov(L->top - 1);
649 }
650
651
lua_rawgetp(lua_State * L,int idx,const void * p)652 LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
653 StkId t;
654 TValue k;
655 lua_lock(L);
656 t = index2addr(L, idx);
657 api_check(L, ttistable(t), "table expected");
658 setpvalue(&k, cast(void *, p));
659 setobj2s(L, L->top, luaH_get(hvalue(t), &k));
660 api_incr_top(L);
661 lua_unlock(L);
662 return ttnov(L->top - 1);
663 }
664
665
lua_createtable(lua_State * L,int narray,int nrec)666 LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
667 Table *t;
668 lua_lock(L);
669 luaC_checkGC(L);
670 t = luaH_new(L);
671 sethvalue(L, L->top, t);
672 api_incr_top(L);
673 if (narray > 0 || nrec > 0)
674 luaH_resize(L, t, narray, nrec);
675 lua_unlock(L);
676 }
677
678
lua_getmetatable(lua_State * L,int objindex)679 LUA_API int lua_getmetatable (lua_State *L, int objindex) {
680 const TValue *obj;
681 Table *mt;
682 int res = 0;
683 lua_lock(L);
684 obj = index2addr(L, objindex);
685 switch (ttnov(obj)) {
686 case LUA_TTABLE:
687 mt = hvalue(obj)->metatable;
688 break;
689 case LUA_TUSERDATA:
690 mt = uvalue(obj)->metatable;
691 break;
692 default:
693 mt = G(L)->mt[ttnov(obj)];
694 break;
695 }
696 if (mt != NULL) {
697 sethvalue(L, L->top, mt);
698 api_incr_top(L);
699 res = 1;
700 }
701 lua_unlock(L);
702 return res;
703 }
704
705
lua_getuservalue(lua_State * L,int idx)706 LUA_API int lua_getuservalue (lua_State *L, int idx) {
707 StkId o;
708 lua_lock(L);
709 o = index2addr(L, idx);
710 api_check(L, ttisfulluserdata(o), "full userdata expected");
711 getuservalue(L, uvalue(o), L->top);
712 api_incr_top(L);
713 lua_unlock(L);
714 return ttnov(L->top - 1);
715 }
716
717
718 /*
719 ** set functions (stack -> Lua)
720 */
721
722
lua_setglobal(lua_State * L,const char * name)723 LUA_API void lua_setglobal (lua_State *L, const char *name) {
724 Table *reg = hvalue(&G(L)->l_registry);
725 const TValue *gt; /* global table */
726 lua_lock(L);
727 api_checknelems(L, 1);
728 gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
729 setsvalue2s(L, L->top, luaS_new(L, name));
730 api_incr_top(L);
731 luaV_settable(L, gt, L->top - 1, L->top - 2);
732 L->top -= 2; /* pop value and key */
733 lua_unlock(L);
734 }
735
736
lua_settable(lua_State * L,int idx)737 LUA_API void lua_settable (lua_State *L, int idx) {
738 StkId t;
739 lua_lock(L);
740 api_checknelems(L, 2);
741 t = index2addr(L, idx);
742 luaV_settable(L, t, L->top - 2, L->top - 1);
743 L->top -= 2; /* pop index and value */
744 lua_unlock(L);
745 }
746
747
lua_setfield(lua_State * L,int idx,const char * k)748 LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
749 StkId t;
750 lua_lock(L);
751 api_checknelems(L, 1);
752 t = index2addr(L, idx);
753 setsvalue2s(L, L->top, luaS_new(L, k));
754 api_incr_top(L);
755 luaV_settable(L, t, L->top - 1, L->top - 2);
756 L->top -= 2; /* pop value and key */
757 lua_unlock(L);
758 }
759
760
lua_seti(lua_State * L,int idx,lua_Integer n)761 LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
762 StkId t;
763 lua_lock(L);
764 api_checknelems(L, 1);
765 t = index2addr(L, idx);
766 setivalue(L->top, n);
767 api_incr_top(L);
768 luaV_settable(L, t, L->top - 1, L->top - 2);
769 L->top -= 2; /* pop value and key */
770 lua_unlock(L);
771 }
772
773
lua_rawset(lua_State * L,int idx)774 LUA_API void lua_rawset (lua_State *L, int idx) {
775 StkId o;
776 Table *t;
777 lua_lock(L);
778 api_checknelems(L, 2);
779 o = index2addr(L, idx);
780 api_check(L, ttistable(o), "table expected");
781 t = hvalue(o);
782 setobj2t(L, luaH_set(L, t, L->top-2), L->top-1);
783 invalidateTMcache(t);
784 luaC_barrierback(L, t, L->top-1);
785 L->top -= 2;
786 lua_unlock(L);
787 }
788
789
lua_rawseti(lua_State * L,int idx,lua_Integer n)790 LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
791 StkId o;
792 Table *t;
793 lua_lock(L);
794 api_checknelems(L, 1);
795 o = index2addr(L, idx);
796 api_check(L, ttistable(o), "table expected");
797 t = hvalue(o);
798 luaH_setint(L, t, n, L->top - 1);
799 luaC_barrierback(L, t, L->top-1);
800 L->top--;
801 lua_unlock(L);
802 }
803
804
lua_rawsetp(lua_State * L,int idx,const void * p)805 LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
806 StkId o;
807 Table *t;
808 TValue k;
809 lua_lock(L);
810 api_checknelems(L, 1);
811 o = index2addr(L, idx);
812 api_check(L, ttistable(o), "table expected");
813 t = hvalue(o);
814 setpvalue(&k, cast(void *, p));
815 setobj2t(L, luaH_set(L, t, &k), L->top - 1);
816 luaC_barrierback(L, t, L->top - 1);
817 L->top--;
818 lua_unlock(L);
819 }
820
821
lua_setmetatable(lua_State * L,int objindex)822 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
823 TValue *obj;
824 Table *mt;
825 lua_lock(L);
826 api_checknelems(L, 1);
827 obj = index2addr(L, objindex);
828 if (ttisnil(L->top - 1))
829 mt = NULL;
830 else {
831 api_check(L, ttistable(L->top - 1), "table expected");
832 mt = hvalue(L->top - 1);
833 }
834 switch (ttnov(obj)) {
835 case LUA_TTABLE: {
836 hvalue(obj)->metatable = mt;
837 if (mt) {
838 luaC_objbarrier(L, gcvalue(obj), mt);
839 luaC_checkfinalizer(L, gcvalue(obj), mt);
840 }
841 break;
842 }
843 case LUA_TUSERDATA: {
844 uvalue(obj)->metatable = mt;
845 if (mt) {
846 luaC_objbarrier(L, uvalue(obj), mt);
847 luaC_checkfinalizer(L, gcvalue(obj), mt);
848 }
849 break;
850 }
851 default: {
852 G(L)->mt[ttnov(obj)] = mt;
853 break;
854 }
855 }
856 L->top--;
857 lua_unlock(L);
858 return 1;
859 }
860
861
lua_setuservalue(lua_State * L,int idx)862 LUA_API void lua_setuservalue (lua_State *L, int idx) {
863 StkId o;
864 lua_lock(L);
865 api_checknelems(L, 1);
866 o = index2addr(L, idx);
867 api_check(L, ttisfulluserdata(o), "full userdata expected");
868 setuservalue(L, uvalue(o), L->top - 1);
869 luaC_barrier(L, gcvalue(o), L->top - 1);
870 L->top--;
871 lua_unlock(L);
872 }
873
874
875 /*
876 ** 'load' and 'call' functions (run Lua code)
877 */
878
879
880 #define checkresults(L,na,nr) \
881 api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
882 "results from function overflow current stack size")
883
884
lua_callk(lua_State * L,int nargs,int nresults,lua_KContext ctx,lua_KFunction k)885 LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
886 lua_KContext ctx, lua_KFunction k) {
887 StkId func;
888 lua_lock(L);
889 api_check(L, k == NULL || !isLua(L->ci),
890 "cannot use continuations inside hooks");
891 api_checknelems(L, nargs+1);
892 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
893 checkresults(L, nargs, nresults);
894 func = L->top - (nargs+1);
895 if (k != NULL && L->nny == 0) { /* need to prepare continuation? */
896 L->ci->u.c.k = k; /* save continuation */
897 L->ci->u.c.ctx = ctx; /* save context */
898 luaD_call(L, func, nresults, 1); /* do the call */
899 }
900 else /* no continuation or no yieldable */
901 luaD_call(L, func, nresults, 0); /* just do the call */
902 adjustresults(L, nresults);
903 lua_unlock(L);
904 }
905
906
907
908 /*
909 ** Execute a protected call.
910 */
911 struct CallS { /* data to 'f_call' */
912 StkId func;
913 int nresults;
914 };
915
916
f_call(lua_State * L,void * ud)917 static void f_call (lua_State *L, void *ud) {
918 struct CallS *c = cast(struct CallS *, ud);
919 luaD_call(L, c->func, c->nresults, 0);
920 }
921
922
923
lua_pcallk(lua_State * L,int nargs,int nresults,int errfunc,lua_KContext ctx,lua_KFunction k)924 LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
925 lua_KContext ctx, lua_KFunction k) {
926 struct CallS c;
927 int status;
928 ptrdiff_t func;
929 lua_lock(L);
930 api_check(L, k == NULL || !isLua(L->ci),
931 "cannot use continuations inside hooks");
932 api_checknelems(L, nargs+1);
933 api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
934 checkresults(L, nargs, nresults);
935 if (errfunc == 0)
936 func = 0;
937 else {
938 StkId o = index2addr(L, errfunc);
939 api_checkstackindex(L, errfunc, o);
940 func = savestack(L, o);
941 }
942 c.func = L->top - (nargs+1); /* function to be called */
943 if (k == NULL || L->nny > 0) { /* no continuation or no yieldable? */
944 c.nresults = nresults; /* do a 'conventional' protected call */
945 status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
946 }
947 else { /* prepare continuation (call is already protected by 'resume') */
948 CallInfo *ci = L->ci;
949 ci->u.c.k = k; /* save continuation */
950 ci->u.c.ctx = ctx; /* save context */
951 /* save information for error recovery */
952 ci->extra = savestack(L, c.func);
953 ci->u.c.old_errfunc = L->errfunc;
954 L->errfunc = func;
955 setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
956 ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
957 luaD_call(L, c.func, nresults, 1); /* do the call */
958 ci->callstatus &= ~CIST_YPCALL;
959 L->errfunc = ci->u.c.old_errfunc;
960 status = LUA_OK; /* if it is here, there were no errors */
961 }
962 adjustresults(L, nresults);
963 lua_unlock(L);
964 return status;
965 }
966
967
lua_load(lua_State * L,lua_Reader reader,void * data,const char * chunkname,const char * mode)968 LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
969 const char *chunkname, const char *mode) {
970 ZIO z;
971 int status;
972 lua_lock(L);
973 if (!chunkname) chunkname = "?";
974 luaZ_init(L, &z, reader, data);
975 status = luaD_protectedparser(L, &z, chunkname, mode);
976 if (status == LUA_OK) { /* no errors? */
977 LClosure *f = clLvalue(L->top - 1); /* get newly created function */
978 if (f->nupvalues >= 1) { /* does it have an upvalue? */
979 /* get global table from registry */
980 Table *reg = hvalue(&G(L)->l_registry);
981 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
982 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
983 setobj(L, f->upvals[0]->v, gt);
984 luaC_upvalbarrier(L, f->upvals[0]);
985 }
986 }
987 lua_unlock(L);
988 return status;
989 }
990
991
lua_dump(lua_State * L,lua_Writer writer,void * data,int strip)992 LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
993 int status;
994 TValue *o;
995 lua_lock(L);
996 api_checknelems(L, 1);
997 o = L->top - 1;
998 if (isLfunction(o))
999 status = luaU_dump(L, getproto(o), writer, data, strip);
1000 else
1001 status = 1;
1002 lua_unlock(L);
1003 return status;
1004 }
1005
1006
lua_status(lua_State * L)1007 LUA_API int lua_status (lua_State *L) {
1008 return L->status;
1009 }
1010
1011
1012 /*
1013 ** Garbage-collection function
1014 */
1015
lua_gc(lua_State * L,int what,int data)1016 LUA_API int lua_gc (lua_State *L, int what, int data) {
1017 int res = 0;
1018 global_State *g;
1019 lua_lock(L);
1020 g = G(L);
1021 switch (what) {
1022 case LUA_GCSTOP: {
1023 g->gcrunning = 0;
1024 break;
1025 }
1026 case LUA_GCRESTART: {
1027 luaE_setdebt(g, 0);
1028 g->gcrunning = 1;
1029 break;
1030 }
1031 case LUA_GCCOLLECT: {
1032 luaC_fullgc(L, 0);
1033 break;
1034 }
1035 case LUA_GCCOUNT: {
1036 /* GC values are expressed in Kbytes: #bytes/2^10 */
1037 res = cast_int(gettotalbytes(g) >> 10);
1038 break;
1039 }
1040 case LUA_GCCOUNTB: {
1041 res = cast_int(gettotalbytes(g) & 0x3ff);
1042 break;
1043 }
1044 case LUA_GCSTEP: {
1045 l_mem debt = 1; /* =1 to signal that it did an actual step */
1046 int oldrunning = g->gcrunning;
1047 g->gcrunning = 1; /* allow GC to run */
1048 if (data == 0) {
1049 luaE_setdebt(g, -GCSTEPSIZE); /* to do a "small" step */
1050 luaC_step(L);
1051 }
1052 else { /* add 'data' to total debt */
1053 debt = cast(l_mem, data) * 1024 + g->GCdebt;
1054 luaE_setdebt(g, debt);
1055 luaC_checkGC(L);
1056 }
1057 g->gcrunning = oldrunning; /* restore previous state */
1058 if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
1059 res = 1; /* signal it */
1060 break;
1061 }
1062 case LUA_GCSETPAUSE: {
1063 res = g->gcpause;
1064 g->gcpause = data;
1065 break;
1066 }
1067 case LUA_GCSETSTEPMUL: {
1068 res = g->gcstepmul;
1069 if (data < 40) data = 40; /* avoid ridiculous low values (and 0) */
1070 g->gcstepmul = data;
1071 break;
1072 }
1073 case LUA_GCISRUNNING: {
1074 res = g->gcrunning;
1075 break;
1076 }
1077 default: res = -1; /* invalid option */
1078 }
1079 lua_unlock(L);
1080 return res;
1081 }
1082
1083
1084
1085 /*
1086 ** miscellaneous functions
1087 */
1088
1089
lua_error(lua_State * L)1090 LUA_API int lua_error (lua_State *L) {
1091 lua_lock(L);
1092 api_checknelems(L, 1);
1093 luaG_errormsg(L);
1094 /* code unreachable; will unlock when control actually leaves the kernel */
1095 return 0; /* to avoid warnings */
1096 }
1097
1098
lua_next(lua_State * L,int idx)1099 LUA_API int lua_next (lua_State *L, int idx) {
1100 StkId t;
1101 int more;
1102 lua_lock(L);
1103 t = index2addr(L, idx);
1104 api_check(L, ttistable(t), "table expected");
1105 more = luaH_next(L, hvalue(t), L->top - 1);
1106 if (more) {
1107 api_incr_top(L);
1108 }
1109 else /* no more elements */
1110 L->top -= 1; /* remove key */
1111 lua_unlock(L);
1112 return more;
1113 }
1114
1115
lua_concat(lua_State * L,int n)1116 LUA_API void lua_concat (lua_State *L, int n) {
1117 lua_lock(L);
1118 api_checknelems(L, n);
1119 if (n >= 2) {
1120 luaC_checkGC(L);
1121 luaV_concat(L, n);
1122 }
1123 else if (n == 0) { /* push empty string */
1124 setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
1125 api_incr_top(L);
1126 }
1127 /* else n == 1; nothing to do */
1128 lua_unlock(L);
1129 }
1130
1131
lua_len(lua_State * L,int idx)1132 LUA_API void lua_len (lua_State *L, int idx) {
1133 StkId t;
1134 lua_lock(L);
1135 t = index2addr(L, idx);
1136 luaV_objlen(L, L->top, t);
1137 api_incr_top(L);
1138 lua_unlock(L);
1139 }
1140
1141
lua_getallocf(lua_State * L,void ** ud)1142 LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
1143 lua_Alloc f;
1144 lua_lock(L);
1145 if (ud) *ud = G(L)->ud;
1146 f = G(L)->frealloc;
1147 lua_unlock(L);
1148 return f;
1149 }
1150
1151
lua_setallocf(lua_State * L,lua_Alloc f,void * ud)1152 LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
1153 lua_lock(L);
1154 G(L)->ud = ud;
1155 G(L)->frealloc = f;
1156 lua_unlock(L);
1157 }
1158
1159
lua_newuserdata(lua_State * L,size_t size)1160 LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
1161 Udata *u;
1162 lua_lock(L);
1163 luaC_checkGC(L);
1164 u = luaS_newudata(L, size);
1165 setuvalue(L, L->top, u);
1166 api_incr_top(L);
1167 lua_unlock(L);
1168 return getudatamem(u);
1169 }
1170
1171
1172
aux_upvalue(StkId fi,int n,TValue ** val,CClosure ** owner,UpVal ** uv)1173 static const char *aux_upvalue (StkId fi, int n, TValue **val,
1174 CClosure **owner, UpVal **uv) {
1175 switch (ttype(fi)) {
1176 case LUA_TCCL: { /* C closure */
1177 CClosure *f = clCvalue(fi);
1178 if (!(1 <= n && n <= f->nupvalues)) return NULL;
1179 *val = &f->upvalue[n-1];
1180 if (owner) *owner = f;
1181 return "";
1182 }
1183 case LUA_TLCL: { /* Lua closure */
1184 LClosure *f = clLvalue(fi);
1185 TString *name;
1186 Proto *p = f->p;
1187 if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1188 *val = f->upvals[n-1]->v;
1189 if (uv) *uv = f->upvals[n - 1];
1190 name = p->upvalues[n-1].name;
1191 return (name == NULL) ? "(*no name)" : getstr(name);
1192 }
1193 default: return NULL; /* not a closure */
1194 }
1195 }
1196
1197
lua_getupvalue(lua_State * L,int funcindex,int n)1198 LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
1199 const char *name;
1200 TValue *val = NULL; /* to avoid warnings */
1201 lua_lock(L);
1202 name = aux_upvalue(index2addr(L, funcindex), n, &val, NULL, NULL);
1203 if (name) {
1204 setobj2s(L, L->top, val);
1205 api_incr_top(L);
1206 }
1207 lua_unlock(L);
1208 return name;
1209 }
1210
1211
lua_setupvalue(lua_State * L,int funcindex,int n)1212 LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
1213 const char *name;
1214 TValue *val = NULL; /* to avoid warnings */
1215 CClosure *owner = NULL;
1216 UpVal *uv = NULL;
1217 StkId fi;
1218 lua_lock(L);
1219 fi = index2addr(L, funcindex);
1220 api_checknelems(L, 1);
1221 name = aux_upvalue(fi, n, &val, &owner, &uv);
1222 if (name) {
1223 L->top--;
1224 setobj(L, val, L->top);
1225 if (owner) { luaC_barrier(L, owner, L->top); }
1226 else if (uv) { luaC_upvalbarrier(L, uv); }
1227 }
1228 lua_unlock(L);
1229 return name;
1230 }
1231
1232
getupvalref(lua_State * L,int fidx,int n,LClosure ** pf)1233 static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1234 LClosure *f;
1235 StkId fi = index2addr(L, fidx);
1236 api_check(L, ttisLclosure(fi), "Lua function expected");
1237 f = clLvalue(fi);
1238 api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1239 if (pf) *pf = f;
1240 return &f->upvals[n - 1]; /* get its upvalue pointer */
1241 }
1242
1243
lua_upvalueid(lua_State * L,int fidx,int n)1244 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1245 StkId fi = index2addr(L, fidx);
1246 switch (ttype(fi)) {
1247 case LUA_TLCL: { /* lua closure */
1248 return *getupvalref(L, fidx, n, NULL);
1249 }
1250 case LUA_TCCL: { /* C closure */
1251 CClosure *f = clCvalue(fi);
1252 api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1253 return &f->upvalue[n - 1];
1254 }
1255 default: {
1256 api_check(L, 0, "closure expected");
1257 return NULL;
1258 }
1259 }
1260 }
1261
1262
lua_upvaluejoin(lua_State * L,int fidx1,int n1,int fidx2,int n2)1263 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
1264 int fidx2, int n2) {
1265 LClosure *f1;
1266 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1267 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1268 luaC_upvdeccount(L, *up1);
1269 *up1 = *up2;
1270 (*up1)->refcount++;
1271 if (upisopen(*up1)) (*up1)->u.open.touched = 1;
1272 luaC_upvalbarrier(L, *up1);
1273 }
1274
1275
1276