lapi.c 32 KB

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