lapi.c 36 KB

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