lapi.c 36 KB

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