interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lapi.c

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