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. StkId func = L->ci->func;
  138. lua_lock(L);
  139. if (idx >= 0) {
  140. StkId newtop = (func + 1) + idx;
  141. api_check(L, idx <= L->stack_last - (func + 1), "new top too large");
  142. while (L->top < newtop)
  143. setnilvalue(s2v(L->top++));
  144. L->top = newtop;
  145. }
  146. else {
  147. api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
  148. L->top += idx+1; /* 'subtract' index (index is negative) */
  149. }
  150. luaF_close(L, L->top, LUA_OK);
  151. lua_unlock(L);
  152. }
  153. /*
  154. ** Reverse the stack segment from 'from' to 'to'
  155. ** (auxiliary to 'lua_rotate')
  156. ** Note that we move(copy) only the value inside the stack.
  157. ** (We do not move additional fields that may exist.)
  158. */
  159. static void reverse (lua_State *L, StkId from, StkId to) {
  160. for (; from < to; from++, to--) {
  161. TValue temp;
  162. setobj(L, &temp, s2v(from));
  163. setobjs2s(L, from, to);
  164. setobj2s(L, to, &temp);
  165. }
  166. }
  167. /*
  168. ** Let x = AB, where A is a prefix of length 'n'. Then,
  169. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  170. */
  171. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  172. StkId p, t, m;
  173. lua_lock(L);
  174. t = L->top - 1; /* end of stack segment being rotated */
  175. p = index2stack(L, idx); /* start of segment */
  176. api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  177. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  178. reverse(L, p, m); /* reverse the prefix with length 'n' */
  179. reverse(L, m + 1, t); /* reverse the suffix */
  180. reverse(L, p, t); /* reverse the entire segment */
  181. lua_unlock(L);
  182. }
  183. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  184. TValue *fr, *to;
  185. lua_lock(L);
  186. fr = index2value(L, fromidx);
  187. to = index2value(L, toidx);
  188. api_check(l, isvalid(L, to), "invalid index");
  189. setobj(L, to, fr);
  190. if (isupvalue(toidx)) /* function upvalue? */
  191. luaC_barrier(L, clCvalue(s2v(L->ci->func)), fr);
  192. /* LUA_REGISTRYINDEX does not need gc barrier
  193. (collector revisits it before finishing collection) */
  194. lua_unlock(L);
  195. }
  196. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  197. lua_lock(L);
  198. setobj2s(L, L->top, index2value(L, idx));
  199. api_incr_top(L);
  200. lua_unlock(L);
  201. }
  202. /*
  203. ** access functions (stack -> C)
  204. */
  205. LUA_API int lua_type (lua_State *L, int idx) {
  206. const TValue *o = index2value(L, idx);
  207. return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
  208. }
  209. LUA_API const char *lua_typename (lua_State *L, int t) {
  210. UNUSED(L);
  211. api_check(L, LUA_TNONE <= t && t < LUA_NUMTAGS, "invalid tag");
  212. return ttypename(t);
  213. }
  214. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  215. const TValue *o = index2value(L, idx);
  216. return (ttislcf(o) || (ttisCclosure(o)));
  217. }
  218. LUA_API int lua_isinteger (lua_State *L, int idx) {
  219. const TValue *o = index2value(L, idx);
  220. return ttisinteger(o);
  221. }
  222. LUA_API int lua_isnumber (lua_State *L, int idx) {
  223. lua_Number n;
  224. const TValue *o = index2value(L, idx);
  225. return tonumber(o, &n);
  226. }
  227. LUA_API int lua_isstring (lua_State *L, int idx) {
  228. const TValue *o = index2value(L, idx);
  229. return (ttisstring(o) || cvt2str(o));
  230. }
  231. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  232. const TValue *o = index2value(L, idx);
  233. return (ttisfulluserdata(o) || ttislightuserdata(o));
  234. }
  235. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  236. const TValue *o1 = index2value(L, index1);
  237. const TValue *o2 = index2value(L, index2);
  238. return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
  239. }
  240. LUA_API void lua_arith (lua_State *L, int op) {
  241. lua_lock(L);
  242. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  243. api_checknelems(L, 2); /* all other operations expect two operands */
  244. else { /* for unary operations, add fake 2nd operand */
  245. api_checknelems(L, 1);
  246. setobjs2s(L, L->top, L->top - 1);
  247. api_incr_top(L);
  248. }
  249. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  250. luaO_arith(L, op, s2v(L->top - 2), s2v(L->top - 1), L->top - 2);
  251. L->top--; /* remove second operand */
  252. lua_unlock(L);
  253. }
  254. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  255. const TValue *o1;
  256. const TValue *o2;
  257. int i = 0;
  258. lua_lock(L); /* may call tag method */
  259. o1 = index2value(L, index1);
  260. o2 = index2value(L, index2);
  261. if (isvalid(L, o1) && isvalid(L, o2)) {
  262. switch (op) {
  263. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  264. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  265. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  266. default: api_check(L, 0, "invalid option");
  267. }
  268. }
  269. lua_unlock(L);
  270. return i;
  271. }
  272. LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
  273. size_t sz = luaO_str2num(s, s2v(L->top));
  274. if (sz != 0)
  275. api_incr_top(L);
  276. return sz;
  277. }
  278. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  279. lua_Number n;
  280. const TValue *o = index2value(L, idx);
  281. int isnum = tonumber(o, &n);
  282. if (!isnum)
  283. n = 0; /* call to 'tonumber' may change 'n' even if it fails */
  284. if (pisnum) *pisnum = isnum;
  285. return n;
  286. }
  287. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  288. lua_Integer res;
  289. const TValue *o = index2value(L, idx);
  290. int isnum = tointeger(o, &res);
  291. if (!isnum)
  292. res = 0; /* call to 'tointeger' may change 'n' even if it fails */
  293. if (pisnum) *pisnum = isnum;
  294. return res;
  295. }
  296. LUA_API int lua_toboolean (lua_State *L, int idx) {
  297. const TValue *o = index2value(L, idx);
  298. return !l_isfalse(o);
  299. }
  300. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  301. TValue *o = index2value(L, idx);
  302. if (!ttisstring(o)) {
  303. if (!cvt2str(o)) { /* not convertible? */
  304. if (len != NULL) *len = 0;
  305. return NULL;
  306. }
  307. lua_lock(L); /* 'luaO_tostring' may create a new string */
  308. luaO_tostring(L, o);
  309. luaC_checkGC(L);
  310. o = index2value(L, idx); /* previous call may reallocate the stack */
  311. lua_unlock(L);
  312. }
  313. if (len != NULL)
  314. *len = vslen(o);
  315. return svalue(o);
  316. }
  317. LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
  318. const TValue *o = index2value(L, idx);
  319. switch (ttypetag(o)) {
  320. case LUA_TSHRSTR: return tsvalue(o)->shrlen;
  321. case LUA_TLNGSTR: return tsvalue(o)->u.lnglen;
  322. case LUA_TUSERDATA: return uvalue(o)->len;
  323. case LUA_TTABLE: return luaH_getn(hvalue(o));
  324. default: return 0;
  325. }
  326. }
  327. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  328. const TValue *o = index2value(L, idx);
  329. if (ttislcf(o)) return fvalue(o);
  330. else if (ttisCclosure(o))
  331. return clCvalue(o)->f;
  332. else return NULL; /* not a C function */
  333. }
  334. static void *touserdata (const TValue *o) {
  335. switch (ttype(o)) {
  336. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  337. case LUA_TLIGHTUSERDATA: return pvalue(o);
  338. default: return NULL;
  339. }
  340. }
  341. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  342. const TValue *o = index2value(L, idx);
  343. return touserdata(o);
  344. }
  345. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  346. const TValue *o = index2value(L, idx);
  347. return (!ttisthread(o)) ? NULL : thvalue(o);
  348. }
  349. /*
  350. ** Returns a pointer to the internal representation of an object.
  351. ** Note that ANSI C does not allow the conversion of a pointer to
  352. ** function to a 'void*', so the conversion here goes through
  353. ** a 'size_t'. (As the returned pointer is only informative, this
  354. ** conversion should not be a problem.)
  355. */
  356. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  357. const TValue *o = index2value(L, idx);
  358. switch (ttypetag(o)) {
  359. case LUA_TLCF: return cast_voidp(cast_sizet(fvalue(o)));
  360. case LUA_TUSERDATA: case LUA_TLIGHTUSERDATA:
  361. return touserdata(o);
  362. default: {
  363. if (iscollectable(o))
  364. return gcvalue(o);
  365. else
  366. return NULL;
  367. }
  368. }
  369. }
  370. /*
  371. ** push functions (C -> stack)
  372. */
  373. LUA_API void lua_pushnil (lua_State *L) {
  374. lua_lock(L);
  375. setnilvalue(s2v(L->top));
  376. api_incr_top(L);
  377. lua_unlock(L);
  378. }
  379. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  380. lua_lock(L);
  381. setfltvalue(s2v(L->top), n);
  382. api_incr_top(L);
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  386. lua_lock(L);
  387. setivalue(s2v(L->top), n);
  388. api_incr_top(L);
  389. lua_unlock(L);
  390. }
  391. /*
  392. ** Pushes on the stack a string with given length. Avoid using 's' when
  393. ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
  394. ** 'memcmp' and 'memcpy'.
  395. */
  396. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  397. TString *ts;
  398. lua_lock(L);
  399. ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
  400. setsvalue2s(L, L->top, ts);
  401. api_incr_top(L);
  402. luaC_checkGC(L);
  403. lua_unlock(L);
  404. return getstr(ts);
  405. }
  406. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  407. lua_lock(L);
  408. if (s == NULL)
  409. setnilvalue(s2v(L->top));
  410. else {
  411. TString *ts;
  412. ts = luaS_new(L, s);
  413. setsvalue2s(L, L->top, ts);
  414. s = getstr(ts); /* internal copy's address */
  415. }
  416. api_incr_top(L);
  417. luaC_checkGC(L);
  418. lua_unlock(L);
  419. return s;
  420. }
  421. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  422. va_list argp) {
  423. const char *ret;
  424. lua_lock(L);
  425. ret = luaO_pushvfstring(L, fmt, argp);
  426. luaC_checkGC(L);
  427. lua_unlock(L);
  428. return ret;
  429. }
  430. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  431. const char *ret;
  432. va_list argp;
  433. lua_lock(L);
  434. va_start(argp, fmt);
  435. ret = luaO_pushvfstring(L, fmt, argp);
  436. va_end(argp);
  437. luaC_checkGC(L);
  438. lua_unlock(L);
  439. return ret;
  440. }
  441. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  442. lua_lock(L);
  443. if (n == 0) {
  444. setfvalue(s2v(L->top), fn);
  445. api_incr_top(L);
  446. }
  447. else {
  448. CClosure *cl;
  449. api_checknelems(L, n);
  450. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  451. cl = luaF_newCclosure(L, n);
  452. cl->f = fn;
  453. L->top -= n;
  454. while (n--) {
  455. setobj2n(L, &cl->upvalue[n], s2v(L->top + n));
  456. /* does not need barrier because closure is white */
  457. }
  458. setclCvalue(L, s2v(L->top), cl);
  459. api_incr_top(L);
  460. luaC_checkGC(L);
  461. }
  462. lua_unlock(L);
  463. }
  464. LUA_API void lua_pushboolean (lua_State *L, int b) {
  465. lua_lock(L);
  466. setbvalue(s2v(L->top), (b != 0)); /* ensure that true is 1 */
  467. api_incr_top(L);
  468. lua_unlock(L);
  469. }
  470. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  471. lua_lock(L);
  472. setpvalue(s2v(L->top), p);
  473. api_incr_top(L);
  474. lua_unlock(L);
  475. }
  476. LUA_API int lua_pushthread (lua_State *L) {
  477. lua_lock(L);
  478. setthvalue(L, s2v(L->top), L);
  479. api_incr_top(L);
  480. lua_unlock(L);
  481. return (G(L)->mainthread == L);
  482. }
  483. /*
  484. ** get functions (Lua -> stack)
  485. */
  486. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  487. const TValue *slot;
  488. TString *str = luaS_new(L, k);
  489. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  490. setobj2s(L, L->top, slot);
  491. api_incr_top(L);
  492. }
  493. else {
  494. setsvalue2s(L, L->top, str);
  495. api_incr_top(L);
  496. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  497. }
  498. lua_unlock(L);
  499. return ttype(s2v(L->top - 1));
  500. }
  501. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  502. Table *reg = hvalue(&G(L)->l_registry);
  503. lua_lock(L);
  504. return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  505. }
  506. LUA_API int lua_gettable (lua_State *L, int idx) {
  507. const TValue *slot;
  508. TValue *t;
  509. lua_lock(L);
  510. t = index2value(L, idx);
  511. if (luaV_fastget(L, t, s2v(L->top - 1), slot, luaH_get)) {
  512. setobj2s(L, L->top - 1, slot);
  513. }
  514. else
  515. luaV_finishget(L, t, s2v(L->top - 1), L->top - 1, slot);
  516. lua_unlock(L);
  517. return ttype(s2v(L->top - 1));
  518. }
  519. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  520. lua_lock(L);
  521. return auxgetstr(L, index2value(L, idx), k);
  522. }
  523. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  524. TValue *t;
  525. const TValue *slot;
  526. lua_lock(L);
  527. t = index2value(L, idx);
  528. if (luaV_fastgeti(L, t, n, slot)) {
  529. setobj2s(L, L->top, slot);
  530. }
  531. else {
  532. TValue aux;
  533. setivalue(&aux, n);
  534. luaV_finishget(L, t, &aux, L->top, slot);
  535. }
  536. api_incr_top(L);
  537. lua_unlock(L);
  538. return ttype(s2v(L->top - 1));
  539. }
  540. static int finishrawget (lua_State *L, const TValue *val) {
  541. if (isempty(val)) /* avoid copying empty items to the stack */
  542. setnilvalue(s2v(L->top));
  543. else
  544. setobj2s(L, L->top, val);
  545. api_incr_top(L);
  546. lua_unlock(L);
  547. return ttype(s2v(L->top - 1));
  548. }
  549. static Table *gettable (lua_State *L, int idx) {
  550. TValue *t = index2value(L, idx);
  551. api_check(L, ttistable(t), "table expected");
  552. return hvalue(t);
  553. }
  554. LUA_API int lua_rawget (lua_State *L, int idx) {
  555. Table *t;
  556. const TValue *val;
  557. lua_lock(L);
  558. api_checknelems(L, 1);
  559. t = gettable(L, idx);
  560. val = luaH_get(t, s2v(L->top - 1));
  561. L->top--; /* remove key */
  562. return finishrawget(L, val);
  563. }
  564. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  565. Table *t;
  566. lua_lock(L);
  567. t = gettable(L, idx);
  568. return finishrawget(L, luaH_getint(t, n));
  569. }
  570. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  571. Table *t;
  572. TValue k;
  573. lua_lock(L);
  574. t = gettable(L, idx);
  575. setpvalue(&k, cast_voidp(p));
  576. return finishrawget(L, luaH_get(t, &k));
  577. }
  578. LUA_API void lua_createtable (lua_State *L, int narray, int nrec) {
  579. Table *t;
  580. lua_lock(L);
  581. t = luaH_new(L);
  582. sethvalue2s(L, L->top, t);
  583. api_incr_top(L);
  584. if (narray > 0 || nrec > 0)
  585. luaH_resize(L, t, narray, nrec);
  586. luaC_checkGC(L);
  587. lua_unlock(L);
  588. }
  589. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  590. const TValue *obj;
  591. Table *mt;
  592. int res = 0;
  593. lua_lock(L);
  594. obj = index2value(L, objindex);
  595. switch (ttype(obj)) {
  596. case LUA_TTABLE:
  597. mt = hvalue(obj)->metatable;
  598. break;
  599. case LUA_TUSERDATA:
  600. mt = uvalue(obj)->metatable;
  601. break;
  602. default:
  603. mt = G(L)->mt[ttype(obj)];
  604. break;
  605. }
  606. if (mt != NULL) {
  607. sethvalue2s(L, L->top, mt);
  608. api_incr_top(L);
  609. res = 1;
  610. }
  611. lua_unlock(L);
  612. return res;
  613. }
  614. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  615. TValue *o;
  616. int t;
  617. lua_lock(L);
  618. o = index2value(L, idx);
  619. api_check(L, ttisfulluserdata(o), "full userdata expected");
  620. if (n <= 0 || n > uvalue(o)->nuvalue) {
  621. setnilvalue(s2v(L->top));
  622. t = LUA_TNONE;
  623. }
  624. else {
  625. setobj2s(L, L->top, &uvalue(o)->uv[n - 1].uv);
  626. t = ttype(s2v(L->top));
  627. }
  628. api_incr_top(L);
  629. lua_unlock(L);
  630. return t;
  631. }
  632. /*
  633. ** set functions (stack -> Lua)
  634. */
  635. /*
  636. ** t[k] = value at the top of the stack (where 'k' is a string)
  637. */
  638. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  639. const TValue *slot;
  640. TString *str = luaS_new(L, k);
  641. api_checknelems(L, 1);
  642. if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
  643. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  644. L->top--; /* pop value */
  645. }
  646. else {
  647. setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */
  648. api_incr_top(L);
  649. luaV_finishset(L, t, s2v(L->top - 1), s2v(L->top - 2), slot);
  650. L->top -= 2; /* pop value and key */
  651. }
  652. lua_unlock(L); /* lock done by caller */
  653. }
  654. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  655. Table *reg = hvalue(&G(L)->l_registry);
  656. lua_lock(L); /* unlock done in 'auxsetstr' */
  657. auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
  658. }
  659. LUA_API void lua_settable (lua_State *L, int idx) {
  660. TValue *t;
  661. const TValue *slot;
  662. lua_lock(L);
  663. api_checknelems(L, 2);
  664. t = index2value(L, idx);
  665. if (luaV_fastget(L, t, s2v(L->top - 2), slot, luaH_get)) {
  666. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  667. }
  668. else
  669. luaV_finishset(L, t, s2v(L->top - 2), s2v(L->top - 1), slot);
  670. L->top -= 2; /* pop index and value */
  671. lua_unlock(L);
  672. }
  673. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  674. lua_lock(L); /* unlock done in 'auxsetstr' */
  675. auxsetstr(L, index2value(L, idx), k);
  676. }
  677. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  678. TValue *t;
  679. const TValue *slot;
  680. lua_lock(L);
  681. api_checknelems(L, 1);
  682. t = index2value(L, idx);
  683. if (luaV_fastgeti(L, t, n, slot)) {
  684. luaV_finishfastset(L, t, slot, s2v(L->top - 1));
  685. }
  686. else {
  687. TValue aux;
  688. setivalue(&aux, n);
  689. luaV_finishset(L, t, &aux, s2v(L->top - 1), slot);
  690. }
  691. L->top--; /* pop value */
  692. lua_unlock(L);
  693. }
  694. LUA_API void lua_rawset (lua_State *L, int idx) {
  695. Table *t;
  696. TValue *slot;
  697. lua_lock(L);
  698. api_checknelems(L, 2);
  699. t = gettable(L, idx);
  700. slot = luaH_set(L, t, s2v(L->top - 2));
  701. setobj2t(L, slot, s2v(L->top - 1));
  702. invalidateTMcache(t);
  703. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  704. L->top -= 2;
  705. lua_unlock(L);
  706. }
  707. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  708. Table *t;
  709. lua_lock(L);
  710. api_checknelems(L, 1);
  711. t = gettable(L, idx);
  712. luaH_setint(L, t, n, s2v(L->top - 1));
  713. luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
  714. L->top--;
  715. lua_unlock(L);
  716. }
  717. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  718. Table *t;
  719. TValue k, *slot;
  720. lua_lock(L);
  721. api_checknelems(L, 1);
  722. t = gettable(L, idx);
  723. setpvalue(&k, cast_voidp(p));
  724. slot = luaH_set(L, t, &k);
  725. setobj2t(L, slot, 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 >= 2) {
  1044. luaV_concat(L, n);
  1045. }
  1046. else if (n == 0) { /* push empty string */
  1047. setsvalue2s(L, L->top, luaS_newlstr(L, "", 0));
  1048. api_incr_top(L);
  1049. }
  1050. /* else n == 1; nothing to do */
  1051. luaC_checkGC(L);
  1052. lua_unlock(L);
  1053. }
  1054. LUA_API void lua_len (lua_State *L, int idx) {
  1055. TValue *t;
  1056. lua_lock(L);
  1057. t = index2value(L, idx);
  1058. luaV_objlen(L, L->top, t);
  1059. api_incr_top(L);
  1060. lua_unlock(L);
  1061. }
  1062. LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {
  1063. lua_Alloc f;
  1064. lua_lock(L);
  1065. if (ud) *ud = G(L)->ud;
  1066. f = G(L)->frealloc;
  1067. lua_unlock(L);
  1068. return f;
  1069. }
  1070. LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {
  1071. lua_lock(L);
  1072. G(L)->ud = ud;
  1073. G(L)->frealloc = f;
  1074. lua_unlock(L);
  1075. }
  1076. void lua_setwarnf (lua_State *L, lua_WarnFunction f, void *ud) {
  1077. lua_lock(L);
  1078. G(L)->ud_warn = ud;
  1079. G(L)->warnf = f;
  1080. lua_unlock(L);
  1081. }
  1082. void lua_warning (lua_State *L, const char *msg, int tocont) {
  1083. lua_lock(L);
  1084. luaE_warning(L, msg, tocont);
  1085. lua_unlock(L);
  1086. }
  1087. LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
  1088. Udata *u;
  1089. lua_lock(L);
  1090. api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
  1091. u = luaS_newudata(L, size, nuvalue);
  1092. setuvalue(L, s2v(L->top), u);
  1093. api_incr_top(L);
  1094. luaC_checkGC(L);
  1095. lua_unlock(L);
  1096. return getudatamem(u);
  1097. }
  1098. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1099. GCObject **owner) {
  1100. switch (ttypetag(fi)) {
  1101. case LUA_TCCL: { /* C closure */
  1102. CClosure *f = clCvalue(fi);
  1103. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1104. return NULL; /* 'n' not in [1, f->nupvalues] */
  1105. *val = &f->upvalue[n-1];
  1106. if (owner) *owner = obj2gco(f);
  1107. return "";
  1108. }
  1109. case LUA_TLCL: { /* Lua closure */
  1110. LClosure *f = clLvalue(fi);
  1111. TString *name;
  1112. Proto *p = f->p;
  1113. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1114. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1115. *val = f->upvals[n-1]->v;
  1116. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1117. name = p->upvalues[n-1].name;
  1118. return (name == NULL) ? "(no name)" : getstr(name);
  1119. }
  1120. default: return NULL; /* not a closure */
  1121. }
  1122. }
  1123. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1124. const char *name;
  1125. TValue *val = NULL; /* to avoid warnings */
  1126. lua_lock(L);
  1127. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1128. if (name) {
  1129. setobj2s(L, L->top, val);
  1130. api_incr_top(L);
  1131. }
  1132. lua_unlock(L);
  1133. return name;
  1134. }
  1135. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1136. const char *name;
  1137. TValue *val = NULL; /* to avoid warnings */
  1138. GCObject *owner = NULL; /* to avoid warnings */
  1139. TValue *fi;
  1140. lua_lock(L);
  1141. fi = index2value(L, funcindex);
  1142. api_checknelems(L, 1);
  1143. name = aux_upvalue(fi, n, &val, &owner);
  1144. if (name) {
  1145. L->top--;
  1146. setobj(L, val, s2v(L->top));
  1147. luaC_barrier(L, owner, val);
  1148. }
  1149. lua_unlock(L);
  1150. return name;
  1151. }
  1152. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1153. LClosure *f;
  1154. TValue *fi = index2value(L, fidx);
  1155. api_check(L, ttisLclosure(fi), "Lua function expected");
  1156. f = clLvalue(fi);
  1157. api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
  1158. if (pf) *pf = f;
  1159. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1160. }
  1161. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1162. TValue *fi = index2value(L, fidx);
  1163. switch (ttypetag(fi)) {
  1164. case LUA_TLCL: { /* lua closure */
  1165. return *getupvalref(L, fidx, n, NULL);
  1166. }
  1167. case LUA_TCCL: { /* C closure */
  1168. CClosure *f = clCvalue(fi);
  1169. api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
  1170. return &f->upvalue[n - 1];
  1171. }
  1172. default: {
  1173. api_check(L, 0, "closure expected");
  1174. return NULL;
  1175. }
  1176. }
  1177. }
  1178. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1179. int fidx2, int n2) {
  1180. LClosure *f1;
  1181. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1182. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1183. *up1 = *up2;
  1184. luaC_objbarrier(L, f1, *up1);
  1185. }