lapi.c 34 KB

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