lapi.c 34 KB

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