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