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