lapi.c 32 KB

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