lapi.c 32 KB

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