lapi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. ** $Id: lapi.c $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lapi_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <limits.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include "lua.h"
  13. #include "lapi.h"
  14. #include "ldebug.h"
  15. #include "ldo.h"
  16. #include "lfunc.h"
  17. #include "lgc.h"
  18. #include "lmem.h"
  19. #include "lobject.h"
  20. #include "lstate.h"
  21. #include "lstring.h"
  22. #include "ltable.h"
  23. #include "ltm.h"
  24. #include "lundump.h"
  25. #include "lvm.h"
  26. const char lua_ident[] =
  27. "$LuaVersion: " LUA_COPYRIGHT " $"
  28. "$LuaAuthors: " LUA_AUTHORS " $";
  29. /*
  30. ** Test for a valid index.
  31. ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
  32. ** However, it covers the most common cases in a faster way.
  33. */
  34. #define isvalid(L, o) (!ttisnil(o) || o != &G(L)->nilvalue)
  35. /* test for pseudo index */
  36. #define ispseudo(i) ((i) <= LUA_REGISTRYINDEX)
  37. /* test for upvalue */
  38. #define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
  39. static TValue *index2value (lua_State *L, int idx) {
  40. CallInfo *ci = L->ci;
  41. if (idx > 0) {
  42. StkId o = ci->func + idx;
  43. api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
  44. if (o >= L->top) return &G(L)->nilvalue;
  45. else return s2v(o);
  46. }
  47. else if (!ispseudo(idx)) { /* negative index */
  48. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  49. return s2v(L->top + idx);
  50. }
  51. else if (idx == LUA_REGISTRYINDEX)
  52. return &G(L)->l_registry;
  53. else { /* upvalues */
  54. idx = LUA_REGISTRYINDEX - idx;
  55. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  56. if (ttislcf(s2v(ci->func))) /* light C function? */
  57. return &G(L)->nilvalue; /* it has no upvalues */
  58. else {
  59. CClosure *func = clCvalue(s2v(ci->func));
  60. return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
  61. }
  62. }
  63. }
  64. static StkId index2stack (lua_State *L, int idx) {
  65. CallInfo *ci = L->ci;
  66. if (idx > 0) {
  67. StkId o = ci->func + idx;
  68. api_check(L, o < L->top, "unacceptable index");
  69. return o;
  70. }
  71. else { /* non-positive index */
  72. api_check(L, idx != 0 && -idx <= L->top - (ci->func + 1), "invalid index");
  73. api_check(L, !ispseudo(idx), "invalid index");
  74. return L->top + idx;
  75. }
  76. }
  77. LUA_API int lua_checkstack (lua_State *L, int n) {
  78. int res;
  79. CallInfo *ci = L->ci;
  80. lua_lock(L);
  81. api_check(L, n >= 0, "negative 'n'");
  82. if (L->stack_last - L->top > n) /* stack large enough? */
  83. res = 1; /* yes; check is OK */
  84. else { /* no; need to grow stack */
  85. int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
  86. if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
  87. res = 0; /* no */
  88. else /* try to grow stack */
  89. res = luaD_growstack(L, n, 0);
  90. }
  91. if (res && ci->top < L->top + n)
  92. ci->top = L->top + n; /* adjust frame top */
  93. lua_unlock(L);
  94. return res;
  95. }
  96. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  97. int i;
  98. if (from == to) return;
  99. lua_lock(to);
  100. api_checknelems(from, n);
  101. api_check(from, G(from) == G(to), "moving among independent states");
  102. api_check(from, to->ci->top - to->top >= n, "stack overflow");
  103. from->top -= n;
  104. for (i = 0; i < n; i++) {
  105. setobjs2s(to, to->top, from->top + i);
  106. to->top++; /* stack already checked by previous 'api_check' */
  107. }
  108. lua_unlock(to);
  109. }
  110. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  111. lua_CFunction old;
  112. lua_lock(L);
  113. old = G(L)->panic;
  114. G(L)->panic = panicf;
  115. lua_unlock(L);
  116. return old;
  117. }
  118. LUA_API lua_Number lua_version (lua_State *L) {
  119. UNUSED(L);
  120. return LUA_VERSION_NUM;
  121. }
  122. /*
  123. ** basic stack manipulation
  124. */
  125. /*
  126. ** convert an acceptable stack index into an absolute index
  127. */
  128. LUA_API int lua_absindex (lua_State *L, int idx) {
  129. return (idx > 0 || ispseudo(idx))
  130. ? idx
  131. : cast_int(L->top - L->ci->func) + idx;
  132. }
  133. LUA_API int lua_gettop (lua_State *L) {
  134. return cast_int(L->top - (L->ci->func + 1));
  135. }
  136. LUA_API void lua_settop (lua_State *L, int idx) {
  137. StkId func = L->ci->func;
  138. int diff; /* difference for new top */
  139. lua_lock(L);
  140. if (idx >= 0) {
  141. api_check(L, idx <= L->ci->top - (func + 1), "new top too large");
  142. diff = (func + 1) + idx - L->top;
  143. for (; diff > 0; diff--)
  144. setnilvalue(s2v(L->top++)); /* clear new slots */
  145. }
  146. else {
  147. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  148. diff = idx + 1; /* will "subtract" index (as it is negative) */
  149. }
  150. luaF_close(L, L->top + diff, LUA_OK);
  151. L->top += diff; /* correct top only after closing any upvalue */
  152. lua_unlock(L);
  153. }
  154. /*
  155. ** Reverse the stack segment from 'from' to 'to'
  156. ** (auxiliary to 'lua_rotate')
  157. ** Note that we move(copy) only the value inside the stack.
  158. ** (We do not move additional fields that may exist.)
  159. */
  160. static void reverse (lua_State *L, StkId from, StkId to) {
  161. for (; from < to; from++, to--) {
  162. TValue temp;
  163. setobj(L, &temp, s2v(from));
  164. setobjs2s(L, from, to);
  165. setobj2s(L, to, &temp);
  166. }
  167. }
  168. /*
  169. ** Let x = AB, where A is a prefix of length 'n'. Then,
  170. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  171. */
  172. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  173. StkId p, t, m;
  174. lua_lock(L);
  175. t = L->top - 1; /* end of stack segment being rotated */
  176. p = index2stack(L, idx); /* start of segment */
  177. api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  178. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  179. reverse(L, p, m); /* reverse the prefix with length 'n' */
  180. reverse(L, m + 1, t); /* reverse the suffix */
  181. reverse(L, p, t); /* reverse the entire segment */
  182. lua_unlock(L);
  183. }
  184. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  185. TValue *fr, *to;
  186. lua_lock(L);
  187. fr = index2value(L, fromidx);
  188. to = index2value(L, toidx);
  189. api_check(l, isvalid(L, to), "invalid index");
  190. setobj(L, to, fr);
  191. if (isupvalue(toidx)) /* function upvalue? */
  192. luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
  193. /* LUA_REGISTRYINDEX does not need gc barrier
  194. (collector revisits it before finishing collection) */
  195. lua_unlock(L);
  196. }
  197. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  198. lua_lock(L);
  199. setobj2s(L, L->top, index2value(L, idx));
  200. api_incr_top(L);
  201. lua_unlock(L);
  202. }
  203. /*
  204. ** access functions (stack -> C)
  205. */
  206. LUA_API int lua_type (lua_State *L, int idx) {
  207. const TValue *o = index2value(L, idx);
  208. return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
  209. }
  210. LUA_API const char *lua_typename (lua_State *L, int t) {
  211. UNUSED(L);
  212. api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
  213. return ttypename(t);
  214. }
  215. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  216. const TValue *o = index2value(L, idx);
  217. return (ttislcf(o) || (ttisCclosure(o)));
  218. }
  219. LUA_API int lua_isinteger (lua_State *L, int idx) {
  220. const TValue *o = index2value(L, idx);
  221. return ttisinteger(o);
  222. }
  223. LUA_API int lua_isnumber (lua_State *L, int idx) {
  224. lua_Number n;
  225. const TValue *o = index2value(L, idx);
  226. return tonumber(o, &n);
  227. }
  228. LUA_API int lua_isstring (lua_State *L, int idx) {
  229. const TValue *o = index2value(L, idx);
  230. return (ttisstring(o) || cvt2str(o));
  231. }
  232. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  233. const TValue *o = index2value(L, idx);
  234. return (ttisfulluserdata(o) || ttislightuserdata(o));
  235. }
  236. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  237. const TValue *o1 = index2value(L, index1);
  238. const TValue *o2 = index2value(L, index2);
  239. return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
  240. }
  241. LUA_API void lua_arith (lua_State *L, int op) {
  242. lua_lock(L);
  243. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  244. api_checknelems(L, 2); /* all other operations expect two operands */
  245. else { /* for unary operations, add fake 2nd operand */
  246. api_checknelems(L, 1);
  247. setobjs2s(L, L->top, L->top - 1);
  248. api_incr_top(L);
  249. }
  250. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  251. luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
  252. L->top--; /* remove second operand */
  253. lua_unlock(L);
  254. }
  255. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  256. const TValue *o1;
  257. const TValue *o2;
  258. int i = 0;
  259. lua_lock(L); /* may call tag method */
  260. o1 = index2value(L, index1);
  261. o2 = index2value(L, index2);
  262. if (isvalid(L, o1) && isvalid(L, o2)) {
  263. switch (op) {
  264. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  265. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  266. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  267. default: api_check(L, 0, "invalid option");
  268. }
  269. }
  270. lua_unlock(L);
  271. return i;
  272. }
  273. LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
  274. size_t sz = luaO_str2num(s, s2v(L->top));
  275. if (sz != 0)
  276. api_incr_top(L);
  277. return sz;
  278. }
  279. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  280. lua_Number n;
  281. const TValue *o = index2value(L, idx);
  282. int isnum = tonumber(o, &n);
  283. if (!isnum)
  284. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  285. if (pisnum) *pisnum = isnum;
  286. return n;
  287. }
  288. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  289. lua_Integer res;
  290. const TValue *o = index2value(L, idx);
  291. int isnum = tointeger(o, &res);
  292. if (!isnum)
  293. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  294. if (pisnum) *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. LUA_API void lua_rawset (lua_State *L, int idx) {
  696. Table *t;
  697. TValue *slot;
  698. lua_lock(L);
  699. api_checknelems(L, 2);
  700. t = gettable(L, idx);
  701. slot = luaH_set(L, t, s2v(L->top - 2));
  702. setobj2t(L, slot, s2v(L->top - 1));
  703. invalidateTMcache(t);
  704. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  705. L->top -= 2;
  706. lua_unlock(L);
  707. }
  708. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  709. Table *t;
  710. lua_lock(L);
  711. api_checknelems(L, 1);
  712. t = gettable(L, idx);
  713. luaH_setint(L, t, n, s2v(L->top - 1));
  714. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  715. L->top--;
  716. lua_unlock(L);
  717. }
  718. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  719. Table *t;
  720. TValue k, *slot;
  721. lua_lock(L);
  722. api_checknelems(L, 1);
  723. t = gettable(L, idx);
  724. setpvalue(&k, cast_voidp(p));
  725. slot = luaH_set(L, t, &k);
  726. setobj2t(L, slot, s2v(L->top - 1));
  727. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  728. L->top--;
  729. lua_unlock(L);
  730. }
  731. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  732. TValue *obj;
  733. Table *mt;
  734. lua_lock(L);
  735. api_checknelems(L, 1);
  736. obj = index2value(L, objindex);
  737. if (ttisnil(s2v(L->top - 1)))
  738. mt = NULL;
  739. else {
  740. api_check(L, ttistable(s2v(L->top - 1)), "table expected");
  741. mt = hvalue(s2v(L->top - 1));
  742. }
  743. switch (ttype(obj)) {
  744. case LUA_TTABLE: {
  745. hvalue(obj)->metatable = mt;
  746. if (mt) {
  747. luaC_objbarrier(L, gcvalue(obj), mt);
  748. luaC_checkfinalizer(L, gcvalue(obj), mt);
  749. }
  750. break;
  751. }
  752. case LUA_TUSERDATA: {
  753. uvalue(obj)->metatable = mt;
  754. if (mt) {
  755. luaC_objbarrier(L, uvalue(obj), mt);
  756. luaC_checkfinalizer(L, gcvalue(obj), mt);
  757. }
  758. break;
  759. }
  760. default: {
  761. G(L)->mt[ttype(obj)] = mt;
  762. break;
  763. }
  764. }
  765. L->top--;
  766. lua_unlock(L);
  767. return 1;
  768. }
  769. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  770. TValue *o;
  771. int res;
  772. lua_lock(L);
  773. api_checknelems(L, 1);
  774. o = index2value(L, idx);
  775. api_check(L, ttisfulluserdata(o), "full userdata expected");
  776. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  777. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  778. else {
  779. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top - 1));
  780. luaC_barrierback(L, gcvalue(o), s2v(L->top - 1));
  781. res = 1;
  782. }
  783. L->top--;
  784. lua_unlock(L);
  785. return res;
  786. }
  787. /*
  788. ** 'load' and 'call' functions (run Lua code)
  789. */
  790. #define checkresults(L,na,nr) \
  791. api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)), \
  792. "results from function overflow current stack size")
  793. LUA_API void lua_callk (lua_State *L, int nargs, int nresults,
  794. lua_KContext ctx, lua_KFunction k) {
  795. StkId func;
  796. lua_lock(L);
  797. api_check(L, k == NULL || !isLua(L->ci),
  798. "cannot use continuations inside hooks");
  799. api_checknelems(L, nargs+1);
  800. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  801. checkresults(L, nargs, nresults);
  802. func = L->top - (nargs+1);
  803. if (k != NULL && yieldable(L)) { /* need to prepare continuation? */
  804. L->ci->u.c.k = k; /* save continuation */
  805. L->ci->u.c.ctx = ctx; /* save context */
  806. luaD_call(L, func, nresults); /* do the call */
  807. }
  808. else /* no continuation or no yieldable */
  809. luaD_callnoyield(L, func, nresults); /* just do the call */
  810. adjustresults(L, nresults);
  811. lua_unlock(L);
  812. }
  813. /*
  814. ** Execute a protected call.
  815. */
  816. struct CallS { /* data to 'f_call' */
  817. StkId func;
  818. int nresults;
  819. };
  820. static void f_call (lua_State *L, void *ud) {
  821. struct CallS *c = cast(struct CallS *, ud);
  822. luaD_callnoyield(L, c->func, c->nresults);
  823. }
  824. LUA_API int lua_pcallk (lua_State *L, int nargs, int nresults, int errfunc,
  825. lua_KContext ctx, lua_KFunction k) {
  826. struct CallS c;
  827. int status;
  828. ptrdiff_t func;
  829. lua_lock(L);
  830. api_check(L, k == NULL || !isLua(L->ci),
  831. "cannot use continuations inside hooks");
  832. api_checknelems(L, nargs+1);
  833. api_check(L, L->status == LUA_OK, "cannot do calls on non-normal thread");
  834. checkresults(L, nargs, nresults);
  835. if (errfunc == 0)
  836. func = 0;
  837. else {
  838. StkId o = index2stack(L, errfunc);
  839. api_check(L, ttisfunction(s2v(o)), "error handler must be a function");
  840. func = savestack(L, o);
  841. }
  842. c.func = L->top - (nargs+1); /* function to be called */
  843. if (k == NULL || !yieldable(L)) { /* no continuation or no yieldable? */
  844. c.nresults = nresults; /* do a 'conventional' protected call */
  845. status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);
  846. }
  847. else { /* prepare continuation (call is already protected by 'resume') */
  848. CallInfo *ci = L->ci;
  849. ci->u.c.k = k; /* save continuation */
  850. ci->u.c.ctx = ctx; /* save context */
  851. /* save information for error recovery */
  852. ci->u2.funcidx = cast_int(savestack(L, c.func));
  853. ci->u.c.old_errfunc = L->errfunc;
  854. L->errfunc = func;
  855. setoah(ci->callstatus, L->allowhook); /* save value of 'allowhook' */
  856. ci->callstatus |= CIST_YPCALL; /* function can do error recovery */
  857. luaD_call(L, c.func, nresults); /* do the call */
  858. ci->callstatus &= ~CIST_YPCALL;
  859. L->errfunc = ci->u.c.old_errfunc;
  860. status = LUA_OK; /* if it is here, there were no errors */
  861. }
  862. adjustresults(L, nresults);
  863. lua_unlock(L);
  864. return status;
  865. }
  866. LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
  867. const char *chunkname, const char *mode) {
  868. ZIO z;
  869. int status;
  870. lua_lock(L);
  871. if (!chunkname) chunkname = "?";
  872. luaZ_init(L, &z, reader, data);
  873. status = luaD_protectedparser(L, &z, chunkname, mode);
  874. if (status == LUA_OK) { /* no errors? */
  875. LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
  876. if (f->nupvalues >= 1) { /* does it have an upvalue? */
  877. /* get global table from registry */
  878. Table *reg = hvalue(&G(L)->l_registry);
  879. const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  880. /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
  881. setobj(L, f->upvals[0]->v, gt);
  882. luaC_barrier(L, f->upvals[0], gt);
  883. }
  884. }
  885. lua_unlock(L);
  886. return status;
  887. }
  888. LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip) {
  889. int status;
  890. TValue *o;
  891. lua_lock(L);
  892. api_checknelems(L, 1);
  893. o = s2v(L->top - 1);
  894. if (isLfunction(o))
  895. status = luaU_dump(L, getproto(o), writer, data, strip);
  896. else
  897. status = 1;
  898. lua_unlock(L);
  899. return status;
  900. }
  901. LUA_API int lua_status (lua_State *L) {
  902. return L->status;
  903. }
  904. /*
  905. ** Garbage-collection function
  906. */
  907. LUA_API int lua_gc (lua_State *L, int what, ...) {
  908. va_list argp;
  909. int res = 0;
  910. global_State *g = G(L);
  911. lua_lock(L);
  912. va_start(argp, what);
  913. switch (what) {
  914. case LUA_GCSTOP: {
  915. g->gcrunning = 0;
  916. break;
  917. }
  918. case LUA_GCRESTART: {
  919. luaE_setdebt(g, 0);
  920. g->gcrunning = 1;
  921. break;
  922. }
  923. case LUA_GCCOLLECT: {
  924. luaC_fullgc(L, 0);
  925. break;
  926. }
  927. case LUA_GCCOUNT: {
  928. /* GC values are expressed in Kbytes: #bytes/2^10 */
  929. res = cast_int(gettotalbytes(g) >> 10);
  930. break;
  931. }
  932. case LUA_GCCOUNTB: {
  933. res = cast_int(gettotalbytes(g) & 0x3ff);
  934. break;
  935. }
  936. case LUA_GCSTEP: {
  937. int data = va_arg(argp, int);
  938. l_mem debt = 1; /* =1 to signal that it did an actual step */
  939. lu_byte oldrunning = g->gcrunning;
  940. g->gcrunning = 1; /* allow GC to run */
  941. if (data == 0) {
  942. luaE_setdebt(g, 0); /* do a basic step */
  943. luaC_step(L);
  944. }
  945. else { /* add 'data' to total debt */
  946. debt = cast(l_mem, data) * 1024 + g->GCdebt;
  947. luaE_setdebt(g, debt);
  948. luaC_checkGC(L);
  949. }
  950. g->gcrunning = oldrunning; /* restore previous state */
  951. if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
  952. res = 1; /* signal it */
  953. break;
  954. }
  955. case LUA_GCSETPAUSE: {
  956. int data = va_arg(argp, int);
  957. res = getgcparam(g->gcpause);
  958. setgcparam(g->gcpause, data);
  959. break;
  960. }
  961. case LUA_GCSETSTEPMUL: {
  962. int data = va_arg(argp, int);
  963. res = getgcparam(g->gcstepmul);
  964. setgcparam(g->gcstepmul, data);
  965. break;
  966. }
  967. case LUA_GCISRUNNING: {
  968. res = g->gcrunning;
  969. break;
  970. }
  971. case LUA_GCGEN: {
  972. int minormul = va_arg(argp, int);
  973. int majormul = va_arg(argp, int);
  974. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  975. if (minormul != 0)
  976. g->genminormul = minormul;
  977. if (majormul != 0)
  978. setgcparam(g->genmajormul, majormul);
  979. luaC_changemode(L, KGC_GEN);
  980. break;
  981. }
  982. case LUA_GCINC: {
  983. int pause = va_arg(argp, int);
  984. int stepmul = va_arg(argp, int);
  985. int stepsize = va_arg(argp, int);
  986. res = isdecGCmodegen(g) ? LUA_GCGEN : LUA_GCINC;
  987. if (pause != 0)
  988. setgcparam(g->gcpause, pause);
  989. if (stepmul != 0)
  990. setgcparam(g->gcstepmul, stepmul);
  991. if (stepsize != 0)
  992. g->gcstepsize = stepsize;
  993. luaC_changemode(L, KGC_INC);
  994. break;
  995. }
  996. default: res = -1; /* invalid option */
  997. }
  998. va_end(argp);
  999. lua_unlock(L);
  1000. return res;
  1001. }
  1002. /*
  1003. ** miscellaneous functions
  1004. */
  1005. LUA_API int lua_error (lua_State *L) {
  1006. lua_lock(L);
  1007. api_checknelems(L, 1);
  1008. luaG_errormsg(L);
  1009. /* code unreachable; will unlock when control actually leaves the kernel */
  1010. return 0; /* to avoid warnings */
  1011. }
  1012. LUA_API int lua_next (lua_State *L, int idx) {
  1013. Table *t;
  1014. int more;
  1015. lua_lock(L);
  1016. api_checknelems(L, 1);
  1017. t = gettable(L, idx);
  1018. more = luaH_next(L, t, L->top - 1);
  1019. if (more) {
  1020. api_incr_top(L);
  1021. }
  1022. else /* no more elements */
  1023. L->top -= 1; /* remove key */
  1024. lua_unlock(L);
  1025. return more;
  1026. }
  1027. LUA_API void lua_toclose (lua_State *L, int idx) {
  1028. int nresults;
  1029. StkId o;
  1030. lua_lock(L);
  1031. o = index2stack(L, idx);
  1032. nresults = L->ci->nresults;
  1033. api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o,
  1034. "marked index below or equal new one");
  1035. luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
  1036. if (!hastocloseCfunc(nresults)) /* function not marked yet? */
  1037. L->ci->nresults = codeNresults(nresults); /* mark it */
  1038. lua_assert(hastocloseCfunc(L->ci->nresults));
  1039. lua_unlock(L);
  1040. }
  1041. LUA_API void lua_concat (lua_State *L, int n) {
  1042. lua_lock(L);
  1043. api_checknelems(L, n);
  1044. if (n >= 2) {
  1045. luaV_concat(L, n);
  1046. }
  1047. else if (n == 0) { /* push empty string */
  1048. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1049. api_incr_top(L);
  1050. }
  1051. /* else n == 1; nothing to do */
  1052. luaC_checkGC(L);
  1053. lua_unlock(L);
  1054. }
  1055. LUA_API void lua_len (lua_State *L, int idx) {
  1056. TValue *t;
  1057. lua_lock(L);
  1058. t = index2value(L, idx);
  1059. luaV_objlen(L, L->top, t);
  1060. api_incr_top(L);
  1061. lua_unlock(L);
  1062. }
  1063. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1064. lua_Alloc f;
  1065. lua_lock(L);
  1066. if (ud) *ud = G(L)->ud;
  1067. f = G(L)->frealloc;
  1068. lua_unlock(L);
  1069. return f;
  1070. }
  1071. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1072. lua_lock(L);
  1073. G(L)->ud = ud;
  1074. G(L)->frealloc = f;
  1075. lua_unlock(L);
  1076. }
  1077. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1078. lua_lock(L);
  1079. G(L)->ud_warn = ud;
  1080. G(L)->warnf = f;
  1081. lua_unlock(L);
  1082. }
  1083. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1084. lua_lock(L);
  1085. luaE_warning(L, msg, tocont);
  1086. lua_unlock(L);
  1087. }
  1088. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1089. Udata *u;
  1090. lua_lock(L);
  1091. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1092. u = luaS_newudata(L, size, nuvalue);
  1093. setuvalue(L, s2v(L->top), u);
  1094. api_incr_top(L);
  1095. luaC_checkGC(L);
  1096. lua_unlock(L);
  1097. return getudatamem(u);
  1098. }
  1099. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1100. GCObject **owner) {
  1101. switch (ttypetag(fi)) {
  1102. case LUA_TCCL: { /* C closure */
  1103. CClosure *f = clCvalue(fi);
  1104. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1105. return NULL; /* 'n' not in [1, f->nupvalues] */
  1106. *val = &f->upvalue[n-1];
  1107. if (owner) *owner = obj2gco(f);
  1108. return "";
  1109. }
  1110. case LUA_TLCL: { /* Lua closure */
  1111. LClosure *f = clLvalue(fi);
  1112. TString *name;
  1113. Proto *p = f->p;
  1114. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1115. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1116. *val = f->upvals[n-1]->v;
  1117. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1118. name = p->upvalues[n-1].name;
  1119. return (name == NULL) ? "(no name)" : getstr(name);
  1120. }
  1121. default: return NULL; /* not a closure */
  1122. }
  1123. }
  1124. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1125. const char *name;
  1126. TValue *val = NULL; /* to avoid warnings */
  1127. lua_lock(L);
  1128. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1129. if (name) {
  1130. setobj2s(L, L->top, val);
  1131. api_incr_top(L);
  1132. }
  1133. lua_unlock(L);
  1134. return name;
  1135. }
  1136. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1137. const char *name;
  1138. TValue *val = NULL; /* to avoid warnings */
  1139. GCObject *owner = NULL; /* to avoid warnings */
  1140. TValue *fi;
  1141. lua_lock(L);
  1142. fi = index2value(L, funcindex);
  1143. api_checknelems(L, 1);
  1144. name = aux_upvalue(fi, n, &val, &owner);
  1145. if (name) {
  1146. L->top--;
  1147. setobj(L, val, s2v(L->top));
  1148. luaC_barrier(L, owner, val);
  1149. }
  1150. lua_unlock(L);
  1151. return name;
  1152. }
  1153. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1154. LClosure *f;
  1155. TValue *fi = index2value(L, fidx);
  1156. api_check(L, ttisLclosure(fi), "Lua function expected");
  1157. f = clLvalue(fi);
  1158. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1159. if (pf) *pf = f;
  1160. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1161. }
  1162. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1163. TValue *fi = index2value(L, fidx);
  1164. switch (ttypetag(fi)) {
  1165. case LUA_TLCL: { /* lua closure */
  1166. return *getupvalref(L, fidx, n, NULL);
  1167. }
  1168. case LUA_TCCL: { /* C closure */
  1169. CClosure *f = clCvalue(fi);
  1170. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1171. return &f->upvalue[n - 1];
  1172. }
  1173. default: {
  1174. api_check(L, 0, "closure expected");
  1175. return NULL;
  1176. }
  1177. }
  1178. }
  1179. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1180. int fidx2, int n2) {
  1181. LClosure *f1;
  1182. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1183. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1184. *up1 = *up2;
  1185. luaC_objbarrier(L, f1, *up1);
  1186. }