lapi.c 34 KB

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