lapi.c 32 KB

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