lapi.c 34 KB

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