lapi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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_NUMTAGS, "invalid tag");
  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_TSHRSTR: return tsvalue(o)->shrlen;
  322. case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
  323. case LUA_TUSERDATA: return uvalue(o)->len;
  324. case LUA_TTABLE: 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_TLCF: return cast_voidp(cast_sizet(fvalue(o)));
  361. case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA:
  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. setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */
  468. api_incr_top(L);
  469. lua_unlock(L);
  470. }
  471. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  472. lua_lock(L);
  473. setpvalue(s2v(L->top), p);
  474. api_incr_top(L);
  475. lua_unlock(L);
  476. }
  477. LUA_API int lua_pushthread (lua_State *L) {
  478. lua_lock(L);
  479. setthvalue(L, s2v(L->top), L);
  480. api_incr_top(L);
  481. lua_unlock(L);
  482. return (G(L)->mainthread == L);
  483. }
  484. /*
  485. ** get functions (Lua -> stack)
  486. */
  487. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  488. const TValue *slot;
  489. TString *str = luaS_new(L, k);
  490. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  491. setobj2s(L, L->top, slot);
  492. api_incr_top(L);
  493. }
  494. else {
  495. setsvalue2s(L, L->top, str);
  496. api_incr_top(L);
  497. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  498. }
  499. lua_unlock(L);
  500. return ttype(s2v(L->top - 1));
  501. }
  502. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  503. Table *reg = hvalue(&G(L)->l_registry);
  504. lua_lock(L);
  505. return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  506. }
  507. LUA_API int lua_gettable (lua_State *L, int idx) {
  508. const TValue *slot;
  509. TValue *t;
  510. lua_lock(L);
  511. t = index2value(L, idx);
  512. if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
  513. setobj2s(L, L->top - 1, slot);
  514. }
  515. else
  516. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  517. lua_unlock(L);
  518. return ttype(s2v(L->top - 1));
  519. }
  520. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  521. lua_lock(L);
  522. return auxgetstr(L, index2value(L, idx), k);
  523. }
  524. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  525. TValue *t;
  526. const TValue *slot;
  527. lua_lock(L);
  528. t = index2value(L, idx);
  529. if (luaV_fastgeti(L, t, n, slot)) {
  530. setobj2s(L, L->top, slot);
  531. }
  532. else {
  533. TValue aux;
  534. setivalue(&aux, n);
  535. luaV_finishget(L, t, &aux, L->top, slot);
  536. }
  537. api_incr_top(L);
  538. lua_unlock(L);
  539. return ttype(s2v(L->top - 1));
  540. }
  541. static int finishrawget (lua_State *L, const TValue *val) {
  542. if (isempty(val)) /* avoid copying empty items to the stack */
  543. setnilvalue(s2v(L->top));
  544. else
  545. setobj2s(L, L->top, val);
  546. api_incr_top(L);
  547. lua_unlock(L);
  548. return ttype(s2v(L->top - 1));
  549. }
  550. static Table *gettable (lua_State *L, int idx) {
  551. TValue *t = index2value(L, idx);
  552. api_check(L, ttistable(t), "table expected");
  553. return hvalue(t);
  554. }
  555. LUA_API int lua_rawget (lua_State *L, int idx) {
  556. Table *t;
  557. const TValue *val;
  558. lua_lock(L);
  559. api_checknelems(L, 1);
  560. t = gettable(L, idx);
  561. val = luaH_get(t, s2v(L->top - 1));
  562. L->top--; /* remove key */
  563. return finishrawget(L, val);
  564. }
  565. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  566. Table *t;
  567. lua_lock(L);
  568. t = gettable(L, idx);
  569. return finishrawget(L, luaH_getint(t, n));
  570. }
  571. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  572. Table *t;
  573. TValue k;
  574. lua_lock(L);
  575. t = gettable(L, idx);
  576. setpvalue(&k, cast_voidp(p));
  577. return finishrawget(L, luaH_get(t, &k));
  578. }
  579. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  580. Table *t;
  581. lua_lock(L);
  582. t = luaH_new(L);
  583. sethvalue2s(L, L->top, t);
  584. api_incr_top(L);
  585. if (narray > 0 || nrec > 0)
  586. luaH_resize(L, t, narray, nrec);
  587. luaC_checkGC(L);
  588. lua_unlock(L);
  589. }
  590. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  591. const TValue *obj;
  592. Table *mt;
  593. int res = 0;
  594. lua_lock(L);
  595. obj = index2value(L, objindex);
  596. switch (ttype(obj)) {
  597. case LUA_TTABLE:
  598. mt = hvalue(obj)->metatable;
  599. break;
  600. case LUA_TUSERDATA:
  601. mt = uvalue(obj)->metatable;
  602. break;
  603. default:
  604. mt = G(L)->mt[ttype(obj)];
  605. break;
  606. }
  607. if (mt != NULL) {
  608. sethvalue2s(L, L->top, mt);
  609. api_incr_top(L);
  610. res = 1;
  611. }
  612. lua_unlock(L);
  613. return res;
  614. }
  615. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  616. TValue *o;
  617. int t;
  618. lua_lock(L);
  619. o = index2value(L, idx);
  620. api_check(L, ttisfulluserdata(o), "full userdata expected");
  621. if (n <= 0 || n > uvalue(o)->nuvalue) {
  622. setnilvalue(s2v(L->top));
  623. t = LUA_TNONE;
  624. }
  625. else {
  626. setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
  627. t = ttype(s2v(L->top));
  628. }
  629. api_incr_top(L);
  630. lua_unlock(L);
  631. return t;
  632. }
  633. /*
  634. ** set functions (stack -> Lua)
  635. */
  636. /*
  637. ** t[k] = value at the top of the stack (where 'k' is a string)
  638. */
  639. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  640. const TValue *slot;
  641. TString *str = luaS_new(L, k);
  642. api_checknelems(L, 1);
  643. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  644. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  645. L->top--; /* pop value */
  646. }
  647. else {
  648. setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
  649. api_incr_top(L);
  650. luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
  651. L->top -= 2; /* pop value and key */
  652. }
  653. lua_unlock(L); /* lock done by caller */
  654. }
  655. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  656. Table *reg = hvalue(&G(L)->l_registry);
  657. lua_lock(L); /* unlock done in 'auxsetstr' */
  658. auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  659. }
  660. LUA_API void lua_settable (lua_State *L, int idx) {
  661. TValue *t;
  662. const TValue *slot;
  663. lua_lock(L);
  664. api_checknelems(L, 2);
  665. t = index2value(L, idx);
  666. if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
  667. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  668. }
  669. else
  670. luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
  671. L->top -= 2; /* pop index and value */
  672. lua_unlock(L);
  673. }
  674. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  675. lua_lock(L); /* unlock done in 'auxsetstr' */
  676. auxsetstr(L, index2value(L, idx), k);
  677. }
  678. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  679. TValue *t;
  680. const TValue *slot;
  681. lua_lock(L);
  682. api_checknelems(L, 1);
  683. t = index2value(L, idx);
  684. if (luaV_fastgeti(L, t, n, slot)) {
  685. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  686. }
  687. else {
  688. TValue aux;
  689. setivalue(&aux, n);
  690. luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
  691. }
  692. L->top--; /* pop value */
  693. lua_unlock(L);
  694. }
  695. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  696. Table *t;
  697. TValue *slot;
  698. lua_lock(L);
  699. api_checknelems(L, n);
  700. t = gettable(L, idx);
  701. slot = luaH_set(L, t, key);
  702. setobj2t(L, slot, s2v(L->top - 1));
  703. L->top -= n;
  704. invalidateTMcache(t);
  705. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  706. lua_unlock(L);
  707. }
  708. LUA_API void lua_rawset (lua_State *L, int idx) {
  709. aux_rawset(L, idx, s2v(L->top - 2), 2);
  710. }
  711. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  712. TValue k;
  713. setpvalue(&k, cast_voidp(p));
  714. aux_rawset(L, idx, &k, 1);
  715. }
  716. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  717. Table *t;
  718. lua_lock(L);
  719. api_checknelems(L, 1);
  720. t = gettable(L, idx);
  721. luaH_setint(L, t, n, s2v(L->top - 1));
  722. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  723. L->top--;
  724. lua_unlock(L);
  725. }
  726. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  727. TValue *obj;
  728. Table *mt;
  729. lua_lock(L);
  730. api_checknelems(L, 1);
  731. obj = index2value(L, objindex);
  732. if (ttisnil(s2v(L->top - 1)))
  733. mt = NULL;
  734. else {
  735. api_check(L, ttistable(s2v(L->top - 1)), "table expected");
  736. mt = hvalue(s2v(L->top - 1));
  737. }
  738. switch (ttype(obj)) {
  739. case LUA_TTABLE: {
  740. hvalue(obj)->metatable = mt;
  741. if (mt) {
  742. luaC_objbarrier(L, gcvalue(obj), mt);
  743. luaC_checkfinalizer(L, gcvalue(obj), mt);
  744. }
  745. break;
  746. }
  747. case LUA_TUSERDATA: {
  748. uvalue(obj)->metatable = mt;
  749. if (mt) {
  750. luaC_objbarrier(L, uvalue(obj), mt);
  751. luaC_checkfinalizer(L, gcvalue(obj), mt);
  752. }
  753. break;
  754. }
  755. default: {
  756. G(L)->mt[ttype(obj)] = mt;
  757. break;
  758. }
  759. }
  760. L->top--;
  761. lua_unlock(L);
  762. return 1;
  763. }
  764. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  765. TValue *o;
  766. int res;
  767. lua_lock(L);
  768. api_checknelems(L, 1);
  769. o = index2value(L, idx);
  770. api_check(L, ttisfulluserdata(o), "full userdata expected");
  771. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  772. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  773. else {
  774. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
  775. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  776. res = 1;
  777. }
  778. L->top--;
  779. lua_unlock(L);
  780. return res;
  781. }
  782. /*
  783. ** 'load' and 'call' functions (run Lua code)
  784. */
  785. #define checkresults(L,na,nr) \
  786. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  787. "results from function overflow current stack size")
  788. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  789. lua_KContext ctx, lua_KFunction k) {
  790. StkId func;
  791. lua_lock(L);
  792. api_check(L, k == NULL || !isLua(L->ci),
  793. "cannot use continuations inside hooks");
  794. api_checknelems(L, nargs+1);
  795. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  796. checkresults(L, nargs, nresults);
  797. func = L->top - (nargs+1);
  798. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  799. L->ci->u.c.k = k; /* save continuation */
  800. L->ci->u.c.ctx = ctx; /* save context */
  801. luaD_call(L, func, nresults); /* do the call */
  802. }
  803. else /* no continuation or no yieldable */
  804. luaD_callnoyield(L, func, nresults); /* just do the call */
  805. adjustresults(L, nresults);
  806. lua_unlock(L);
  807. }
  808. /*
  809. ** Execute a protected call.
  810. */
  811. struct CallS { /* data to 'f_call' */
  812. StkId func;
  813. int nresults;
  814. };
  815. static void f_call (lua_State *L, void *ud) {
  816. struct CallS *c = cast(struct CallS *, ud);
  817. luaD_callnoyield(L, c->func, c->nresults);
  818. }
  819. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  820. lua_KContext ctx, lua_KFunction k) {
  821. struct CallS c;
  822. int status;
  823. ptrdiff_t func;
  824. lua_lock(L);
  825. api_check(L, k == NULL || !isLua(L->ci),
  826. "cannot use continuations inside hooks");
  827. api_checknelems(L, nargs+1);
  828. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  829. checkresults(L, nargs, nresults);
  830. if (errfunc == 0)
  831. func = 0;
  832. else {
  833. StkId o = index2stack(L, errfunc);
  834. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  835. func = savestack(L, o);
  836. }
  837. c.func = L->top - (nargs+1); /* function to be called */
  838. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  839. c.nresults = nresults; /* do a 'conventional' protected call */
  840. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  841. }
  842. else { /* prepare continuation (call is already protected by 'resume') */
  843. CallInfo *ci = L->ci;
  844. ci->u.c.k = k; /* save continuation */
  845. ci->u.c.ctx = ctx; /* save context */
  846. /* save information for error recovery */
  847. ci->u2.funcidx = cast_int(savestack(L, c.func));
  848. ci->u.c.old_errfunc = L->errfunc;
  849. L->errfunc = func;
  850. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  851. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  852. luaD_call(L, c.func, nresults); /* do the call */
  853. ci->callstatus &= ~CIST_YPCALL;
  854. L->errfunc = ci->u.c.old_errfunc;
  855. status = LUA_OK; /* if it is here, there were no errors */
  856. }
  857. adjustresults(L, nresults);
  858. lua_unlock(L);
  859. return status;
  860. }
  861. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  862. const char *chunkname, const char *mode) {
  863. ZIO z;
  864. int status;
  865. lua_lock(L);
  866. if (!chunkname) chunkname = "?";
  867. luaZ_init(L, &z, reader, data);
  868. status = luaD_protectedparser(L, &z, chunkname, mode);
  869. if (status == LUA_OK) { /* no errors? */
  870. LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
  871. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  872. /* get global table from registry */
  873. Table *reg = hvalue(&G(L)->l_registry);
  874. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  875. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  876. setobj(L, f->upvals[0]->v, gt);
  877. luaC_barrier(L, f->upvals[0], gt);
  878. }
  879. }
  880. lua_unlock(L);
  881. return status;
  882. }
  883. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  884. int status;
  885. TValue *o;
  886. lua_lock(L);
  887. api_checknelems(L, 1);
  888. o = s2v(L->top - 1);
  889. if (isLfunction(o))
  890. status = luaU_dump(L, getproto(o), writer, data, strip);
  891. else
  892. status = 1;
  893. lua_unlock(L);
  894. return status;
  895. }
  896. LUA_API int lua_status (lua_State *L) {
  897. return L->status;
  898. }
  899. /*
  900. ** Garbage-collection function
  901. */
  902. LUA_API int lua_gc (lua_State *L, int what, ...) {
  903. va_list argp;
  904. int res = 0;
  905. global_State *g = G(L);
  906. lua_lock(L);
  907. va_start(argp, what);
  908. switch (what) {
  909. case LUA_GCSTOP: {
  910. g->gcrunning = 0;
  911. break;
  912. }
  913. case LUA_GCRESTART: {
  914. luaE_setdebt(g, 0);
  915. g->gcrunning = 1;
  916. break;
  917. }
  918. case LUA_GCCOLLECT: {
  919. luaC_fullgc(L, 0);
  920. break;
  921. }
  922. case LUA_GCCOUNT: {
  923. /* GC values are expressed in Kbytes: #bytes/2^10 */
  924. res = cast_int(gettotalbytes(g) >> 10);
  925. break;
  926. }
  927. case LUA_GCCOUNTB: {
  928. res = cast_int(gettotalbytes(g) & 0x3ff);
  929. break;
  930. }
  931. case LUA_GCSTEP: {
  932. int data = va_arg(argp, int);
  933. l_mem debt = 1; /* =1 to signal that it did an actual step */
  934. lu_byte oldrunning = g->gcrunning;
  935. g->gcrunning = 1; /* allow GC to run */
  936. if (data == 0) {
  937. luaE_setdebt(g, 0); /* do a basic step */
  938. luaC_step(L);
  939. }
  940. else { /* add 'data' to total debt */
  941. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  942. luaE_setdebt(g, debt);
  943. luaC_checkGC(L);
  944. }
  945. g->gcrunning = oldrunning; /* restore previous state */
  946. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  947. res = 1; /* signal it */
  948. break;
  949. }
  950. case LUA_GCSETPAUSE: {
  951. int data = va_arg(argp, int);
  952. res = getgcparam(g->gcpause);
  953. setgcparam(g->gcpause, data);
  954. break;
  955. }
  956. case LUA_GCSETSTEPMUL: {
  957. int data = va_arg(argp, int);
  958. res = getgcparam(g->gcstepmul);
  959. setgcparam(g->gcstepmul, data);
  960. break;
  961. }
  962. case LUA_GCISRUNNING: {
  963. res = g->gcrunning;
  964. break;
  965. }
  966. case LUA_GCGEN: {
  967. int minormul = va_arg(argp, int);
  968. int majormul = va_arg(argp, int);
  969. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  970. if (minormul != 0)
  971. g->genminormul = minormul;
  972. if (majormul != 0)
  973. setgcparam(g->genmajormul, majormul);
  974. luaC_changemode(L, KGC_GEN);
  975. break;
  976. }
  977. case LUA_GCINC: {
  978. int pause = va_arg(argp, int);
  979. int stepmul = va_arg(argp, int);
  980. int stepsize = va_arg(argp, int);
  981. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  982. if (pause != 0)
  983. setgcparam(g->gcpause, pause);
  984. if (stepmul != 0)
  985. setgcparam(g->gcstepmul, stepmul);
  986. if (stepsize != 0)
  987. g->gcstepsize = stepsize;
  988. luaC_changemode(L, KGC_INC);
  989. break;
  990. }
  991. default: res = -1; /* invalid option */
  992. }
  993. va_end(argp);
  994. lua_unlock(L);
  995. return res;
  996. }
  997. /*
  998. ** miscellaneous functions
  999. */
  1000. LUA_API int lua_error (lua_State *L) {
  1001. lua_lock(L);
  1002. api_checknelems(L, 1);
  1003. luaG_errormsg(L);
  1004. /* code unreachable; will unlock when control actually leaves the kernel */
  1005. return 0; /* to avoid warnings */
  1006. }
  1007. LUA_API int lua_next (lua_State *L, int idx) {
  1008. Table *t;
  1009. int more;
  1010. lua_lock(L);
  1011. api_checknelems(L, 1);
  1012. t = gettable(L, idx);
  1013. more = luaH_next(L, t, L->top - 1);
  1014. if (more) {
  1015. api_incr_top(L);
  1016. }
  1017. else /* no more elements */
  1018. L->top -= 1; /* remove key */
  1019. lua_unlock(L);
  1020. return more;
  1021. }
  1022. LUA_API void lua_toclose (lua_State *L, int idx) {
  1023. int nresults;
  1024. StkId o;
  1025. lua_lock(L);
  1026. o = index2stack(L, idx);
  1027. nresults = L->ci->nresults;
  1028. api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o,
  1029. "marked index below or equal new one");
  1030. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1031. if (!hastocloseCfunc(nresults)) /* function not marked yet? */
  1032. L->ci->nresults = codeNresults(nresults); /* mark it */
  1033. lua_assert(hastocloseCfunc(L->ci->nresults));
  1034. lua_unlock(L);
  1035. }
  1036. LUA_API void lua_concat (lua_State *L, int n) {
  1037. lua_lock(L);
  1038. api_checknelems(L, n);
  1039. if (n >= 2) {
  1040. luaV_concat(L, n);
  1041. }
  1042. else if (n == 0) { /* push empty string */
  1043. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1044. api_incr_top(L);
  1045. }
  1046. /* else n == 1; nothing to do */
  1047. luaC_checkGC(L);
  1048. lua_unlock(L);
  1049. }
  1050. LUA_API void lua_len (lua_State *L, int idx) {
  1051. TValue *t;
  1052. lua_lock(L);
  1053. t = index2value(L, idx);
  1054. luaV_objlen(L, L->top, t);
  1055. api_incr_top(L);
  1056. lua_unlock(L);
  1057. }
  1058. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1059. lua_Alloc f;
  1060. lua_lock(L);
  1061. if (ud) *ud = G(L)->ud;
  1062. f = G(L)->frealloc;
  1063. lua_unlock(L);
  1064. return f;
  1065. }
  1066. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1067. lua_lock(L);
  1068. G(L)->ud = ud;
  1069. G(L)->frealloc = f;
  1070. lua_unlock(L);
  1071. }
  1072. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1073. lua_lock(L);
  1074. G(L)->ud_warn = ud;
  1075. G(L)->warnf = f;
  1076. lua_unlock(L);
  1077. }
  1078. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1079. lua_lock(L);
  1080. luaE_warning(L, msg, tocont);
  1081. lua_unlock(L);
  1082. }
  1083. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1084. Udata *u;
  1085. lua_lock(L);
  1086. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1087. u = luaS_newudata(L, size, nuvalue);
  1088. setuvalue(L, s2v(L->top), u);
  1089. api_incr_top(L);
  1090. luaC_checkGC(L);
  1091. lua_unlock(L);
  1092. return getudatamem(u);
  1093. }
  1094. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1095. GCObject **owner) {
  1096. switch (ttypetag(fi)) {
  1097. case LUA_TCCL: { /* C closure */
  1098. CClosure *f = clCvalue(fi);
  1099. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1100. return NULL; /* 'n' not in [1, f->nupvalues] */
  1101. *val = &f->upvalue[n-1];
  1102. if (owner) *owner = obj2gco(f);
  1103. return "";
  1104. }
  1105. case LUA_TLCL: { /* Lua closure */
  1106. LClosure *f = clLvalue(fi);
  1107. TString *name;
  1108. Proto *p = f->p;
  1109. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1110. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1111. *val = f->upvals[n-1]->v;
  1112. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1113. name = p->upvalues[n-1].name;
  1114. return (name == NULL) ? "(no name)" : getstr(name);
  1115. }
  1116. default: return NULL; /* not a closure */
  1117. }
  1118. }
  1119. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1120. const char *name;
  1121. TValue *val = NULL; /* to avoid warnings */
  1122. lua_lock(L);
  1123. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1124. if (name) {
  1125. setobj2s(L, L->top, val);
  1126. api_incr_top(L);
  1127. }
  1128. lua_unlock(L);
  1129. return name;
  1130. }
  1131. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1132. const char *name;
  1133. TValue *val = NULL; /* to avoid warnings */
  1134. GCObject *owner = NULL; /* to avoid warnings */
  1135. TValue *fi;
  1136. lua_lock(L);
  1137. fi = index2value(L, funcindex);
  1138. api_checknelems(L, 1);
  1139. name = aux_upvalue(fi, n, &val, &owner);
  1140. if (name) {
  1141. L->top--;
  1142. setobj(L, val, s2v(L->top));
  1143. luaC_barrier(L, owner, val);
  1144. }
  1145. lua_unlock(L);
  1146. return name;
  1147. }
  1148. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1149. LClosure *f;
  1150. TValue *fi = index2value(L, fidx);
  1151. api_check(L, ttisLclosure(fi), "Lua function expected");
  1152. f = clLvalue(fi);
  1153. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1154. if (pf) *pf = f;
  1155. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1156. }
  1157. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1158. TValue *fi = index2value(L, fidx);
  1159. switch (ttypetag(fi)) {
  1160. case LUA_TLCL: { /* lua closure */
  1161. return *getupvalref(L, fidx, n, NULL);
  1162. }
  1163. case LUA_TCCL: { /* C closure */
  1164. CClosure *f = clCvalue(fi);
  1165. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1166. return &f->upvalue[n - 1];
  1167. }
  1168. default: {
  1169. api_check(L, 0, "closure expected");
  1170. return NULL;
  1171. }
  1172. }
  1173. }
  1174. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1175. int fidx2, int n2) {
  1176. LClosure *f1;
  1177. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1178. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1179. *up1 = *up2;
  1180. luaC_objbarrier(L, f1, *up1);
  1181. }