lapi.c 33 KB

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