lapi.c 34 KB

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