lapi.c 33 KB

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