lapi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. /*
  40. ** Convert an acceptable index to a pointer to its respective value.
  41. ** Non-valid indices return the special nil value 'G(L)->nilvalue'.
  42. */
  43. static TValue *index2value (lua_State *L, int idx) {
  44. CallInfo *ci = L->ci;
  45. if (idx > 0) {
  46. StkId o = ci->func.p + idx;
  47. api_check(L, idx <= ci->top.p - (ci->func.p + 1), "unacceptable index");
  48. if (o >= L->top.p) return &G(L)->nilvalue;
  49. else return s2v(o);
  50. }
  51. else if (!ispseudo(idx)) { /* negative index */
  52. api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
  53. "invalid index");
  54. return s2v(L->top.p + idx);
  55. }
  56. else if (idx == LUA_REGISTRYINDEX)
  57. return &G(L)->l_registry;
  58. else { /* upvalues */
  59. idx = LUA_REGISTRYINDEX - idx;
  60. api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
  61. if (ttisCclosure(s2v(ci->func.p))) { /* C closure? */
  62. CClosure *func = clCvalue(s2v(ci->func.p));
  63. return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
  64. : &G(L)->nilvalue;
  65. }
  66. else { /* light C function or Lua function (through a hook)?) */
  67. api_check(L, ttislcf(s2v(ci->func.p)), "caller not a C function");
  68. return &G(L)->nilvalue; /* no upvalues */
  69. }
  70. }
  71. }
  72. /*
  73. ** Convert a valid actual index (not a pseudo-index) to its address.
  74. */
  75. static StkId index2stack (lua_State *L, int idx) {
  76. CallInfo *ci = L->ci;
  77. if (idx > 0) {
  78. StkId o = ci->func.p + idx;
  79. api_check(L, o < L->top.p, "invalid index");
  80. return o;
  81. }
  82. else { /* non-positive index */
  83. api_check(L, idx != 0 && -idx <= L->top.p - (ci->func.p + 1),
  84. "invalid index");
  85. api_check(L, !ispseudo(idx), "invalid index");
  86. return L->top.p + idx;
  87. }
  88. }
  89. LUA_API int lua_checkstack (lua_State *L, int n) {
  90. int res;
  91. CallInfo *ci;
  92. lua_lock(L);
  93. ci = L->ci;
  94. api_check(L, n >= 0, "negative 'n'");
  95. if (L->stack_last.p - L->top.p > n) /* stack large enough? */
  96. res = 1; /* yes; check is OK */
  97. else /* need to grow stack */
  98. res = luaD_growstack(L, n, 0);
  99. if (res && ci->top.p < L->top.p + n)
  100. ci->top.p = L->top.p + n; /* adjust frame top */
  101. lua_unlock(L);
  102. return res;
  103. }
  104. LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {
  105. int i;
  106. if (from == to) return;
  107. lua_lock(to);
  108. api_checkpop(from, n);
  109. api_check(from, G(from) == G(to), "moving among independent states");
  110. api_check(from, to->ci->top.p - to->top.p >= n, "stack overflow");
  111. from->top.p -= n;
  112. for (i = 0; i < n; i++) {
  113. setobjs2s(to, to->top.p, from->top.p + i);
  114. to->top.p++; /* stack already checked by previous 'api_check' */
  115. }
  116. lua_unlock(to);
  117. }
  118. LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
  119. lua_CFunction old;
  120. lua_lock(L);
  121. old = G(L)->panic;
  122. G(L)->panic = panicf;
  123. lua_unlock(L);
  124. return old;
  125. }
  126. LUA_API lua_Number lua_version (lua_State *L) {
  127. UNUSED(L);
  128. return LUA_VERSION_NUM;
  129. }
  130. /*
  131. ** basic stack manipulation
  132. */
  133. /*
  134. ** convert an acceptable stack index into an absolute index
  135. */
  136. LUA_API int lua_absindex (lua_State *L, int idx) {
  137. return (idx > 0 || ispseudo(idx))
  138. ? idx
  139. : cast_int(L->top.p - L->ci->func.p) + idx;
  140. }
  141. LUA_API int lua_gettop (lua_State *L) {
  142. return cast_int(L->top.p - (L->ci->func.p + 1));
  143. }
  144. LUA_API void lua_settop (lua_State *L, int idx) {
  145. CallInfo *ci;
  146. StkId func, newtop;
  147. ptrdiff_t diff; /* difference for new top */
  148. lua_lock(L);
  149. ci = L->ci;
  150. func = ci->func.p;
  151. if (idx >= 0) {
  152. api_check(L, idx <= ci->top.p - (func + 1), "new top too large");
  153. diff = ((func + 1) + idx) - L->top.p;
  154. for (; diff > 0; diff--)
  155. setnilvalue(s2v(L->top.p++)); /* clear new slots */
  156. }
  157. else {
  158. api_check(L, -(idx+1) <= (L->top.p - (func + 1)), "invalid new top");
  159. diff = idx + 1; /* will "subtract" index (as it is negative) */
  160. }
  161. newtop = L->top.p + diff;
  162. if (diff < 0 && L->tbclist.p >= newtop) {
  163. lua_assert(ci->callstatus & CIST_CLSRET);
  164. newtop = luaF_close(L, newtop, CLOSEKTOP, 0);
  165. }
  166. L->top.p = newtop; /* correct top only after closing any upvalue */
  167. lua_unlock(L);
  168. }
  169. LUA_API void lua_closeslot (lua_State *L, int idx) {
  170. StkId level;
  171. lua_lock(L);
  172. level = index2stack(L, idx);
  173. api_check(L, (L->ci->callstatus & CIST_CLSRET) && L->tbclist.p == level,
  174. "no variable to close at given level");
  175. level = luaF_close(L, level, CLOSEKTOP, 0);
  176. setnilvalue(s2v(level));
  177. lua_unlock(L);
  178. }
  179. /*
  180. ** Reverse the stack segment from 'from' to 'to'
  181. ** (auxiliary to 'lua_rotate')
  182. ** Note that we move(copy) only the value inside the stack.
  183. ** (We do not move additional fields that may exist.)
  184. */
  185. static void reverse (lua_State *L, StkId from, StkId to) {
  186. for (; from < to; from++, to--) {
  187. TValue temp;
  188. setobj(L, &temp, s2v(from));
  189. setobjs2s(L, from, to);
  190. setobj2s(L, to, &temp);
  191. }
  192. }
  193. /*
  194. ** Let x = AB, where A is a prefix of length 'n'. Then,
  195. ** rotate x n == BA. But BA == (A^r . B^r)^r.
  196. */
  197. LUA_API void lua_rotate (lua_State *L, int idx, int n) {
  198. StkId p, t, m;
  199. lua_lock(L);
  200. t = L->top.p - 1; /* end of stack segment being rotated */
  201. p = index2stack(L, idx); /* start of segment */
  202. api_check(L, L->tbclist.p < p, "moving a to-be-closed slot");
  203. api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'");
  204. m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */
  205. reverse(L, p, m); /* reverse the prefix with length 'n' */
  206. reverse(L, m + 1, t); /* reverse the suffix */
  207. reverse(L, p, t); /* reverse the entire segment */
  208. lua_unlock(L);
  209. }
  210. LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
  211. TValue *fr, *to;
  212. lua_lock(L);
  213. fr = index2value(L, fromidx);
  214. to = index2value(L, toidx);
  215. api_check(L, isvalid(L, to), "invalid index");
  216. setobj(L, to, fr);
  217. if (isupvalue(toidx)) /* function upvalue? */
  218. luaC_barrier(L, clCvalue(s2v(L->ci->func.p)), fr);
  219. /* LUA_REGISTRYINDEX does not need gc barrier
  220. (collector revisits it before finishing collection) */
  221. lua_unlock(L);
  222. }
  223. LUA_API void lua_pushvalue (lua_State *L, int idx) {
  224. lua_lock(L);
  225. setobj2s(L, L->top.p, index2value(L, idx));
  226. api_incr_top(L);
  227. lua_unlock(L);
  228. }
  229. /*
  230. ** access functions (stack -> C)
  231. */
  232. LUA_API int lua_type (lua_State *L, int idx) {
  233. const TValue *o = index2value(L, idx);
  234. return (isvalid(L, o) ? ttype(o) : LUA_TNONE);
  235. }
  236. LUA_API const char *lua_typename (lua_State *L, int t) {
  237. UNUSED(L);
  238. api_check(L, LUA_TNONE <= t && t < LUA_NUMTYPES, "invalid type");
  239. return ttypename(t);
  240. }
  241. LUA_API int lua_iscfunction (lua_State *L, int idx) {
  242. const TValue *o = index2value(L, idx);
  243. return (ttislcf(o) || (ttisCclosure(o)));
  244. }
  245. LUA_API int lua_isinteger (lua_State *L, int idx) {
  246. const TValue *o = index2value(L, idx);
  247. return ttisinteger(o);
  248. }
  249. LUA_API int lua_isnumber (lua_State *L, int idx) {
  250. lua_Number n;
  251. const TValue *o = index2value(L, idx);
  252. return tonumber(o, &n);
  253. }
  254. LUA_API int lua_isstring (lua_State *L, int idx) {
  255. const TValue *o = index2value(L, idx);
  256. return (ttisstring(o) || cvt2str(o));
  257. }
  258. LUA_API int lua_isuserdata (lua_State *L, int idx) {
  259. const TValue *o = index2value(L, idx);
  260. return (ttisfulluserdata(o) || ttislightuserdata(o));
  261. }
  262. LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
  263. const TValue *o1 = index2value(L, index1);
  264. const TValue *o2 = index2value(L, index2);
  265. return (isvalid(L, o1) && isvalid(L, o2)) ? luaV_rawequalobj(o1, o2) : 0;
  266. }
  267. LUA_API void lua_arith (lua_State *L, int op) {
  268. lua_lock(L);
  269. if (op != LUA_OPUNM && op != LUA_OPBNOT)
  270. api_checkpop(L, 2); /* all other operations expect two operands */
  271. else { /* for unary operations, add fake 2nd operand */
  272. api_checkpop(L, 1);
  273. setobjs2s(L, L->top.p, L->top.p - 1);
  274. api_incr_top(L);
  275. }
  276. /* first operand at top - 2, second at top - 1; result go to top - 2 */
  277. luaO_arith(L, op, s2v(L->top.p - 2), s2v(L->top.p - 1), L->top.p - 2);
  278. L->top.p--; /* pop second operand */
  279. lua_unlock(L);
  280. }
  281. LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
  282. const TValue *o1;
  283. const TValue *o2;
  284. int i = 0;
  285. lua_lock(L); /* may call tag method */
  286. o1 = index2value(L, index1);
  287. o2 = index2value(L, index2);
  288. if (isvalid(L, o1) && isvalid(L, o2)) {
  289. switch (op) {
  290. case LUA_OPEQ: i = luaV_equalobj(L, o1, o2); break;
  291. case LUA_OPLT: i = luaV_lessthan(L, o1, o2); break;
  292. case LUA_OPLE: i = luaV_lessequal(L, o1, o2); break;
  293. default: api_check(L, 0, "invalid option");
  294. }
  295. }
  296. lua_unlock(L);
  297. return i;
  298. }
  299. LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff) {
  300. const TValue *o = index2value(L, idx);
  301. if (ttisnumber(o)) {
  302. unsigned len = luaO_tostringbuff(o, buff);
  303. buff[len++] = '\0'; /* add final zero */
  304. return len;
  305. }
  306. else
  307. return 0;
  308. }
  309. LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
  310. size_t sz = luaO_str2num(s, s2v(L->top.p));
  311. if (sz != 0)
  312. api_incr_top(L);
  313. return sz;
  314. }
  315. LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
  316. lua_Number n = 0;
  317. const TValue *o = index2value(L, idx);
  318. int isnum = tonumber(o, &n);
  319. if (pisnum)
  320. *pisnum = isnum;
  321. return n;
  322. }
  323. LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
  324. lua_Integer res = 0;
  325. const TValue *o = index2value(L, idx);
  326. int isnum = tointeger(o, &res);
  327. if (pisnum)
  328. *pisnum = isnum;
  329. return res;
  330. }
  331. LUA_API int lua_toboolean (lua_State *L, int idx) {
  332. const TValue *o = index2value(L, idx);
  333. return !l_isfalse(o);
  334. }
  335. LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
  336. TValue *o;
  337. lua_lock(L);
  338. o = index2value(L, idx);
  339. if (!ttisstring(o)) {
  340. if (!cvt2str(o)) { /* not convertible? */
  341. if (len != NULL) *len = 0;
  342. lua_unlock(L);
  343. return NULL;
  344. }
  345. luaO_tostring(L, o);
  346. luaC_checkGC(L);
  347. o = index2value(L, idx); /* previous call may reallocate the stack */
  348. }
  349. lua_unlock(L);
  350. if (len != NULL)
  351. return getlstr(tsvalue(o), *len);
  352. else
  353. return getstr(tsvalue(o));
  354. }
  355. LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
  356. const TValue *o = index2value(L, idx);
  357. switch (ttypetag(o)) {
  358. case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
  359. case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
  360. case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
  361. case LUA_VTABLE: return luaH_getn(hvalue(o));
  362. default: return 0;
  363. }
  364. }
  365. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
  366. const TValue *o = index2value(L, idx);
  367. if (ttislcf(o)) return fvalue(o);
  368. else if (ttisCclosure(o))
  369. return clCvalue(o)->f;
  370. else return NULL; /* not a C function */
  371. }
  372. l_sinline void *touserdata (const TValue *o) {
  373. switch (ttype(o)) {
  374. case LUA_TUSERDATA: return getudatamem(uvalue(o));
  375. case LUA_TLIGHTUSERDATA: return pvalue(o);
  376. default: return NULL;
  377. }
  378. }
  379. LUA_API void *lua_touserdata (lua_State *L, int idx) {
  380. const TValue *o = index2value(L, idx);
  381. return touserdata(o);
  382. }
  383. LUA_API lua_State *lua_tothread (lua_State *L, int idx) {
  384. const TValue *o = index2value(L, idx);
  385. return (!ttisthread(o)) ? NULL : thvalue(o);
  386. }
  387. /*
  388. ** Returns a pointer to the internal representation of an object.
  389. ** Note that ANSI C does not allow the conversion of a pointer to
  390. ** function to a 'void*', so the conversion here goes through
  391. ** a 'size_t'. (As the returned pointer is only informative, this
  392. ** conversion should not be a problem.)
  393. */
  394. LUA_API const void *lua_topointer (lua_State *L, int idx) {
  395. const TValue *o = index2value(L, idx);
  396. switch (ttypetag(o)) {
  397. case LUA_VLCF: return cast_voidp(cast_sizet(fvalue(o)));
  398. case LUA_VUSERDATA: case LUA_VLIGHTUSERDATA:
  399. return touserdata(o);
  400. default: {
  401. if (iscollectable(o))
  402. return gcvalue(o);
  403. else
  404. return NULL;
  405. }
  406. }
  407. }
  408. /*
  409. ** push functions (C -> stack)
  410. */
  411. LUA_API void lua_pushnil (lua_State *L) {
  412. lua_lock(L);
  413. setnilvalue(s2v(L->top.p));
  414. api_incr_top(L);
  415. lua_unlock(L);
  416. }
  417. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  418. lua_lock(L);
  419. setfltvalue(s2v(L->top.p), n);
  420. api_incr_top(L);
  421. lua_unlock(L);
  422. }
  423. LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
  424. lua_lock(L);
  425. setivalue(s2v(L->top.p), n);
  426. api_incr_top(L);
  427. lua_unlock(L);
  428. }
  429. /*
  430. ** Pushes on the stack a string with given length. Avoid using 's' when
  431. ** 'len' == 0 (as 's' can be NULL in that case), due to later use of
  432. ** 'memcmp' and 'memcpy'.
  433. */
  434. LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
  435. TString *ts;
  436. lua_lock(L);
  437. ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len);
  438. setsvalue2s(L, L->top.p, ts);
  439. api_incr_top(L);
  440. luaC_checkGC(L);
  441. lua_unlock(L);
  442. return getstr(ts);
  443. }
  444. LUA_API const char *lua_pushextlstring (lua_State *L,
  445. const char *s, size_t len, lua_Alloc falloc, void *ud) {
  446. TString *ts;
  447. lua_lock(L);
  448. api_check(L, len <= MAX_SIZE, "string too large");
  449. api_check(L, s[len] == '\0', "string not ending with zero");
  450. ts = luaS_newextlstr (L, s, len, falloc, ud);
  451. setsvalue2s(L, L->top.p, ts);
  452. api_incr_top(L);
  453. luaC_checkGC(L);
  454. lua_unlock(L);
  455. return getstr(ts);
  456. }
  457. LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  458. lua_lock(L);
  459. if (s == NULL)
  460. setnilvalue(s2v(L->top.p));
  461. else {
  462. TString *ts;
  463. ts = luaS_new(L, s);
  464. setsvalue2s(L, L->top.p, ts);
  465. s = getstr(ts); /* internal copy's address */
  466. }
  467. api_incr_top(L);
  468. luaC_checkGC(L);
  469. lua_unlock(L);
  470. return s;
  471. }
  472. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  473. va_list argp) {
  474. const char *ret;
  475. lua_lock(L);
  476. ret = luaO_pushvfstring(L, fmt, argp);
  477. luaC_checkGC(L);
  478. lua_unlock(L);
  479. return ret;
  480. }
  481. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  482. const char *ret;
  483. va_list argp;
  484. lua_lock(L);
  485. va_start(argp, fmt);
  486. ret = luaO_pushvfstring(L, fmt, argp);
  487. va_end(argp);
  488. luaC_checkGC(L);
  489. if (ret == NULL) /* error? */
  490. luaD_throw(L, LUA_ERRMEM);
  491. lua_unlock(L);
  492. return ret;
  493. }
  494. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  495. lua_lock(L);
  496. if (n == 0) {
  497. setfvalue(s2v(L->top.p), fn);
  498. api_incr_top(L);
  499. }
  500. else {
  501. int i;
  502. CClosure *cl;
  503. api_checkpop(L, n);
  504. api_check(L, n <= MAXUPVAL, "upvalue index too large");
  505. cl = luaF_newCclosure(L, n);
  506. cl->f = fn;
  507. for (i = 0; i < n; i++) {
  508. setobj2n(L, &cl->upvalue[i], s2v(L->top.p - n + i));
  509. /* does not need barrier because closure is white */
  510. lua_assert(iswhite(cl));
  511. }
  512. L->top.p -= n;
  513. setclCvalue(L, s2v(L->top.p), cl);
  514. api_incr_top(L);
  515. luaC_checkGC(L);
  516. }
  517. lua_unlock(L);
  518. }
  519. LUA_API void lua_pushboolean (lua_State *L, int b) {
  520. lua_lock(L);
  521. if (b)
  522. setbtvalue(s2v(L->top.p));
  523. else
  524. setbfvalue(s2v(L->top.p));
  525. api_incr_top(L);
  526. lua_unlock(L);
  527. }
  528. LUA_API void lua_pushlightuserdata (lua_State *L, void *p) {
  529. lua_lock(L);
  530. setpvalue(s2v(L->top.p), p);
  531. api_incr_top(L);
  532. lua_unlock(L);
  533. }
  534. LUA_API int lua_pushthread (lua_State *L) {
  535. lua_lock(L);
  536. setthvalue(L, s2v(L->top.p), L);
  537. api_incr_top(L);
  538. lua_unlock(L);
  539. return (G(L)->mainthread == L);
  540. }
  541. /*
  542. ** get functions (Lua -> stack)
  543. */
  544. static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  545. lu_byte tag;
  546. TString *str = luaS_new(L, k);
  547. luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
  548. if (!tagisempty(tag)) {
  549. api_incr_top(L);
  550. }
  551. else {
  552. setsvalue2s(L, L->top.p, str);
  553. api_incr_top(L);
  554. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  555. }
  556. lua_unlock(L);
  557. return novariant(tag);
  558. }
  559. static void getGlobalTable (lua_State *L, TValue *gt) {
  560. Table *registry = hvalue(&G(L)->l_registry);
  561. lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
  562. (void)tag; /* avoid not-used warnings when checks are off */
  563. api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
  564. }
  565. LUA_API int lua_getglobal (lua_State *L, const char *name) {
  566. TValue gt;
  567. lua_lock(L);
  568. getGlobalTable(L, &gt);
  569. return auxgetstr(L, &gt, name);
  570. }
  571. LUA_API int lua_gettable (lua_State *L, int idx) {
  572. lu_byte tag;
  573. TValue *t;
  574. lua_lock(L);
  575. api_checkpop(L, 1);
  576. t = index2value(L, idx);
  577. luaV_fastget(t, s2v(L->top.p - 1), s2v(L->top.p - 1), luaH_get, tag);
  578. if (tagisempty(tag))
  579. tag = luaV_finishget(L, t, s2v(L->top.p - 1), L->top.p - 1, tag);
  580. lua_unlock(L);
  581. return novariant(tag);
  582. }
  583. LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
  584. lua_lock(L);
  585. return auxgetstr(L, index2value(L, idx), k);
  586. }
  587. LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
  588. TValue *t;
  589. lu_byte tag;
  590. lua_lock(L);
  591. t = index2value(L, idx);
  592. luaV_fastgeti(t, n, s2v(L->top.p), tag);
  593. if (tagisempty(tag)) {
  594. TValue key;
  595. setivalue(&key, n);
  596. tag = luaV_finishget(L, t, &key, L->top.p, tag);
  597. }
  598. api_incr_top(L);
  599. lua_unlock(L);
  600. return novariant(tag);
  601. }
  602. static int finishrawget (lua_State *L, lu_byte tag) {
  603. if (tagisempty(tag)) /* avoid copying empty items to the stack */
  604. setnilvalue(s2v(L->top.p));
  605. api_incr_top(L);
  606. lua_unlock(L);
  607. return novariant(tag);
  608. }
  609. l_sinline Table *gettable (lua_State *L, int idx) {
  610. TValue *t = index2value(L, idx);
  611. api_check(L, ttistable(t), "table expected");
  612. return hvalue(t);
  613. }
  614. LUA_API int lua_rawget (lua_State *L, int idx) {
  615. Table *t;
  616. lu_byte tag;
  617. lua_lock(L);
  618. api_checkpop(L, 1);
  619. t = gettable(L, idx);
  620. tag = luaH_get(t, s2v(L->top.p - 1), s2v(L->top.p - 1));
  621. L->top.p--; /* pop key */
  622. return finishrawget(L, tag);
  623. }
  624. LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
  625. Table *t;
  626. lu_byte tag;
  627. lua_lock(L);
  628. t = gettable(L, idx);
  629. luaH_fastgeti(t, n, s2v(L->top.p), tag);
  630. return finishrawget(L, tag);
  631. }
  632. LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) {
  633. Table *t;
  634. TValue k;
  635. lua_lock(L);
  636. t = gettable(L, idx);
  637. setpvalue(&k, cast_voidp(p));
  638. return finishrawget(L, luaH_get(t, &k, s2v(L->top.p)));
  639. }
  640. LUA_API void lua_createtable (lua_State *L, unsigned narray, unsigned nrec) {
  641. Table *t;
  642. lua_lock(L);
  643. t = luaH_new(L);
  644. sethvalue2s(L, L->top.p, t);
  645. api_incr_top(L);
  646. if (narray > 0 || nrec > 0)
  647. luaH_resize(L, t, narray, nrec);
  648. luaC_checkGC(L);
  649. lua_unlock(L);
  650. }
  651. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  652. const TValue *obj;
  653. Table *mt;
  654. int res = 0;
  655. lua_lock(L);
  656. obj = index2value(L, objindex);
  657. switch (ttype(obj)) {
  658. case LUA_TTABLE:
  659. mt = hvalue(obj)->metatable;
  660. break;
  661. case LUA_TUSERDATA:
  662. mt = uvalue(obj)->metatable;
  663. break;
  664. default:
  665. mt = G(L)->mt[ttype(obj)];
  666. break;
  667. }
  668. if (mt != NULL) {
  669. sethvalue2s(L, L->top.p, mt);
  670. api_incr_top(L);
  671. res = 1;
  672. }
  673. lua_unlock(L);
  674. return res;
  675. }
  676. LUA_API int lua_getiuservalue (lua_State *L, int idx, int n) {
  677. TValue *o;
  678. int t;
  679. lua_lock(L);
  680. o = index2value(L, idx);
  681. api_check(L, ttisfulluserdata(o), "full userdata expected");
  682. if (n <= 0 || n > uvalue(o)->nuvalue) {
  683. setnilvalue(s2v(L->top.p));
  684. t = LUA_TNONE;
  685. }
  686. else {
  687. setobj2s(L, L->top.p, &uvalue(o)->uv[n - 1].uv);
  688. t = ttype(s2v(L->top.p));
  689. }
  690. api_incr_top(L);
  691. lua_unlock(L);
  692. return t;
  693. }
  694. /*
  695. ** set functions (stack -> Lua)
  696. */
  697. /*
  698. ** t[k] = value at the top of the stack (where 'k' is a string)
  699. */
  700. static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  701. int hres;
  702. TString *str = luaS_new(L, k);
  703. api_checkpop(L, 1);
  704. luaV_fastset(t, str, s2v(L->top.p - 1), hres, luaH_psetstr);
  705. if (hres == HOK) {
  706. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  707. L->top.p--; /* pop value */
  708. }
  709. else {
  710. setsvalue2s(L, L->top.p, str); /* push 'str' (to make it a TValue) */
  711. api_incr_top(L);
  712. luaV_finishset(L, t, s2v(L->top.p - 1), s2v(L->top.p - 2), hres);
  713. L->top.p -= 2; /* pop value and key */
  714. }
  715. lua_unlock(L); /* lock done by caller */
  716. }
  717. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  718. TValue gt;
  719. lua_lock(L); /* unlock done in 'auxsetstr' */
  720. getGlobalTable(L, &gt);
  721. auxsetstr(L, &gt, name);
  722. }
  723. LUA_API void lua_settable (lua_State *L, int idx) {
  724. TValue *t;
  725. int hres;
  726. lua_lock(L);
  727. api_checkpop(L, 2);
  728. t = index2value(L, idx);
  729. luaV_fastset(t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres, luaH_pset);
  730. if (hres == HOK) {
  731. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  732. }
  733. else
  734. luaV_finishset(L, t, s2v(L->top.p - 2), s2v(L->top.p - 1), hres);
  735. L->top.p -= 2; /* pop index and value */
  736. lua_unlock(L);
  737. }
  738. LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  739. lua_lock(L); /* unlock done in 'auxsetstr' */
  740. auxsetstr(L, index2value(L, idx), k);
  741. }
  742. LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
  743. TValue *t;
  744. int hres;
  745. lua_lock(L);
  746. api_checkpop(L, 1);
  747. t = index2value(L, idx);
  748. luaV_fastseti(t, n, s2v(L->top.p - 1), hres);
  749. if (hres == HOK)
  750. luaV_finishfastset(L, t, s2v(L->top.p - 1));
  751. else {
  752. TValue temp;
  753. setivalue(&temp, n);
  754. luaV_finishset(L, t, &temp, s2v(L->top.p - 1), hres);
  755. }
  756. L->top.p--; /* pop value */
  757. lua_unlock(L);
  758. }
  759. static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
  760. Table *t;
  761. lua_lock(L);
  762. api_checkpop(L, n);
  763. t = gettable(L, idx);
  764. luaH_set(L, t, key, s2v(L->top.p - 1));
  765. invalidateTMcache(t);
  766. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  767. L->top.p -= n;
  768. lua_unlock(L);
  769. }
  770. LUA_API void lua_rawset (lua_State *L, int idx) {
  771. aux_rawset(L, idx, s2v(L->top.p - 2), 2);
  772. }
  773. LUA_API void lua_rawsetp (lua_State *L, int idx, const void *p) {
  774. TValue k;
  775. setpvalue(&k, cast_voidp(p));
  776. aux_rawset(L, idx, &k, 1);
  777. }
  778. LUA_API void lua_rawseti (lua_State *L, int idx, lua_Integer n) {
  779. Table *t;
  780. lua_lock(L);
  781. api_checkpop(L, 1);
  782. t = gettable(L, idx);
  783. luaH_setint(L, t, n, s2v(L->top.p - 1));
  784. luaC_barrierback(L, obj2gco(t), s2v(L->top.p - 1));
  785. L->top.p--;
  786. lua_unlock(L);
  787. }
  788. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  789. TValue *obj;
  790. Table *mt;
  791. lua_lock(L);
  792. api_checkpop(L, 1);
  793. obj = index2value(L, objindex);
  794. if (ttisnil(s2v(L->top.p - 1)))
  795. mt = NULL;
  796. else {
  797. api_check(L, ttistable(s2v(L->top.p - 1)), "table expected");
  798. mt = hvalue(s2v(L->top.p - 1));
  799. }
  800. switch (ttype(obj)) {
  801. case LUA_TTABLE: {
  802. hvalue(obj)->metatable = mt;
  803. if (mt) {
  804. luaC_objbarrier(L, gcvalue(obj), mt);
  805. luaC_checkfinalizer(L, gcvalue(obj), mt);
  806. }
  807. break;
  808. }
  809. case LUA_TUSERDATA: {
  810. uvalue(obj)->metatable = mt;
  811. if (mt) {
  812. luaC_objbarrier(L, uvalue(obj), mt);
  813. luaC_checkfinalizer(L, gcvalue(obj), mt);
  814. }
  815. break;
  816. }
  817. default: {
  818. G(L)->mt[ttype(obj)] = mt;
  819. break;
  820. }
  821. }
  822. L->top.p--;
  823. lua_unlock(L);
  824. return 1;
  825. }
  826. LUA_API int lua_setiuservalue (lua_State *L, int idx, int n) {
  827. TValue *o;
  828. int res;
  829. lua_lock(L);
  830. api_checkpop(L, 1);
  831. o = index2value(L, idx);
  832. api_check(L, ttisfulluserdata(o), "full userdata expected");
  833. if (!(cast_uint(n) - 1u < cast_uint(uvalue(o)->nuvalue)))
  834. res = 0; /* 'n' not in [1, uvalue(o)->nuvalue] */
  835. else {
  836. setobj(L, &uvalue(o)->uv[n - 1].uv, s2v(L->top.p - 1));
  837. luaC_barrierback(L, gcvalue(o), s2v(L->top.p - 1));
  838. res = 1;
  839. }
  840. L->top.p--;
  841. lua_unlock(L);
  842. return res;
  843. }
  844. /*
  845. ** 'load' and 'call' functions (run Lua code)
  846. */
  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(gettotalbytes(g) >> 10);
  995. break;
  996. }
  997. case LUA_GCCOUNTB: {
  998. res = cast_int(gettotalbytes(g) & 0x3ff);
  999. break;
  1000. }
  1001. case LUA_GCSTEP: {
  1002. lu_byte oldstp = g->gcstp;
  1003. l_mem n = cast(l_mem, va_arg(argp, size_t));
  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 < SHRT_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. luaC_checkGC(L);
  1137. lua_unlock(L);
  1138. return getudatamem(u);
  1139. }
  1140. static const char *aux_upvalue (TValue *fi, int n, TValue **val,
  1141. GCObject **owner) {
  1142. switch (ttypetag(fi)) {
  1143. case LUA_VCCL: { /* C closure */
  1144. CClosure *f = clCvalue(fi);
  1145. if (!(cast_uint(n) - 1u < cast_uint(f->nupvalues)))
  1146. return NULL; /* 'n' not in [1, f->nupvalues] */
  1147. *val = &f->upvalue[n-1];
  1148. if (owner) *owner = obj2gco(f);
  1149. return "";
  1150. }
  1151. case LUA_VLCL: { /* Lua closure */
  1152. LClosure *f = clLvalue(fi);
  1153. TString *name;
  1154. Proto *p = f->p;
  1155. if (!(cast_uint(n) - 1u < cast_uint(p->sizeupvalues)))
  1156. return NULL; /* 'n' not in [1, p->sizeupvalues] */
  1157. *val = f->upvals[n-1]->v.p;
  1158. if (owner) *owner = obj2gco(f->upvals[n - 1]);
  1159. name = p->upvalues[n-1].name;
  1160. return (name == NULL) ? "(no name)" : getstr(name);
  1161. }
  1162. default: return NULL; /* not a closure */
  1163. }
  1164. }
  1165. LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
  1166. const char *name;
  1167. TValue *val = NULL; /* to avoid warnings */
  1168. lua_lock(L);
  1169. name = aux_upvalue(index2value(L, funcindex), n, &val, NULL);
  1170. if (name) {
  1171. setobj2s(L, L->top.p, val);
  1172. api_incr_top(L);
  1173. }
  1174. lua_unlock(L);
  1175. return name;
  1176. }
  1177. LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
  1178. const char *name;
  1179. TValue *val = NULL; /* to avoid warnings */
  1180. GCObject *owner = NULL; /* to avoid warnings */
  1181. TValue *fi;
  1182. lua_lock(L);
  1183. fi = index2value(L, funcindex);
  1184. api_checknelems(L, 1);
  1185. name = aux_upvalue(fi, n, &val, &owner);
  1186. if (name) {
  1187. L->top.p--;
  1188. setobj(L, val, s2v(L->top.p));
  1189. luaC_barrier(L, owner, val);
  1190. }
  1191. lua_unlock(L);
  1192. return name;
  1193. }
  1194. static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
  1195. static const UpVal *const nullup = NULL;
  1196. LClosure *f;
  1197. TValue *fi = index2value(L, fidx);
  1198. api_check(L, ttisLclosure(fi), "Lua function expected");
  1199. f = clLvalue(fi);
  1200. if (pf) *pf = f;
  1201. if (1 <= n && n <= f->p->sizeupvalues)
  1202. return &f->upvals[n - 1]; /* get its upvalue pointer */
  1203. else
  1204. return (UpVal**)&nullup;
  1205. }
  1206. LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
  1207. TValue *fi = index2value(L, fidx);
  1208. switch (ttypetag(fi)) {
  1209. case LUA_VLCL: { /* lua closure */
  1210. return *getupvalref(L, fidx, n, NULL);
  1211. }
  1212. case LUA_VCCL: { /* C closure */
  1213. CClosure *f = clCvalue(fi);
  1214. if (1 <= n && n <= f->nupvalues)
  1215. return &f->upvalue[n - 1];
  1216. /* else */
  1217. } /* FALLTHROUGH */
  1218. case LUA_VLCF:
  1219. return NULL; /* light C functions have no upvalues */
  1220. default: {
  1221. api_check(L, 0, "function expected");
  1222. return NULL;
  1223. }
  1224. }
  1225. }
  1226. LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
  1227. int fidx2, int n2) {
  1228. LClosure *f1;
  1229. UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
  1230. UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
  1231. api_check(L, *up1 != NULL && *up2 != NULL, "invalid upvalue index");
  1232. *up1 = *up2;
  1233. luaC_objbarrier(L, f1, *up1);
  1234. }