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